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