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