f230beb07219d0c0656ff97de23e373dd2c81fc8
[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     /* Empty keys are the same as no keys. */
135     if (key && !key[0])
136       key = 0;
137
138     clean_channelname(name);
139     if (!IsChannelName(name))
140     {
141       /* bad channel name */
142       send_reply(sptr, ERR_NOSUCHCHANNEL, name);
143       continue;
144     }
145
146     if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER)
147         && !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
148       send_reply(sptr, ERR_TOOMANYCHANNELS, name);
149       break; /* no point processing the other channels */
150     }
151
152     /* BADCHANed channel */
153     if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
154         GlineIsActive(gline) && !IsAnOper(sptr)) {
155       send_reply(sptr, ERR_BANNEDFROMCHAN, name);
156       continue;
157     }
158
159     if (!(chptr = FindChannel(name))) {
160       if ((name[0] == '&') && !feature_bool(FEAT_LOCAL_CHANNELS)) {
161         send_reply(sptr, ERR_NOSUCHCHANNEL, name);
162         continue;
163       }
164
165       if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
166         continue;
167
168       /* Try to add the new channel as a recent target for the user. */
169       if (check_target_limit(sptr, chptr, chptr->chname, 1)) {
170         chptr->members = 0;
171         destruct_channel(chptr);
172         continue;
173       }
174
175       joinbuf_join(&create, chptr, CHFL_CHANOP | CHFL_CHANNEL_MANAGER);
176     } else if (find_member_link(chptr, sptr)) {
177       continue; /* already on channel */
178     } else if (check_target_limit(sptr, chptr, chptr->chname, 0)) {
179       continue;
180     } else {
181       int flags = CHFL_DEOPPED;
182       int err = 0;
183
184       /* Check target change limits. */
185
186       /* Check Apass/Upass -- since we only ever look at a single
187        * "key" per channel now, this hampers brute force attacks. */
188       if (key && !strcmp(key, chptr->mode.apass))
189         flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
190       else if (key && !strcmp(key, chptr->mode.upass))
191         flags = CHFL_CHANOP;
192       else if (IsInvited(sptr, chptr)) {
193         /* Invites bypass these other checks. */
194       } else if (chptr->mode.mode & MODE_INVITEONLY)
195         err = ERR_INVITEONLYCHAN;
196       else if (chptr->mode.limit && (chptr->users >= chptr->mode.limit))
197         err = ERR_CHANNELISFULL;
198       else if ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
199         err = ERR_NEEDREGGEDNICK;
200       else if (find_ban(sptr, chptr->banlist))
201         err = ERR_BANNEDFROMCHAN;
202       else if (*chptr->mode.key && (!key || strcmp(key, chptr->mode.key)))
203         err = ERR_BADCHANNELKEY;
204
205       /* An oper with WALK_LCHAN privilege can join a local channel
206        * he otherwise could not join by using "OVERRIDE" as the key.
207        * This will generate a HACK(4) notice, but fails if the oper
208        * could normally join the channel. */
209       if (IsLocalChannel(chptr->chname)
210           && HasPriv(sptr, PRIV_WALK_LCHAN)
211           && !(flags & CHFL_CHANOP)
212           && key && !strcmp(key, "OVERRIDE")
213           && strcmp(chptr->mode.key, "OVERRIDE"))
214       {
215         switch (err) {
216         case 0:
217           send_reply(sptr, ERR_DONTCHEAT, chptr->chname);
218           continue;
219         case ERR_INVITEONLYCHAN: err = 'i'; break;
220         case ERR_CHANNELISFULL:  err = 'l'; break;
221         case ERR_BANNEDFROMCHAN: err = 'b'; break;
222         case ERR_BADCHANNELKEY:  err = 'k'; break;
223         case ERR_NEEDREGGEDNICK: err = 'r'; break;
224         default: err = '?'; break;
225         }
226         /* send accountability notice */
227         sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
228                              "(overriding +%c)", sptr, chptr, err);
229         err = 0;
230       }
231
232       /* Is there some reason the user may not join? */
233       if (err) {
234         send_reply(sptr, err, chptr->chname);
235         continue;
236       }
237
238       joinbuf_join(&join, chptr, flags);
239     }
240
241     del_invite(sptr, chptr);
242
243     if (chptr->topic[0]) {
244       send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
245       send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
246                  chptr->topic_time);
247     }
248
249     do_names(sptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
250   }
251
252   joinbuf_flush(&join); /* must be first, if there's a JOIN 0 */
253   joinbuf_flush(&create);
254
255   return 0;
256 }
257
258 /** Handle a JOIN message from a server connection.
259  * See @ref m_functions for discussion of the arguments.
260  * @param[in] cptr Client that sent us the message.
261  * @param[in] sptr Original source of message.
262  * @param[in] parc Number of arguments.
263  * @param[in] parv Argument vector.
264  */
265 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
266 {
267   struct Membership *member;
268   struct Channel *chptr;
269   struct JoinBuf join;
270   unsigned int flags;
271   time_t creation = 0;
272   char *p = 0;
273   char *chanlist;
274   char *name;
275
276   if (IsServer(sptr))
277   {
278     return protocol_violation(cptr,
279                               "%s tried to JOIN %s, duh!",
280                               cli_name(sptr),
281                               (parc < 2 || *parv[1] == '\0') ? "a channel" :
282                                                                parv[1]
283                               );
284   }
285
286   if (parc < 2 || *parv[1] == '\0')
287     return need_more_params(sptr, "JOIN");
288
289   if (parc > 2 && parv[2])
290     creation = atoi(parv[2]);
291
292   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
293
294   chanlist = last0(cptr, sptr, parv[1]); /* find last "JOIN 0" */
295
296   for (name = ircd_strtok(&p, chanlist, ","); name;
297        name = ircd_strtok(&p, 0, ",")) {
298
299     if (name[0] == '0' && name[1] == ':')
300     {
301       flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
302       name += 2;
303     }
304     else if (name[0] == '1' && name[1] == ':')
305     {
306       flags = CHFL_CHANOP;
307       name += 2;
308     }
309     else
310       flags = CHFL_DEOPPED;
311
312     if (IsLocalChannel(name) || !IsChannelName(name))
313     {
314       protocol_violation(cptr, "%s tried to join %s", cli_name(sptr), name);
315       continue;
316     }
317
318     if (!(chptr = FindChannel(name)))
319     {
320       /* No channel exists, so create one */
321       if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
322       {
323         protocol_violation(sptr,"couldn't get channel %s for %s",
324                            name,cli_name(sptr));
325         continue;
326       }
327       flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
328
329       /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */
330       chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
331     }
332     else { /* We have a valid channel? */
333       if ((member = find_member_link(chptr, sptr)))
334       {
335         /* It is impossible to get here --Run */
336         if (!IsZombie(member)) /* already on channel */
337           continue;
338
339         flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
340         remove_user_from_channel(sptr, chptr);
341         chptr = FindChannel(name);
342       }
343       else
344         flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
345       /* Always copy the timestamp when it is older, that is the only way to
346          ensure network-wide synchronization of creation times. */
347       if (creation && creation < chptr->creationtime)
348         chptr->creationtime = creation;
349     }
350
351     joinbuf_join(&join, chptr, flags);
352   }
353
354   joinbuf_flush(&join); /* flush joins... */
355
356   return 0;
357 }