82f36551aeb520aac16535eba643df8cadc983b3
[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 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #if 0
85 /*
86  * No need to include handlers.h here the signatures must match
87  * and we don't need to force a rebuild of all the handlers everytime
88  * we add a new one to the list. --Bleep
89  */
90 #include "handlers.h"
91 #endif /* 0 */
92 #include "channel.h"
93 #include "client.h"
94 #include "gline.h"
95 #include "hash.h"
96 #include "ircd.h"
97 #include "ircd_chattr.h"
98 #include "ircd_features.h"
99 #include "ircd_reply.h"
100 #include "ircd_string.h"
101 #include "msg.h"
102 #include "numeric.h"
103 #include "numnicks.h"
104 #include "s_debug.h"
105 #include "s_user.h"
106 #include "send.h"
107
108 #include <assert.h>
109 #include <stdlib.h>
110 #include <string.h>
111
112 /*
113  * Helper function to find last 0 in a comma-separated list of
114  * channel names.
115  */
116 static char *
117 last0(char *chanlist)
118 {
119   char *p;
120
121   for (p = chanlist; p[0]; p++) /* find last "JOIN 0" */
122     if (p[0] == '0' && (p[1] == ',' || p[1] == '\0' || !IsChannelChar(p[1]))) {
123       chanlist = p; /* we'll start parsing here */
124
125       if (!p[1]) /* hit the end */
126         break;
127
128       p++;
129     } else {
130       while (p[0] != ',' && p[0] != '\0') /* skip past channel name */
131         p++;
132
133       if (!p[0]) /* hit the end */
134         break;
135     }
136
137   return chanlist;
138 }
139
140 /*
141  * Helper function to perform a JOIN 0 if needed; returns 0 if channel
142  * name is not 0, else removes user from all channels and returns 1.
143  */
144 static int
145 join0(struct JoinBuf *join, struct Client *cptr, struct Client *sptr,
146       char *chan)
147 {
148   struct Membership *member;
149   struct JoinBuf part;
150
151   /* is it a JOIN 0? */
152   if (chan[0] != '0' || chan[1] != '\0')
153     return 0;
154   
155   joinbuf_join(join, 0, 0); /* join special channel 0 */
156
157   /* leave all channels */
158   joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
159                "Left all channels", 0);
160
161   while ((member = cli_user(sptr)->channel))
162     joinbuf_join(&part, member->channel,
163                  IsZombie(member) ? CHFL_ZOMBIE : 0);
164
165   joinbuf_flush(&part);
166
167   return 1;
168 }
169
170 /*
171  * m_join - generic message handler
172  */
173 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
174 {
175   struct Channel *chptr;
176   struct JoinBuf join;
177   struct JoinBuf create;
178   struct Gline *gline;
179   unsigned int flags = 0;
180   int i;
181   char *p = 0;
182   char *chanlist;
183   char *name;
184   char *keys;
185
186   if (parc < 2 || *parv[1] == '\0')
187     return need_more_params(sptr, "JOIN");
188
189   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
190   joinbuf_init(&create, sptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
191
192   chanlist = last0(parv[1]); /* find last "JOIN 0" */
193
194   keys = parv[2]; /* remember where keys are */
195
196   for (name = ircd_strtok(&p, chanlist, ","); name;
197        name = ircd_strtok(&p, 0, ",")) {
198     clean_channelname(name);
199
200     if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
201       continue;
202
203     if (!IsChannelName(name)) { /* bad channel name */
204       send_reply(sptr, ERR_NOSUCHCHANNEL, name);
205       continue;
206     }
207
208     /* BADCHANed channel */
209     if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
210         GlineIsActive(gline) && !IsAnOper(sptr)) {
211       send_reply(sptr, ERR_BANNEDFROMCHAN, name);
212       continue;
213     }
214
215     if ((chptr = FindChannel(name))) {
216       if (find_member_link(chptr, sptr))
217         continue; /* already on channel */
218
219       flags = CHFL_DEOPPED;
220     } else
221       flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
222
223     if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER) &&
224         !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
225       send_reply(sptr, ERR_TOOMANYCHANNELS, chptr ? chptr->chname : name);
226       break; /* no point processing the other channels */
227     }
228
229     if (chptr) {
230       if (check_target_limit(sptr, chptr, chptr->chname, 0))
231         continue; /* exceeded target limit */
232       else if ((i = can_join(sptr, chptr, keys))) {
233         if (i > MAGIC_OPER_OVERRIDE) { /* oper overrode mode */
234           switch (i - MAGIC_OPER_OVERRIDE) {
235           case ERR_CHANNELISFULL: /* figure out which mode */
236             i = 'l';
237             break;
238
239           case ERR_INVITEONLYCHAN:
240             i = 'i';
241             break;
242
243           case ERR_BANNEDFROMCHAN:
244             i = 'b';
245             break;
246
247           case ERR_BADCHANNELKEY:
248             i = 'k';
249             break;
250           }
251
252           /* send accountability notice */
253           sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
254                                "(overriding +%c)", sptr, chptr, i);
255         } else {
256           send_reply(sptr, i, chptr->chname);
257           continue;
258         }
259       } /* else if ((i = can_join(sptr, chptr, keys))) { */
260
261       joinbuf_join(&join, chptr, flags);
262     } else if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
263       continue; /* couldn't get channel */
264     else if (check_target_limit(sptr, chptr, chptr->chname, 1)) {
265       /* Note: check_target_limit will only ever return 0 here */
266       sub1_from_channel(chptr); /* created it... */
267       continue;
268     } else
269       joinbuf_join(&create, chptr, flags);
270
271     del_invite(sptr, chptr);
272
273     if (chptr->topic[0]) {
274       send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
275       send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
276                  chptr->topic_time);
277     }
278
279     do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
280   }
281
282   joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
283   joinbuf_flush(&create);
284
285   return 0;
286 }
287
288 /*
289  * ms_join - server message handler
290  */
291 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
292 {
293   struct Membership *member;
294   struct Channel *chptr;
295   struct JoinBuf join;
296   unsigned int flags = 0;
297   time_t creation = 0;
298   char *p = 0;
299   char *chanlist;
300   char *name;
301
302   if (IsServer(sptr)) {
303     return protocol_violation(sptr,"%s tried to JOIN a channel, duh!", cli_name(sptr));
304   }
305
306   if (parc < 2 || *parv[1] == '\0')
307     return need_more_params(sptr, "JOIN");
308
309   if (parc > 2 && parv[2])
310     creation = atoi(parv[2]);
311
312   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
313
314   chanlist = last0(parv[1]); /* find last "JOIN 0" */
315
316   for (name = ircd_strtok(&p, chanlist, ","); name;
317        name = ircd_strtok(&p, 0, ",")) {
318     clean_channelname(name);
319
320     if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
321       continue;
322
323     if (IsLocalChannel(name) || !IsChannelName(name)) {
324       protocol_violation(sptr,"%s tried to join %s",cli_name(cptr),name);
325       continue;
326     }
327
328     if (!(chptr = FindChannel(name))) {
329       /* No channel exists, so create one */
330       if (!(chptr = get_channel(sptr, name, CGT_CREATE))) {
331         protocol_violation(sptr,"couldn't get channel %s for %s",
332                            name,cli_name(sptr));
333         continue;
334       }
335       flags = CHFL_DEOPPED | ((cli_flags(sptr) & FLAGS_TS8) ? CHFL_SERVOPOK : 0);
336
337       /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */ 
338       chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
339     }
340     else { /* We have a valid channel? */
341       if ((member = find_member_link(chptr, sptr))) {
342         if (!IsZombie(member)) /* already on channel */
343           continue;
344
345         flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
346         remove_user_from_channel(sptr, chptr);
347         chptr = FindChannel(name);
348       } else
349         flags = CHFL_DEOPPED | ((cli_flags(sptr) & FLAGS_TS8) ? CHFL_SERVOPOK : 0);
350     } 
351
352     joinbuf_join(&join, chptr, flags);
353   }
354
355   joinbuf_flush(&join); /* flush joins... */
356
357   return 0;
358 }