926d345e9dbf5267302e6a59a0699afb33e01f21
[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.21 2002/12/16 23:06:46 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <string.h>
22
23 #include "defines.h"
24 #include "conn.h"
25 #include "client.h"
26 #include "channel.h"
27 #include "lists.h"
28 #include "log.h"
29 #include "match.h"
30 #include "messages.h"
31 #include "parse.h"
32 #include "irc-info.h"
33 #include "irc-write.h"
34 #include "resolve.h"
35 #include "conf.h"
36
37 #include "exp.h"
38 #include "irc-channel.h"
39
40
41 GLOBAL BOOLEAN
42 IRC_JOIN( CLIENT *Client, REQUEST *Req )
43 {
44         CHAR *channame, *key, *flags, *topic, modes[8];
45         BOOLEAN is_new_chan, is_invited, is_banned;
46         CLIENT *target;
47         CHANNEL *chan;
48
49         assert( Client != NULL );
50         assert( Req != NULL );
51
52         /* Bad number of arguments? */
53         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
54
55         /* Who is the sender? */
56         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
57         else target = Client;
58         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
59
60         /* Are channel keys given? */
61         if( Req->argc > 1 ) key = Req->argv[1];
62         else key = NULL;
63
64         /* Channel-Namen durchgehen */
65         chan = NULL;
66         channame = strtok( Req->argv[0], "," );
67         while( channame )
68         {
69                 chan = flags = NULL;
70
71                 /* wird der Channel neu angelegt? */
72                 if( Channel_Search( channame )) is_new_chan = FALSE;
73                 else is_new_chan = TRUE;
74
75                 /* Hat ein Server Channel-User-Modes uebergeben? */
76                 if( Client_Type( Client ) == CLIENT_SERVER )
77                 {
78                         /* Channel-Flags extrahieren */
79                         flags = strchr( channame, 0x7 );
80                         if( flags )
81                         {
82                                 *flags = '\0';
83                                 flags++;
84                         }
85                 }
86
87                 /* Lokaler Client? */
88                 if( Client_Type( Client ) == CLIENT_USER )
89                 {
90                         /* Test if the user has reached his maximum channel count */
91                         if( Client_Type( Client ) == CLIENT_USER )
92                         {
93                                 if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
94                                 {
95                                         IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG, Client_ID( Client ), channame );
96                                         return CONNECTED;
97                                 }
98                         }
99
100                         /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
101                         if( is_new_chan )
102                         {
103                                 /* Erster User im Channel: Operator-Flag setzen */
104                                 flags = "o";
105                         }
106                         else
107                         {
108                                 /* Existierenden Channel suchen */
109                                 chan = Channel_Search( channame );
110                                 assert( chan != NULL );
111
112                                 is_banned = Lists_CheckBanned( target, chan );
113                                 is_invited = Lists_CheckInvited( target, chan );
114
115                                 /* Testen, ob Client gebanned ist */
116                                 if(( is_banned == TRUE ) &&  ( is_invited == FALSE ))
117                                 {
118                                         /* Client ist gebanned (und nicht invited): */
119                                         IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
120
121                                         /* Try next name, if any */
122                                         channame = strtok( NULL, "," );
123                                         continue;
124                                 }
125
126                                 /* Ist der Channel "invite-only"? */
127                                 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == FALSE ))
128                                 {
129                                         /* Channel ist "invite-only" und Client wurde nicht invited: */
130                                         IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
131
132                                         /* Try next name, if any */
133                                         channame = strtok( NULL, "," );
134                                         continue;
135                                 }
136
137                                 /* Is the channel protected by a key? */
138                                 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
139                                 {
140                                         /* Bad channel key! */
141                                         IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
142
143                                         /* Try next name, if any */
144                                         channame = strtok( NULL, "," );
145                                         continue;
146                                 }
147
148                                 /* Are there already too many members? */
149                                 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
150                                 {
151                                         /* Bad channel key! */
152                                         IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
153
154                                         /* Try next name, if any */
155                                         channame = strtok( NULL, "," );
156                                         continue;
157                                 }
158                         }
159                 }
160
161                 /* Channel joinen (und ggf. anlegen) */
162                 if( ! Channel_Join( target, channame ))
163                 {
164                         /* naechsten Namen ermitteln */
165                         channame = strtok( NULL, "," );
166                         continue;
167                 }
168                 if( ! chan ) chan = Channel_Search( channame );
169                 assert( chan != NULL );
170
171                 /* Modes setzen (wenn vorhanden) */
172                 while( flags && *flags )
173                 {
174                         Channel_UserModeAdd( chan, target, *flags );
175                         flags++;
176                 }
177
178                 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
179                 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
180
181                 /* Muessen Modes an andere Server gemeldet werden? */
182                 strcpy( &modes[1], Channel_UserModes( chan, target ));
183                 if( modes[1] ) modes[0] = 0x7;
184                 else modes[0] = '\0';
185
186                 /* An andere Server weiterleiten */
187                 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
188
189                 /* im Channel bekannt machen */
190                 IRC_WriteStrChannelPrefix( Client, chan, target, FALSE, "JOIN :%s", channame );
191                 if( modes[1] )
192                 {
193                         /* Modes im Channel bekannt machen */
194                         IRC_WriteStrChannelPrefix( Client, chan, target, FALSE, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
195                 }
196
197                 if( Client_Type( Client ) == CLIENT_USER )
198                 {
199                         /* an Client bestaetigen */
200                         IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
201
202                         /* Topic an Client schicken */
203                         topic = Channel_Topic( chan );
204                         if( *topic ) IRC_WriteStrClient( Client, RPL_TOPIC_MSG, Client_ID( Client ), channame, topic );
205
206                         /* Mitglieder an Client Melden */
207                         IRC_Send_NAMES( Client, chan );
208                         IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
209                 }
210
211                 /* naechsten Namen ermitteln */
212                 channame = strtok( NULL, "," );
213         }
214         return CONNECTED;
215 } /* IRC_JOIN */
216
217
218 GLOBAL BOOLEAN
219 IRC_PART( CLIENT *Client, REQUEST *Req )
220 {
221         CLIENT *target;
222         CHAR *chan;
223
224         assert( Client != NULL );
225         assert( Req != NULL );
226
227         /* Falsche Anzahl Parameter? */
228         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
229
230         /* Wer ist der Absender? */
231         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
232         else target = Client;
233         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
234
235         /* Channel-Namen durchgehen */
236         chan = strtok( Req->argv[0], "," );
237         while( chan )
238         {
239                 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
240                 {
241                         /* naechsten Namen ermitteln */
242                         chan = strtok( NULL, "," );
243                         continue;
244                 }
245
246                 /* naechsten Namen ermitteln */
247                 chan = strtok( NULL, "," );
248         }
249         return CONNECTED;
250 } /* IRC_PART */
251
252
253 GLOBAL BOOLEAN
254 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
255 {
256         CHANNEL *chan;
257         CLIENT *from;
258         CHAR *topic;
259
260         assert( Client != NULL );
261         assert( Req != NULL );
262
263         /* Falsche Anzahl Parameter? */
264         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
265
266         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
267         else from = Client;
268         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
269
270         /* Welcher Channel? */
271         chan = Channel_Search( Req->argv[0] );
272         if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
273
274         /* Ist der User Mitglied in dem Channel? */
275         if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
276
277         if( Req->argc == 1 )
278         {
279                 /* Topic erfragen */
280                 topic = Channel_Topic( chan );
281                 if( *topic ) return IRC_WriteStrClient( from, RPL_TOPIC_MSG, Client_ID( from ), Channel_Name( chan ), topic );
282                 else return IRC_WriteStrClient( from, RPL_NOTOPIC_MSG, Client_ID( from ), Channel_Name( chan ));
283         }
284
285         if( strchr( Channel_Modes( chan ), 't' ))
286         {
287                 /* Topic Lock. Ist der User ein Channel Operator? */
288                 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
289         }
290
291         /* Topic setzen */
292         Channel_SetTopic( chan, Req->argv[1] );
293         Log( LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s", Client_Mask( from ), Channel_Name( chan ), Req->argv[1][0] ? Req->argv[1] : "<none>" );
294
295         /* im Channel bekannt machen und an Server weiterleiten */
296         IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
297         IRC_WriteStrChannelPrefix( Client, chan, from, FALSE, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
298
299         if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
300         else return CONNECTED;
301 } /* IRC_TOPIC */
302
303
304 GLOBAL BOOLEAN
305 IRC_LIST( CLIENT *Client, REQUEST *Req )
306 {
307         CHAR *pattern;
308         CHANNEL *chan;
309         CLIENT *from, *target;
310
311         assert( Client != NULL );
312         assert( Req != NULL );
313
314         /* Falsche Anzahl Parameter? */
315         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
316
317         if( Req->argc > 0 ) pattern = strtok( Req->argv[0], "," );
318         else pattern = "*";
319
320         /* From aus Prefix ermitteln */
321         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
322         else from = Client;
323         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
324
325         if( Req->argc == 2 )
326         {
327                 /* an anderen Server forwarden */
328                 target = Client_Search( Req->argv[1] );
329                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
330
331                 if( target != Client_ThisServer( ))
332                 {
333                         /* Ok, anderer Server ist das Ziel: forwarden */
334                         return IRC_WriteStrClientPrefix( target, from, "LIST %s :%s", from, Req->argv[1] );
335                 }
336         }
337         
338         while( pattern )
339         {
340                 /* alle Channel durchgehen */
341                 chan = Channel_First( );
342                 while( chan )
343                 {
344                         /* Passt die Suchmaske auf diesen Channel? */
345                         if( Match( pattern, Channel_Name( chan )))
346                         {
347                                 /* Treffer! */
348                                 if( ! IRC_WriteStrClient( from, RPL_LIST_MSG, from, Channel_Name( chan ), Channel_MemberCount( chan ), Channel_Topic( chan ))) return DISCONNECTED;
349                         }
350                         chan = Channel_Next( chan );
351                 }
352                 
353                 /* naechsten Namen ermitteln */
354                 if( Req->argc > 0 ) pattern = strtok( NULL, "," );
355                 else pattern = NULL;
356         }
357         
358         return IRC_WriteStrClient( from, RPL_LISTEND_MSG, from );
359 } /* IRC_LIST */
360
361
362 GLOBAL BOOLEAN
363 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
364 {
365         CLIENT *from;
366         CHANNEL *chan;
367         CHAR *ptr;
368
369         assert( Client != NULL );
370         assert( Req != NULL );
371
372         /* Falsche Anzahl Parameter? */
373         if(( Req->argc < 1 ) || ( Req->argc > 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
374
375         /* From-Server suchen */
376         from = Client_Search( Req->prefix );
377         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
378
379         /* Channel suchen bzw. erzeugen */
380         chan = Channel_Search( Req->argv[0] );
381         if( ! chan ) chan = Channel_Create( Req->argv[0] );
382         if( ! chan ) return CONNECTED;
383
384         if( Req->argv[1][0] == '+' )
385         {
386                 ptr = Channel_Modes( chan );
387                 if( ! *ptr )
388                 {
389                         /* OK, es sind noch keine Modes gesetzt */
390                         Channel_SetModes( chan, &Req->argv[1][1] );
391                         IRC_WriteStrChannelPrefix( Client, chan, from, FALSE, "MODE %s +%s", Req->argv[0], &Req->argv[1][1] );
392                 }
393         }
394         else Log( LOG_WARNING, "CHANNELINFO: invalid MODE format ignored!" );
395
396         if( Req->argc == 3 )
397         {
398                 /* Es wurde auch ein Topic mit uebermittelt */
399                 ptr = Channel_Topic( chan );
400                 if( ! *ptr )
401                 {
402                         /* OK, es ist bisher kein Topic gesetzt */
403                         Channel_SetTopic( chan, Req->argv[2] );
404                         IRC_WriteStrChannelPrefix( Client, chan, from, FALSE, "TOPIC %s :%s", Req->argv[0], Req->argv[2] );
405                 }
406         }
407
408         /* an andere Server forwarden */
409         IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
410         return CONNECTED;
411 } /* IRC_CHANINFO */
412
413
414 /* -eof- */