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