Document the ChanServ toy commands.
[srvx.git] / src / mod-helpserv.c
index 08010cc37ec2c231d961e119e0a0a16665f4b272..192fea5a8a2f19003b8340eaef7870ad10a4aa92 100644 (file)
@@ -123,6 +123,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." },
@@ -477,6 +481,7 @@ static struct {
     const char *reqlogfile;
     unsigned long db_backup_frequency;
     unsigned int expire_age;
+    unsigned long modstats_level;
     char user_escape;
 } helpserv_conf;
 
@@ -503,7 +508,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 */
 
@@ -630,7 +635,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 +650,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 +969,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,7 +1060,7 @@ 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)) {
+        if ((hs->persist_lengths[PERSIST_T_REQUEST] == PERSIST_PART) && !GetUserMode(hs->helpchan, user)) {
             helpserv_msguser(user, "HSMSG_REQ_YOU_NOT_IN_HELPCHAN_OPEN", hs->helpchan->name);
             return;
         }
@@ -1594,7 +1605,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 +1875,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);
@@ -2018,7 +2029,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;
     }
@@ -2893,6 +2904,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 +3199,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;
 }
 
@@ -3506,7 +3586,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);
@@ -3536,7 +3616,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);
@@ -3634,7 +3714,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;
@@ -3707,6 +3787,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,7 +3830,7 @@ 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)) {
@@ -3791,7 +3873,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 +3930,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 +3964,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");
                 }
             }
@@ -4024,7 +4106,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 +4285,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 +4309,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 +4412,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 +4631,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);