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