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