fixed multiple compiler warnings
[srvx.git] / src / mod-helpserv.c
index 08010cc37ec2c231d961e119e0a0a16665f4b272..bd1cf1c3ffde831e7cc1993e3efe6ad635adf984 100644 (file)
@@ -53,6 +53,7 @@ const char *helpserv_module_deps[] = { NULL };
 #define KEY_NICK "nick"
 #define KEY_DB_BADCHANS "badchans"
 #define KEY_HELP_CHANNEL "help_channel"
+#define KEY_PUBLIC_CHANNEL "public_channel"
 #define KEY_PAGE_DEST "page_dest"
 #define KEY_CMDWORD "cmdword"
 #define KEY_PERSIST_LENGTH "persist_length"
@@ -85,6 +86,7 @@ const char *helpserv_module_deps[] = { NULL };
 #define KEY_PRIVMSG_ONLY "privmsg_only"
 #define KEY_REQ_ON_JOIN "req_on_join"
 #define KEY_AUTO_VOICE "auto_voice"
+#define KEY_AUTO_JOIN "auto_join"
 #define KEY_AUTO_DEVOICE "auto_devoice"
 #define KEY_LAST_ACTIVE "last_active"
 
@@ -123,6 +125,10 @@ static const struct message_entry msgtab[] = {
     { "HSMSG_EXPIRATION_DONE", "%d eligible HelpServ bots have retired." },
     { "HSMSG_BAD_WEEKDAY", "I do not know which day of the week $b%s$b is." },
     { "HSMSG_WEEK_STARTS", "$b%s$b's weeks start on $b%s$b." },
+    { "HSMSG_MODSTATS_BAD_FIELD", "The specified field does not exist." },
+    { "HSMSG_MODSTATS_BAD_WEEK", "The specified week is invalid." },
+    { "HSMSG_MODSTATS_NEGATIVE", "This modification would result in a negative value." },
+    { "HSMSG_MODSTATS_SUCCESS", "$b%s$b's stats have been modified successfully." },
 
 /* Registration */
     { "HSMSG_ILLEGAL_NICK", "$b%s$b is an illegal nick; cannot use it." },
@@ -166,7 +172,9 @@ static const struct message_entry msgtab[] = {
     { "HSMSG_SET_PRIVMSGONLY",    "$bPrivmsgOnly     $b %s" },
     { "HSMSG_SET_REQONJOIN",      "$bReqOnJoin       $b %s" },
     { "HSMSG_SET_AUTOVOICE",      "$bAutoVoice       $b %s" },
+    { "HSMSG_SET_AUTOJOIN",       "$bAutoJoin        $b %s" },
     { "HSMSG_SET_AUTODEVOICE",    "$bAutoDevoice     $b %s" },
+                               { "HSMSG_SET_PUBLICCHAN",     "$bPublicChan      $b %s" },
     { "HSMSG_PAGE_NOTICE", "notice" },
     { "HSMSG_PAGE_PRIVMSG", "privmsg" },
     { "HSMSG_PAGE_ONOTICE", "onotice" },
@@ -477,6 +485,7 @@ static struct {
     const char *reqlogfile;
     unsigned long db_backup_frequency;
     unsigned int expire_age;
+    unsigned long modstats_level;
     char user_escape;
 } helpserv_conf;
 
@@ -495,6 +504,7 @@ struct helpserv_bot {
     struct userNode *helpserv;
 
     struct chanNode *helpchan;
+    struct chanNode *publicchan;
 
     struct chanNode *page_targets[PGSRC_COUNT];
     enum page_type page_types[PGSRC_COUNT];
@@ -503,7 +513,7 @@ struct helpserv_bot {
     enum notification_type notify;
 
     /* This is a default; it can be changed on a per-request basis */
-    enum persistence_type persist_types[PERSIST_T_COUNT];
+    enum persistence_length persist_lengths[PERSIST_T_COUNT];
 
     dict_t users; /* indexed by handle */
 
@@ -516,6 +526,7 @@ struct helpserv_bot {
     unsigned int privmsg_only : 1;
     unsigned int req_on_join : 1;
     unsigned int auto_voice : 1;
+    unsigned int auto_join : 1;
     unsigned int auto_devoice : 1;
 
     unsigned int helpchan_empty : 1;
@@ -630,7 +641,10 @@ static HELPSERV_FUNC(cmd_help);
 /* For messages going to helpers */
 # define helpserv_notice(target, ARGS...) send_message((target), (from_opserv ? opserv : hs->helpserv), ARGS)
 # define helpserv_notify(helper, ARGS...) do { struct userNode *_target; for (_target = (helper)->handle->users; _target; _target = _target->next_authed) { \
-        send_message(_target, (helper)->hs->helpserv, ARGS); \
+        if(!_target->next_authed || GetUserMode(helper->hs->helpchan, _target)) {\
+          send_message(_target, (helper)->hs->helpserv, ARGS); \
+          break; \
+        } \
     } } while (0)
 # define helpserv_page(TYPE, ARGS...) do { \
     int msg_type=0; struct chanNode *target=helpserv_get_page_type(hs, (TYPE), &msg_type); \
@@ -642,7 +656,10 @@ static HELPSERV_FUNC(cmd_help);
 /* For messages going to helpers */
 # define helpserv_notice(target, ...) send_message((target), (from_opserv ? opserv : hs->helpserv), __VA_ARGS__)
 # define helpserv_notify(helper, ...) do { struct userNode *_target; for (_target = (helper)->handle->users; _target; _target = _target->next_authed) { \
-        send_message(_target, (helper)->hs->helpserv, __VA_ARGS__); \
+        if(!_target->next_authed || GetUserMode(helper->hs->helpchan, _target)) {\
+          send_message(_target, (helper)->hs->helpserv, __VA_ARGS__); \
+          break; \
+        } \
     } } while (0)
 # define helpserv_page(TYPE, ...) do { \
     int msg_type=0; struct chanNode *target=helpserv_get_page_type(hs, (TYPE), &msg_type); \
@@ -958,7 +975,7 @@ static struct helpserv_request * create_request(struct userNode *user, struct he
         fmt = user_find_message(user, "HSMSG_REQ_NO_UNHANDLED");
         sprintf(lbuf[1], "%s", fmt);
     }
-    switch (hs->persist_types[PERSIST_T_REQUEST]) {
+    switch (hs->persist_lengths[PERSIST_T_REQUEST]) {
         case PERSIST_PART:
             fmt = user_find_message(user, "HSMSG_REQ_PERSIST_PART");
             sprintf(lbuf[2], fmt, hs->helpchan->name, hs->helpchan->name);
@@ -1049,8 +1066,11 @@ static void helpserv_usermsg(struct userNode *user, struct helpserv_bot *hs, con
             helpserv_msguser(user, "HSMSG_USERCMD_NO_REQUEST");
             return;
         }
-        if ((hs->persist_types[PERSIST_T_REQUEST] == PERSIST_PART) && !GetUserMode(hs->helpchan, user)) {
-            helpserv_msguser(user, "HSMSG_REQ_YOU_NOT_IN_HELPCHAN_OPEN", hs->helpchan->name);
+        if ((hs->persist_lengths[PERSIST_T_REQUEST] == PERSIST_PART) && !GetUserMode(hs->helpchan, user) && (!hs->publicchan || (hs->publicchan && !GetUserMode(hs->publicchan, user)))) {
+             if(hs->publicchan)
+                helpserv_msguser(user, "HSMSG_REQ_YOU_NOT_IN_HELPCHAN_OPEN", hs->publicchan->name);
+             else
+                helpserv_msguser(user, "HSMSG_REQ_YOU_NOT_IN_HELPCHAN_OPEN", hs->helpchan->name);
             return;
         }
 
@@ -1351,7 +1371,7 @@ static void helpserv_del_user(struct helpserv_bot *hs, struct helpserv_user *hs_
 }
 
 static int cmd_add_user(struct helpserv_bot *hs, int from_opserv, struct userNode *user, enum helpserv_level level, int argc, char *argv[]) {
-    struct helpserv_user *actor, *new_user;
+    struct helpserv_user *actor;
     struct handle_info *handle;
 
     REQUIRE_PARMS(2);
@@ -1379,7 +1399,7 @@ static int cmd_add_user(struct helpserv_bot *hs, int from_opserv, struct userNod
         return 0;
     }
 
-    new_user = helpserv_add_user(hs, handle, level);
+    helpserv_add_user(hs, handle, level);
 
     helpserv_notice(user, "HSMSG_ADDED_USER", helpserv_level2str(level), handle->handle);
     return 1;
@@ -1594,7 +1614,7 @@ static void free_request(void *data) {
     struct helpserv_request *req = data;
 
     /* Logging */
-    if (shutting_down && (req->hs->persist_types[PERSIST_T_REQUEST] != PERSIST_CLOSE || !req->handle)) {
+    if (shutting_down && (req->hs->persist_lengths[PERSIST_T_REQUEST] != PERSIST_CLOSE || !req->handle)) {
         helpserv_log_request(req, "srvx shutdown");
     }
 
@@ -1864,7 +1884,7 @@ static int helpserv_assign(int from_opserv, struct helpserv_bot *hs, struct user
 
     if (!user->handle_info)
         return 0;
-    if ((hs->persist_types[PERSIST_T_HELPER] == PERSIST_PART) && !GetUserMode(hs->helpchan, user)) {
+    if ((hs->persist_lengths[PERSIST_T_HELPER] == PERSIST_PART) && !GetUserMode(hs->helpchan, user)) {
         struct helpserv_user *hsuser_actor = GetHSUser(hs, actor->handle_info);
         if (hsuser_actor->level < HlManager) {
             helpserv_notice(user, "HSMSG_REQ_YOU_NOT_IN_HELPCHAN", hs->helpchan->name);
@@ -1891,7 +1911,11 @@ static int helpserv_assign(int from_opserv, struct helpserv_bot *hs, struct user
     req->helper = GetHSUser(hs, user->handle_info);
     assert(req->helper);
     req->assigned = now;
-
+    
+    if (req->user && hs->auto_join) {
+        irc_svsjoin(hs->helpserv,req->user,hs->helpchan);
+    }
+    
     if (old_helper) {
         helpserv_notice(user, "HSMSG_REQ_REASSIGNED", req->id, old_helper->handle->handle);
         req->helper->reassigned_to[0]++;
@@ -1930,7 +1954,7 @@ static int helpserv_assign(int from_opserv, struct helpserv_bot *hs, struct user
         if ((change.args[0].u.member = GetUserMode(hs->helpchan, req->user)))
             mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
     }
-
+    
     return 1;
 }
 
@@ -2018,7 +2042,7 @@ static HELPSERV_FUNC(cmd_reassign) {
         return 0;
     }
 
-    if ((hs->persist_types[PERSIST_T_HELPER] == PERSIST_PART) && !GetUserMode(hs->helpchan, user) && (hs_user->level < HlManager)) {
+    if ((hs->persist_lengths[PERSIST_T_HELPER] == PERSIST_PART) && !GetUserMode(hs->helpchan, user) && (hs_user->level < HlManager)) {
         helpserv_notice(user, "HSMSG_REQ_HIM_NOT_IN_HELPCHAN", targetuser->nick, hs->helpchan->name);
         return 0;
     }
@@ -2684,7 +2708,6 @@ static struct helpserv_bot *register_helpserv(const char *nick, const char *help
         dict_insert(helpserv_bots_bychan_dict, hs->helpchan->name, botlist);
     }
     helpserv_botlist_append(botlist, hs);
-
     return hs;
 }
 
@@ -2692,7 +2715,7 @@ static HELPSERV_FUNC(cmd_register) {
     char *nick, *helpchan, reason[MAXLEN];
     struct handle_info *handle;
 
-    REQUIRE_PARMS(4);
+    REQUIRE_PARMS(3);
     nick = argv[1];
     if (!is_valid_nick(nick)) {
         helpserv_notice(user, "HSMSG_ILLEGAL_NICK", nick);
@@ -2753,7 +2776,7 @@ static void helpserv_free_bot(void *data) {
 }
 
 static void helpserv_unregister(struct helpserv_bot *bot, const char *quit_fmt, const char *global_fmt, const char *actor) {
-    char reason[MAXLEN], channame[CHANNELLEN], botname[NICKLEN];
+    char reason[MAXLEN], channame[CHANNELLEN], *botname;
     struct helpserv_botlist *botlist;
     size_t len;
 
@@ -2761,8 +2784,7 @@ static void helpserv_unregister(struct helpserv_bot *bot, const char *quit_fmt,
     helpserv_botlist_remove(botlist, bot);
     if (!botlist->used)
         dict_remove(helpserv_bots_bychan_dict, bot->helpchan->name);
-    len = strlen(bot->helpserv->nick) + 1;
-    safestrncpy(botname, bot->helpserv->nick, len);
+    botname=bot->helpserv->nick;
     len = strlen(bot->helpchan->name) + 1;
     safestrncpy(channame, bot->helpchan->name, len);
     snprintf(reason, sizeof(reason), quit_fmt, actor);
@@ -2893,6 +2915,75 @@ static HELPSERV_FUNC(cmd_weekstart) {
     return changed;
 }
 
+static HELPSERV_FUNC(cmd_modstats) {
+    struct handle_info *hi;
+    struct helpserv_user *victim;
+    const char *field_name;
+    int week, mod;
+    unsigned int *field = NULL;
+    char *errptr;
+
+    REQUIRE_PARMS(5);
+    if (!oper_has_access(user, (from_opserv ? opserv : hs->helpserv), helpserv_conf.modstats_level, 0))
+        return 0;
+    if (!(hi = helpserv_get_handle_info(user, argv[1])))
+        return 0;
+    if (!(victim = GetHSUser(hs, hi))) {
+        helpserv_notice(user, "HSMSG_NOT_IN_USERLIST", hi->handle, hs->helpserv->nick);
+        return 0;
+    }
+
+    field_name = argv[2];
+    if (!strcasecmp(argv[3], "total"))
+        week = 4;
+    else if(!strcasecmp(argv[3], "current"))
+        week = 0;
+    else {
+        week = strtoul(argv[3], &errptr, 0);
+        if (*errptr != '\0') {
+            helpserv_notice(user, "HSMSG_MODSTATS_BAD_WEEK");
+            return 0;
+        }
+    }
+    mod = strtol(argv[4], NULL, 0);
+
+    if (week < 0 || week > 4) {
+        helpserv_notice(user, "HSMSG_MODSTATS_BAD_WEEK");
+        return 0;
+    }
+
+    if (!strcasecmp(field_name, "time")) {
+        if (victim->join_time && (week == 0 || week == 4)) {
+            victim->time_per_week[0] += now - victim->join_time;
+            victim->time_per_week[4] += now - victim->join_time;
+            victim->join_time = now;
+        }
+        field = victim->time_per_week;
+    }
+    else if (!strcasecmp(field_name, "picked") || !strcasecmp(field_name, "picked_up") || !strcasecmp(field_name, "reqs"))
+        field = victim->picked_up;
+    else if (!strcasecmp(field_name, "closed"))
+        field = victim->closed;
+    else if (!strcasecmp(field_name, "ra_from") || !strcasecmp(field_name, "reassigned_from"))
+        field = victim->reassigned_from;
+    else if (!strcasecmp(field_name, "ra_to") || !strcasecmp(field_name, "reassigned_to"))
+        field = victim->reassigned_to;
+    else {
+        helpserv_notice(user, "HSMSG_MODSTATS_BAD_FIELD");
+        return 0;
+    }
+
+    if (mod < 0 && mod < -(int)field[week]) {
+        helpserv_notice(user, "HSMSG_MODSTATS_NEGATIVE");
+        return 0;
+    }
+
+    field[week] += mod;
+    helpserv_notice(user, "HSMSG_MODSTATS_SUCCESS", victim->handle->handle);
+
+    return (mod != 0);
+}
+
 static void set_page_target(struct helpserv_bot *hs, enum page_source idx, const char *target) {
     struct chanNode *new_target, *old_target;
 
@@ -3119,11 +3210,11 @@ static int opt_persist(struct userNode *user, struct helpserv_bot *hs, int from_
             helpserv_notice(user, "HSMSG_INVALID_OPTION", argv[0]);
             return 0;
         }
-        hs->persist_types[idx] = new_pers;
+        hs->persist_lengths[idx] = new_pers;
         changed = 1;
     }
-    helpserv_notice(user, persistence_types[idx].print_name,
-                    user_find_message(user, persistence_lengths[hs->persist_types[idx]].print_name));
+    helpserv_notice(user, persistence_lengths[idx].print_name,
+                    user_find_message(user, persistence_lengths[hs->persist_lengths[idx]].print_name));
     return changed;
 }
 
@@ -3211,23 +3302,72 @@ static HELPSERV_OPTION(opt_auto_voice) {
     OPTION_BINARY(hs->auto_voice, "HSMSG_SET_AUTOVOICE");
 }
 
+static HELPSERV_OPTION(opt_auto_join) {
+    OPTION_BINARY(hs->auto_join, "HSMSG_SET_AUTOJOIN");
+}
+
 static HELPSERV_OPTION(opt_auto_devoice) {
     OPTION_BINARY(hs->auto_devoice, "HSMSG_SET_AUTODEVOICE");
 }
 
+static HELPSERV_OPTION(opt_publicchan) {
+ char *publicchan;
+ int changed=0;
+ if (argc > 0) {
+  publicchan = argv[0];
+               if(strcmp(publicchan, "*")) {
+         if (!IsChannelName(publicchan)) {
+    helpserv_notice(user, "HSMSG_ILLEGAL_CHANNEL", publicchan);
+    HELPSERV_SYNTAX();
+    return 0;
+   }
+   if (opserv_bad_channel(publicchan)) {
+    helpserv_notice(user, "HSMSG_ILLEGAL_CHANNEL", publicchan);
+    return 0;
+   }
+               }
+               if (!hs->publicchan || (hs->publicchan && irccasecmp(hs->publicchan->name, publicchan))) {
+   if(hs->publicchan) {
+                 //there is another public chan o.O
+                        //part
+                        DelChannelUser(hs->helpserv, hs->publicchan, "unregistered.", 0);
+                               hs->publicchan = NULL;
+                }
+                       changed = 1;
+                       if(strcmp(publicchan, "*")) {
+                        if (!(hs->publicchan = GetChannel(publicchan))) {
+     hs->publicchan = AddChannel(publicchan, now, NULL, NULL);
+     AddChannelUser(hs->helpserv, hs->publicchan)->modes |= MODE_CHANOP;
+    } else {
+     struct mod_chanmode change;
+     mod_chanmode_init(&change);
+     change.argc = 1;
+     change.args[0].mode = MODE_CHANOP;
+     change.args[0].u.member = AddChannelUser(hs->helpserv, hs->publicchan);
+     mod_chanmode_announce(hs->helpserv, hs->publicchan, &change);
+                        }
+                       }
+  }
+ } else {
+        changed = 0;
+       }
+       helpserv_notice(user, "HSMSG_SET_PUBLICCHAN", (hs->publicchan) ? hs->publicchan->name : user_find_message(user,"MSG_NONE")); \
+ return changed;
+}
+
 static HELPSERV_FUNC(cmd_set) {
     helpserv_option_func_t *opt;
 
     if (argc < 2) {
         unsigned int i;
         helpserv_option_func_t *display[] = {
-            opt_pagetarget_command, opt_pagetarget_alert, opt_pagetarget_status,
+            opt_publicchan, opt_pagetarget_command, opt_pagetarget_alert, opt_pagetarget_status,
             opt_pagetype, opt_alert_page_type, opt_status_page_type,
             opt_greeting, opt_req_opened, opt_req_assigned, opt_req_closed,
             opt_idle_delay, opt_whine_delay, opt_whine_interval,
             opt_empty_interval, opt_stale_delay, opt_request_persistence,
             opt_helper_persistence, opt_notification, opt_id_wrap,
-            opt_req_maxlen, opt_privmsg_only, opt_req_on_join, opt_auto_voice,
+            opt_req_maxlen, opt_privmsg_only, opt_req_on_join, opt_auto_voice, opt_auto_join,
             opt_auto_devoice
         };
 
@@ -3506,7 +3646,7 @@ helpserv_bot_write(const char *key, void *data, void *extra) {
     saxdb_end_record(ctx);
 
     /* Open requests */
-    if (hs->persist_types[PERSIST_T_REQUEST] == PERSIST_CLOSE) {
+    if (hs->persist_lengths[PERSIST_T_REQUEST] == PERSIST_CLOSE) {
         saxdb_start_record(ctx, KEY_REQUESTS, 0);
         dict_foreach(hs->requests, request_write_helper, ctx);
         saxdb_end_record(ctx);
@@ -3514,6 +3654,7 @@ helpserv_bot_write(const char *key, void *data, void *extra) {
 
     /* Other settings and state */
     saxdb_write_string(ctx, KEY_HELP_CHANNEL, hs->helpchan->name);
+    if(hs->publicchan) saxdb_write_string(ctx, KEY_PUBLIC_CHANNEL, hs->publicchan->name);
     slist = alloc_string_list(PGSRC_COUNT);
     for (pagesrc=0; pagesrc<PGSRC_COUNT; pagesrc++) {
         struct chanNode *target = hs->page_targets[pagesrc];
@@ -3536,7 +3677,7 @@ helpserv_bot_write(const char *key, void *data, void *extra) {
         saxdb_write_int(ctx, interval_types[inttype].db_name, hs->intervals[inttype]);
     }
     for (persisttype=0; persisttype<PERSIST_T_COUNT; persisttype++) {
-        const char *persist = persistence_lengths[hs->persist_types[persisttype]].db_name;
+        const char *persist = persistence_lengths[hs->persist_lengths[persisttype]].db_name;
         saxdb_write_string(ctx, persistence_types[persisttype].db_name, persist);
     }
     saxdb_write_string(ctx, KEY_NOTIFICATION, notification_types[hs->notify].db_name);
@@ -3549,6 +3690,7 @@ helpserv_bot_write(const char *key, void *data, void *extra) {
     saxdb_write_int(ctx, KEY_PRIVMSG_ONLY, hs->privmsg_only);
     saxdb_write_int(ctx, KEY_REQ_ON_JOIN, hs->req_on_join);
     saxdb_write_int(ctx, KEY_AUTO_VOICE, hs->auto_voice);
+    saxdb_write_int(ctx, KEY_AUTO_JOIN, hs->auto_join);
     saxdb_write_int(ctx, KEY_AUTO_DEVOICE, hs->auto_devoice);
     saxdb_write_int(ctx, KEY_LAST_ACTIVE, hs->last_active);
 
@@ -3569,7 +3711,7 @@ helpserv_saxdb_write(struct saxdb_context *ctx) {
 static int helpserv_bot_read(const char *key, void *data, UNUSED_ARG(void *extra)) {
     struct record_data *br = data, *raw_record;
     struct helpserv_bot *hs;
-    char *registrar, *helpchannel_name, *str;
+    char *registrar, *helpchannel_name, *publicchannel_name, *str;
     dict_t users, requests;
     enum page_source pagesrc;
     enum message_type msgtype;
@@ -3590,6 +3732,25 @@ static int helpserv_bot_read(const char *key, void *data, UNUSED_ARG(void *extra
 
     hs = register_helpserv(key, helpchannel_name, registrar);
 
+                               publicchannel_name = database_get_data(GET_RECORD_OBJECT(br), KEY_PUBLIC_CHANNEL, RECDB_QSTRING);
+    if (publicchannel_name) {
+                                if(!IsChannelName(publicchannel_name)) {
+        log_module(HS_LOG, LOG_ERROR, "%s has an invalid channel name.", key);
+        return 0;
+                                       } else {
+                                        if (!(hs->publicchan = GetChannel(publicchannel_name))) {
+       hs->publicchan = AddChannel(publicchannel_name, now, NULL, NULL);
+       AddChannelUser(hs->helpserv, hs->publicchan)->modes |= MODE_CHANOP;
+      } else {
+       struct mod_chanmode change;
+       mod_chanmode_init(&change);
+       change.argc = 1;
+       change.args[0].mode = MODE_CHANOP;
+       change.args[0].u.member = AddChannelUser(hs->helpserv, hs->publicchan);
+       mod_chanmode_announce(hs->helpserv, hs->publicchan, &change);
+                                               }
+                                       }
+    }
     raw_record = dict_find(GET_RECORD_OBJECT(br), KEY_PAGE_DEST, NULL);
     switch (raw_record ? raw_record->type : RECDB_INVALID) {
     case RECDB_QSTRING:
@@ -3634,7 +3795,7 @@ static int helpserv_bot_read(const char *key, void *data, UNUSED_ARG(void *extra
 
     for (persisttype=0; persisttype<PERSIST_T_COUNT; persisttype++) {
         str = database_get_data(GET_RECORD_OBJECT(br), persistence_types[persisttype].db_name, RECDB_QSTRING);
-        hs->persist_types[persisttype] = str ? persistence_from_name(str) : PERSIST_QUIT;
+        hs->persist_lengths[persisttype] = str ? persistence_from_name(str) : PERSIST_QUIT;
     }
     str = database_get_data(GET_RECORD_OBJECT(br), KEY_NOTIFICATION, RECDB_QSTRING);
     hs->notify = str ? notification_from_name(str) : NOTIFY_NONE;
@@ -3656,6 +3817,8 @@ static int helpserv_bot_read(const char *key, void *data, UNUSED_ARG(void *extra
     hs->req_on_join = str ? enabled_string(str) : 0;
     str = database_get_data(GET_RECORD_OBJECT(br), KEY_AUTO_VOICE, RECDB_QSTRING);
     hs->auto_voice = str ? enabled_string(str) : 0;
+    str = database_get_data(GET_RECORD_OBJECT(br), KEY_AUTO_JOIN, RECDB_QSTRING);
+    hs->auto_join = str ? enabled_string(str) : 0;
     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);
@@ -3707,6 +3870,8 @@ static void helpserv_conf_read(void) {
 
     str = database_get_data(conf_node, "expiration", RECDB_QSTRING);
     helpserv_conf.expire_age = ParseInterval(str ? str : "60d");
+    str = database_get_data(conf_node, "modstats_level", RECDB_QSTRING);
+    helpserv_conf.modstats_level = str ? strtoul(str, NULL, 0) : 850;
     str = database_get_data(conf_node, "user_escape", RECDB_QSTRING);
     helpserv_conf.user_escape = str ? str[0] : '@';
 
@@ -3748,13 +3913,15 @@ static void handle_part(struct modeNode *mn, UNUSED_ARG(const char *reason)) {
             hs = botlist->list[i];
             if (!hs->helpserv)
                 continue;
-            if (hs->persist_types[PERSIST_T_REQUEST] != PERSIST_PART)
+            if (hs->persist_lengths[PERSIST_T_REQUEST] != PERSIST_PART)
                 continue;
 
             for (it=dict_first(hs->requests); it; it=iter_next(it)) {
                 struct helpserv_request *req = iter_data(it);
 
                 if (mn->user != req->user)
+                    continue;
+                                                                                                                               if (GetUserMode(hs->helpchan, mn->user)) //publicchan
                     continue;
                 if (req->text->used) {
                     helpserv_message(hs, mn->user, MSGTYPE_REQ_DROPPED);
@@ -3791,7 +3958,7 @@ static void handle_part(struct modeNode *mn, UNUSED_ARG(const char *reason)) {
             for (it=dict_first(hs->requests); it; it=iter_next(it)) {
                 struct helpserv_request *req=iter_data(it);
 
-                if ((hs->persist_types[PERSIST_T_HELPER] == PERSIST_PART)
+                if ((hs->persist_lengths[PERSIST_T_HELPER] == PERSIST_PART)
                     && (req->helper == hs_user)) {
                     char our_reason[CHANNELLEN + 8];
                     sprintf(our_reason, "parted %s", mn->channel->name);
@@ -3848,7 +4015,7 @@ static void handle_quit(struct userNode *user, UNUSED_ARG(struct userNode *kille
         for (i=0; i < n; i++) {
             struct helpserv_request *req = reqlist->list[0];
 
-            if ((req->hs->persist_types[PERSIST_T_REQUEST] == PERSIST_QUIT) || !req->handle) {
+            if ((req->hs->persist_lengths[PERSIST_T_REQUEST] == PERSIST_QUIT) || !req->handle) {
                 char buf[12];
                 sprintf(buf, "%lu", req->id);
 
@@ -3882,7 +4049,7 @@ static void handle_quit(struct userNode *user, UNUSED_ARG(struct userNode *kille
             for (it=dict_first(hs->requests); it; it=iter_next(it)) {
                 struct helpserv_request *req=iter_data(it);
 
-                if ((hs->persist_types[PERSIST_T_HELPER] == PERSIST_QUIT) && (req->helper == hs_user)) {
+                if ((hs->persist_lengths[PERSIST_T_HELPER] == PERSIST_QUIT) && (req->helper == hs_user)) {
                     helpserv_page_helper_gone(hs, req, "disconnected");
                 }
             }
@@ -3894,7 +4061,6 @@ static void associate_requests_bybot(struct helpserv_bot *hs, struct userNode *u
     struct helpserv_reqlist *reqlist, *hand_reqlist=NULL;
     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;
@@ -3945,6 +4111,7 @@ static void associate_requests_bybot(struct helpserv_bot *hs, struct userNode *u
         if (!nicknewest || (nicknewest->opened < req->opened))
             nicknewest = req;
 
+        
         if (hs->auto_voice && req->helper)
         {
             struct mod_chanmode change;
@@ -3959,7 +4126,7 @@ static void associate_requests_bybot(struct helpserv_bot *hs, struct userNode *u
     if ((force_greet && nicknewest) || (newest && (nicknewest == newest))) {
         /* Let the user know. Either the user is forced to be greeted, or the
          * above has changed which request will get their next message. */
-        helpserv_msguser(user, "HSMSG_GREET_EXISTING_REQ", hs->helpchan->name, nicknewest->id);
+        //helpserv_msguser(user, "HSMSG_GREET_EXISTING_REQ", hs->helpchan->name, nicknewest->id);
     }
 }
 
@@ -4024,7 +4191,7 @@ static int handle_join(struct modeNode *mNode) {
 
             if ((reqlist = dict_find(helpserv_reqs_bynick_dict, user->nick, NULL))) {
                 for (j=0; j < reqlist->used; j++)
-                    if (reqlist->list[i]->hs == hs)
+                    if (reqlist->list[j]->hs == hs)
                         break;
                 if (j < reqlist->used)
                     continue;
@@ -4203,7 +4370,7 @@ static void handle_nickserv_auth(struct userNode *user, struct handle_info *old_
         struct helpserv_request *req = reqlist->list[i];
         struct helpserv_bot *hs=req->hs;
 
-        if (!old_handle || hs->persist_types[PERSIST_T_REQUEST] == PERSIST_PART || hs->persist_types[PERSIST_T_REQUEST] == PERSIST_QUIT) {
+        if (!old_handle || hs->persist_lengths[PERSIST_T_REQUEST] == PERSIST_PART || hs->persist_lengths[PERSIST_T_REQUEST] == PERSIST_QUIT) {
             /* The request needs to be assigned to the new handle; either it
              * only persists until part/quit (so it makes sense to keep it as
              * close to the user as possible, and if it's made persistent later
@@ -4227,7 +4394,7 @@ static void handle_nickserv_auth(struct userNode *user, struct handle_info *old_
             if (old_handle) {
                 char buf[CHANNELLEN + 14];
 
-                if (hs->persist_types[PERSIST_T_REQUEST] == PERSIST_PART) {
+                if (hs->persist_lengths[PERSIST_T_REQUEST] == PERSIST_PART) {
                     sprintf(buf, "part channel %s", hs->helpchan->name);
                 } else {
                     strcpy(buf, "quit irc");
@@ -4330,7 +4497,7 @@ static void handle_nickserv_unreg(struct userNode *user, struct handle_info *han
                 }
                 helpserv_reqlist_append(req->parent_nick_list, req);
 
-                if (hs->persist_types[PERSIST_T_REQUEST] == PERSIST_CLOSE)
+                if (hs->persist_lengths[PERSIST_T_REQUEST] == PERSIST_CLOSE)
                     helpserv_msguser(req->user, "HSMSG_REQ_WARN_UNREG", handle->handle, hs->helpchan->name, req->id);
             } else {
                 if (handle->users) {
@@ -4549,6 +4716,7 @@ int helpserv_init() {
     helpserv_define_func("BOTS", cmd_bots, HlOper, CMD_FROM_OPSERV_ONLY|CMD_IGNORE_EVENT);
     helpserv_define_func("EXPIRE", cmd_expire, HlOper, CMD_FROM_OPSERV_ONLY);
     helpserv_define_func("WEEKSTART", cmd_weekstart, HlTrial, CMD_NEED_BOT);
+    helpserv_define_func("MODSTATS", cmd_modstats, HlOwner, CMD_NEED_BOT);
 
     helpserv_option_dict = dict_new();
     helpserv_define_option("PAGETARGET", opt_pagetarget_command);
@@ -4575,7 +4743,9 @@ int helpserv_init() {
     helpserv_define_option("PRIVMSGONLY", opt_privmsg_only);
     helpserv_define_option("REQONJOIN", opt_req_on_join);
     helpserv_define_option("AUTOVOICE", opt_auto_voice);
+    helpserv_define_option("AUTOJOIN", opt_auto_join);
     helpserv_define_option("AUTODEVOICE", opt_auto_devoice);
+                               helpserv_define_option("PUBLICCHAN", opt_publicchan);
 
     helpserv_usercmd_dict = dict_new();
     dict_insert(helpserv_usercmd_dict, "WAIT", usercmd_wait);