Further reorganize and streamline join handling.
[ircu2.10.12-pk.git] / ircd / m_join.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_join.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 1, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26 #include "config.h"
27
28 #include "channel.h"
29 #include "client.h"
30 #include "gline.h"
31 #include "hash.h"
32 #include "ircd.h"
33 #include "ircd_chattr.h"
34 #include "ircd_features.h"
35 #include "ircd_log.h"
36 #include "ircd_reply.h"
37 #include "ircd_string.h"
38 #include "msg.h"
39 #include "numeric.h"
40 #include "numnicks.h"
41 #include "s_debug.h"
42 #include "s_user.h"
43 #include "send.h"
44
45 /* #include <assert.h> -- Now using assert in ircd_log.h */
46 #include <stdlib.h>
47 #include <string.h>
48
49 /** Searches for and handles a 0 in a join list.
50  * @param[in] cptr Client that sent us the message.
51  * @param[in] sptr Original source of message.
52  * @param[in] chanlist List of channels to join.
53  * @return First token in \a chanlist after the final 0 entry, which
54  * may be its nul terminator (if the final entry is a 0 entry).
55  */
56 static char *
57 last0(struct Client *cptr, struct Client *sptr, char *chanlist)
58 {
59   char *p;
60   int join0 = 0;
61
62   for (p = chanlist; p[0]; p++) /* find last "JOIN 0" */
63     if (p[0] == '0' && (p[1] == ',' || p[1] == '\0' || !IsChannelChar(p[1]))) {
64       if (*++p == ',')
65         p++;
66       chanlist = p;
67       join0 = 1;
68     } else {
69       while (p[0] != ',' && p[0] != '\0') /* skip past channel name */
70         p++;
71
72       if (!p[0]) /* hit the end */
73         break;
74     }
75
76   if (join0) {
77     struct JoinBuf part;
78     struct Membership *member;
79
80     joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
81                  "Left all channels", 0);
82
83     joinbuf_join(&part, 0, 0);
84
85     while ((member = cli_user(sptr)->channel))
86       joinbuf_join(&part, member->channel,
87                    IsZombie(member) ? CHFL_ZOMBIE :
88                    IsDelayedJoin(member) ? CHFL_DELAYED :
89                    0);
90
91     joinbuf_flush(&part);
92   }
93
94   return chanlist;
95 }
96
97 /** Handle a JOIN message from a client connection.
98  * See @ref m_functions for discussion of the arguments.
99  * @param[in] cptr Client that sent us the message.
100  * @param[in] sptr Original source of message.
101  * @param[in] parc Number of arguments.
102  * @param[in] parv Argument vector.
103  */
104 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
105 {
106   struct Channel *chptr;
107   struct JoinBuf join;
108   struct JoinBuf create;
109   struct Gline *gline;
110   char *p = 0;
111   char *chanlist;
112   char *name;
113   char *keys;
114
115   if (parc < 2 || *parv[1] == '\0')
116     return need_more_params(sptr, "JOIN");
117
118   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
119   joinbuf_init(&create, sptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
120
121   chanlist = last0(cptr, sptr, parv[1]); /* find last "JOIN 0" */
122
123   keys = parv[2]; /* remember where keys are */
124
125   for (name = ircd_strtok(&p, chanlist, ","); name;
126        name = ircd_strtok(&p, 0, ",")) {
127     char *key = 0;
128
129     /* If we have any more keys, take the first for this channel. */
130     if (!BadPtr(keys)
131         && (keys = strchr(key = keys, ',')))
132       *keys++ = '\0';
133
134     clean_channelname(name);
135     if (!IsChannelName(name))
136     {
137       /* bad channel name */
138       send_reply(sptr, ERR_NOSUCHCHANNEL, name);
139       continue;
140     }
141
142     if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER)
143         && !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
144       send_reply(sptr, ERR_TOOMANYCHANNELS, name);
145       break; /* no point processing the other channels */
146     }
147
148     /* BADCHANed channel */
149     if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
150         GlineIsActive(gline) && !IsAnOper(sptr)) {
151       send_reply(sptr, ERR_BANNEDFROMCHAN, name);
152       continue;
153     }
154
155     if (!(chptr = FindChannel(name))) {
156       if ((name[0] == '&') && !feature_bool(FEAT_LOCAL_CHANNELS)) {
157         send_reply(sptr, ERR_NOSUCHCHANNEL, name);
158         continue;
159       }
160
161       if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
162         continue;
163
164       /* Try to add the new channel as a recent target for the user. */
165       if (check_target_limit(sptr, chptr, chptr->chname, 1)) {
166         chptr->members = 0;
167         destruct_channel(chptr);
168         continue;
169       }
170
171       joinbuf_join(&create, chptr, CHFL_CHANOP | CHFL_CHANNEL_MANAGER);
172     } else if (find_member_link(chptr, sptr)) {
173       continue; /* already on channel */
174     } else if (check_target_limit(sptr, chptr, chptr->chname, 0)) {
175       continue;
176     } else {
177       int flags = CHFL_DEOPPED;
178       int err = 0;
179
180       /* Check target change limits. */
181
182       /* Check Apass/Upass -- since we only ever look at a single
183        * "key" per channel now, this hampers brute force attacks. */
184       if (key && !strcmp(key, chptr->mode.apass))
185         flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
186       else if (key && !strcmp(key, chptr->mode.upass))
187         flags = CHFL_CHANOP;
188       else if (IsInvited(sptr, chptr)) {
189         /* Invites bypass these other checks. */
190       } else if (chptr->mode.mode & MODE_INVITEONLY)
191         err = ERR_INVITEONLYCHAN;
192       else if (chptr->mode.limit && (chptr->users >= chptr->mode.limit))
193         err = ERR_CHANNELISFULL;
194       else if ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
195         err = ERR_NEEDREGGEDNICK;
196       else if (find_ban(sptr, chptr->banlist))
197         err = ERR_BANNEDFROMCHAN;
198       else if (*chptr->mode.key && (!key || strcmp(key, chptr->mode.key)))
199         err = ERR_BADCHANNELKEY;
200
201       /* An oper with WALK_LCHAN privilege can join a local channel
202        * he otherwise could not join by using "OVERRIDE" as the key.
203        * This will generate a HACK(4) notice, but fails if the oper
204        * could normally join the channel. */
205       if (IsLocalChannel(chptr->chname)
206           && HasPriv(sptr, PRIV_WALK_LCHAN)
207           && !(flags & CHFL_CHANOP)
208           && key && !strcmp(key, "OVERRIDE")
209           && strcmp(chptr->mode.key, "OVERRIDE"))
210       {
211         switch (err) {
212         case 0:
213           send_reply(sptr, ERR_DONTCHEAT, chptr->chname);
214           continue;
215         case ERR_INVITEONLYCHAN: err = 'i'; break;
216         case ERR_CHANNELISFULL:  err = 'l'; break;
217         case ERR_BANNEDFROMCHAN: err = 'b'; break;
218         case ERR_BADCHANNELKEY:  err = 'k'; break;
219         case ERR_NEEDREGGEDNICK: err = 'r'; break;
220         default: err = '?'; break;
221         }
222         /* send accountability notice */
223         sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
224                              "(overriding +%c)", sptr, chptr, err);
225         err = 0;
226       }
227
228       /* Is there some reason the user may not join? */
229       if (err) {
230         send_reply(sptr, err, chptr->chname);
231         continue;
232       }
233
234       joinbuf_join(&join, chptr, flags);
235     }
236
237     del_invite(sptr, chptr);
238
239     if (chptr->topic[0]) {
240       send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
241       send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
242                  chptr->topic_time);
243     }
244
245     do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
246   }
247
248   joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
249   joinbuf_flush(&create);
250
251   return 0;
252 }
253
254 /** Handle a JOIN message from a server connection.
255  * See @ref m_functions for discussion of the arguments.
256  * @param[in] cptr Client that sent us the message.
257  * @param[in] sptr Original source of message.
258  * @param[in] parc Number of arguments.
259  * @param[in] parv Argument vector.
260  */
261 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
262 {
263   struct Membership *member;
264   struct Channel *chptr;
265   struct JoinBuf join;
266   unsigned int flags;
267   time_t creation = 0;
268   char *p = 0;
269   char *chanlist;
270   char *name;
271
272   if (IsServer(sptr))
273   {
274     return protocol_violation(cptr,
275                               "%s tried to JOIN %s, duh!",
276                               cli_name(sptr),
277                               (parc < 2 || *parv[1] == '\0') ? "a channel" :
278                                                                parv[1]
279                               );
280   }
281
282   if (parc < 2 || *parv[1] == '\0')
283     return need_more_params(sptr, "JOIN");
284
285   if (parc > 2 && parv[2])
286     creation = atoi(parv[2]);
287
288   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
289
290   chanlist = last0(cptr, sptr, parv[1]); /* find last "JOIN 0" */
291
292   for (name = ircd_strtok(&p, chanlist, ","); name;
293        name = ircd_strtok(&p, 0, ",")) {
294
295     if (name[0] == '0' && name[1] == ':')
296     {
297       flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
298       name += 2;
299     }
300     else if (name[0] == '1' && name[1] == ':')
301     {
302       flags = CHFL_CHANOP;
303       name += 2;
304     }
305     else
306       flags = CHFL_DEOPPED;
307
308     if (IsLocalChannel(name) || !IsChannelName(name))
309     {
310       protocol_violation(cptr, "%s tried to join %s", cli_name(sptr), name);
311       continue;
312     }
313
314     if (!(chptr = FindChannel(name)))
315     {
316       /* No channel exists, so create one */
317       if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
318       {
319         protocol_violation(sptr,"couldn't get channel %s for %s",
320                            name,cli_name(sptr));
321         continue;
322       }
323       flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
324
325       /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */
326       chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
327     }
328     else { /* We have a valid channel? */
329       if ((member = find_member_link(chptr, sptr)))
330       {
331         /* It is impossible to get here --Run */
332         if (!IsZombie(member)) /* already on channel */
333           continue;
334
335         flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
336         remove_user_from_channel(sptr, chptr);
337         chptr = FindChannel(name);
338       }
339       else
340         flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
341       /* Always copy the timestamp when it is older, that is the only way to
342          ensure network-wide synchronization of creation times. */
343       if (creation && creation < chptr->creationtime)
344         chptr->creationtime = creation;
345     }
346
347     joinbuf_join(&join, chptr, flags);
348   }
349
350   joinbuf_flush(&join); /* flush joins... */
351
352   return 0;
353 }