Remove extraneous semicolons from end of macro uses.
[srvx.git] / src / mod-helpserv.c
index 145acd2ac1586eb495365a26c400ae661574dcba..b211da2751ea72eacba8920b5b2738ac4e9008b3 100644 (file)
@@ -1,11 +1,12 @@
-/* helpserv.c - Support Helper assistant service
- * Copyright 2002-2003 srvx Development Team
+/* mod-helpserv.c - Support Helper assistant service
+ * Copyright 2002-2003, 2006 srvx Development Team
  *
- * This program is free software; you can redistribute it and/or modify
+ * This file is part of srvx.
+ *
+ * srvx is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.  Important limitations are
- * listed in the COPYING file that accompanies this software.
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -13,7 +14,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, email srvx-maintainers@srvx.net.
+ * along with srvx; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  */
 
 /* Wishlist for helpserv.c
@@ -89,7 +91,7 @@ const char *helpserv_module_deps[] = { NULL };
 /* General */
 #define HSFMT_TIME               "%a, %d %b %Y %H:%M:%S %Z"
 static const struct message_entry msgtab[] = {
-    { "HSMSG_READHELP_SUCCESS", "Read HelpServ help database in "FMT_TIME_T".%03ld seconds." },
+    { "HSMSG_READHELP_SUCCESS", "Read HelpServ help database in %lu.%03lu seconds." },
     { "HSMSG_INVALID_BOT", "This command requires a valid HelpServ bot name." },
     { "HSMSG_ILLEGAL_CHANNEL", "$b%s$b is an illegal channel; cannot use it." },
     { "HSMSG_INTERNAL_COMMAND", "$b%s$b appears to be an internal HelpServ command, sorry." },
@@ -287,6 +289,12 @@ static const struct message_entry msgtab[] = {
     { "HSMSG_STATS_TIME", "$uTime spent helping in %s:$u" },
     { "HSMSG_STATS_REQS", "$uRequest activity statistics:$u" },
 
+/* Status report headers */
+    { "HSMSG_STATS_REPORT_0", "Stats report for current week" },
+    { "HSMSG_STATS_REPORT_1", "Stats report for one week ago" },
+    { "HSMSG_STATS_REPORT_2", "Stats report for two weeks ago" },
+    { "HSMSG_STATS_REPORT_3", "Stats report for three weeks ago" },
+
 /* Responses to user commands */
     { "HSMSG_YOU_BEING_HELPED", "You are already being helped." },
     { "HSMSG_YOU_BEING_HELPED_BY", "You are already being helped by $b%s$b." },
@@ -457,10 +465,10 @@ static const char *weekday_names[] = {
 };
 
 static const char *statsreport_week[] = {
-    "Stats report for current week",
-    "Stats report for one week ago",
-    "Stats report for two weeks ago",
-    "Stats report for three weeks ago"
+    "HSMSG_STATS_REPORT_0",
+    "HSMSG_STATS_REPORT_1",
+    "HSMSG_STATS_REPORT_2",
+    "HSMSG_STATS_REPORT_3"
 };
 
 static struct {
@@ -471,10 +479,9 @@ static struct {
     char user_escape;
 } helpserv_conf;
 
-static time_t last_stats_update;
+static unsigned long last_stats_update;
 static int shutting_down;
 static FILE *reqlog_f;
-static struct saxdb_context *reqlog_ctx;
 static struct log_type *HS_LOG;
 
 #define CMD_NEED_BOT            0x001
@@ -512,8 +519,8 @@ struct helpserv_bot {
 
     unsigned int helpchan_empty : 1;
 
-    time_t registered;
-    time_t last_active;
+    unsigned long registered;
+    unsigned long last_active;
     char *registrar;
 };
 
@@ -524,7 +531,7 @@ struct helpserv_user {
     unsigned int week_start : 3;
     enum helpserv_level level;
     /* statistics */
-    time_t join_time; /* when they joined, or 0 if not in channel */
+    unsigned long join_time; /* when they joined, or 0 if not in channel */
     /* [0] through [3] are n weeks ago, [4] is the total of everything before that */
     unsigned int time_per_week[5]; /* how long they've were in the channel the past 4 weeks */
     unsigned int picked_up[5]; /* how many requests they have picked up */
@@ -555,9 +562,9 @@ struct helpserv_request {
     struct handle_info *handle;
 
     unsigned long id;
-    time_t opened;
-    time_t assigned;
-    time_t updated;
+    unsigned long opened;
+    unsigned long assigned;
+    unsigned long updated;
 };
 
 #define DEFINE_LIST_ALLOC(STRUCTNAME) \
@@ -574,16 +581,16 @@ void STRUCTNAME##_free(void *data) {\
 }
 
 DECLARE_LIST(helpserv_botlist, struct helpserv_bot *);
-DEFINE_LIST(helpserv_botlist, struct helpserv_bot *);
-DEFINE_LIST_ALLOC(helpserv_botlist);
+DEFINE_LIST(helpserv_botlist, struct helpserv_bot *)
+DEFINE_LIST_ALLOC(helpserv_botlist)
 
 DECLARE_LIST(helpserv_reqlist, struct helpserv_request *);
-DEFINE_LIST(helpserv_reqlist, struct helpserv_request *);
-DEFINE_LIST_ALLOC(helpserv_reqlist);
+DEFINE_LIST(helpserv_reqlist, struct helpserv_request *)
+DEFINE_LIST_ALLOC(helpserv_reqlist)
 
 DECLARE_LIST(helpserv_userlist, struct helpserv_user *);
-DEFINE_LIST(helpserv_userlist, struct helpserv_user *);
-DEFINE_LIST_ALLOC(helpserv_userlist);
+DEFINE_LIST(helpserv_userlist, struct helpserv_user *)
+DEFINE_LIST_ALLOC(helpserv_userlist)
 
 struct helpfile *helpserv_helpfile;
 static struct module *helpserv_module;
@@ -652,7 +659,7 @@ struct helpserv_cmd {
 
 static void run_empty_interval(void *data);
 
-static void helpserv_interval(char *output, time_t interval) {
+static void helpserv_interval(char *output, unsigned long interval) {
     int num_hours = interval / 3600;
     int num_minutes = (interval % 3600) / 60;
     sprintf(output, "%u hour%s, %u minute%s", num_hours, num_hours == 1 ? "" : "s", num_minutes, num_minutes == 1 ? "" : "s");
@@ -684,33 +691,38 @@ static struct helpserv_user *GetHSUser(struct helpserv_bot *hs, struct handle_in
 static void helpserv_log_request(struct helpserv_request *req, const char *reason) {
     char key[27+NICKLEN];
     char userhost[USERLEN+HOSTLEN+2];
+    struct saxdb_context *ctx;
+    int res;
 
-    if (!reqlog_ctx || !req)
+    assert(req != NULL);
+    assert(reason != NULL);
+    if (!(ctx = saxdb_open_context(reqlog_f)))
         return;
-    if (!reason)
-        reason = "";
-
-    sprintf(key, "%s-" FMT_TIME_T "-%lu", req->hs->helpserv->nick, req->opened, req->id);
-    saxdb_start_record(reqlog_ctx, key, 1);
-    if (req->helper) {
-        saxdb_write_string(reqlog_ctx, KEY_REQUEST_HELPER, req->helper->handle->handle);
-        saxdb_write_int(reqlog_ctx, KEY_REQUEST_ASSIGNED, req->assigned);
-    }
-    if (req->handle) {
-        saxdb_write_string(reqlog_ctx, KEY_REQUEST_HANDLE, req->handle->handle);
-    }
-    if (req->user) {
-        saxdb_write_string(reqlog_ctx, KEY_REQUEST_NICK, req->user->nick);
-        sprintf(userhost, "%s@%s", req->user->ident, req->user->hostname);
-        saxdb_write_string(reqlog_ctx, KEY_REQUEST_USERHOST, userhost);
+    sprintf(key, "%s-%lu-%lu", req->hs->helpserv->nick, (unsigned long)req->opened, req->id);
+    if ((res = setjmp(ctx->jbuf)) != 0) {
+        log_module(HS_LOG, LOG_ERROR, "Unable to log helpserv request: %s.", strerror(res));
+    } else {
+        saxdb_start_record(ctx, key, 1);
+        if (req->helper) {
+            saxdb_write_string(ctx, KEY_REQUEST_HELPER, req->helper->handle->handle);
+            saxdb_write_int(ctx, KEY_REQUEST_ASSIGNED, req->assigned);
+        }
+        if (req->handle) {
+            saxdb_write_string(ctx, KEY_REQUEST_HANDLE, req->handle->handle);
+        }
+        if (req->user) {
+            saxdb_write_string(ctx, KEY_REQUEST_NICK, req->user->nick);
+            sprintf(userhost, "%s@%s", req->user->ident, req->user->hostname);
+            saxdb_write_string(ctx, KEY_REQUEST_USERHOST, userhost);
+        }
+        saxdb_write_int(ctx, KEY_REQUEST_OPENED, req->opened);
+        saxdb_write_int(ctx, KEY_REQUEST_CLOSED, now);
+        saxdb_write_string(ctx, KEY_REQUEST_CLOSEREASON, reason);
+        saxdb_write_string_list(ctx, KEY_REQUEST_TEXT, req->text);
+        saxdb_end_record(ctx);
+        saxdb_close_context(ctx);
+        fflush(reqlog_f);
     }
-    saxdb_write_int(reqlog_ctx, KEY_REQUEST_OPENED, req->opened);
-    saxdb_write_int(reqlog_ctx, KEY_REQUEST_CLOSED, now);
-    saxdb_write_string(reqlog_ctx, KEY_REQUEST_CLOSEREASON, reason);
-    saxdb_write_string_list(reqlog_ctx, KEY_REQUEST_TEXT, req->text);
-    saxdb_end_record(reqlog_ctx);
-
-    fflush(reqlog_f);
 }
 
 /* Searches for a request by number, nick, or account (num|nick|*account).
@@ -852,7 +864,7 @@ static struct helpserv_request * create_request(struct userNode *user, struct he
     req->user = user;
     req->handle = user->handle_info;
     if (from_join && self->burst) {
-        extern time_t burst_begin;
+        extern unsigned long burst_begin;
         /* We need to keep all the requests during a burst join together,
          * even if the burst takes more than 1 second. ircu seems to burst
          * in reverse-join order. */
@@ -912,7 +924,7 @@ static struct helpserv_request * create_request(struct userNode *user, struct he
         sprintf(lbuf[0], fmt, req->id);
     }
     if (req != hs->unhandled) {
-        intervalString(unh, now - hs->unhandled->opened);
+        intervalString(unh, now - hs->unhandled->opened, user->handle_info);
         fmt = user_find_message(user, "HSMSG_REQ_UNHANDLED_TIME");
         sprintf(lbuf[1], fmt, unh);
     } else {
@@ -946,7 +958,7 @@ static struct helpserv_request * create_request(struct userNode *user, struct he
     else
         send_message_type(4, user, hs->helpserv, "%s %s %s", lbuf[0], lbuf[1], lbuf[2]);
 
-    if (hs->req_on_join && req == hs->unhandled && hs->helpchan_empty) {
+    if (hs->req_on_join && req == hs->unhandled && hs->helpchan_empty && !user->uplink->burst) {
         timeq_del(0, run_empty_interval, hs, TIMEQ_IGNORE_WHEN);
         run_empty_interval(hs);
     }
@@ -955,7 +967,7 @@ static struct helpserv_request * create_request(struct userNode *user, struct he
 }
 
 /* Handle a message from a user to a HelpServ bot. */
-static void helpserv_usermsg(struct userNode *user, struct helpserv_bot *hs, char *text) {
+static void helpserv_usermsg(struct userNode *user, struct helpserv_bot *hs, const char *text) {
     const int from_opserv = 0; /* for helpserv_notice */
     struct helpserv_request *req=NULL, *newest=NULL;
     struct helpserv_reqlist *reqlist, *hand_reqlist;
@@ -1050,23 +1062,26 @@ static void helpserv_usermsg(struct userNode *user, struct helpserv_bot *hs, cha
             helpserv_msguser(user, "HSMSG_USERCMD_UNKNOWN", cmdname);
         return;
     } else if (hs->intervals[INTERVAL_STALE_DELAY]
-               && (req->updated < (time_t)(now - hs->intervals[INTERVAL_STALE_DELAY]))
+               && (req->updated < now - hs->intervals[INTERVAL_STALE_DELAY])
                && (!hs->req_maxlen || req->text->used < hs->req_maxlen)) {
         char buf[MAX_LINE_SIZE], updatestr[INTERVALLEN], timestr[MAX_LINE_SIZE];
+        time_t feh;
 
-        strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&req->opened));
-        intervalString(updatestr, now - req->updated);
+        feh = req->opened;
+        strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&feh));
+        intervalString(updatestr, now - req->updated, user->handle_info);
         if (req->helper && (hs->notify >= NOTIFY_USER))
             if (user->handle_info)
                 helpserv_notify(req->helper, "HSMSG_PAGE_UPD_REQUEST_AUTHED", req->id, user->nick, user->handle_info->handle, timestr, updatestr);
             else
-                helpserv_notify(req->helper, "HSMSG_PAGE_UPD_REQUESTNOT_AUTHED", req->id, user->nick, timestr, updatestr);
+                helpserv_notify(req->helper, "HSMSG_PAGE_UPD_REQUEST_NOT_AUTHED", req->id, user->nick, timestr, updatestr);
         else
             if (user->handle_info)
                 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_UPD_REQUEST_AUTHED", req->id, user->nick, user->handle_info->handle, timestr, updatestr);
             else
                 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_UPD_REQUEST_NOT_AUTHED", req->id, user->nick, timestr, updatestr);
-        strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&now));
+        feh = now;
+        strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&feh));
         snprintf(buf, MAX_LINE_SIZE, "[Stale request updated at %s]", timestr);
         string_list_append(req->text, strdup(buf));
     }
@@ -1079,11 +1094,12 @@ static void helpserv_usermsg(struct userNode *user, struct helpserv_bot *hs, cha
 }
 
 /* Handle messages direct to a HelpServ bot. */
-static void helpserv_botmsg(struct userNode *user, struct userNode *target, char *text, UNUSED_ARG(int server_qualified)) {
+static void helpserv_botmsg(struct userNode *user, struct userNode *target, const char *text, UNUSED_ARG(int server_qualified)) {
     struct helpserv_bot *hs;
     struct helpserv_cmd *cmd;
     struct helpserv_user *hs_user;
     char *argv[MAXNUMPARAMS];
+    char tmpline[MAXLEN];
     int argc, argv_shift;
     const int from_opserv = 0; /* for helpserv_notice */
 
@@ -1101,7 +1117,8 @@ static void helpserv_botmsg(struct userNode *user, struct userNode *target, char
     }
 
     argv_shift = 1;
-    argc = split_line(text, false, ArrayLength(argv)-argv_shift, argv+argv_shift);
+    safestrncpy(tmpline, text, sizeof(tmpline));
+    argc = split_line(tmpline, false, ArrayLength(argv)-argv_shift, argv+argv_shift);
     if (!argc)
         return;
 
@@ -1121,8 +1138,8 @@ static void helpserv_botmsg(struct userNode *user, struct userNode *target, char
     if (!cmd->func) {
         helpserv_notice(user, "HSMSG_INTERNAL_COMMAND", argv[argv_shift]);
     } else if (cmd->func(user, hs, 0, argc, argv+argv_shift)) {
-        unsplit_string(argv+argv_shift, argc, text);
-        log_audit(HS_LOG, LOG_COMMAND, user, hs->helpserv, hs->helpchan->name, 0, text);
+        unsplit_string(argv+argv_shift, argc, tmpline);
+        log_audit(HS_LOG, LOG_COMMAND, user, hs->helpserv, hs->helpchan->name, 0, tmpline);
     }
 }
 
@@ -1222,14 +1239,14 @@ static HELPSERV_USERCMD(usercmd_wait) {
         return;
     }
 
-    for (other = req->hs->unhandled, pos = -1, count = 0; 
+    for (other = req->hs->unhandled, pos = -1, count = 0;
          other;
          other = other->next_unhandled, ++count) {
         if (other == req)
             pos = count;
     }
     assert(pos >= 0);
-    intervalString(buf, now - req->hs->unhandled->opened);
+    intervalString(buf, now - req->hs->unhandled->opened, req->user->handle_info);
     helpserv_user_reply("HSMSG_WAIT_STATUS", pos+1, count, buf);
 }
 
@@ -1263,7 +1280,7 @@ static HELPSERV_FUNC(cmd_readhelp) {
         stop.tv_sec -= 1;
         stop.tv_usec += 1000000;
     }
-    helpserv_notice(user, "HSMSG_READHELP_SUCCESS", stop.tv_sec, stop.tv_usec/1000);
+    helpserv_notice(user, "HSMSG_READHELP_SUCCESS", (unsigned long)stop.tv_sec, (unsigned long)stop.tv_usec/1000);
 
     return 1;
 }
@@ -1661,10 +1678,10 @@ static HELPSERV_FUNC(cmd_close) {
         struct modeNode *mn = GetUserMode(hs->helpchan, req_user);
         if ((!newest || !newest->helper) && mn && (mn->modes & MODE_VOICE)) {
             struct mod_chanmode change;
-            change.modes_set = change.modes_clear = 0;
+            mod_chanmode_init(&change);
             change.argc = 1;
             change.args[0].mode = MODE_REMOVE | MODE_VOICE;
-            change.args[0].member = mn;
+            change.args[0].u.member = mn;
             mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
         }
     }
@@ -1756,7 +1773,7 @@ static HELPSERV_FUNC(cmd_list) {
         }
         tbl.contents[line][1] = strdup(username);
         tbl.contents[line][2] = req->helper ? req->helper->handle->handle : "(Unassigned)";
-        intervalString(opentime, now - req->opened);
+        intervalString(opentime, now - req->opened, user->handle_info);
         tbl.contents[line][3] = strdup(opentime);
         tbl.contents[line][4] = ((req->user || req->handle->users) ? "Online" : "Offline");
     }
@@ -1776,6 +1793,7 @@ static void helpserv_show(int from_opserv, struct helpserv_bot *hs, struct userN
     unsigned int nn;
     char buf[MAX_LINE_SIZE];
     char buf2[INTERVALLEN];
+    time_t feh;
 
     if (req->user)
         if (req->handle)
@@ -1789,8 +1807,9 @@ static void helpserv_show(int from_opserv, struct helpserv_bot *hs, struct userN
             helpserv_notice(user, "HSMSG_REQ_INFO_2d", req->handle->handle);
     else
         helpserv_notice(user, "HSMSG_REQ_INFO_2e");
-    strftime(buf, MAX_LINE_SIZE, HSFMT_TIME, localtime(&req->opened));
-    intervalString(buf2, now - req->opened);
+    feh = req->opened;
+    strftime(buf, MAX_LINE_SIZE, HSFMT_TIME, localtime(&feh));
+    intervalString(buf2, now - req->opened, user->handle_info);
     helpserv_notice(user, "HSMSG_REQ_INFO_3", buf, buf2);
     helpserv_notice(user, "HSMSG_REQ_INFO_4");
     for (nn=0; nn < req->text->used; nn++)
@@ -1865,10 +1884,10 @@ static int helpserv_assign(int from_opserv, struct helpserv_bot *hs, struct user
 
     if (req->user && hs->auto_voice) {
         struct mod_chanmode change;
-        change.modes_set = change.modes_clear = 0;
+        mod_chanmode_init(&change);
         change.argc = 1;
         change.args[0].mode = MODE_VOICE;
-        if ((change.args[0].member = GetUserMode(hs->helpchan, req->user)))
+        if ((change.args[0].u.member = GetUserMode(hs->helpchan, req->user)))
             mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
     }
 
@@ -1892,8 +1911,6 @@ static HELPSERV_FUNC(cmd_show) {
 
     REQUIRE_PARMS(2);
 
-    assert(hs_user);
-
     if (!(req = smart_get_request(hs, hs_user, argv[1], &num_requests))) {
         helpserv_notice(user, "HSMSG_REQ_INVALID", argv[1]);
         return 0;
@@ -1975,11 +1992,10 @@ static HELPSERV_FUNC(cmd_addnote) {
     struct helpserv_request *req;
     struct helpserv_user *hs_user=GetHSUser(hs, user->handle_info);
     int num_requests=0;
+    time_t feh;
 
     REQUIRE_PARMS(3);
 
-    assert(hs_user);
-
     if (!(req = smart_get_request(hs, hs_user, argv[1], &num_requests))) {
         helpserv_notice(user, "HSMSG_REQ_INVALID", argv[1]);
         return 0;
@@ -1990,7 +2006,8 @@ static HELPSERV_FUNC(cmd_addnote) {
 
     note = unsplit_string(argv+2, argc-2, NULL);
 
-    strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&now));
+    feh = now;
+    strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&feh));
     snprintf(text, MAX_LINE_SIZE, "[Helper note at %s]:", timestr);
     string_list_append(req->text, strdup(text));
     snprintf(text, MAX_LINE_SIZE, "  <%s> %s", user->handle_info->handle, note);
@@ -2242,10 +2259,10 @@ static HELPSERV_FUNC(cmd_move) {
             AddChannelUser(hs->helpserv, hs->helpchan)->modes |= MODE_CHANOP;
         } else if (!helpserv_in_channel(hs, old_helpchan)) {
             struct mod_chanmode change;
-            change.modes_set = change.modes_clear = 0;
+            mod_chanmode_init(&change);
             change.argc = 1;
             change.args[0].mode = MODE_CHANOP;
-            change.args[0].member = AddChannelUser(hs->helpserv, hs->helpchan);
+            change.args[0].u.member = AddChannelUser(hs->helpserv, hs->helpchan);
             mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
         }
 
@@ -2288,7 +2305,7 @@ static HELPSERV_FUNC(cmd_bots) {
         struct helpserv_user *owner=NULL;
 
         bot = iter_data(it);
-        
+
         for (it2=dict_first(bot->users); it2; it2=iter_next(it2)) {
             if (((struct helpserv_user *)iter_data(it2))->level == HlOwner) {
                 owner = iter_data(it2);
@@ -2301,7 +2318,7 @@ static HELPSERV_FUNC(cmd_bots) {
         tbl.contents[i][1] = bot->helpchan->name;
         tbl.contents[i][2] = owner ? owner->handle->handle : "None";
         tbl.contents[i][3] = alloca(INTERVALLEN);
-        intervalString((char*)tbl.contents[i][3], now - bot->last_active);
+        intervalString((char*)tbl.contents[i][3], now - bot->last_active, user->handle_info);
     }
 
     table_send((from_opserv ? opserv : hs->helpserv), user->nick, 0, NULL, tbl);
@@ -2321,10 +2338,10 @@ static void helpserv_page_helper_gone(struct helpserv_bot *hs, struct helpserv_r
         helpserv_msguser(req->user, "HSMSG_REQ_UNASSIGNED", req->id, reason);
         if (hs->auto_devoice && mn && (mn->modes & MODE_VOICE)) {
             struct mod_chanmode change;
-            change.modes_set = change.modes_clear = 0;
+            mod_chanmode_init(&change);
             change.argc = 1;
             change.args[0].mode = MODE_REMOVE | MODE_VOICE;
-            change.args[0].member = mn;
+            change.args[0].u.member = mn;
             mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
         }
         if(req->handle)
@@ -2379,7 +2396,7 @@ static void run_whine_interval(void *data) {
 
         for (unh = hs->unhandled; unh; unh = unh->next_unhandled) {
             queuesize++;
-            if ((now - unh->opened) >= (time_t)hs->intervals[INTERVAL_WHINE_DELAY]) {
+            if ((now - unh->opened) >= hs->intervals[INTERVAL_WHINE_DELAY]) {
                 helpserv_reqlist_append(&reqlist, unh);
             }
         }
@@ -2387,7 +2404,7 @@ static void run_whine_interval(void *data) {
         if (reqlist.used) {
             char strwhinedelay[INTERVALLEN];
 
-            intervalString(strwhinedelay, (time_t)hs->intervals[INTERVAL_WHINE_DELAY]);
+            intervalString(strwhinedelay, hs->intervals[INTERVAL_WHINE_DELAY], NULL);
 #if ANNOYING_ALERT_PAGES
             tbl.length = reqlist.used + 1;
             tbl.width = 4;
@@ -2408,7 +2425,7 @@ static void run_whine_interval(void *data) {
                 tbl.contents[i][0] = strdup(reqid);
                 tbl.contents[i][1] = unh->user ? unh->user->nick : "Not online";
                 tbl.contents[i][2] = unh->handle ? unh->handle->handle : "Not authed";
-                intervalString(unh_time, now - unh->opened);
+                intervalString(unh_time, now - unh->opened, NULL);
                 tbl.contents[i][3] = strdup(unh_time);
             }
 
@@ -2488,11 +2505,11 @@ static void run_whine_interval(void *data) {
                 }
                 tbl.contents[i][2] = strdup(reqid);
 
-                intervalString(idle_time, now - mn->idle_since);
+                intervalString(idle_time, now - mn->idle_since, NULL);
                 tbl.contents[i][3] = strdup(idle_time);
             }
 
-            intervalString(stridledelay, (time_t)hs->intervals[INTERVAL_IDLE_DELAY]);
+            intervalString(stridledelay, hs->intervals[INTERVAL_IDLE_DELAY], NULL);
             helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_IDLE_HEADER", mode_list.used, hs->helpchan->name, stridledelay);
             table_send(hs->helpserv, hs->page_targets[PGSRC_STATUS]->name, 0, page_types[hs->page_types[PGSRC_STATUS]].func, tbl);
 
@@ -2591,7 +2608,7 @@ static struct helpserv_bot *register_helpserv(const char *nick, const char *help
      * it's a harmless default */
     hs = calloc(1, sizeof(struct helpserv_bot));
 
-    if (!(hs->helpserv = AddService(nick, helpserv_conf.description))) {
+    if (!(hs->helpserv = AddLocalUser(nick, nick, NULL, helpserv_conf.description, NULL))) {
         free(hs);
         return NULL;
     }
@@ -2603,10 +2620,10 @@ static struct helpserv_bot *register_helpserv(const char *nick, const char *help
         AddChannelUser(hs->helpserv, hs->helpchan)->modes |= MODE_CHANOP;
     } else {
         struct mod_chanmode change;
-        change.modes_set = change.modes_clear = 0;
+        mod_chanmode_init(&change);
         change.argc = 1;
         change.args[0].mode = MODE_CHANOP;
-        change.args[0].member = AddChannelUser(hs->helpserv, hs->helpchan);
+        change.args[0].u.member = AddChannelUser(hs->helpserv, hs->helpchan);
         mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
     }
 
@@ -2860,10 +2877,10 @@ static void set_page_target(struct helpserv_bot *hs, enum page_source idx, const
         DelChannelUser(hs->helpserv, old_target, "Changing page target.", 0);
     if (new_target && !helpserv_in_channel(hs, new_target)) {
         struct mod_chanmode change;
-        change.modes_set = change.modes_clear = 0;
+        mod_chanmode_init(&change);
         change.argc = 1;
         change.args[0].mode = MODE_CHANOP;
-        change.args[0].member = AddChannelUser(hs->helpserv, new_target);
+        change.args[0].u.member = AddChannelUser(hs->helpserv, new_target);
         mod_chanmode_announce(hs->helpserv, new_target, &change);
     }
     hs->page_targets[idx] = new_target;
@@ -2988,7 +3005,7 @@ static int opt_interval(struct userNode *user, struct helpserv_bot *hs, int from
             return 0;
         }
         if (new_int && new_int < min) {
-            intervalString(buf, min);
+            intervalString(buf, min, user->handle_info);
             helpserv_notice(user, "HSMSG_INVALID_INTERVAL", user_find_message(user, interval_types[idx].print_name), buf);
             return 0;
         }
@@ -2996,7 +3013,7 @@ static int opt_interval(struct userNode *user, struct helpserv_bot *hs, int from
         changed = 1;
     }
     if (hs->intervals[idx]) {
-        intervalString(buf, hs->intervals[idx]);
+        intervalString(buf, hs->intervals[idx], user->handle_info);
         helpserv_notice(user, interval_types[idx].print_name, buf);
     } else
         helpserv_notice(user, interval_types[idx].print_name, user_find_message(user, "HSMSG_0_DISABLED"));
@@ -3388,12 +3405,12 @@ static int request_read_helper(const char *key, void *data, void *extra) {
         log_module(HS_LOG, LOG_ERROR, "Request %s:%s has a nonexistant opening time. Using time(NULL).", hs->helpserv->nick, key);
         request->opened = time(NULL);
     } else {
-        request->opened = (time_t)strtoul(str, NULL, 0);
+        request->opened = strtoul(str, NULL, 0);
     }
 
     str = database_get_data(rd->d.object, KEY_REQUEST_ASSIGNED, RECDB_QSTRING);
     if (str)
-        request->assigned = (time_t)strtoul(str, NULL, 0);
+        request->assigned = strtoul(str, NULL, 0);
 
     str = database_get_data(rd->d.object, KEY_REQUEST_HELPER, RECDB_QSTRING);
     if (str) {
@@ -3583,7 +3600,7 @@ static int helpserv_bot_read(const char *key, void *data, UNUSED_ARG(void *extra
     hs->notify = str ? notification_from_name(str) : NOTIFY_NONE;
     str = database_get_data(GET_RECORD_OBJECT(br), KEY_REGISTERED, RECDB_QSTRING);
     if (str)
-        hs->registered = (time_t)strtol(str, NULL, 0);
+        hs->registered = strtol(str, NULL, 0);
     str = database_get_data(GET_RECORD_OBJECT(br), KEY_IDWRAP, RECDB_QSTRING);
     if (str)
         hs->id_wrap = strtoul(str, NULL, 0);
@@ -3602,7 +3619,7 @@ static int helpserv_bot_read(const char *key, void *data, UNUSED_ARG(void *extra
     str = database_get_data(GET_RECORD_OBJECT(br), KEY_AUTO_DEVOICE, RECDB_QSTRING);
     hs->auto_devoice = str ? enabled_string(str) : 0;
     str = database_get_data(GET_RECORD_OBJECT(br), KEY_LAST_ACTIVE, RECDB_QSTRING);
-    hs->last_active = str ? atoi(str) : now;
+    hs->last_active = str ? strtoul(str, NULL, 0) : now;
 
     dict_foreach(users, user_read_helper, hs);
 
@@ -3623,7 +3640,7 @@ helpserv_saxdb_read(struct dict *conf_db) {
     }
 
     str = database_get_data(conf_db, KEY_LAST_STATS_UPDATE, RECDB_QSTRING);
-    last_stats_update = str ? (time_t)strtol(str, NULL, 0) : now;
+    last_stats_update = str ? strtoul(str, NULL, 0) : now;
     return 0;
 }
 
@@ -3640,7 +3657,7 @@ static void helpserv_conf_read(void) {
     helpserv_conf.db_backup_frequency = str ? ParseInterval(str) : 7200;
 
     str = database_get_data(conf_node, "description", RECDB_QSTRING);
-    helpserv_conf.description = str;
+    helpserv_conf.description = str ? str : "Help Queue Manager";
 
     str = database_get_data(conf_node, "reqlogfile", RECDB_QSTRING);
     if (str && strlen(str))
@@ -3653,20 +3670,13 @@ static void helpserv_conf_read(void) {
     str = database_get_data(conf_node, "user_escape", RECDB_QSTRING);
     helpserv_conf.user_escape = str ? str[0] : '@';
 
-    if (reqlog_ctx) {
-        saxdb_close_context(reqlog_ctx);
-        reqlog_ctx = NULL;
-    }
     if (reqlog_f) {
         fclose(reqlog_f);
         reqlog_f = NULL;
     }
-    if (helpserv_conf.reqlogfile) {
-        if (!(reqlog_f = fopen(helpserv_conf.reqlogfile, "a"))) {
-            log_module(HS_LOG, LOG_ERROR, "Unable to open request logfile (%s): %s", helpserv_conf.reqlogfile, strerror(errno));
-        } else {
-            reqlog_ctx = saxdb_open_context(reqlog_f);
-        }
+    if (helpserv_conf.reqlogfile
+        && !(reqlog_f = fopen(helpserv_conf.reqlogfile, "a"))) {
+        log_module(HS_LOG, LOG_ERROR, "Unable to open request logfile (%s): %s", helpserv_conf.reqlogfile, strerror(errno));
     }
 }
 
@@ -3684,13 +3694,13 @@ helpserv_define_func(const char *name, helpserv_func_t *func, enum helpserv_leve
 }
 
 /* Drop requests that persist until part when a user leaves the chan */
-static void handle_part(struct userNode *user, struct chanNode *chan, UNUSED_ARG(const char *reason)) {
+static void handle_part(struct modeNode *mn, UNUSED_ARG(const char *reason)) {
     struct helpserv_botlist *botlist;
     struct helpserv_userlist *userlist;
     const int from_opserv = 0; /* for helpserv_notice */
     unsigned int i;
 
-    if ((botlist = dict_find(helpserv_bots_bychan_dict, chan->name, NULL))) {
+    if ((botlist = dict_find(helpserv_bots_bychan_dict, mn->channel->name, NULL))) {
         for (i=0; i < botlist->used; i++) {
             struct helpserv_bot *hs;
             dict_iterator_t it;
@@ -3704,13 +3714,13 @@ static void handle_part(struct userNode *user, struct chanNode *chan, UNUSED_ARG
             for (it=dict_first(hs->requests); it; it=iter_next(it)) {
                 struct helpserv_request *req = iter_data(it);
 
-                if (user != req->user)
+                if (mn->user != req->user)
                     continue;
                 if (req->text->used) {
-                    helpserv_message(hs, user, MSGTYPE_REQ_DROPPED);
-                    helpserv_msguser(user, "HSMSG_REQ_DROPPED_PART", chan->name, req->id);
+                    helpserv_message(hs, mn->user, MSGTYPE_REQ_DROPPED);
+                    helpserv_msguser(mn->user, "HSMSG_REQ_DROPPED_PART", mn->channel->name, req->id);
                     if (req->helper && (hs->notify >= NOTIFY_DROP))
-                        helpserv_notify(req->helper, "HSMSG_NOTIFY_REQ_DROP_PART", req->id, user->nick);
+                        helpserv_notify(req->helper, "HSMSG_NOTIFY_REQ_DROP_PART", req->id, mn->user->nick);
                 }
                 helpserv_log_request(req, "Dropped");
                 dict_remove(hs->requests, iter_key(it));
@@ -3718,14 +3728,14 @@ static void handle_part(struct userNode *user, struct chanNode *chan, UNUSED_ARG
             }
         }
     }
-    
-    if (user->handle_info && (userlist = dict_find(helpserv_users_byhand_dict, user->handle_info->handle, NULL))) {
+
+    if (mn->user->handle_info && (userlist = dict_find(helpserv_users_byhand_dict, mn->user->handle_info->handle, NULL))) {
         for (i=0; i < userlist->used; i++) {
             struct helpserv_user *hs_user = userlist->list[i];
             struct helpserv_bot *hs = hs_user->hs;
             dict_iterator_t it;
 
-            if ((hs->helpserv == NULL) || (hs->helpchan != chan) || find_handle_in_channel(hs->helpchan, user->handle_info, user))
+            if ((hs->helpserv == NULL) || (hs->helpchan != mn->channel) || find_handle_in_channel(hs->helpchan, mn->user->handle_info, mn->user))
                 continue;
 
             /* In case of the clock being set back for whatever reason,
@@ -3744,7 +3754,7 @@ static void handle_part(struct userNode *user, struct chanNode *chan, UNUSED_ARG
                 if ((hs->persist_types[PERSIST_T_HELPER] == PERSIST_PART)
                     && (req->helper == hs_user)) {
                     char reason[CHANNELLEN + 8];
-                    sprintf(reason, "parted %s", chan->name);
+                    sprintf(reason, "parted %s", mn->channel->name);
                     helpserv_page_helper_gone(hs, req, reason);
                 }
             }
@@ -3760,9 +3770,9 @@ static void handle_part(struct userNode *user, struct chanNode *chan, UNUSED_ARG
                         unh = unh->next_unhandled;
 
                     if (num_trials) {
-                        helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_FIRSTONLYTRIALALERT", hs->helpchan->name, user->nick, num_trials, num_unh);
+                        helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_FIRSTONLYTRIALALERT", hs->helpchan->name, mn->user->nick, num_trials, num_unh);
                     } else {
-                        helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_FIRSTEMPTYALERT", hs->helpchan->name, user->nick, num_unh);
+                        helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_FIRSTEMPTYALERT", hs->helpchan->name, mn->user->nick, num_unh);
                     }
                     if (num_unh || !hs->req_on_join) {
                         timeq_del(0, run_empty_interval, hs, TIMEQ_IGNORE_WHEN);
@@ -3845,7 +3855,7 @@ static void associate_requests_bybot(struct helpserv_bot *hs, struct userNode *u
     struct helpserv_request *newest=NULL, *nicknewest=NULL;
     unsigned int i;
     const int from_opserv = 0; /* For helpserv_notice */
-    
+
     if (!(user->handle_info && (hand_reqlist = dict_find(helpserv_reqs_byhand_dict, user->handle_info->handle, NULL))) && !force_greet) {
         return;
     }
@@ -3898,10 +3908,10 @@ static void associate_requests_bybot(struct helpserv_bot *hs, struct userNode *u
         if (hs->auto_voice && req->helper)
         {
             struct mod_chanmode change;
-            change.modes_set = change.modes_clear = 0;
+            mod_chanmode_init(&change);
             change.argc = 1;
             change.args[0].mode = MODE_VOICE;
-            if ((change.args[0].member = GetUserMode(hs->helpchan, user)))
+            if ((change.args[0].u.member = GetUserMode(hs->helpchan, user)))
                 mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
         }
     }
@@ -3937,7 +3947,7 @@ static int handle_join(struct modeNode *mNode) {
 
     if (IsLocal(user))
         return 0;
-    
+
     if (!(botlist = dict_find(helpserv_bots_bychan_dict, chan->name, NULL)))
         return 0;
 
@@ -4030,7 +4040,7 @@ static void handle_nickserv_rename(struct handle_info *handle, const char *old_h
         for (i=0; i < userlist->used; i++)
             dict_insert(userlist->list[i]->hs->users, handle->handle, userlist->list[i]);
     }
-    
+
     if (reqlist) {
         for (i=0; i < reqlist->used; i++) {
             struct helpserv_request *req=reqlist->list[i];
@@ -4104,6 +4114,7 @@ static void handle_nickserv_auth(struct userNode *user, struct handle_info *old_
                 for (j=1; j <= helper_reqs.used; j++) {
                     struct helpserv_request *req=helper_reqs.list[j-1];
                     char reqid[12], timestr[MAX_LINE_SIZE];
+                    time_t feh;
 
                     tbl.contents[j] = alloca(tbl.width * sizeof(**tbl.contents));
                     tbl.contents[j][0] = req->hs->helpserv->nick;
@@ -4111,7 +4122,8 @@ static void handle_nickserv_auth(struct userNode *user, struct handle_info *old_
                     tbl.contents[j][1] = strdup(reqid);
                     tbl.contents[j][2] = req->user ? req->user->nick : "Not online";
                     tbl.contents[j][3] = req->handle ? req->handle->handle : "Not authed";
-                    strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&req->opened));
+                    feh = req->opened;
+                    strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&feh));
                     tbl.contents[j][4] = strdup(timestr);
                 }
 
@@ -4374,7 +4386,7 @@ static void handle_nickserv_failpw(struct userNode *user, struct handle_info *ha
     }
 }
 
-static time_t helpserv_next_stats(time_t after_when) {
+static unsigned long helpserv_next_stats(time_t after_when) {
     struct tm *timeinfo = localtime(&after_when);
 
     /* This works because mktime(3) says it will accept out-of-range values
@@ -4388,15 +4400,17 @@ static time_t helpserv_next_stats(time_t after_when) {
 }
 
 /* If data != NULL, then don't add to the timeq */
-static void helpserv_run_stats(time_t when) {
-    struct tm when_s;
+static void helpserv_run_stats(unsigned long when) {
     struct helpserv_bot *hs;
     struct helpserv_user *hs_user;
+    time_t feh;
+    unsigned int day;
     int i;
     dict_iterator_t it, it2;
 
     last_stats_update = when;
-    localtime_r(&when, &when_s);
+    feh = when;
+    day = localtime(&feh)->tm_wday;
     for (it=dict_first(helpserv_bots_dict); it; it=iter_next(it)) {
         hs = iter_data(it);
 
@@ -4404,7 +4418,7 @@ static void helpserv_run_stats(time_t when) {
             hs_user = iter_data(it2);
 
             /* Skip the helper if it's not their week-start day. */
-            if (hs_user->week_start != when_s.tm_wday)
+            if (hs_user->week_start != day)
                 continue;
 
             /* Adjust their credit if they are in-channel at rollover. */
@@ -4453,8 +4467,6 @@ static void helpserv_db_cleanup(void) {
     dict_delete(helpserv_reqs_byhand_dict);
     dict_delete(helpserv_users_byhand_dict);
 
-    if (reqlog_ctx)
-        saxdb_close_context(reqlog_ctx);
     if (reqlog_f)
         fclose(reqlog_f);
 }
@@ -4530,7 +4542,7 @@ int helpserv_init() {
 
     helpserv_bots_dict = dict_new();
     dict_set_free_data(helpserv_bots_dict, helpserv_free_bot);
-    
+
     helpserv_bots_bychan_dict = dict_new();
     dict_set_free_data(helpserv_bots_bychan_dict, helpserv_botlist_free);
 
@@ -4548,7 +4560,7 @@ int helpserv_init() {
     /* Make up for downtime... though this will only really affect the
      * time_per_week */
     if (last_stats_update && (helpserv_next_stats(last_stats_update) < now)) {
-        time_t statsrun = last_stats_update;
+        unsigned long statsrun = last_stats_update;
         while ((statsrun = helpserv_next_stats(statsrun)) < now)
             helpserv_run_stats(statsrun);
     }