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