]> jfr.im git - irc/evilnet/x3.git/blobdiff - src/chanserv.c
whoops
[irc/evilnet/x3.git] / src / chanserv.c
index c1d4873e6c6ced69d049ca7a8657955ab72b0a78..e963adec541af791d61ded98c37d15bc7b64077e 100644 (file)
@@ -24,6 +24,7 @@
 #include "modcmd.h"
 #include "opserv.h" /* for opserv_bad_channel() */
 #include "saxdb.h"
+#include "spamserv.h"
 #include "timeq.h"
 
 #define CHANSERV_CONF_NAME     "services/chanserv"
@@ -290,6 +291,7 @@ static const struct message_entry msgtab[] = {
     { "CSMSG_SET_CTCPREACTION",  "$bCTCPReaction$b %d - %s" },
     { "CSMSG_SET_TOPICREFRESH",  "$bTopicRefresh$b %d - %s" },
     { "CSMSG_SET_RESYNC",        "$bResync      $b %d - %s" },
+    { "CSMSG_SET_BANTYPE",       "$bBanType     $b %d - %s" },
     { "CSMSG_SET_BANTIMEOUT",    "$bBanTimeout  $b %d - %s" },
 
     { "CSMSG_USET_AUTOOP",       "$bAutoOp      $b %s" },
@@ -349,6 +351,16 @@ static const struct message_entry msgtab[] = {
     { "CSMSG_BANTIMEOUT_1D", "Bans will be removed after 24 hours."},
     { "CSMSG_BANTIMEOUT_1W", "Bans will be removed after 1 week."},
 
+    { "CSMSG_BANTYPE_A", "*!user@host" },
+    { "CSMSG_BANTYPE_B", "*!*user@host" },
+    { "CSMSG_BANTYPE_C", "*!*@host" },
+    { "CSMSG_BANTYPE_D", "*!*user@*.host" },
+    { "CSMSG_BANTYPE_E", "*!*@*.host" },
+    { "CSMSG_BANTYPE_F", "nick!user@host" },
+    { "CSMSG_BANTYPE_G", "nick!*@host" },
+    { "CSMSG_BANTYPE_H", "nick!*user@*.host" },
+    { "CSMSG_BANTYPE_I", "nick!*@*.host" },
+
     { "CSMSG_INVITED_USER", "Invited $b%s$b to join %s." },
     { "CSMSG_INVITING_YOU_REASON", "$b%s$b invites you to join %s: %s" },
     { "CSMSG_INVITING_YOU", "$b%s$b invites you to join %s." },
@@ -548,6 +560,7 @@ extern struct string_list *autojoin_channels;
 static dict_t plain_dnrs, mask_dnrs, handle_dnrs;
 static struct log_type *CS_LOG;
 struct adduserPending* adduser_pendings = NULL;
+extern const char *hidden_host_suffix;
 unsigned int adduser_pendings_count = 0;
 unsigned long god_timeout;
 
@@ -711,13 +724,22 @@ struct charOptionValues {
     { '3', "CSMSG_BANTIMEOUT_4H" },
     { '4', "CSMSG_BANTIMEOUT_1D" },
     { '5', "CSMSG_BANTIMEOUT_1W" }
-},
-resyncValues[] = {
+}, resyncValues[] = {
     { 'n', "CSMSG_RESYNC_NEVER" },
     { '1', "CSMSG_RESYNC_3_HOURS" },
     { '2', "CSMSG_RESYNC_6_HOURS" },
     { '3', "CSMSG_RESYNC_12_HOURS" },
     { '4', "CSMSG_RESYNC_24_HOURS" }
+}, banTypeValues[] = {
+    { '1', "CSMSG_BANTYPE_A" },
+    { '2', "CSMSG_BANTYPE_B" },
+    { '3', "CSMSG_BANTYPE_C" },
+    { '4', "CSMSG_BANTYPE_D" },
+    { '5', "CSMSG_BANTYPE_E" },
+    { '6', "CSMSG_BANTYPE_F" },
+    { '7', "CSMSG_BANTYPE_G" },
+    { '8', "CSMSG_BANTYPE_H" },
+    { '9', "CSMSG_BANTYPE_I" }
 };
 
 static const struct {
@@ -735,6 +757,7 @@ static const struct {
     { "CSMSG_SET_CTCPREACTION", "ctcpreaction", 'n', 10, ArrayLength(ctcpReactionValues), ctcpReactionValues },
     { "CSMSG_SET_BANTIMEOUT",   "bantimeout",   '0', 11, ArrayLength(banTimeoutValues), banTimeoutValues },
     { "CSMSG_SET_RESYNC",       "resync",       'n', 12, ArrayLength(resyncValues), resyncValues },
+    { "CSMSG_SET_BANTYPE",      "bantype",      '4', 13, ArrayLength(banTypeValues), banTypeValues },
 };
 
 struct userData *helperList;
@@ -745,7 +768,6 @@ unsigned int chanserv_read_version = 0; /* db version control */
 
 #define CHANSERV_DB_VERSION 2
 
-#define GetChannelUser(channel, handle) _GetChannelUser(channel, handle, 1, 0)
 #define GetChannelAccess(channel, handle) _GetChannelUser(channel, handle, 0, 0)
 #define GetTrueChannelAccess(channel, handle) _GetChannelUser(channel, handle, 0, 1)
 
@@ -1290,8 +1312,10 @@ del_channel_user(struct userData *user, int do_gc)
 
     free(user->info);
     free(user);
-    if(do_gc && !channel->users && !IsProtected(channel))
+    if(do_gc && !channel->users && !IsProtected(channel)) {
+        spamserv_cs_unregister(NULL, channel->channel, lost_all_users, NULL);
         unregister_channel(channel, "lost all users.");
+    }
 }
 
 static struct adduserPending* 
@@ -1409,7 +1433,7 @@ expire_adduser_pending()
 
 static void expire_ban(void *data);
 
-static struct banData*
+struct banData*
 add_channel_ban(struct chanData *channel, const char *mask, char *owner, time_t set, time_t triggered, time_t expires, char *reason)
 {
     struct banData *bd;
@@ -1620,6 +1644,7 @@ expire_channels(UNUSED_ARG(void *data))
 
         /* Unregister the channel */
         log_module(CS_LOG, LOG_INFO, "(%s) Channel registration expired.", channel->channel->name);
+        spamserv_cs_unregister(NULL, channel->channel, expire, NULL);
         unregister_channel(channel, "registration expired.");
     }
 
@@ -2162,11 +2187,39 @@ static CHANSERV_FUNC(cmd_unregister)
     sprintf(reason, "unregistered by %s.", user->handle_info->handle);
     name = strdup(channel->name);
     unregister_channel(cData, reason);
+    spamserv_cs_unregister(user, channel, manually, "unregistered");
     reply("CSMSG_UNREG_SUCCESS", name);
     free(name);
     return 1;
 }
 
+static void
+ss_cs_join_channel(struct chanNode *channel, int spamserv_join)
+{
+    extern struct userNode *spamserv;
+    struct mod_chanmode *change;
+
+    if(spamserv && spamserv_join && get_chanInfo(channel->name))
+    {
+        change = mod_chanmode_alloc(2);
+        change->argc = 2;
+        change->args[0].mode = MODE_CHANOP;
+        change->args[0].u.member = AddChannelUser(chanserv, channel);
+        change->args[1].mode = MODE_CHANOP;
+        change->args[1].u.member = AddChannelUser(spamserv, channel);
+    }
+    else
+    {
+        change = mod_chanmode_alloc(1);
+        change->argc = 1;
+        change->args[0].mode = MODE_CHANOP;
+        change->args[0].u.member = AddChannelUser(chanserv, channel);
+    }
+
+    mod_chanmode_announce(chanserv, channel, change);
+       mod_chanmode_free(change);
+}
+
 static CHANSERV_FUNC(cmd_move)
 {
     struct mod_chanmode change;
@@ -2175,6 +2228,7 @@ static CHANSERV_FUNC(cmd_move)
     struct userData *uData;
     char reason[MAXLEN];
     struct do_not_register *dnr;
+    int chanserv_join = 0, spamserv_join;
 
     REQUIRE_PARAMS(2);
 
@@ -2216,7 +2270,7 @@ static CHANSERV_FUNC(cmd_move)
     {
         target = AddChannel(argv[1], now, NULL, NULL, NULL);
         if(!IsSuspended(channel->channel_info))
-            AddChannelUser(chanserv, target);
+            chanserv_join = 1;
     }
     else if(target->channel_info)
     {
@@ -2230,12 +2284,7 @@ static CHANSERV_FUNC(cmd_move)
         return 0;
     }
     else if(!IsSuspended(channel->channel_info))
-    {
-        change.argc = 1;
-        change.args[0].mode = MODE_CHANOP;
-        change.args[0].u.member = AddChannelUser(chanserv, target);
-        mod_chanmode_announce(chanserv, target, &change);
-    }
+        chanserv_join = 1;
 
     if(off_channel > 0)
     {
@@ -2255,7 +2304,10 @@ static CHANSERV_FUNC(cmd_move)
     target->channel_info->channel = target;
     channel->channel_info = NULL;
 
-    reply("CSMSG_MOVE_SUCCESS", target->name);
+    spamserv_join = spamserv_cs_move_merge(user, channel, target, 1);
+
+    if (chanserv_join)
+        ss_cs_join_channel(target, spamserv_join);
 
     sprintf(reason, "%s moved to %s by %s.", channel->name, target->name, user->handle_info->handle);
     if(!IsSuspended(target->channel_info))
@@ -2267,6 +2319,7 @@ static CHANSERV_FUNC(cmd_move)
     UnlockChannel(channel);
     LockChannel(target);
     global_message(MESSAGE_RECIPIENT_OPERS | MESSAGE_RECIPIENT_HELPERS, reason);
+    reply("CSMSG_MOVE_SUCCESS", target->name);
     return 1;
 }
 
@@ -2500,6 +2553,7 @@ static CHANSERV_FUNC(cmd_merge)
 
     /* Merge the channel structures and associated data. */
     merge_channel(channel->channel_info, target->channel_info);
+    spamserv_cs_move_merge(user, channel, target, 0);
     sprintf(reason, "merged into %s by %s.", target->name, user->handle_info->handle);
     unregister_channel(channel->channel_info, reason);
     reply("CSMSG_MERGE_SUCCESS", target->name);
@@ -3082,13 +3136,113 @@ bad_channel_ban(struct chanNode *channel, struct userNode *user, const char *ban
     return 0;
 }
 
+#define i_isdigit(x) isdigit((int) (unsigned char) (x))
+
+int is_ipv4_address(const char *host)
+{
+    while (*host != '\0') {
+       if (*host != '.' && !i_isdigit(*host))
+       return 0;
+       host++;
+   }
+    return 1;
+}
+
+static char *get_domain_mask(char *host)
+{
+    char *ptr;
+
+    if (strchr(host, '.') == NULL) {
+       /* no dots - toplevel domain or IPv6 address */
+       ptr = strrchr(host, ':');
+       if (ptr != NULL) {
+          /* IPv6 address, ban the last 64k addresses */
+          if (ptr[1] != '\0') strcpy(ptr+1, "*");
+       }
+       return host;
+    }
+
+    if (is_ipv4_address(host)) {
+       /* it's an IP address, change last digit to * */
+       ptr = strrchr(host, '.');
+       if (ptr != NULL && i_isdigit(ptr[1]))
+           strcpy(ptr+1, "*");
+    } else {
+       /* if more than one dot, skip the first
+          (dyn123.blah.net -> *.blah.net) */
+          ptr = strchr(host, '.');
+          if (ptr != NULL && strchr(ptr+1, '.') != NULL) {
+             host = ptr-1;
+             host[0] = '*';
+          }
+    }
+    return host;
+}
+
+char *generate_ban_hostmask(struct userNode *user, const char banopt)
+{
+    char *nickname = NULL;
+    char *ident = "*";
+    char *hostname = NULL;
+    char *mask = NULL;
+    char *usemask = NULL;
+    int len;
+
+    usemask = user->hostname;
+    if (IsFakeHost(user) && IsHiddenHost(user))
+        usemask = user->fakehost;
+    else if (IsSetHost(user))
+        usemask = strchr(user->sethost, '@') + 1;
+    else if (IsHiddenHost(user) && user->handle_info && hidden_host_suffix) {
+        usemask = alloca(strlen(user->handle_info->handle) + strlen(hidden_host_suffix) + 2);
+        sprintf(usemask, "%s.%s", user->handle_info->handle, hidden_host_suffix);
+    }
+
+    if((banopt == '6') || (banopt == '7') || (banopt == '8') || (banopt == '9'))
+       nickname = user->nick;
+    else
+       nickname = "*";
+
+    if((banopt == '4') || (banopt == '5') || (banopt == '8') || (banopt == '9'))
+        hostname = get_domain_mask(usemask);
+    else
+        hostname = usemask;
+
+    if((banopt == '1') || (banopt == '6')) {
+        if (IsSetHost(user)) {
+            ident = alloca(strcspn(user->sethost, "@")+2);
+            safestrncpy(ident, user->sethost, strcspn(user->sethost, "@")+1);
+        } else
+            ident = user->ident;
+    }
+    else if((banopt == '2') || (banopt == '4') || (banopt == '8')) {
+        if (IsSetHost(user)) {
+            ident = alloca(strcspn(user->sethost, "@")+3);
+            ident[0] = '*';
+            safestrncpy(ident+1, user->sethost, strcspn(user->sethost, "@")+1);
+        } else {
+            ident = malloc(strlen(user->ident)+1);
+            sprintf(ident, "*%s", user->ident);
+        }
+    } else
+        ident = "*";
+
+    /* Put it all together. */
+    len = strlen(ident) + strlen(hostname) + strlen(nickname) + 3;
+    mask = malloc(len);
+    sprintf(mask, "%s!%s@%s", nickname, ident, hostname);
+
+    return mask;
+}
+
 static int
 eject_user(struct userNode *user, struct chanNode *channel, unsigned int argc, char *argv[], struct svccmd *cmd, int action)
 {
     struct userNode *victim;
     struct modeNode **victims;
+    struct chanData *cData;
     unsigned int offset, n, victimCount, duration = 0;
-    char *reason = "Bye.", *ban, *name;
+    char *reason = "Bye.", *ban, *name, banopt;
     char interval[INTERVALLEN];
 
     offset = (action & ACTION_ADD_TIMED_LAMER) ? 3 : 2;
@@ -3139,7 +3293,17 @@ eject_user(struct userNode *user, struct chanNode *channel, unsigned int argc, c
            return 0;
        }
 
-       ban = generate_hostmask(victim, GENMASK_STRICT_HOST|GENMASK_ANY_IDENT);
+        if(!(cData = channel->channel_info)) {
+            banopt = '4';
+        }
+
+        if (!(cData->chOpts[chBanType])) {
+            banopt = '4';
+        } else {
+            banopt = cData->chOpts[chBanType];
+        }
+
+        ban = generate_ban_hostmask(victim, banopt);
        name = victim->nick;
     }
     else
@@ -5283,12 +5447,8 @@ chanserv_expire_suspension(void *data)
     suspended->cData->flags &= ~CHANNEL_SUSPENDED;
     if(!IsOffChannel(suspended->cData))
     {
-        struct mod_chanmode change;
-        mod_chanmode_init(&change);
-        change.argc = 1;
-        change.args[0].mode = MODE_CHANOP;
-        change.args[0].u.member = AddChannelUser(chanserv, channel);
-        mod_chanmode_announce(chanserv, channel, &change);
+        spamserv_cs_suspend(channel, 0, 0, NULL);
+        ss_cs_join_channel(channel, 1);
     }
 }
 
@@ -5363,6 +5523,7 @@ static CHANSERV_FUNC(cmd_csuspend)
 
         /* Mark the channel as suspended, then part. */
         channel->channel_info->flags |= CHANNEL_SUSPENDED;
+        spamserv_cs_suspend(channel, expiry, 1, suspended->reason);
         DelChannelUser(chanserv, channel, suspended->reason, 0);
         reply("CSMSG_SUSPENDED", channel->name);
         sprintf(reason, "%s suspended by %s.", channel->name, suspended->suspender);
@@ -6055,6 +6216,11 @@ static MODCMD_FUNC(chan_opt_resync)
     return channel_multiple_option(chResync, CSFUNC_ARGS);
 }
 
+static MODCMD_FUNC(chan_opt_bantype)
+{
+    return channel_multiple_option(chBanType, CSFUNC_ARGS);
+}
+
 static struct svccmd_list set_shows_list;
 
 static void
@@ -7588,7 +7754,8 @@ chanserv_conf_read(void)
             "PubCmd", "InviteMe", "UserInfo","EnfOps",
             "EnfHalfOps", "EnfModes", "EnfTopic", "TopicSnarf", "Setters", 
             /* multiple choice options */
-            "AutoMode", "CtcpReaction", "Protect", "Toys", "TopicRefresh", "Resync",
+            "AutoMode", "CtcpReaction", "Protect", "Toys", "TopicRefresh",
+            "Resync", "BanType"
             /* binary options */
             "DynLimit", "NoDelete", "BanTimeout",
             /* delimiter */
@@ -8535,6 +8702,7 @@ init_chanserv(const char *nick)
     DEFINE_CHANNEL_OPTION(setters);
     DEFINE_CHANNEL_OPTION(topicrefresh);
     DEFINE_CHANNEL_OPTION(resync);
+    DEFINE_CHANNEL_OPTION(bantype);
     DEFINE_CHANNEL_OPTION(ctcpreaction);
     DEFINE_CHANNEL_OPTION(bantimeout);
     DEFINE_CHANNEL_OPTION(inviteme);