]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-channel.c
Clean up and document IRC_JOIN() and join_allowed() functions
[ngircd-alex.git] / src / ngircd / irc-channel.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * IRC channel commands
12  */
13
14
15 #include "portab.h"
16
17 #include "imp.h"
18 #include <assert.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "defines.h"
24 #include "conn.h"
25 #include "client.h"
26 #include "channel.h"
27 #include "conn-func.h"
28 #include "lists.h"
29 #include "log.h"
30 #include "match.h"
31 #include "messages.h"
32 #include "parse.h"
33 #include "irc-info.h"
34 #include "irc-write.h"
35 #include "resolve.h"
36 #include "conf.h"
37
38 #include "exp.h"
39 #include "irc-channel.h"
40
41
42 /*
43  * RFC 2812, (3.2.1 Join message Command):
44  *  Note that this message
45  *  accepts a special argument ("0"), which is a special request to leave all
46  *  channels the user is currently a member of. The server will process this
47  *  message as if the user had sent a PART command (See Section 3.2.2) for
48  *  each channel he is a member of.
49  */
50 static bool
51 part_from_all_channels(CLIENT* client, CLIENT *target)
52 {
53         CL2CHAN *cl2chan;
54         CHANNEL *chan;
55
56         while ((cl2chan = Channel_FirstChannelOf(target))) {
57                 chan = Channel_GetChannel(cl2chan);
58                 assert( chan != NULL );
59                 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
60         }
61         return CONNECTED;
62 }
63
64
65 /**
66  * Check weather a local client is allowed to join an already existing
67  * channel or not.
68  * @param Client Client that sent the JOIN command
69  * @param chan Channel to check
70  * @param channame Name of the channel
71  * @param key Provided channel key (or NULL if none has been provided)
72  * @return true if client is allowed to join channel, false otherwise
73  */
74 static bool
75 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
76              const char *key)
77 {
78         bool is_invited, is_banned;
79         const char *channel_modes;
80
81         /* Allow IRC operators to overwrite channel limits */
82         if (strchr(Client_Modes(Client), 'o'))
83                 return true;
84
85         is_banned = Lists_Check(Channel_GetListBans(chan), Client);
86         is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
87
88         if (is_banned && !is_invited) {
89                 /* Client is banned from channel (and not on invite list) */
90                 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
91                                    Client_ID(Client), channame);
92                 return false;
93         }
94
95         channel_modes = Channel_Modes(chan);
96         if ((strchr(channel_modes, 'i')) && !is_invited) {
97                 /* Channel is "invite-only" and client is not on invite list */
98                 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
99                                    Client_ID(Client), channame);
100                 return false;
101         }
102
103         if (!Channel_CheckKey(chan, Client, key ? key : "")) {
104                 /* Channel is protected by a channel key and the client
105                  * didn't specify the correct one */
106                 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
107                                    Client_ID(Client), channame);
108                 return false;
109         }
110
111         if ((strchr(channel_modes, 'l')) &&
112             (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
113                 /* There are more clints joined to this channel than allowed */
114                 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
115                                    Client_ID(Client), channame);
116                 return false;
117         }
118
119         return true;
120 }
121
122
123 static void
124 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
125 {
126         if (flags) {
127                 while (*flags) {
128                         Channel_UserModeAdd(chan, target, *flags);
129                         flags++;
130                 }
131         }
132
133         /* If channel persistent and client is ircop: make client chanop */
134         if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
135                 Channel_UserModeAdd(chan, target, 'o');
136 }
137
138
139 static void
140 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
141 {
142         CONN_ID conn;
143         char str[COMMAND_LEN], *ptr = NULL;
144
145         strlcpy(str, (char *)Data, sizeof(str));
146         conn = Client_Conn(To);
147
148         if (Conn_Options(conn) & CONN_RFC1459) {
149                 /* RFC 1459 compatibility mode, appended modes are NOT
150                  * supported, so strip them off! */
151                 ptr = strchr(str, 0x7);
152                 if (ptr)
153                         *ptr++ = '\0';
154         }
155
156         IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
157         if (ptr && *ptr)
158                 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
159                                          Client_ID(Prefix));
160 } /* cb_join_forward */
161
162
163 static void
164 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
165                                         const char *channame)
166 {
167         char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
168
169         strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
170         if (modes[1])
171                 modes[0] = 0x7;
172         else
173                 modes[0] = '\0';
174
175         /* forward to other servers (if it is not a local channel) */
176         if (!Channel_IsLocal(chan)) {
177                 snprintf(str, sizeof(str), "%s%s", channame, modes);
178                 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
179                                                  cb_join_forward, str);
180         }
181
182         /* tell users in this channel about the new client */
183         IRC_WriteStrChannelPrefix(Client, chan, target, false,
184                                   "JOIN :%s",  channame);
185
186         /* syncronize channel modes */
187         if (modes[1]) {
188                 IRC_WriteStrChannelPrefix(Client, chan, target, false,
189                                           "MODE %s +%s %s", channame,
190                                           &modes[1], Client_ID(target));
191         }
192 } /* join_forward */
193
194
195 static bool
196 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
197                                         const char *channame)
198 {
199         const char *topic;
200
201         if (Client_Type(Client) != CLIENT_USER)
202                 return true;
203         /* acknowledge join */
204         if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
205                 return false;
206
207         /* Send topic to client, if any */
208         topic = Channel_Topic(chan);
209         assert(topic != NULL);
210         if (*topic) {
211                 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
212                         Client_ID(Client), channame, topic))
213                                 return false;
214 #ifndef STRICT_RFC
215                 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
216                         Client_ID(Client), channame,
217                         Channel_TopicWho(chan),
218                         Channel_TopicTime(chan)))
219                                 return false;
220 #endif
221         }
222         /* send list of channel members to client */
223         if (!IRC_Send_NAMES(Client, chan))
224                 return false;
225         return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
226 }
227
228
229 GLOBAL bool
230 IRC_JOIN( CLIENT *Client, REQUEST *Req )
231 {
232         char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
233         CLIENT *target;
234         CHANNEL *chan;
235
236         assert( Client != NULL );
237         assert( Req != NULL );
238
239         /* Bad number of arguments? */
240         if (Req->argc < 1 || Req->argc > 2)
241                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
242                                           Client_ID(Client), Req->command);
243
244         /* Who is the sender? */
245         if (Client_Type(Client) == CLIENT_SERVER)
246                 target = Client_Search(Req->prefix);
247         else
248                 target = Client;
249
250         if (!target)
251                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
252
253         /* Is argument "0"? */
254         if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
255                 return part_from_all_channels(Client, target);
256
257         /* Are channel keys given? */
258         if (Req->argc > 1)
259                 key = strtok_r(Req->argv[1], ",", &lastkey);
260
261         channame = Req->argv[0];
262         channame = strtok_r(channame, ",", &lastchan);
263
264         /* Make sure that "channame" is not the empty string ("JOIN :") */
265         if (! channame)
266                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
267                                           Client_ID(Client), Req->command);
268
269         while (channame) {
270                 flags = NULL;
271
272                 /* Did the server include channel-user-modes? */
273                 if (Client_Type(Client) == CLIENT_SERVER) {
274                         flags = strchr(channame, 0x7);
275                         if (flags) {
276                                 *flags = '\0';
277                                 flags++;
278                         }
279                 }
280
281                 chan = Channel_Search(channame);
282                 if (!chan && Conf_PredefChannelsOnly) {
283                          /* channel must be created, but server does not allow this */
284                         IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
285                         break;
286                 }
287
288                 /* Local client? */
289                 if (Client_Type(Client) == CLIENT_USER) {
290                         /* Test if the user has reached the channel limit */
291                         if ((Conf_MaxJoins > 0) &&
292                             (Channel_CountForUser(Client) >= Conf_MaxJoins))
293                                 return IRC_WriteStrClient(Client,
294                                                 ERR_TOOMANYCHANNELS_MSG,
295                                                 Client_ID(Client), channame);
296                         if (chan) {
297                                 /* Already existing channel: check if the
298                                  * client is allowed to join */
299                                 if (!join_allowed(Client, chan, channame, key))
300                                         break;
301                         } else {
302                                 /* New channel: first user will become channel
303                                  * operator unless this is a modeless channel */
304                                 if (*channame != '+')
305                                         flags = "o";
306                         }
307
308                         /* Local client: update idle time */
309                         Conn_UpdateIdle(Client_Conn(Client));
310                 } else {
311                         /* Remote server: we don't need to know whether the
312                          * client is invited or not, but we have to make sure
313                          * that the "one shot" entries (generated by INVITE
314                          * commands) in this list become deleted when a user
315                          * joins a channel this way. */
316                         if (chan)
317                                 (void)Lists_Check(Channel_GetListInvites(chan),
318                                                   target);
319                 }
320
321                 /* Join channel (and create channel if it doesn't exist) */
322                 if (!Channel_Join(target, channame))
323                         break;
324
325                 if (!chan) { /* channel is new; it has been created above */
326                         chan = Channel_Search(channame);
327                         assert(chan != NULL);
328                         if (Channel_IsModeless(chan)) {
329                                 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
330                                 Channel_ModeAdd(chan, 'n'); /* no external msgs */
331                         }
332                 }
333                 assert(chan != NULL);
334
335                 join_set_channelmodes(chan, target, flags);
336
337                 join_forward(Client, target, chan, channame);
338
339                 if (!join_send_topic(Client, target, chan, channame))
340                         break; /* write error */
341
342                 /* next channel? */
343                 channame = strtok_r(NULL, ",", &lastchan);
344                 if (channame && key)
345                         key = strtok_r(NULL, ",", &lastkey);
346         }
347         return CONNECTED;
348 } /* IRC_JOIN */
349
350
351 /**
352  * Handler for the IRC "PART" command.
353  */
354 GLOBAL bool
355 IRC_PART(CLIENT * Client, REQUEST * Req)
356 {
357         CLIENT *target;
358         char *chan;
359
360         assert(Client != NULL);
361         assert(Req != NULL);
362
363         if (Req->argc < 1 || Req->argc > 2)
364                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
365                                           Client_ID(Client), Req->command);
366
367         /* Get the sender */
368         if (Client_Type(Client) == CLIENT_SERVER)
369                 target = Client_Search(Req->prefix);
370         else
371                 target = Client;
372         if (!target)
373                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
374                                           Client_ID(Client), Req->prefix);
375
376         /* Loop over all the given channel names */
377         chan = strtok(Req->argv[0], ",");
378
379         /* Make sure that "chan" is not the empty string ("PART :") */
380         if (! chan)
381                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
382                                           Client_ID(Client), Req->command);
383
384         while (chan) {
385                 Channel_Part(target, Client, chan,
386                              Req->argc > 1 ? Req->argv[1] : Client_ID(target));
387                 chan = strtok(NULL, ",");
388         }
389
390         /* Update idle time, if local client */
391         if (Client_Conn(Client) > NONE)
392                 Conn_UpdateIdle(Client_Conn(Client));
393
394         return CONNECTED;
395 } /* IRC_PART */
396
397
398 GLOBAL bool
399 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
400 {
401         CHANNEL *chan;
402         CLIENT *from;
403         char *topic;
404         bool r;
405
406         assert( Client != NULL );
407         assert( Req != NULL );
408
409         if ((Req->argc < 1) || (Req->argc > 2))
410                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
411
412         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
413         else from = Client;
414         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
415
416         /* Welcher Channel? */
417         chan = Channel_Search( Req->argv[0] );
418         if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
419
420         /* Ist der User Mitglied in dem Channel? */
421         if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
422
423         if( Req->argc == 1 )
424         {
425                 /* Request actual topic */
426                 topic = Channel_Topic(chan);
427                 if (*topic) {
428                         r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
429                                 Client_ID(Client), Channel_Name(chan), topic);
430 #ifndef STRICT_RFC
431                         r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
432                                 Client_ID(Client), Channel_Name(chan),
433                                 Channel_TopicWho(chan),
434                                 Channel_TopicTime(chan));
435 #endif
436                         return r;
437                 }
438                 else
439                          return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
440                                         Client_ID(from), Channel_Name(chan));
441         }
442
443         if( strchr( Channel_Modes( chan ), 't' ))
444         {
445                 /* Topic Lock. Ist der User ein Channel Operator? */
446                 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
447         }
448
449         /* Set new topic */
450         Channel_SetTopic(chan, from, Req->argv[1]);
451         LogDebug("%s \"%s\" set topic on \"%s\": %s",
452                  Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
453                  Req->argv[1][0] ? Req->argv[1] : "<none>");
454
455         /* Update channel and forward new topic to other servers */
456         if (!Channel_IsLocal(chan))
457                 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
458                                           Req->argv[0], Req->argv[1]);
459         IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
460                                   Req->argv[0], Req->argv[1]);
461
462         if (Client_Type(Client) == CLIENT_USER)
463                 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
464                                                 Req->argv[0], Req->argv[1]);
465         else
466                 return CONNECTED;
467 } /* IRC_TOPIC */
468
469
470 /**
471  * Handler for the IRC "LIST" command.
472  * This implementation handles the local case as well as the forwarding of the
473  * LIST command to other servers in the IRC network.
474  */
475 GLOBAL bool
476 IRC_LIST( CLIENT *Client, REQUEST *Req )
477 {
478         char *pattern;
479         CHANNEL *chan;
480         CLIENT *from, *target;
481
482         assert( Client != NULL );
483         assert( Req != NULL );
484
485         /* Bad number of prameters? */
486         if( Req->argc > 2 )
487                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
488                         Client_ID( Client ), Req->command );
489
490         if( Req->argc > 0 )
491                 pattern = strtok( Req->argv[0], "," );
492         else
493                 pattern = "*";
494
495         /* Get sender from prefix, if any */
496         if( Client_Type( Client ) == CLIENT_SERVER )
497                 from = Client_Search( Req->prefix );
498         else
499                 from = Client;
500
501         if( ! from )
502                 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
503                                 Client_ID( Client ), Req->prefix );
504
505         if( Req->argc == 2 )
506         {
507                 /* Forward to other server? */
508                 target = Client_Search( Req->argv[1] );
509                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
510                         return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
511                                         Client_ID( Client ), Req->argv[1] );
512
513                 if( target != Client_ThisServer( ))
514                 {
515                         /* Target is indeed an other server, forward it! */
516                         return IRC_WriteStrClientPrefix( target, from,
517                                         "LIST %s :%s", Client_ID( from ),
518                                         Req->argv[1] );
519                 }
520         }
521
522         while( pattern )
523         {
524                 /* Loop through all the channels */
525                 chan = Channel_First( );
526                 while( chan )
527                 {
528                         /* Check search pattern */
529                         if( Match( pattern, Channel_Name( chan )))
530                         {
531                                 /* Gotcha! */
532                                 if( ! strchr( Channel_Modes( chan ), 's' ) ||
533                                     Channel_IsMemberOf( chan, from ))
534                                 {
535                                         if( ! IRC_WriteStrClient( from,
536                                             RPL_LIST_MSG, Client_ID( from ),
537                                             Channel_Name( chan ),
538                                             Channel_MemberCount( chan ),
539                                             Channel_Topic( chan )))
540                                                 return DISCONNECTED;
541                                 }
542                         }
543                         chan = Channel_Next( chan );
544                 }
545
546                 /* Get next name ... */
547                 if( Req->argc > 0 )
548                         pattern = strtok( NULL, "," );
549                 else
550                         pattern = NULL;
551         }
552
553         return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
554 } /* IRC_LIST */
555
556
557 GLOBAL bool
558 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
559 {
560         char modes_add[COMMAND_LEN], l[16], *ptr;
561         CLIENT *from;
562         CHANNEL *chan;
563         int arg_topic;
564
565         assert( Client != NULL );
566         assert( Req != NULL );
567
568         /* Bad number of parameters? */
569         if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
570
571         /* Compatibility kludge */
572         if( Req->argc == 5 ) arg_topic = 4;
573         else if( Req->argc == 3 ) arg_topic = 2;
574         else arg_topic = -1;
575
576         /* Search origin */
577         from = Client_Search( Req->prefix );
578         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
579
580         /* Search or create channel */
581         chan = Channel_Search( Req->argv[0] );
582         if( ! chan ) chan = Channel_Create( Req->argv[0] );
583         if( ! chan ) return CONNECTED;
584
585         if( Req->argv[1][0] == '+' )
586         {
587                 ptr = Channel_Modes( chan );
588                 if( ! *ptr )
589                 {
590                         /* OK, this channel doesn't have modes jet, set the received ones: */
591                         Channel_SetModes( chan, &Req->argv[1][1] );
592
593                         if( Req->argc == 5 )
594                         {
595                                 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
596                                 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
597                         }
598                         else
599                         {
600                                 /* Delete modes which we never want to inherit */
601                                 Channel_ModeDel( chan, 'l' );
602                                 Channel_ModeDel( chan, 'k' );
603                         }
604
605                         strcpy( modes_add, "" );
606                         ptr = Channel_Modes( chan );
607                         while( *ptr )
608                         {
609                                 if( *ptr == 'l' )
610                                 {
611                                         snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
612                                         strlcat( modes_add, l, sizeof( modes_add ));
613                                 }
614                                 if( *ptr == 'k' )
615                                 {
616                                         strlcat( modes_add, " ", sizeof( modes_add ));
617                                         strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
618                                 }
619                                 ptr++;
620                         }
621
622                         /* Inform members of this channel */
623                         IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
624                 }
625         }
626         else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
627
628         if( arg_topic > 0 )
629         {
630                 /* We got a topic */
631                 ptr = Channel_Topic( chan );
632                 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
633                 {
634                         /* OK, there is no topic jet */
635                         Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
636                         IRC_WriteStrChannelPrefix(Client, chan, from, false,
637                              "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
638                 }
639         }
640
641         /* Forward CHANINFO to other serevrs */
642         if( Req->argc == 5 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2], Req->argv[3], Req->argv[4] );
643         else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
644         else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
645
646         return CONNECTED;
647 } /* IRC_CHANINFO */
648
649
650 /* -eof- */