fixed possibility crash
[srvx.git] / src / opserv.c
index 0702a1072e9cebbf1783cc85429df6ab2271c1df..d358d57637b4c59f10a51e819e99ee32115d2e6c 100644 (file)
@@ -22,6 +22,7 @@
 #include "gline.h"
 #include "global.h"
 #include "nickserv.h"
+#include "chanserv.h"
 #include "modcmd.h"
 #include "opserv.h"
 #include "timeq.h"
@@ -72,6 +73,7 @@
 #define KEY_EXPIRES "expires"
 #define KEY_STAFF_AUTH_CHANNEL "staff_auth_channel"
 #define KEY_STAFF_AUTH_CHANNEL_MODES "staff_auth_channel_modes"
+#define KEY_STAFF_AUTH_FORCE_OPS "staff_auth_force_opers"
 #define KEY_CLONE_GLINE_DURATION "clone_gline_duration"
 #define KEY_BLOCK_GLINE_DURATION "block_gline_duration"
 #define KEY_ISSUER "issuer"
@@ -262,6 +264,12 @@ static const struct message_entry msgtab[] = {
     { "OSMSG_CSEARCH_CHANNEL_INFO", "%s [%d users] %s %s" },
     { "OSMSG_TRACE_MAX_CHANNELS", "You may not use the 'channel' criterion more than %d times." },
     { "OSMSG_FORCEKICK_LOCAL", "You cannot kick $b%s$b forcefully." },
+    { "OSMSG_SVSNONICK", "$b%s$b is not a valid nick." },
+    { "OSMSG_SVSNICKUSED", "$b%s$b is an already used nickname." },
+    { "OSMSG_SVSNICK", "You have renamed $b%s$b to $b%s$b." },
+    { "OSMSG_SVSJOIN", "$b%s$b joined $b%s$b." },
+    { "OSMSG_SVSMODE", "You have set mode $b%s$b for $b%s$b." },
+    { "OSMSG_SIMUL", "You have simuled $b%s$b: %s" },
     { "OSMSG_DEVNULL_USER" , "[%s] %s  %s" },
     { "OSMSG_DEVNULL_MATCH" , "%d Users found." },
     { "OSMSG_DEVNULL_CLASS" , "%s is not a valid DevNull class." },
@@ -289,6 +297,7 @@ static const struct message_entry msgtab[] = {
     { "OSMSG_DEVNULL_SET_M", "OverrideNoAmsg:   %s" },
     { "OSMSG_DEVNULL_SET_N", "MaxSendQ:         %s" },
     { "OSMSG_DEVNULL_SET_N_i", "MaxSendQ:         %i" },
+    { "OSMSG_DEVNULL_SET_OPME", "OpMe:             %s" },
     { "OSMSG_DEVNULL_SET_DONE", "Done." },
     { "OSMSG_DEVNULL_RENAMED", "Devnull class %s renamed to %s" },
     { "OSMSG_DEVNULL_SET_INVALID", "Invalid Option for setting %s" },
@@ -323,6 +332,7 @@ static struct {
     struct chanNode *debug_channel;
     struct chanNode *alert_channel;
     struct chanNode *staff_auth_channel;
+    int staff_auth_force;
     struct policer_params *join_policer_params;
     struct policer new_user_policer;
     unsigned long untrusted_max;
@@ -1230,7 +1240,7 @@ static MODCMD_FUNC(cmd_part)
 
 static MODCMD_FUNC(cmd_mode)
 {
-    if (!modcmd_chanmode(argv+1, argc-1, MCP_ALLOW_OVB|MCP_KEY_FREE|MC_ANNOUNCE)) {
+    if (!modcmd_chanmode(argv+1, argc-1, MCP_ALLOW_OVB|MCP_KEY_FREE|MC_ANNOUNCE|MCP_OPERMODE)) {
         reply("MSG_INVALID_MODES", unsplit_string(argv+1, argc-1, NULL));
         return 0;
     }
@@ -1329,7 +1339,7 @@ static MODCMD_FUNC(cmd_whois)
     reply("OSMSG_WHOIS_ACCOUNT", (target->handle_info ? target->handle_info->handle : "Not authenticated"));
     intervalString(buffer, now - target->timestamp, user->handle_info);
     reply("OSMSG_WHOIS_NICK_AGE", buffer);
-    if (target->channels.used <= MAX_CHANNELS_WHOIS)
+    if (target->channels.used <= MAX_CHANNELS_WHOIS || HANDLE_FLAGGED(user->handle_info, BOT))
         opserv_ison(user, target, "OSMSG_WHOIS_CHANNELS");
     else
         reply("OSMSG_WHOIS_HIDECHANS");
@@ -4084,6 +4094,8 @@ opserv_staff_alert(struct userNode *user, UNUSED_ARG(struct handle_info *old_han
         || user->uplink->burst
         || !user->handle_info)
         return;
+    else if (IsBot(user))
+        return;
     else if (user->handle_info->opserv_level)
         type = "OPER";
     else if (IsNetworkHelper(user))
@@ -4094,9 +4106,9 @@ opserv_staff_alert(struct userNode *user, UNUSED_ARG(struct handle_info *old_han
         return;
 
     if (irc_in_addr_is_valid(user->ip))
-        send_channel_notice(opserv_conf.staff_auth_channel, opserv, IDENT_FORMAT" authed to %s account %s", IDENT_DATA(user), type, user->handle_info->handle);
+        send_channel_message(opserv_conf.staff_auth_channel, opserv, IDENT_FORMAT" authed to %s account %s", IDENT_DATA(user), type, user->handle_info->handle);
     else
-        send_channel_notice(opserv_conf.staff_auth_channel, opserv, "%s [%s@%s] authed to %s account %s", user->nick, user->ident, user->hostname, type, user->handle_info->handle);
+        send_channel_message(opserv_conf.staff_auth_channel, opserv, "%s [%s@%s] authed to %s account %s", user->nick, user->ident, user->hostname, type, user->handle_info->handle);
 }
 
 static void
@@ -4262,7 +4274,7 @@ static MODCMD_FUNC(cmd_listdevnull)
         count++;
     }
     tbl.length = count+1;
-    tbl.width = 14;
+    tbl.width = 15;
     tbl.flags = 0;
     tbl.flags = TABLE_NO_FREE;
     tbl.contents = malloc(tbl.length * sizeof(tbl.contents[0]));
@@ -4281,6 +4293,7 @@ static MODCMD_FUNC(cmd_listdevnull)
     tbl.contents[0][11] = "+S";
     tbl.contents[0][12] = "+X";
     tbl.contents[0][13] = "MaxQ";
+    tbl.contents[0][14] = "OpMe";
     if(!count)
     {
         table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
@@ -4360,6 +4373,11 @@ static MODCMD_FUNC(cmd_listdevnull)
         } else {
             tbl.contents[ii][13] = off;
         }
+        if(DEVNULL_FLAGGED(th, MODE_OPME)) {
+            tbl.contents[ii][14] = on;
+        } else {
+            tbl.contents[ii][14] = off;
+        }
     }
     table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
     for(ii = 1; ii < tbl.length; ++ii)
@@ -4396,6 +4414,7 @@ static MODCMD_FUNC(cmd_listdevnull)
        send_message_type(4, user, opserv,"+k = ChanServ Mode (mode +k)");
     send_message_type(4, user, opserv,"+S = NetServ Mode (mode +S)");
     send_message_type(4, user, opserv,"+X = XtraOp Mode (mode +X)");
+    send_message_type(4, user, opserv,"OpMe = ChanServ opme command");
     return 1;
 }
 
@@ -4603,6 +4622,17 @@ static MODCMD_FUNC(cmd_setdevnull)
                   th->maxsendq = strtoul(argv[offset+3], NULL, 0);
                   reply("OSMSG_DEVNULL_SET_DONE");
                  }
+            } 
+            else if(!strcmp("OPME",argv[offset+2])) {
+                 if (!strcmp("OFF",argv[offset+3]) || !strcmp("0",argv[offset+3])) {
+                  DEVNULL_CLEAR_FLAG(th, MODE_OPME);
+                  reply("OSMSG_DEVNULL_SET_DONE");
+                 } else if (!strcmp("ON",argv[offset+3]) || !strcmp("1",argv[offset+3])) {
+                  DEVNULL_SET_FLAG(th, MODE_OPME);
+                  reply("OSMSG_DEVNULL_SET_DONE");
+                 } else {
+                  reply("OSMSG_DEVNULL_SET_INVALID", argv[offset+3]);
+                 }
             } else {
                  reply("OSMSG_DEVNULL_SET_INVALID", argv[offset+2]);
             }
@@ -4676,6 +4706,11 @@ static MODCMD_FUNC(cmd_setdevnull)
             } else {
                 reply("OSMSG_DEVNULL_SET_N", "off");
             }
+            if(DEVNULL_FLAGGED(th, MODE_OPME)) {
+                reply("OSMSG_DEVNULL_SET_OPME", "on");
+            } else {
+                reply("OSMSG_DEVNULL_SET_OPME", "off");
+            }
         }
     } else {
         reply("OSMSG_DEVNULL_NOTFOUND", argv[offset+1]);
@@ -4696,6 +4731,113 @@ struct devnull_class*
     return dict_find(opserv_devnull_classes, name, NULL);
 }
 
+void operpart(struct chanNode *chan, struct userNode *user)
+{
+    if(opserv_conf.alert_channel && opserv_conf.staff_auth_force > 0 &&
+      !(irccasecmp(chan->name,opserv_conf.alert_channel->name))) {
+        struct mod_chanmode *change;
+        change = find_matching_bans(&chan->banlist, user, NULL); //don't join them if they're banned (exceptions from forced join)
+        if(change)
+            return;
+        irc_svsjoin(opserv,user,chan);
+    }
+}
+
+void operadd(struct userNode *user)
+{
+    if(opserv_conf.alert_channel && opserv_conf.staff_auth_force > 0)
+        irc_svsjoin(opserv,user,opserv_conf.alert_channel);
+}
+
+void operdel(struct userNode *user)
+{
+    if(opserv_conf.alert_channel && opserv_conf.staff_auth_force == 2)
+        irc_kick(opserv, user, opserv_conf.alert_channel, "mode -o");
+}
+
+static MODCMD_FUNC(cmd_svsjoin)
+{
+    struct userNode *target;
+    if(!(target=GetUserH(argv[1]))) {
+        reply("OSMSG_SVSNONICK", argv[1]);
+        return 0;
+    }
+    if(!IsChannelName(argv[2]))
+    {
+        reply("MSG_NOT_CHANNEL_NAME");
+        return 0;
+    }
+    irc_svsjoinchan(opserv,target,argv[2]);
+    reply("OSMSG_SVSJOIN",target->nick,argv[2]);
+    return 1;
+}
+
+static MODCMD_FUNC(cmd_svsnick)
+{
+    struct userNode *target;
+    if(!(target=GetUserH(argv[1]))) {
+        reply("OSMSG_SVSNONICK", argv[1]);
+        return 0;
+    }
+    if(GetUserH(argv[2]))
+    {
+        reply("OSMSG_SVSNICKUSED",argv[2]);
+        return 0;
+    }
+    irc_svsnick(opserv,target,argv[2]);
+    reply("OSMSG_SVSNICK",target->nick,argv[2]);
+    return 1;
+}
+
+static MODCMD_FUNC(cmd_svsmode)
+{
+    struct userNode *target;
+    char *modestr;
+    if(!(target=GetUserH(argv[1]))) {
+        reply("OSMSG_SVSNONICK", argv[1]);
+        return 0;
+    }
+    modestr = unsplit_string(argv + 2, argc - 2, NULL);
+    irc_svsmode(opserv,target,modestr);
+    reply("OSMSG_SVSMODE",modestr,target->nick);
+    return 1;
+}
+
+static MODCMD_FUNC(cmd_simul)
+{
+    struct userNode *target;
+    char *line;
+    if(argc > 2) {
+               if(!(target=GetUserH(argv[1]))) {
+                       reply("OSMSG_SVSNONICK", argv[1]);
+                       return 0;
+               }
+               line = unsplit_string(argv + 2, argc - 2, NULL);
+               irc_simul(target,line);
+               reply("OSMSG_SIMUL",target->nick,line);
+               return 1;
+    }
+    return 0;
+}
+
+static MODCMD_FUNC(cmd_relay)
+{
+    struct userNode *target;
+    char *line;
+    if(!(target=GetUserH(argv[1]))) {
+        reply("OSMSG_SVSNONICK", argv[1]);
+        return 0;
+    }
+    line = unsplit_string(argv + 2, argc - 2, NULL);
+    char sendline[512];
+    if(channel)
+        sprintf(sendline, "relay %s %s :%s",user->nick,channel->name,line);
+    else
+        sprintf(sendline, "relay %s query :%s",user->nick,line);
+    irc_privmsg(opserv,target->numeric,sendline);
+    return 1;
+}
+
 static void
 opserv_conf_read(void)
 {
@@ -4738,8 +4880,11 @@ opserv_conf_read(void)
             str2 = "+timns";
         opserv_conf.staff_auth_channel = AddChannel(str, now, str2, NULL);
         AddChannelUser(opserv, opserv_conf.staff_auth_channel)->modes |= MODE_CHANOP;
+        str2 = database_get_data(conf_node, KEY_STAFF_AUTH_FORCE_OPS, RECDB_QSTRING);
+        opserv_conf.staff_auth_force = str2 ? atoi(str2) : 0;
     } else {
         opserv_conf.staff_auth_channel = NULL;
+        opserv_conf.staff_auth_force = 0;
     }
     str = database_get_data(conf_node, KEY_UNTRUSTED_MAX, RECDB_QSTRING);
     opserv_conf.untrusted_max = str ? strtoul(str, NULL, 0) : 5;
@@ -4939,6 +5084,11 @@ init_opserv(const char *nick)
     opserv_define_func("DEVNULL RENAME", cmd_renamedevnull, 200, 0, 3);
     opserv_define_func("DEVNULL SET", cmd_setdevnull, 200, 0, 2);
     opserv_define_func("DEVNULL LIST", cmd_listdevnull, 200, 0, 0);
+    opserv_define_func("SVSJOIN", cmd_svsjoin, 800, 0, 3);
+    opserv_define_func("SVSMODE", cmd_svsmode, 800, 0, 3);
+    opserv_define_func("SVSNICK", cmd_svsnick, 800, 0, 3);
+    opserv_define_func("RELAY", cmd_relay, 800, 0, 0);
+    opserv_define_func("SIMUL", cmd_simul, 999, 0, 2);
     opserv_define_func("TRACE", cmd_trace, 100, 0, 3);
     opserv_define_func("TRACE PRINT", NULL, 0, 0, 0);
     opserv_define_func("TRACE COUNT", NULL, 0, 0, 0);