]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-channel.c
IRC_JOIN cleanups.
[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.44 2008/02/16 11:21:33 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                 chan = Channel_Search(channame);
221                 if (!chan && Conf_PredefChannelsOnly) {
222                          /* channel must be created, but server does not allow this */
223                         IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
224                         break;
225                 }
226
227                 /* Did the server include channel-user-modes? */
228                 if (Client_Type(Client) == CLIENT_SERVER) {
229                         flags = strchr(channame, 0x7);
230                         if (flags) {
231                                 *flags = '\0';
232                                 flags++;
233                         }
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 GLOBAL bool
290 IRC_PART( CLIENT *Client, REQUEST *Req )
291 {
292         CLIENT *target;
293         char *chan;
294
295         assert( Client != NULL );
296         assert( Req != NULL );
297
298         if (Req->argc < 1 || Req->argc > 2)
299                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
300                                         Client_ID(Client), Req->command);
301
302         /* Wer ist der Absender? */
303         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
304         else target = Client;
305         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
306
307         /* Channel-Namen durchgehen */
308         chan = strtok(Req->argv[0], ",");
309         while (chan) {
310                 Channel_Part(target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID(target));
311
312                 chan = strtok(NULL, ",");
313         }
314         return CONNECTED;
315 } /* IRC_PART */
316
317
318 GLOBAL bool
319 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
320 {
321         CHANNEL *chan;
322         CLIENT *from;
323         char *topic;
324         bool r;
325
326         assert( Client != NULL );
327         assert( Req != NULL );
328
329         /* Falsche Anzahl Parameter? */
330         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
331
332         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
333         else from = Client;
334         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
335
336         /* Welcher Channel? */
337         chan = Channel_Search( Req->argv[0] );
338         if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
339
340         /* Ist der User Mitglied in dem Channel? */
341         if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
342
343         if( Req->argc == 1 )
344         {
345                 /* Request actual topic */
346                 topic = Channel_Topic(chan);
347                 if (*topic) {
348                         r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
349                                 Client_ID(Client), Channel_Name(chan), topic);
350 #ifndef STRICT_RFC
351                         r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
352                                 Client_ID(Client), Channel_Name(chan),
353                                 Channel_TopicWho(chan),
354                                 Channel_TopicTime(chan));
355 #endif
356                         return r;
357                 }
358                 else
359                          return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
360                                         Client_ID(from), Channel_Name(chan));
361         }
362
363         if( strchr( Channel_Modes( chan ), 't' ))
364         {
365                 /* Topic Lock. Ist der User ein Channel Operator? */
366                 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
367         }
368
369         /* Set new topic */
370         Channel_SetTopic(chan, from, Req->argv[1]);
371         Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
372                 Client_Mask(from), Channel_Name(chan),
373                 Req->argv[1][0] ? Req->argv[1] : "<none>");
374
375         /* im Channel bekannt machen und an Server weiterleiten */
376         IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
377         IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
378
379         if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
380         else return CONNECTED;
381 } /* IRC_TOPIC */
382
383
384 /**
385  * Handler for the IRC "LIST" command.
386  * This implementation handles the local case as well as the forwarding of the
387  * LIST command to other servers in the IRC network.
388  */
389 GLOBAL bool
390 IRC_LIST( CLIENT *Client, REQUEST *Req )
391 {
392         char *pattern;
393         CHANNEL *chan;
394         CLIENT *from, *target;
395
396         assert( Client != NULL );
397         assert( Req != NULL );
398
399         /* Bad number of prameters? */
400         if( Req->argc > 2 )
401                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
402                         Client_ID( Client ), Req->command );
403
404         if( Req->argc > 0 )
405                 pattern = strtok( Req->argv[0], "," );
406         else
407                 pattern = "*";
408
409         /* Get sender from prefix, if any */
410         if( Client_Type( Client ) == CLIENT_SERVER )
411                 from = Client_Search( Req->prefix );
412         else
413                 from = Client;
414
415         if( ! from )
416                 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
417                                 Client_ID( Client ), Req->prefix );
418
419         if( Req->argc == 2 )
420         {
421                 /* Forward to other server? */
422                 target = Client_Search( Req->argv[1] );
423                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
424                         return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
425                                         Client_ID( Client ), Req->argv[1] );
426
427                 if( target != Client_ThisServer( ))
428                 {
429                         /* Target is indeed an other server, forward it! */
430                         return IRC_WriteStrClientPrefix( target, from,
431                                         "LIST %s :%s", Client_ID( from ),
432                                         Req->argv[1] );
433                 }
434         }
435         
436         while( pattern )
437         {
438                 /* Loop through all the channels */
439                 chan = Channel_First( );
440                 while( chan )
441                 {
442                         /* Check search pattern */
443                         if( Match( pattern, Channel_Name( chan )))
444                         {
445                                 /* Gotcha! */
446                                 if( ! strchr( Channel_Modes( chan ), 's' ) ||
447                                     Channel_IsMemberOf( chan, from ))
448                                 {
449                                         if( ! IRC_WriteStrClient( from,
450                                             RPL_LIST_MSG, Client_ID( from ),
451                                             Channel_Name( chan ),
452                                             Channel_MemberCount( chan ),
453                                             Channel_Topic( chan )))
454                                                 return DISCONNECTED;
455                                 }
456                         }
457                         chan = Channel_Next( chan );
458                 }
459                 
460                 /* Get next name ... */
461                 if( Req->argc > 0 )
462                         pattern = strtok( NULL, "," );
463                 else
464                         pattern = NULL;
465         }
466         
467         return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
468 } /* IRC_LIST */
469
470
471 GLOBAL bool
472 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
473 {
474         char modes_add[COMMAND_LEN], l[16], *ptr;
475         CLIENT *from;
476         CHANNEL *chan;
477         int arg_topic;
478
479         assert( Client != NULL );
480         assert( Req != NULL );
481
482         /* Bad number of parameters? */
483         if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
484
485         /* Compatibility kludge */
486         if( Req->argc == 5 ) arg_topic = 4;
487         else if( Req->argc == 3 ) arg_topic = 2;
488         else arg_topic = -1;
489
490         /* Search origin */
491         from = Client_Search( Req->prefix );
492         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
493
494         /* Search or create channel */
495         chan = Channel_Search( Req->argv[0] );
496         if( ! chan ) chan = Channel_Create( Req->argv[0] );
497         if( ! chan ) return CONNECTED;
498
499         if( Req->argv[1][0] == '+' )
500         {
501                 ptr = Channel_Modes( chan );
502                 if( ! *ptr )
503                 {
504                         /* OK, this channel doesn't have modes jet, set the received ones: */
505                         Channel_SetModes( chan, &Req->argv[1][1] );
506
507                         if( Req->argc == 5 )
508                         {
509                                 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
510                                 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
511                         }
512                         else
513                         {
514                                 /* Delete modes which we never want to inherit */
515                                 Channel_ModeDel( chan, 'l' );
516                                 Channel_ModeDel( chan, 'k' );
517                         }
518
519                         strcpy( modes_add, "" );
520                         ptr = Channel_Modes( chan );
521                         while( *ptr )
522                         {
523                                 if( *ptr == 'l' )
524                                 {
525                                         snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
526                                         strlcat( modes_add, l, sizeof( modes_add ));
527                                 }
528                                 if( *ptr == 'k' )
529                                 {
530                                         strlcat( modes_add, " ", sizeof( modes_add ));
531                                         strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
532                                 }
533                                 ptr++;
534                         }
535                         
536                         /* Inform members of this channel */
537                         IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
538                 }
539         }
540         else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
541
542         if( arg_topic > 0 )
543         {
544                 /* We got a topic */
545                 ptr = Channel_Topic( chan );
546                 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
547                 {
548                         /* OK, there is no topic jet */
549                         Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
550                         IRC_WriteStrChannelPrefix(Client, chan, from, false,
551                              "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
552                 }
553         }
554
555         /* Forward CHANINFO to other serevrs */
556         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] );
557         else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
558         else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
559
560         return CONNECTED;
561 } /* IRC_CHANINFO */
562
563
564 /* -eof- */