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