]> jfr.im git - solanum.git/blobdiff - src/channel.c
newconf: fix a warning
[solanum.git] / src / channel.c
index 7483b6e50f690b4f0bbfd7b62e0531f0e9741ecd..2496f67b0f16ad7519a834a7105cba44e702d923 100644 (file)
@@ -62,6 +62,8 @@ static struct ChCapCombo chcap_combos[NCHCAP_COMBOS];
 static void free_topic(struct Channel *chptr);
 
 static int h_can_join;
+static int h_can_send;
+int h_get_channel_access;
 
 /* init_channels()
  *
@@ -78,6 +80,8 @@ init_channels(void)
        member_heap = rb_bh_create(sizeof(struct membership), MEMBER_HEAP_SIZE, "member_heap");
 
        h_can_join = register_hook("can_join");
+       h_can_send = register_hook("can_send");
+       h_get_channel_access = register_hook("get_channel_access");
 }
 
 /*
@@ -96,6 +100,7 @@ void
 free_channel(struct Channel *chptr)
 {
        rb_free(chptr->chname);
+       rb_free(chptr->mode_lock);
        rb_bh_free(channel_heap, chptr);
 }
 
@@ -118,6 +123,27 @@ free_ban(struct Ban *bptr)
        rb_bh_free(ban_heap, bptr);
 }
 
+/*
+ * send_channel_join()
+ *
+ * input        - channel to join, client joining.
+ * output       - none
+ * side effects - none
+ */
+void
+send_channel_join(struct Channel *chptr, struct Client *client_p)
+{
+       if (!IsClient(client_p))
+               return;
+
+       sendto_channel_local_with_capability(ALL_MEMBERS, NOCAPS, CLICAP_EXTENDED_JOIN, chptr, ":%s!%s@%s JOIN %s",
+                                            client_p->name, client_p->username, client_p->host, chptr->chname);
+
+       sendto_channel_local_with_capability(ALL_MEMBERS, CLICAP_EXTENDED_JOIN, NOCAPS, chptr, ":%s!%s@%s JOIN %s %s :%s",
+                                            client_p->name, client_p->username, client_p->host, chptr->chname,
+                                            EmptyString(client_p->user->suser) ? "*" : client_p->user->suser,
+                                            client_p->info);
+}
 
 /* find_channel_membership()
  *
@@ -704,6 +730,7 @@ is_quieted(struct Channel *chptr, struct Client *who, struct membership *msptr,
  * input       - client to check, channel to check for, key
  * output      - reason for not being able to join, else 0
  * side effects -
+ * caveats      - this function should only be called on a local user.
  */
 int
 can_join(struct Client *source_p, struct Channel *chptr, char *key)
@@ -720,6 +747,10 @@ can_join(struct Client *source_p, struct Channel *chptr, char *key)
 
        s_assert(source_p->localClient != NULL);
 
+       moduledata.client = source_p;
+       moduledata.chptr = chptr;
+       moduledata.approved = 0;
+
        rb_sprintf(src_host, "%s!%s@%s", source_p->name, source_p->username, source_p->host);
        rb_sprintf(src_iphost, "%s!%s@%s", source_p->name, source_p->username, source_p->sockhost);
        if(source_p->localClient->mangledhost != NULL)
@@ -740,7 +771,10 @@ can_join(struct Client *source_p, struct Channel *chptr, char *key)
        }
 
        if((is_banned(chptr, source_p, NULL, src_host, src_iphost)) == CHFL_BAN)
-               return (ERR_BANNEDFROMCHAN);
+       {
+               moduledata.approved = ERR_BANNEDFROMCHAN;
+               goto finish_join_check;
+       }
 
        if(chptr->mode.mode & MODE_INVITEONLY)
        {
@@ -752,7 +786,7 @@ can_join(struct Client *source_p, struct Channel *chptr, char *key)
                if(invite == NULL)
                {
                        if(!ConfigChannel.use_invex)
-                               return (ERR_INVITEONLYCHAN);
+                               moduledata.approved = ERR_INVITEONLYCHAN;
                        RB_DLINK_FOREACH(ptr, chptr->invexlist.head)
                        {
                                invex = ptr->data;
@@ -764,12 +798,12 @@ can_join(struct Client *source_p, struct Channel *chptr, char *key)
                                        break;
                        }
                        if(ptr == NULL)
-                               return (ERR_INVITEONLYCHAN);
+                               moduledata.approved = ERR_INVITEONLYCHAN;
                }
        }
 
        if(*chptr->mode.key && (EmptyString(key) || irccmp(chptr->mode.key, key)))
-               return (ERR_BADCHANNELKEY);
+               moduledata.approved = ERR_BADCHANNELKEY;
 
        if(chptr->mode.limit &&
           rb_dlink_list_length(&chptr->members) >= (unsigned long) chptr->mode.limit)
@@ -794,13 +828,10 @@ can_join(struct Client *source_p, struct Channel *chptr, char *key)
                                break;
                }
                if (invite == NULL)
-                       return i;
+                       moduledata.approved = i;
        }
 
-       moduledata.client = source_p;
-       moduledata.chptr = chptr;
-       moduledata.approved = 0;
-
+finish_join_check:
        call_hook(h_can_join, &moduledata);
 
        return moduledata.approved;
@@ -815,12 +846,16 @@ can_join(struct Client *source_p, struct Channel *chptr, char *key)
 int
 can_send(struct Channel *chptr, struct Client *source_p, struct membership *msptr)
 {
+       hook_data_channel_approval moduledata;
+
+       moduledata.approved = CAN_SEND_NONOP;
+
        if(IsServer(source_p) || IsService(source_p))
                return CAN_SEND_OPV;
 
        if(MyClient(source_p) && hash_find_resv(chptr->chname) &&
           !IsOper(source_p) && !IsExemptResv(source_p))
-               return CAN_SEND_NO;
+               moduledata.approved = CAN_SEND_NO;
 
        if(msptr == NULL)
        {
@@ -833,17 +868,16 @@ can_send(struct Channel *chptr, struct Client *source_p, struct membership *mspt
                         * theres no possibility of caching them --fl
                         */
                        if(chptr->mode.mode & MODE_NOPRIVMSGS || chptr->mode.mode & MODE_MODERATED)
-                               return CAN_SEND_NO;
+                               moduledata.approved = CAN_SEND_NO;
                        else
-                               return CAN_SEND_NONOP;
+                               moduledata.approved = CAN_SEND_NONOP;
+
+                       return moduledata.approved;
                }
        }
 
-       if(is_chanop_voiced(msptr))
-               return CAN_SEND_OPV;
-
        if(chptr->mode.mode & MODE_MODERATED)
-               return CAN_SEND_NO;
+               moduledata.approved = CAN_SEND_NO;
 
        if(MyClient(source_p))
        {
@@ -851,14 +885,75 @@ can_send(struct Channel *chptr, struct Client *source_p, struct membership *mspt
                if(msptr->bants == chptr->bants)
                {
                        if(can_send_banned(msptr))
-                               return CAN_SEND_NO;
+                               moduledata.approved = CAN_SEND_NO;
                }
                else if(is_banned(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN
                        || is_quieted(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN)
-                       return CAN_SEND_NO;
+                       moduledata.approved = CAN_SEND_NO;
+       }
+
+       if(is_chanop_voiced(msptr))
+               moduledata.approved = CAN_SEND_OPV;
+
+       call_hook(h_can_send, &moduledata);
+
+       return moduledata.approved;
+}
+
+/*
+ * flood_attack_channel
+ * inputs       - flag 0 if PRIVMSG 1 if NOTICE. RFC
+ *                says NOTICE must not auto reply
+ *              - pointer to source Client 
+ *             - pointer to target channel
+ * output      - 1 if target is under flood attack
+ * side effects        - check for flood attack on target chptr
+ */
+int
+flood_attack_channel(int p_or_n, struct Client *source_p, struct Channel *chptr, char *chname)
+{
+       int delta;
+
+       if(GlobalSetOptions.floodcount && MyClient(source_p))
+       {
+               if((chptr->first_received_message_time + 1) < rb_current_time())
+               {
+                       delta = rb_current_time() - chptr->first_received_message_time;
+                       chptr->received_number_of_privmsgs -= delta;
+                       chptr->first_received_message_time = rb_current_time();
+                       if(chptr->received_number_of_privmsgs <= 0)
+                       {
+                               chptr->received_number_of_privmsgs = 0;
+                               chptr->flood_noticed = 0;
+                       }
+               }
+
+               if((chptr->received_number_of_privmsgs >= GlobalSetOptions.floodcount)
+                  || chptr->flood_noticed)
+               {
+                       if(chptr->flood_noticed == 0)
+                       {
+                               sendto_realops_snomask(SNO_BOTS, *chptr->chname == '&' ? L_ALL : L_NETWIDE,
+                                                    "Possible Flooder %s[%s@%s] on %s target: %s",
+                                                    source_p->name, source_p->username,
+                                                    source_p->orighost,
+                                                    source_p->servptr->name, chptr->chname);
+                               chptr->flood_noticed = 1;
+
+                               /* Add a bit of penalty */
+                               chptr->received_number_of_privmsgs += 2;
+                       }
+                       if(MyClient(source_p) && (p_or_n != 1))
+                               sendto_one(source_p,
+                                          ":%s NOTICE %s :*** Message to %s throttled due to flooding",
+                                          me.name, source_p->name, chptr->chname);
+                       return 1;
+               }
+               else
+                       chptr->received_number_of_privmsgs++;
        }
 
-       return CAN_SEND_NONOP;
+       return 0;
 }
 
 /* find_bannickchange_channel()
@@ -1073,10 +1168,9 @@ set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_in
        }
 }
 
-/* channel_modes_real()
+/* channel_modes()
  *
  * inputs       - pointer to channel
- *              - pointer to channel Mode struct
  *              - pointer to client
  * output       - string with simple modes
  * side effects - result from previous calls overwritten
@@ -1084,7 +1178,7 @@ set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_in
  * Stolen from ShadowIRCd 4 --nenolod
  */
 const char *
-channel_modes_real(struct Channel *chptr, struct Mode *mode, struct Client *client_p)
+channel_modes(struct Channel *chptr, struct Client *client_p)
 {
        int i;
        char buf1[BUFSIZE];
@@ -1097,40 +1191,40 @@ channel_modes_real(struct Channel *chptr, struct Mode *mode, struct Client *clie
        *pbuf = '\0';
 
        for (i = 0; i < 256; i++)
-               if(mode->mode & chmode_flags[i])
+               if(chptr->mode.mode & chmode_flags[i])
                        *mbuf++ = i;
 
-       if(mode->limit)
+       if(chptr->mode.limit)
        {
                *mbuf++ = 'l';
 
                if(!IsClient(client_p) || IsMember(client_p, chptr))
-                       pbuf += rb_sprintf(pbuf, " %d", mode->limit);
+                       pbuf += rb_sprintf(pbuf, " %d", chptr->mode.limit);
        }
 
-       if(*mode->key)
+       if(*chptr->mode.key)
        {
                *mbuf++ = 'k';
 
                if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
-                       pbuf += rb_sprintf(pbuf, " %s", mode->key);
+                       pbuf += rb_sprintf(pbuf, " %s", chptr->mode.key);
        }
 
-       if(mode->join_num)
+       if(chptr->mode.join_num)
        {
                *mbuf++ = 'j';
 
                if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
-                       pbuf += rb_sprintf(pbuf, " %d:%d", mode->join_num,
-                                          mode->join_time);
+                       pbuf += rb_sprintf(pbuf, " %d:%d", chptr->mode.join_num,
+                                          chptr->mode.join_time);
        }
 
-       if(*mode->forward && (ConfigChannel.use_forward || !IsClient(client_p)))
+       if(*chptr->mode.forward && (ConfigChannel.use_forward || !IsClient(client_p)))
        {
                *mbuf++ = 'f';
 
                if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
-                       pbuf += rb_sprintf(pbuf, " %s", mode->forward);
+                       pbuf += rb_sprintf(pbuf, " %s", chptr->mode.forward);
        }
 
        *mbuf = '\0';
@@ -1358,3 +1452,54 @@ send_cap_mode_changes(struct Client *client_p, struct Client *source_p,
                        sendto_server(client_p, chptr, cap, nocap, "%s %s", modebuf, parabuf);
        }
 }
+
+void 
+resv_chan_forcepart(const char *name, const char *reason, int temp_time)
+{
+       rb_dlink_node *ptr;
+       rb_dlink_node *next_ptr;
+       struct Channel *chptr;
+       struct membership *msptr;
+       struct Client *target_p;
+
+       if(!ConfigChannel.resv_forcepart)
+               return;
+
+       /* for each user on our server in the channel list
+        * send them a PART, and notify opers.
+        */
+       chptr = find_channel(name);
+       if(chptr != NULL)
+       {
+               RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
+               {
+                       msptr = ptr->data;
+                       target_p = msptr->client_p;
+
+                       if(IsExemptResv(target_p))
+                               continue;
+
+                       sendto_server(target_p, chptr, CAP_TS6, NOCAPS,
+                                     ":%s PART %s", target_p->id, chptr->chname);
+
+                       sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s :%s",
+                                            target_p->name, target_p->username,
+                                            target_p->host, chptr->chname, target_p->name);
+
+                       remove_user_from_channel(msptr);
+
+                       /* notify opers & user they were removed from the channel */
+                       sendto_realops_snomask(SNO_GENERAL, L_ALL,
+                                            "Forced PART for %s!%s@%s from %s (%s)",
+                                            target_p->name, target_p->username, 
+                                            target_p->host, name, reason);
+
+                       if(temp_time > 0)
+                               sendto_one_notice(target_p, ":*** Channel %s is temporarily unavailable on this server.",
+                                          name);
+                       else
+                               sendto_one_notice(target_p, ":*** Channel %s is no longer available on this server.",
+                                          name);
+               }
+       }
+}