xbot/mods/uptime/uptime.c

179 lines
5.0 KiB
C
Raw Normal View History

2024-02-17 09:50:15 +00:00
#define MY_DLL_EXPORTS 1
2015-04-18 15:51:15 +00:00
#include "irc.h"
#include "events.h"
#include "module.h"
#include <stdio.h>
#include <stdlib.h>
2024-02-13 07:22:10 +00:00
#include <string.h>
2015-04-18 15:51:15 +00:00
2024-02-17 09:50:15 +00:00
#ifdef _WIN32
#define BUFFER_SIZE 512
#include <windows.h>
void parseUptime(const char *output, struct irc_conn *bot, const char *where) {
const char *keyword = "Statistics since ";
const char *uptime_start;
int month, day, year, hour, minute;
SYSTEMTIME uptime_systemtime, current_time;
ULONGLONG uptime_ticks, current_ticks;
ULONGLONG uptime_seconds;
2024-02-21 14:24:49 +00:00
char buf[BUFFER_SIZE];
2024-02-17 09:50:15 +00:00
int days, hours, minutes;
2024-02-21 14:24:49 +00:00
OSVERSIONINFOEX osInfo;
ZeroMemory(&osInfo, sizeof(OSVERSIONINFOEX));
osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
2024-02-17 09:50:15 +00:00
uptime_start = strstr(output, keyword);
if (uptime_start == NULL) {
printf("Failed to parse uptime information.\n");
return;
}
// Skip the "Statistics since " keyword
uptime_start += strlen(keyword);
// Parse the date and time
if (sscanf(uptime_start, "%d/%d/%d %d:%d", &month, &day, &year, &hour, &minute) != 5) {
printf("Failed to parse date and time.\n");
return;
}
// Get the current time
GetLocalTime(&current_time);
// Calculate the uptime
uptime_systemtime.wYear = year;
uptime_systemtime.wMonth = month;
uptime_systemtime.wDayOfWeek = 0;
uptime_systemtime.wDay = day;
uptime_systemtime.wHour = hour;
uptime_systemtime.wMinute = minute;
uptime_systemtime.wSecond = 0;
uptime_systemtime.wMilliseconds = 0;
SystemTimeToFileTime(&uptime_systemtime, (FILETIME *)&uptime_ticks);
SystemTimeToFileTime(&current_time, (FILETIME *)&current_ticks);
uptime_seconds = (current_ticks - uptime_ticks) / 10000000UL; // Convert to seconds
2024-02-21 14:24:49 +00:00
uptime_seconds -= 1470;
2024-02-17 09:50:15 +00:00
days = uptime_seconds / (24 * 3600);
hours = (uptime_seconds % (24 * 3600)) / 3600;
minutes = (uptime_seconds % 3600) / 60;
2024-02-21 14:24:49 +00:00
if (GetVersionEx((OSVERSIONINFO*)&osInfo)) {
if (osInfo.dwMajorVersion == 10 && osInfo.dwMinorVersion == 0) {
sprintf(buf, "Windows 10");
} else if (osInfo.dwMajorVersion == 6 && osInfo.dwMinorVersion == 3) {
sprintf(buf, "Windows 8.1");
} else if (osInfo.dwMajorVersion == 6 && osInfo.dwMinorVersion == 2) {
sprintf(buf, "Windows 8");
} else if (osInfo.dwMajorVersion == 6 && osInfo.dwMinorVersion == 1) {
// detect Windows 7 or Windows Server 2008 R2
if (osInfo.wProductType == VER_NT_WORKSTATION) {
sprintf(buf, "Windows 7");
} else {
sprintf(buf, "Windows Server 2008 R2");
}
} else if (osInfo.dwMajorVersion == 6 && osInfo.dwMinorVersion == 0) {
// detect Windows Vista or Windows Server 2008
if (osInfo.wProductType == VER_NT_WORKSTATION) {
sprintf(buf, "Windows Vista");
} else {
sprintf(buf, "Windows Server 2008");
}
} else if (osInfo.dwMajorVersion == 5 && osInfo.dwMinorVersion == 1) {
// detect Windows XP or Windows Server 2003
if (osInfo.wProductType == VER_NT_WORKSTATION) {
sprintf(buf, "Windows XP");
} else {
sprintf(buf, "Windows Server 2003");
}
} else if (osInfo.dwMajorVersion == 5 && osInfo.dwMinorVersion == 0) {
sprintf(buf, "Windows 2000");
} else {
sprintf(buf, "Windows");
}
}
irc_privmsg(bot, where, "%s: %d days, %d hours, and %d minutes up\n", buf, days, hours, minutes);
2024-02-17 09:50:15 +00:00
}
char *executeCommand(const char *command)
{
char buffer[BUFFER_SIZE];
char *old_result = NULL;
char *result = NULL;
FILE *fp;
fp = _popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n");
exit(1);
}
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
if (result == NULL) {
result = _strdup(buffer);
} else {
old_result = result;
result = malloc(strlen(old_result) + strlen(buffer) + 1);
strcpy(result, old_result);
strcat(result, buffer);
free(old_result);
}
}
_pclose(fp);
return result;
}
#endif
2024-02-16 21:28:11 +00:00
MY_API void up(struct irc_conn *bot, char *user, char *host, char *chan, char *text)
2015-04-18 15:51:15 +00:00
{
2024-02-17 09:50:15 +00:00
#ifdef _WIN32
2024-02-24 01:57:59 +00:00
char *output;
2024-02-17 09:50:15 +00:00
if (!strcmp(text, "!uptime"))
{
2024-02-24 01:57:59 +00:00
output = executeCommand("net statistics server");
2024-02-17 09:50:15 +00:00
parseUptime(output, bot, chan);
2024-02-24 01:57:59 +00:00
2024-02-17 09:50:15 +00:00
free(output);
}
#else
2015-04-18 15:51:15 +00:00
char buf[100];
FILE* file;
if (!strcmp(text, "!uptime"))
{
file = popen("uptime", "r");
fgets(buf, 100, file);
pclose(file);
irc_privmsg(bot, chan, "%s", buf);
}
2024-02-17 09:50:15 +00:00
#endif
2015-04-18 15:51:15 +00:00
}
2024-02-13 12:07:59 +00:00
MY_API void mod_init()
2015-04-18 15:51:15 +00:00
{
2024-02-17 08:21:22 +00:00
register_module("uptime", "Aaron Blakely", "v0.1", "Uptime module");
2024-02-13 12:07:59 +00:00
printf("installing up handler\n");
2015-04-18 15:51:15 +00:00
add_handler(PRIVMSG_CHAN, up);
2024-02-13 07:22:10 +00:00
}
MY_API void mod_unload()
{
2024-02-17 08:21:22 +00:00
unregister_module("uptime");
printf("unloading up handler\n");
del_handler(PRIVMSG_CHAN, up);
}