Author: Kev <klmitch@mit.edu>
[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 #if 0
83 /*
84  * No need to include handlers.h here the signatures must match
85  * and we don't need to force a rebuild of all the handlers everytime
86  * we add a new one to the list. --Bleep
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "channel.h"
91 #include "client.h"
92 #include "gline.h"
93 #include "hash.h"
94 #include "ircd.h"
95 #include "ircd_chattr.h"
96 #include "ircd_features.h"
97 #include "ircd_reply.h"
98 #include "ircd_string.h"
99 #include "msg.h"
100 #include "numeric.h"
101 #include "numnicks.h"
102 #include "s_debug.h"
103 #include "s_user.h"
104 #include "send.h"
105
106 #include <assert.h>
107 #include <stdlib.h>
108 #include <string.h>
109
110 /*
111  * Helper function to find last 0 in a comma-separated list of
112  * channel names.
113  */
114 static char *
115 last0(char *chanlist)
116 {
117   char *p;
118
119   for (p = chanlist; p[0]; p++) /* find last "JOIN 0" */
120     if (p[0] == '0' && (p[1] == ',' || p[1] == '\0' || !IsChannelChar(p[1]))) {
121       chanlist = p; /* we'll start parsing here */
122
123       if (!p[1]) /* hit the end */
124         break;
125
126       p++;
127     } else {
128       while (p[0] != ',' && p[0] != '\0') /* skip past channel name */
129         p++;
130
131       if (!p[0]) /* hit the end */
132         break;
133     }
134
135   return chanlist;
136 }
137
138 /*
139  * Helper function to perform a JOIN 0 if needed; returns 0 if channel
140  * name is not 0, else removes user from all channels and returns 1.
141  */
142 static int
143 join0(struct JoinBuf *join, struct Client *cptr, struct Client *sptr,
144       char *chan)
145 {
146   struct Membership *member;
147   struct JoinBuf part;
148
149   /* is it a JOIN 0? */
150   if (chan[0] != '0' || chan[1] != '\0')
151     return 0;
152   
153   joinbuf_join(join, 0, 0); /* join special channel 0 */
154
155   /* leave all channels */
156   joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
157                "Left all channels", 0);
158
159   while ((member = cli_user(sptr)->channel))
160     joinbuf_join(&part, member->channel,
161                  IsZombie(member) ? CHFL_ZOMBIE : 0);
162
163   joinbuf_flush(&part);
164
165   return 1;
166 }
167
168 /*
169  * m_join - generic message handler
170  */
171 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
172 {
173   struct Channel *chptr;
174   struct JoinBuf join;
175   struct JoinBuf create;
176   struct Gline *gline;
177   unsigned int flags = 0;
178   int i;
179   char *p = 0;
180   char *chanlist;
181   char *name;
182   char *keys;
183
184   if (parc < 2 || *parv[1] == '\0')
185     return need_more_params(sptr, "JOIN");
186
187   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
188   joinbuf_init(&create, sptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
189
190   chanlist = last0(parv[1]); /* find last "JOIN 0" */
191
192   keys = parv[2]; /* remember where keys are */
193
194   for (name = ircd_strtok(&p, chanlist, ","); name;
195        name = ircd_strtok(&p, 0, ",")) {
196     clean_channelname(name);
197
198     if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
199       continue;
200
201     if (!IsChannelName(name)) { /* bad channel name */
202       send_reply(sptr, ERR_NOSUCHCHANNEL, name);
203       continue;
204     }
205
206     /* BADCHANed channel */
207     if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
208         GlineIsActive(gline) && !IsAnOper(sptr)) {
209       send_reply(sptr, ERR_BANNEDFROMCHAN, name);
210       continue;
211     }
212
213     if ((chptr = FindChannel(name))) {
214       if (find_member_link(chptr, sptr))
215         continue; /* already on channel */
216
217       flags = CHFL_DEOPPED;
218     } else
219       flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
220
221     if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER) &&
222         !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
223       send_reply(sptr, ERR_TOOMANYCHANNELS, chptr ? chptr->chname : name);
224       break; /* no point processing the other channels */
225     }
226
227     if (chptr) {
228       if (check_target_limit(sptr, chptr, chptr->chname, 0))
229         continue; /* exceeded target limit */
230       else if ((i = can_join(sptr, chptr, keys))) {
231         if (i > MAGIC_OPER_OVERRIDE) { /* oper overrode mode */
232           switch (i - MAGIC_OPER_OVERRIDE) {
233           case ERR_CHANNELISFULL: /* figure out which mode */
234             i = 'l';
235             break;
236
237           case ERR_INVITEONLYCHAN:
238             i = 'i';
239             break;
240
241           case ERR_BANNEDFROMCHAN:
242             i = 'b';
243             break;
244
245           case ERR_BADCHANNELKEY:
246             i = 'k';
247             break;
248           }
249
250           /* send accountability notice */
251           sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
252                                "(overriding +%c)", sptr, chptr, i);
253         } else {
254           send_reply(sptr, i, chptr->chname);
255           continue;
256         }
257       } /* else if ((i = can_join(sptr, chptr, keys))) { */
258
259       joinbuf_join(&join, chptr, flags);
260     } else if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
261       continue; /* couldn't get channel */
262     else if (check_target_limit(sptr, chptr, chptr->chname, 1)) {
263       /* Note: check_target_limit will only ever return 0 here */
264       sub1_from_channel(chptr); /* created it... */
265       continue;
266     } else
267       joinbuf_join(&create, chptr, flags);
268
269     del_invite(sptr, chptr);
270
271     if (chptr->topic[0]) {
272       send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
273       send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
274                  chptr->topic_time);
275     }
276
277     do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
278   }
279
280   joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
281   joinbuf_flush(&create);
282
283   return 0;
284 }
285
286 /*
287  * ms_join - server message handler
288  */
289 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
290 {
291   struct Membership *member;
292   struct Channel *chptr;
293   struct JoinBuf join;
294   unsigned int flags = 0;
295   time_t creation = 0;
296   char *p = 0;
297   char *chanlist;
298   char *name;
299
300   if (IsServer(sptr)) {
301     return protocol_violation(sptr,"%s tried to JOIN a channel, duh!", cli_name(sptr));
302   }
303
304   if (parc < 2 || *parv[1] == '\0')
305     return need_more_params(sptr, "JOIN");
306
307   if (parc > 2 && parv[2])
308     creation = atoi(parv[2]);
309
310   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
311
312   chanlist = last0(parv[1]); /* find last "JOIN 0" */
313
314   for (name = ircd_strtok(&p, chanlist, ","); name;
315        name = ircd_strtok(&p, 0, ",")) {
316     clean_channelname(name);
317
318     if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
319       continue;
320
321     if (IsLocalChannel(name) || !IsChannelName(name)) {
322       protocol_violation(sptr,"%s tried to join %s",cli_name(cptr),name);
323       continue;
324     }
325
326     if (!(chptr = FindChannel(name))) {
327       /* No channel exists, so create one */
328       if (!(chptr = get_channel(sptr, name, CGT_CREATE))) {
329         protocol_violation(sptr,"couldn't get channel %s for %s",
330                            name,cli_name(sptr));
331         continue;
332       }
333       flags = CHFL_DEOPPED | ((cli_flags(sptr) & FLAGS_TS8) ? CHFL_SERVOPOK : 0);
334
335       /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */ 
336       chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
337     }
338     else { /* We have a valid channel? */
339       if ((member = find_member_link(chptr, sptr))) {
340         if (!IsZombie(member)) /* already on channel */
341           continue;
342
343         flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
344         remove_user_from_channel(sptr, chptr);
345         chptr = FindChannel(name);
346       } else
347         flags = CHFL_DEOPPED | ((cli_flags(sptr) & FLAGS_TS8) ? CHFL_SERVOPOK : 0);
348     } 
349
350     joinbuf_join(&join, chptr, flags);
351   }
352
353   joinbuf_flush(&join); /* flush joins... */
354
355   return 0;
356 }