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