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