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