-TIMEQ_CALLBACK(main_statistics) {
- int update_minutes = get_int_field("statistics.frequency");
- if(!update_minutes) update_minutes = 2;
- timeq_add(update_minutes * 60, 0, main_statistics, NULL);
- if(get_int_field("statistics.enable")) {
- statistics_enabled = 1;
- statistics_requested_lusers = 1;
- if(get_int_field("statistics.include_lusers")) {
- struct ClientSocket *bot, *lusersbot = NULL;
- for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
- if(bot->flags & SOCKET_FLAG_PREFERRED)
- lusersbot = bot;
- }
- bot = lusersbot;
- if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
- putsock(bot, "LUSERS");
- } else {
- statistics_update();
- }
- } else
- statistics_enabled = 0;
-}
-
-void statistics_update() {
- if(get_int_field("statistics.enable") && statistics_requested_lusers && get_string_field("statistics.execute")) {
- statistics_requested_lusers = 0;
- char command[MAXLEN];
- /* parameters:
- - visible users
- - visible chanusers
- - visible channels
- - privmsg per minute
- - commands per minute
- - network users
- - network channels
- */
- sprintf(command, "%s %d %d %d %d %d %d %d", get_string_field("statistics.execute"), getUserCount(), getChanUserCount(), getChannelCount(), statistics_privmsg, statistics_commands, statistics_network_users, statistics_network_channels);
- statistics_privmsg = 0;
- statistics_commands = 0;
- system(command);
- }
-}
-
-time_t getStartTime() {
- return start_time;
-}
-
-int getRunningThreads() {
- return running_threads;
-}
-
-void write_log(int loglevel, const char *line, int len) {
- SYNCHRONIZE(log_sync);
- if(!daemonized && (print_loglevel & loglevel)) {
- printf("%s", line);
- } else if(!daemonized && loglevel == LOGLEVEL_ERROR) {
- fprintf(stderr, "%s", line);
- }
- if(get_int_field("log.loglevel") & loglevel) {
- if(!log_fptr) {
- log_fptr = fopen(LOG_FILE, "a");
- if(!log_fptr) goto write_log_end;
- }
- time_t rawtime;
- struct tm *timeinfo;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- char timestr[20];
- int timepos = strftime(timestr, 20, "%x %X ", timeinfo);
- fwrite(timestr, 1, timepos, log_fptr);
- fwrite(line, 1, len, log_fptr);
- }
- write_log_end:
- DESYNCHRONIZE(log_sync);
-}
-
-void putlog(int loglevel, const char *text, ...) {
- va_list arg_list;
- char logBuf[MAXLOGLEN];
- int pos;
- logBuf[0] = '\0';
- va_start(arg_list, text);
- pos = vsnprintf(logBuf, MAXLOGLEN - 1, text, arg_list);
- va_end(arg_list);
- if (pos < 0 || pos > (MAXLOGLEN - 1)) pos = MAXLOGLEN - 1;
- logBuf[pos] = '\0';
- write_log(loglevel, logBuf, pos);
-}