Merge remote branch 'upstream/master'
[srvx.git] / src / nickserv.c
index f8941033ec820bfa388e0035707808e01ecdfc57..a370fd4463d923d00bb6e1dd12235190501c8de9 100644 (file)
@@ -85,6 +85,8 @@
 #define KEY_REGISTER_ON "register"
 #define KEY_LAST_SEEN "lastseen"
 #define KEY_INFO "info"
+#define KEY_DEVNULL "devnull"
+#define KEY_WEBSITE "website"
 #define KEY_USERLIST_STYLE "user_style"
 #define KEY_SCREEN_WIDTH "screen_width"
 #define KEY_LAST_AUTHED_HOST "last_authed_host"
@@ -212,6 +214,9 @@ static const struct message_entry msgtab[] = {
     { "NSMSG_HANDLEINFO_COOKIE_ALLOWAUTH", "  Cookie: There is currently an allowauth cookie issued for this account" },
     { "NSMSG_HANDLEINFO_COOKIE_UNKNOWN", "  Cookie: There is currently an unknown cookie issued for this account" },
     { "NSMSG_HANDLEINFO_INFOLINE", "  Infoline: %s" },
+    { "NSMSG_HANDLEINFO_DEVNULL", "  DevNull Class: %s" },
+    { "NSMSG_HANDLEINFO_WEBSITE", "  Website: %s" },
+    { "NSMSG_HANDLEINFO_ACCESS", "  Access: %i" },
     { "NSMSG_HANDLEINFO_FLAGS", "  Flags: %s" },
     { "NSMSG_HANDLEINFO_EPITHET", "  Epithet: %s" },
     { "NSMSG_HANDLEINFO_FAKEIDENT", "  Fake ident: %s" },
@@ -305,6 +310,9 @@ static const struct message_entry msgtab[] = {
     { "NSMSG_SETTING_LIST", "$b$N account settings:$b" },
     { "NSMSG_INVALID_OPTION", "$b%s$b is an invalid account setting." },
     { "NSMSG_SET_INFO", "$bINFO:         $b%s" },
+    { "NSMSG_SET_DEVNULL", "$bDEVNULL:      $b%s" },
+    { "NSMSG_SET_AUTOHIDE", "$bAUTOHIDE:     $b%s" },
+    { "NSMSG_SET_WEBSITE", "$bWEBSITE:      $b%s" },
     { "NSMSG_SET_WIDTH", "$bWIDTH:        $b%d" },
     { "NSMSG_SET_TABLEWIDTH", "$bTABLEWIDTH:   $b%d" },
     { "NSMSG_SET_COLOR", "$bCOLOR:        $b%s" },
@@ -463,6 +471,8 @@ register_handle(const char *handle, const char *passwd, unsigned long id)
     dict_insert(nickserv_handle_dict, hi->handle, hi);
 
     hi->id = id;
+    hi->website = NULL;
+    hi->devnull = NULL;
     dict_insert(nickserv_id_dict, strdup(id_base64), hi);
 
     return hi;
@@ -549,6 +559,8 @@ free_handle_info(void *vhi)
     free(hi->infoline);
     free(hi->epithet);
     free(hi->fakehost);
+    free(hi->devnull);
+    free(hi->website);
     free(hi->fakeident);
     if (hi->cookie) {
         timeq_del(hi->cookie->expires, nickserv_free_cookie, hi->cookie, 0);
@@ -785,7 +797,7 @@ valid_user_for(struct userNode *user, struct handle_info *hi)
     unsigned int ii;
 
     /* If no hostmasks on the account, allow it. */
-    if (!hi->masks->used)
+    if (!hi->masks->used || IsDummy(user))
         return 1;
     /* If any hostmask matches, allow it. */
     for (ii=0; ii<hi->masks->used; ii++)
@@ -891,6 +903,10 @@ generate_fakehost(struct handle_info *handle)
         /* A leading dot indicates the stored value is actually a title. */
         snprintf(buffer, sizeof(buffer), "%s.%s.%s", handle->handle, handle->fakehost+1, titlehost_suffix);
         return buffer;
+    } else if (handle->fakehost[0] == '$') {
+        /* A leading $ indicates the stored value begins with the user handle. */
+        snprintf(buffer, sizeof(buffer), "%s%s", handle->handle, handle->fakehost+1);
+        return buffer;
     }
     return handle->fakehost;
 }
@@ -1395,6 +1411,12 @@ static NICKSERV_FUNC(cmd_handleinfo)
     }
 
     reply("NSMSG_HANDLEINFO_INFOLINE", (hi->infoline ? hi->infoline : nsmsg_none));
+    if (oper_has_access(user, cmd->parent->bot, 200, 1))
+        reply("NSMSG_HANDLEINFO_DEVNULL", (hi->devnull ? hi->devnull : nsmsg_none));
+    if (user->handle_info && HANDLE_FLAGGED(user->handle_info, BOT))
+        reply("NSMSG_HANDLEINFO_WEBSITE", (hi->website ? hi->website : nsmsg_none));
+    if(hi->opserv_level > 0 && user->handle_info && HANDLE_FLAGGED(user->handle_info, BOT))
+        reply("NSMSG_HANDLEINFO_ACCESS", hi->opserv_level);
     if (HANDLE_FLAGGED(hi, FROZEN))
         reply("NSMSG_HANDLEINFO_VACATION");
 
@@ -1659,6 +1681,7 @@ static NICKSERV_FUNC(cmd_rename_handle)
     reply("NSMSG_HANDLE_CHANGED", old_handle, hi->handle);
     global_message(MESSAGE_RECIPIENT_STAFF, msgbuf);
     free(old_handle);
+    apply_fakehost(hi, NULL);
     return 1;
 }
 
@@ -1779,6 +1802,10 @@ static NICKSERV_FUNC(cmd_auth)
             return 1;
         }
     }
+    if (HANDLE_FLAGGED(hi, AUTOHIDE)) {
+        //ok  we have a fakehost set... but we need to set mode +x
+        irc_svsmode(nickserv,user,"+x");
+    }
 
     set_user_handle_info(user, hi, 1);
     if (nickserv_conf.email_required && !hi->email_addr)
@@ -1798,6 +1825,65 @@ static NICKSERV_FUNC(cmd_auth)
     return 1;
 }
 
+struct handle_info *checklogin(const char *user, const char *pass, const char *numeric, const char *hostmask, const char *ipmask)
+{
+    struct handle_info *hi;
+    unsigned int match = 0, ii = 0;
+    hi = dict_find(nickserv_handle_dict, user, NULL);
+    if(!hi)
+        return NULL;
+    /* If no hostmasks on the account, allow it. */
+    if (hi->masks->used) {
+        /* If any hostmask matches, allow it. */
+        for (ii=0; ii<hi->masks->used; ii++)
+          if (match_ircglob(hostmask, hi->masks->list[ii]) || match_ircglob(ipmask, hi->masks->list[ii])) {
+            match = 1;
+            break;
+          }
+        if(!match) 
+          return NULL;
+    }
+    if(!checkpass(pass, hi->passwd))
+        return NULL;
+    if (HANDLE_FLAGGED(hi, SUSPENDED))
+        return NULL;
+    /** following in one of the next commits
+    struct last_login *login,*clogin,*old;
+    unsigned int ii = 0;
+    login = calloc(1, sizeof(*login));
+    login->last_login = hi->last_login;
+    login->hostmask = strdup(hostmask);
+    login->authtime = now;
+    login->quittime = 0;
+    login->quit = NULL;
+    login->user = NULL;
+    login->loc_pending = strdup(numeric);
+    for (clogin = hi->last_login; clogin != NULL && ii < 9; clogin = clogin->last_login) {
+        if(ii == 8 && clogin->last_login) {
+            old = clogin->last_login;
+            clogin->last_login = NULL;
+            free(old->hostmask);
+            if(old->quit)
+                free(old->quit);
+            if(old->loc_pending)
+                free(old->loc_pending);
+            free(old);
+        }
+    }
+    hi->last_login = login;
+    */
+    return hi;
+}
+
+char *getfakehost(const char *user)
+{
+    struct handle_info *hi;
+    hi = dict_find(nickserv_handle_dict, user, NULL);
+    if(!hi)
+        return 0;
+    return generate_fakehost(hi);
+}
+
 static allowauth_func_t *allowauth_func_list;
 static unsigned int allowauth_func_size = 0, allowauth_func_used = 0;
 
@@ -2271,7 +2357,7 @@ set_list(struct userNode *user, struct handle_info *hi, int override)
     unsigned int i;
     char *set_display[] = {
         "INFO", "WIDTH", "TABLEWIDTH", "COLOR", "PRIVMSG", "STYLE",
-        "EMAIL", "MAXLOGINS", "LANGUAGE"
+        "EMAIL", "MAXLOGINS", "LANGUAGE", "DEVNULL"
     };
 
     send_message(user, nickserv, "NSMSG_SETTING_LIST");
@@ -2346,6 +2432,80 @@ static OPTION_FUNC(opt_info)
     return 1;
 }
 
+static OPTION_FUNC(opt_devnull)
+{
+    const char *devnull;
+    
+    if (argc > 1) {
+        if (!override) {
+            send_message(user, nickserv, "MSG_SETTING_PRIVILEGED", argv[0]);
+            return 0;
+        }
+        if ((argv[1][0] == '*') && (argv[1][1] == 0)) {
+            free(hi->devnull);
+            hi->devnull = NULL;
+        } else {
+            devnull = unsplit_string(argv+1, argc-1, NULL);
+            if(devnull_check(devnull) == 1) { 
+                hi->devnull = strdup(devnull);
+            }
+        }
+    }
+
+    devnull = hi->devnull ? hi->devnull : user_find_message(user, "MSG_NONE");
+    send_message(user, nickserv, "NSMSG_SET_DEVNULL", devnull);
+    return 1;
+}
+
+void nickserv_devnull_delete(char *name) {
+    dict_iterator_t it;
+    struct handle_info *hi;
+
+    for (it = dict_first(nickserv_handle_dict); it; it = iter_next(it)) {
+        hi = iter_data(it);
+        if (hi->devnull && !irccasecmp(name, hi->devnull)) {
+            free(hi->devnull);
+            hi->devnull = NULL;
+        }
+    }
+}
+
+void nickserv_devnull_rename(char *oldname, char *newname) {
+    dict_iterator_t it;
+    struct handle_info *hi;
+
+    for (it = dict_first(nickserv_handle_dict); it; it = iter_next(it)) {
+        hi = iter_data(it);
+        if (hi->devnull && !irccasecmp(oldname, hi->devnull)) {
+            hi->devnull = strdup(newname);
+        }
+    }
+}
+
+static OPTION_FUNC(opt_website)
+{
+    const char *website;
+    
+    if (argc > 1) {
+        if (!HANDLE_FLAGGED(user->handle_info, BOT)) {
+            send_message(user, nickserv, "MSG_SETTING_PRIVILEGED", argv[0]);
+            return 0;
+        }
+        if ((argv[1][0] == '*') && (argv[1][1] == 0)) {
+            free(hi->website);
+            hi->website = NULL;
+        } else {
+            website = unsplit_string(argv+1, argc-1, NULL);
+            hi->website = strdup(website);
+        }
+    }
+    if (HANDLE_FLAGGED(user->handle_info, BOT)) {
+        website = hi->website ? hi->website : user_find_message(user, "MSG_NONE");
+        send_message(user, nickserv, "NSMSG_SET_WEBSITE", website);
+    }
+    return 1;
+}
+
 static OPTION_FUNC(opt_width)
 {
     if (argc > 1)
@@ -2408,6 +2568,23 @@ static OPTION_FUNC(opt_privmsg)
     return 1;
 }
 
+static OPTION_FUNC(opt_autohide)
+{
+    if (argc > 1) {
+        if (enabled_string(argv[1]))
+            HANDLE_SET_FLAG(hi, AUTOHIDE);
+        else if (disabled_string(argv[1]))
+            HANDLE_CLEAR_FLAG(hi, AUTOHIDE);
+        else {
+            send_message(user, nickserv, "MSG_INVALID_BINARY", argv[1]);
+            return 0;
+        }
+    }
+
+    send_message(user, nickserv, "NSMSG_SET_AUTOHIDE", user_find_message(user, HANDLE_FLAGGED(hi, AUTOHIDE) ? "MSG_ON" : "MSG_OFF"));
+    return 1;
+}
+
 static OPTION_FUNC(opt_style)
 {
     char *style;
@@ -3072,6 +3249,10 @@ nickserv_saxdb_write(struct saxdb_context *ctx) {
         saxdb_write_int(ctx, KEY_ID, hi->id);
         if (hi->infoline)
             saxdb_write_string(ctx, KEY_INFO, hi->infoline);
+        if (hi->devnull)
+            saxdb_write_string(ctx, KEY_DEVNULL, hi->devnull);
+        if (hi->website)
+            saxdb_write_string(ctx, KEY_WEBSITE, hi->website);
         if (hi->last_quit_host[0])
             saxdb_write_string(ctx, KEY_LAST_QUIT_HOST, hi->last_quit_host);
         saxdb_write_int(ctx, KEY_LAST_SEEN, hi->lastseen);
@@ -3261,6 +3442,8 @@ struct nickserv_discrim {
     const char *hostmask;
     const char *fakehostmask;
     const char *fakeidentmask;
+    const char *website;
+    const char *devnullclass;
     const char *handlemask;
     const char *emailmask;
 };
@@ -3366,6 +3549,18 @@ nickserv_discrim_create(struct userNode *user, unsigned int argc, char *argv[])
             } else {
                 discrim->fakeidentmask = argv[i];
             }
+        } else if (!irccasecmp(argv[i], "website")) {
+            if (!irccasecmp(argv[++i], "*")) {
+                discrim->website = 0;
+            } else {
+                discrim->website = argv[i];
+            }
+        } else if (!irccasecmp(argv[i], "devnull")) {
+            if (!irccasecmp(argv[++i], "*")) {
+                discrim->devnullclass = 0;
+            } else {
+                discrim->devnullclass = argv[i];
+            }
         } else if (!irccasecmp(argv[i], "handlemask") || !irccasecmp(argv[i], "accountmask")) {
             if (!irccasecmp(argv[++i], "*")) {
                 discrim->handlemask = 0;
@@ -3442,6 +3637,8 @@ nickserv_discrim_match(struct nickserv_discrim *discrim, struct handle_info *hi)
         || (discrim->handlemask && !match_ircglob(hi->handle, discrim->handlemask))
         || (discrim->fakehostmask && (!hi->fakehost || !match_ircglob(hi->fakehost, discrim->fakehostmask)))
         || (discrim->fakeidentmask && (!hi->fakeident || !match_ircglob(hi->fakeident, discrim->fakeidentmask)))
+        || (discrim->website && (!hi->website || !match_ircglob(hi->website, discrim->website)))
+        || (discrim->devnullclass && (!hi->devnull || !match_ircglob(hi->devnull, discrim->devnullclass)))
         || (discrim->emailmask && (!hi->email_addr || !match_ircglob(hi->email_addr, discrim->emailmask)))
         || (discrim->min_level > hi->opserv_level)
         || (discrim->max_level < hi->opserv_level)
@@ -3688,6 +3885,12 @@ nickserv_db_read_handle(const char *handle, dict_t obj)
     str = database_get_data(obj, KEY_INFO, RECDB_QSTRING);
     if (str)
         hi->infoline = strdup(str);
+    str = database_get_data(obj, KEY_WEBSITE, RECDB_QSTRING);
+    if (str)
+        hi->website = strdup(str);
+    str = database_get_data(obj, KEY_DEVNULL, RECDB_QSTRING);
+    if (str)
+        hi->devnull = strdup(str);
     str = database_get_data(obj, KEY_REGISTER_ON, RECDB_QSTRING);
     hi->registered = str ? strtoul(str, NULL, 0) : now;
     str = database_get_data(obj, KEY_LAST_SEEN, RECDB_QSTRING);
@@ -4312,9 +4515,12 @@ init_nickserv(const char *nick)
     dict_insert(nickserv_opt_dict, "COLOR", opt_color);
     dict_insert(nickserv_opt_dict, "PRIVMSG", opt_privmsg);
     dict_insert(nickserv_opt_dict, "STYLE", opt_style);
+    dict_insert(nickserv_opt_dict, "AUTOHIDE", opt_autohide);
     dict_insert(nickserv_opt_dict, "PASS", opt_password);
     dict_insert(nickserv_opt_dict, "PASSWORD", opt_password);
     dict_insert(nickserv_opt_dict, "FLAGS", opt_flags);
+    dict_insert(nickserv_opt_dict, "WEBSITE", opt_website);
+    dict_insert(nickserv_opt_dict, "DEVNULL", opt_devnull);
     dict_insert(nickserv_opt_dict, "ACCESS", opt_level);
     dict_insert(nickserv_opt_dict, "LEVEL", opt_level);
     dict_insert(nickserv_opt_dict, "EPITHET", opt_epithet);