]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-channel.c
Add support for modeless channels
[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) {
243                                 /*
244                                  * New Channel: first user will be channel operator
245                                  * unless this is a modeless channel... */
246 #ifndef STRICT_RFC
247                                 if (*channame != '+')
248 #endif
249                                         flags = "o";
250                         } else
251                                 if (!join_allowed(Client, target, chan, channame, key))
252                                         break;
253                 } else {
254                         /* Remote server: we don't need to know whether the
255                          * client is invited or not, but we have to make sure
256                          * that the "one shot" entries (generated by INVITE
257                          * commands) in this list become deleted when a user
258                          * joins a channel this way. */
259                         if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
260                 }
261
262                 /* Join channel (and create channel if it doesn't exist) */
263                 if (!Channel_Join(target, channame))
264                         break;
265
266                 if (!chan) { /* channel is new; it has been created above */
267                         chan = Channel_Search(channame);
268                         assert(chan != NULL);
269                         if (*channame == '+') { /* modeless channel... */
270                                 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
271                                 Channel_ModeAdd(chan, 'n'); /* no external msgs */
272                         }
273                 }
274                 assert(chan != NULL);
275
276                 join_set_channelmodes(chan, target, flags);
277
278                 join_forward(Client, target, chan, channame);
279
280                 if (!join_send_topic(Client, target, chan, channame))
281                         break; /* write error */
282
283                 /* next channel? */
284                 channame = channame_ptr;
285                 if (channame) {
286                         channame++;
287                         channame_ptr = strchr(channame, ',');
288                         if (channame_ptr) *channame_ptr = '\0';
289
290                         if (key_ptr) {
291                                 key = ++key_ptr;
292                                 key_ptr = strchr(key, ',');
293                                 if (key_ptr) *key_ptr = '\0';
294                         }
295                 }
296         }
297         return CONNECTED;
298 } /* IRC_JOIN */
299
300
301 /**
302  * Handler for the IRC "PART" command.
303  */
304 GLOBAL bool
305 IRC_PART(CLIENT * Client, REQUEST * Req)
306 {
307         CLIENT *target;
308         char *chan;
309
310         assert(Client != NULL);
311         assert(Req != NULL);
312
313         if (Req->argc < 1 || Req->argc > 2)
314                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
315                                           Client_ID(Client), Req->command);
316
317         /* Get the sender */
318         if (Client_Type(Client) == CLIENT_SERVER)
319                 target = Client_Search(Req->prefix);
320         else
321                 target = Client;
322         if (!target)
323                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
324                                           Client_ID(Client), Req->prefix);
325
326         /* Loop over all the given channel names */
327         chan = strtok(Req->argv[0], ",");
328         while (chan) {
329                 Channel_Part(target, Client, chan,
330                              Req->argc > 1 ? Req->argv[1] : Client_ID(target));
331                 chan = strtok(NULL, ",");
332         }
333         return CONNECTED;
334 } /* IRC_PART */
335
336
337 GLOBAL bool
338 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
339 {
340         CHANNEL *chan;
341         CLIENT *from;
342         char *topic;
343         bool r;
344
345         assert( Client != NULL );
346         assert( Req != NULL );
347
348         if ((Req->argc < 1) || (Req->argc > 2))
349                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
350
351         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
352         else from = Client;
353         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
354
355         /* Welcher Channel? */
356         chan = Channel_Search( Req->argv[0] );
357         if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
358
359         /* Ist der User Mitglied in dem Channel? */
360         if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
361
362         if( Req->argc == 1 )
363         {
364                 /* Request actual topic */
365                 topic = Channel_Topic(chan);
366                 if (*topic) {
367                         r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
368                                 Client_ID(Client), Channel_Name(chan), topic);
369 #ifndef STRICT_RFC
370                         r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
371                                 Client_ID(Client), Channel_Name(chan),
372                                 Channel_TopicWho(chan),
373                                 Channel_TopicTime(chan));
374 #endif
375                         return r;
376                 }
377                 else
378                          return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
379                                         Client_ID(from), Channel_Name(chan));
380         }
381
382         if( strchr( Channel_Modes( chan ), 't' ))
383         {
384                 /* Topic Lock. Ist der User ein Channel Operator? */
385                 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
386         }
387
388         /* Set new topic */
389         Channel_SetTopic(chan, from, Req->argv[1]);
390         Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
391                 Client_Mask(from), Channel_Name(chan),
392                 Req->argv[1][0] ? Req->argv[1] : "<none>");
393
394         /* im Channel bekannt machen und an Server weiterleiten */
395         IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
396         IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
397
398         if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
399         else return CONNECTED;
400 } /* IRC_TOPIC */
401
402
403 /**
404  * Handler for the IRC "LIST" command.
405  * This implementation handles the local case as well as the forwarding of the
406  * LIST command to other servers in the IRC network.
407  */
408 GLOBAL bool
409 IRC_LIST( CLIENT *Client, REQUEST *Req )
410 {
411         char *pattern;
412         CHANNEL *chan;
413         CLIENT *from, *target;
414
415         assert( Client != NULL );
416         assert( Req != NULL );
417
418         /* Bad number of prameters? */
419         if( Req->argc > 2 )
420                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
421                         Client_ID( Client ), Req->command );
422
423         if( Req->argc > 0 )
424                 pattern = strtok( Req->argv[0], "," );
425         else
426                 pattern = "*";
427
428         /* Get sender from prefix, if any */
429         if( Client_Type( Client ) == CLIENT_SERVER )
430                 from = Client_Search( Req->prefix );
431         else
432                 from = Client;
433
434         if( ! from )
435                 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
436                                 Client_ID( Client ), Req->prefix );
437
438         if( Req->argc == 2 )
439         {
440                 /* Forward to other server? */
441                 target = Client_Search( Req->argv[1] );
442                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
443                         return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
444                                         Client_ID( Client ), Req->argv[1] );
445
446                 if( target != Client_ThisServer( ))
447                 {
448                         /* Target is indeed an other server, forward it! */
449                         return IRC_WriteStrClientPrefix( target, from,
450                                         "LIST %s :%s", Client_ID( from ),
451                                         Req->argv[1] );
452                 }
453         }
454         
455         while( pattern )
456         {
457                 /* Loop through all the channels */
458                 chan = Channel_First( );
459                 while( chan )
460                 {
461                         /* Check search pattern */
462                         if( Match( pattern, Channel_Name( chan )))
463                         {
464                                 /* Gotcha! */
465                                 if( ! strchr( Channel_Modes( chan ), 's' ) ||
466                                     Channel_IsMemberOf( chan, from ))
467                                 {
468                                         if( ! IRC_WriteStrClient( from,
469                                             RPL_LIST_MSG, Client_ID( from ),
470                                             Channel_Name( chan ),
471                                             Channel_MemberCount( chan ),
472                                             Channel_Topic( chan )))
473                                                 return DISCONNECTED;
474                                 }
475                         }
476                         chan = Channel_Next( chan );
477                 }
478                 
479                 /* Get next name ... */
480                 if( Req->argc > 0 )
481                         pattern = strtok( NULL, "," );
482                 else
483                         pattern = NULL;
484         }
485         
486         return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
487 } /* IRC_LIST */
488
489
490 GLOBAL bool
491 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
492 {
493         char modes_add[COMMAND_LEN], l[16], *ptr;
494         CLIENT *from;
495         CHANNEL *chan;
496         int arg_topic;
497
498         assert( Client != NULL );
499         assert( Req != NULL );
500
501         /* Bad number of parameters? */
502         if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
503
504         /* Compatibility kludge */
505         if( Req->argc == 5 ) arg_topic = 4;
506         else if( Req->argc == 3 ) arg_topic = 2;
507         else arg_topic = -1;
508
509         /* Search origin */
510         from = Client_Search( Req->prefix );
511         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
512
513         /* Search or create channel */
514         chan = Channel_Search( Req->argv[0] );
515         if( ! chan ) chan = Channel_Create( Req->argv[0] );
516         if( ! chan ) return CONNECTED;
517
518         if( Req->argv[1][0] == '+' )
519         {
520                 ptr = Channel_Modes( chan );
521                 if( ! *ptr )
522                 {
523                         /* OK, this channel doesn't have modes jet, set the received ones: */
524                         Channel_SetModes( chan, &Req->argv[1][1] );
525
526                         if( Req->argc == 5 )
527                         {
528                                 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
529                                 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
530                         }
531                         else
532                         {
533                                 /* Delete modes which we never want to inherit */
534                                 Channel_ModeDel( chan, 'l' );
535                                 Channel_ModeDel( chan, 'k' );
536                         }
537
538                         strcpy( modes_add, "" );
539                         ptr = Channel_Modes( chan );
540                         while( *ptr )
541                         {
542                                 if( *ptr == 'l' )
543                                 {
544                                         snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
545                                         strlcat( modes_add, l, sizeof( modes_add ));
546                                 }
547                                 if( *ptr == 'k' )
548                                 {
549                                         strlcat( modes_add, " ", sizeof( modes_add ));
550                                         strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
551                                 }
552                                 ptr++;
553                         }
554                         
555                         /* Inform members of this channel */
556                         IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
557                 }
558         }
559         else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
560
561         if( arg_topic > 0 )
562         {
563                 /* We got a topic */
564                 ptr = Channel_Topic( chan );
565                 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
566                 {
567                         /* OK, there is no topic jet */
568                         Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
569                         IRC_WriteStrChannelPrefix(Client, chan, from, false,
570                              "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
571                 }
572         }
573
574         /* Forward CHANINFO to other serevrs */
575         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] );
576         else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
577         else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
578
579         return CONNECTED;
580 } /* IRC_CHANINFO */
581
582
583 /* -eof- */