- removed Conf_MaxPChannels and reverted to old behavior.
[ngircd-alex.git] / src / ngircd / irc-mode.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 commands for mode changes (MODE, AWAY, ...)
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc-mode.c,v 1.19 2002/12/14 13:36:19 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <string.h>
22
23 #include "conn.h"
24 #include "client.h"
25 #include "channel.h"
26 #include "defines.h"
27 #include "irc-write.h"
28 #include "lists.h"
29 #include "log.h"
30 #include "parse.h"
31 #include "messages.h"
32 #include "resolve.h"
33 #include "conf.h"
34
35 #include "exp.h"
36 #include "irc-mode.h"
37
38
39 LOCAL BOOLEAN Add_Invite PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern ));
40 LOCAL BOOLEAN Add_Ban PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern ));
41
42 LOCAL BOOLEAN Del_Invite PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern ));
43 LOCAL BOOLEAN Del_Ban PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern ));
44
45 LOCAL BOOLEAN Send_ListChange PARAMS(( CHAR *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Mask ));
46
47
48 GLOBAL BOOLEAN
49 IRC_MODE( CLIENT *Client, REQUEST *Req )
50 {
51         CHAR *mode_ptr, the_modes[CLIENT_MODE_LEN], x[2];
52         CLIENT *cl, *chan_cl, *prefix;
53         BOOLEAN set, ok, modeok;
54         CHANNEL *chan;
55         
56         assert( Client != NULL );
57         assert( Req != NULL );
58
59         cl = chan_cl = prefix = NULL;
60         chan = NULL;
61
62         /* Keine Parameter? */
63         if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
64
65         /* Ziel suchen: Client bzw. Channel */
66         if( Client_IsValidNick( Req->argv[0] )) cl = Client_Search( Req->argv[0] );
67         if( Channel_IsValidName( Req->argv[0] )) chan = Channel_Search( Req->argv[0] );
68
69         /* Kein Ziel gefunden? */
70         if(( ! cl ) && ( ! chan )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
71
72         assert(( cl && chan ) != TRUE );
73
74         /* Falsche Anzahl Parameter? */
75         if(( cl ) && ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
76         if(( chan ) && ( Req->argc > 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
77
78         /* Prefix fuer Antworten etc. ermitteln */
79         if( Client_Type( Client ) == CLIENT_SERVER )
80         {
81                 prefix = Client_Search( Req->prefix );
82                 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
83         }
84         else prefix = Client;
85         
86         if(( chan ) && (( Req->argc == 2 ) || ( Req->argc == 3 )))
87         {
88                 /* pruefen, ob "Listen-Operation": Invite, Ban */
89                 if(( Req->argv[1][0] == '-'  ) || ( Req->argv[1][0] == '+' )) mode_ptr = &Req->argv[1][1];
90                 else mode_ptr = &Req->argv[1][0];
91
92                 if( Req->argc == 2 )
93                 {
94                         /* Liste anzeigen */
95                         if( *mode_ptr == 'I' ) return Lists_ShowInvites( prefix, chan );
96                         if( *mode_ptr == 'b' ) return Lists_ShowBans( prefix, chan );
97                 }
98                 else
99                 {
100                         /* Listen veraendern */
101
102                         if( Client_Type( Client ) == CLIENT_USER )
103                         {
104                                 /* Ist der User Channel-Operator? */
105                                 modeok = FALSE;
106                                 if( strchr( Channel_UserModes( chan, Client ), 'o' )) modeok = TRUE;
107                                 if( Conf_OperCanMode )
108                                 {
109                                         /* auch IRC-Operatoren duerfen MODE verwenden */
110                                         if( Client_OperByMe( Client )) modeok = TRUE;
111                                 }
112
113                                 if( ! modeok )
114                                 {
115                                         Log( LOG_DEBUG, "Can't change modes: \"%s\" is not operator on %s!", Client_ID( Client ), Channel_Name( chan ));
116                                         return IRC_WriteStrClient( Client, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Client ), Channel_Name( chan ));
117                                 }
118                         }
119                         
120                         if( Req->argv[1][0] == '+' )
121                         {
122                                 /* Listen-Eintrag hinzufuegen */
123                                 if( *mode_ptr == 'I' ) return Add_Invite( prefix, Client, chan, Req->argv[2] );
124                                 if( *mode_ptr == 'b' ) return Add_Ban( prefix, Client, chan, Req->argv[2] );
125                         }
126                         else if( Req->argv[1][0] == '-' )
127                         {
128                                 /* Listen-Eintrag loeschen */
129                                 if( *mode_ptr == 'I' ) return Del_Invite( prefix, Client, chan, Req->argv[2] );
130                                 if( *mode_ptr == 'b' ) return Del_Ban( prefix, Client, chan, Req->argv[2] );
131                         }
132                 }
133         }
134
135         /* Client ermitteln, wenn bei Channel-Modes mit 3 Parametern */
136         if(( chan ) && (Req->argc == 3 ))
137         {
138                 chan_cl = Client_Search( Req->argv[2] );
139                 if( ! chan_cl ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
140         }
141         
142         /* Wenn Anfragender ein User ist: Zugriff erlaubt? */
143         if( Client_Type( Client ) == CLIENT_USER )
144         {
145                 if( cl )
146                 {
147                         /* MODE ist nur fuer sich selber zulaessig! */
148                         if( cl != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
149                 }
150                 if( chan )
151                 {
152                         /* Darf der User die Channel-Modes ermitteln? */
153                 }
154         }
155
156         /* Werden die Modes "nur" erfragt? */
157         if(( cl ) && ( Req->argc == 1 )) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( cl ));
158         if(( chan ) && ( Req->argc == 1 )) return IRC_WriteStrClient( Client, RPL_CHANNELMODEIS_MSG, Client_ID( Client ), Channel_Name( chan ), Channel_Modes( chan ));
159
160         mode_ptr = Req->argv[1];
161
162         /* Sollen Modes gesetzt oder geloescht werden? */
163         if( cl )
164         {
165                 if( *mode_ptr == '+' ) set = TRUE;
166                 else if( *mode_ptr == '-' ) set = FALSE;
167                 else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
168                 mode_ptr++;
169         }
170         else
171         {
172                 if( *mode_ptr == '-' ) set = FALSE;
173                 else set = TRUE;
174                 if(( *mode_ptr == '-' ) || ( *mode_ptr == '+' )) mode_ptr++;
175         }
176
177         /* Reply-String mit Aenderungen vorbereiten */
178         if( set ) strcpy( the_modes, "+" );
179         else strcpy( the_modes, "-" );
180
181         ok = TRUE;
182         x[1] = '\0';
183         while( *mode_ptr )
184         {
185                 x[0] = '\0';
186                 if( Client_Type( Client ) == CLIENT_SERVER )
187                 {
188                         /* Befehl kommt von einem Server, daher
189                          * trauen wir ihm "unbesehen" ... */
190                         x[0] = *mode_ptr;
191                 }
192                 else
193                 {
194                         /* Modes validieren */
195                         if( cl )
196                         {
197                                 /* User-Modes */
198                                 switch( *mode_ptr )
199                                 {
200                                         case 'i':
201                                                 /* invisible */
202                                                 x[0] = 'i';
203                                                 break;
204                                         case 'o':
205                                                 /* operator (kann nur geloescht werden) */
206                                                 if( ! set )
207                                                 {
208                                                         Client_SetOperByMe( Client, FALSE );
209                                                         x[0] = 'o';
210                                                 }
211                                                 else ok = IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
212                                                 break;
213                                         case 'r':
214                                                 /* restricted (kann nur gesetzt werden) */
215                                                 if( set ) x[0] = 'r';
216                                                 else ok = IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
217                                                 break;
218                                         case 's':
219                                                 /* server messages */
220                                                 x[0] = 's';
221                                                 break;
222                                         default:
223                                                 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Client ));
224                                                 ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Client ), set ? '+' : '-', *mode_ptr );
225                                                 x[0] = '\0';
226                                 }
227                         }
228                         if( chan )
229                         {
230                                 /* Ist der User ein Channel Operator? */
231                                 modeok = FALSE;
232                                 if( strchr( Channel_UserModes( chan, Client ), 'o' )) modeok = TRUE;
233                                 if( Conf_OperCanMode )
234                                 {
235                                         /* auch IRC-Operatoren duerfen MODE verwenden */
236                                         if( Client_OperByMe( Client )) modeok = TRUE;
237                                 }
238
239                                 if( ! modeok )
240                                 {
241                                         Log( LOG_DEBUG, "Can't change modes: \"%s\" is not operator on %s!", Client_ID( Client ), Channel_Name( chan ));
242                                         ok = IRC_WriteStrClient( Client, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Client ), Channel_Name( chan ));
243                                         break;
244                                 }
245                                 
246                                 /* Channel-Modes oder Channel-User-Modes */
247                                 if( chan_cl )
248                                 {
249                                         /* Channel-User-Modes */
250                                         switch( *mode_ptr )
251                                         {
252                                                 case 'o':
253                                                         /* Channel Operator */
254                                                         x[0] = 'o';
255                                                         break;
256                                                 case 'v':
257                                                         /* Voice */
258                                                         x[0] = 'v';
259                                                         break;
260                                                 default:
261                                                         Log( LOG_DEBUG, "Unknown channel-user-mode \"%c%c\" from \"%s\" on \"%s\" at %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Client ), Client_ID( chan_cl ), Channel_Name( chan ));
262                                                         ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Client ), set ? '+' : '-', *mode_ptr );
263                                                         x[0] = '\0';
264                                         }
265                                 }
266                                 else
267                                 {
268                                         /* Channel-Modes */
269                                         switch( *mode_ptr )
270                                         {
271                                                 case 'i':
272                                                         /* Invite-Only */
273                                                         x[0] = 'i';
274                                                         break;
275                                                 case 'm':
276                                                         /* Moderated */
277                                                         x[0] = 'm';
278                                                         break;
279                                                 case 'n':
280                                                         /* kein Schreiben in den Channel von aussen */
281                                                         x[0] = 'n';
282                                                         break;
283                                                 case 't':
284                                                         /* Topic Lock */
285                                                         x[0] = 't';
286                                                         break;
287                                                 case 'P':
288                                                         /* Persistent channel */
289                                                         if( set && ( ! Client_OperByMe( Client )))
290                                                         {
291                                                                 /* there are too many persistent channels in the network! */
292                                                                 ok = IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
293                                                         }
294                                                         else x[0] = 'P';
295                                                         break;
296                                                 default:
297                                                         Log( LOG_DEBUG, "Unknown channel-mode \"%c%c\" from \"%s\" at %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Client ), Channel_Name( chan ));
298                                                         ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Client ), set ? '+' : '-', *mode_ptr );
299                                                         x[0] = '\0';
300                                         }
301                                 }
302                         }
303                 }
304                 if( ! ok ) break;
305                 
306                 mode_ptr++;
307                 if( ! x[0] ) continue;
308
309                 /* Okay, gueltigen Mode gefunden */
310                 if( cl )
311                 {
312                         /* Es geht um User-Modes */
313                         if( set )
314                         {
315                                 /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
316                                 if( Client_ModeAdd( cl, x[0] )) strcat( the_modes, x );
317                                 
318                         }
319                         else
320                         {
321                                 /* Modes geloescht. Wenn der Client ihn hatte: merken */
322                                 if( Client_ModeDel( cl, x[0] )) strcat( the_modes, x );
323                         }
324
325                         /* "nachbearbeiten" */
326                         if( x[0] == 'a' )
327                         {
328                                 /* away */
329                                 if( set ) Client_SetAway( cl, DEFAULT_AWAY_MSG );
330                                 else Client_SetAway( cl, NULL );
331                         }
332                 }
333                 if( chan )
334                 {
335                         /* Es geht um Channel-Modes oder Channel-User-Modes */
336                         if( chan_cl )
337                         {
338                                 /* Channel-User-Modes */
339                                 if( set )
340                                 {
341                                         /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
342                                         if( Channel_UserModeAdd( chan, chan_cl, x[0] )) strcat( the_modes, x );
343                                 }
344                                 else
345                                 {
346                                         /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
347                                         if( Channel_UserModeDel( chan, chan_cl, x[0] )) strcat( the_modes, x );
348                                 }
349                         }
350                         else
351                         {
352                                 /* Channel-Mode */
353                                 if( set )
354                                 {
355                                         /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
356                                         if( Channel_ModeAdd( chan, x[0] )) strcat( the_modes, x );
357                                 }
358                                 else
359                                 {
360                                         /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
361                                         if( Channel_ModeDel( chan, x[0] )) strcat( the_modes, x );
362                                 }
363                         }
364                 }
365         }
366
367         /* Wurden Modes geaendert? */
368         if( the_modes[1] )
369         {
370                 if( cl )
371                 {
372                         /* Client-Mode */
373                         if( Client_Type( Client ) == CLIENT_SERVER )
374                         {
375                                 /* Modes an andere Server forwarden */
376                                 IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Client_ID( cl ), the_modes );
377                         }
378                         else
379                         {
380                                 /* Bestaetigung an Client schicken & andere Server informieren */
381                                 ok = IRC_WriteStrClientPrefix( Client, prefix, "MODE %s %s", Client_ID( cl ), the_modes );
382                                 IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Client_ID( cl ), the_modes );
383                         }
384                         Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( cl ), Client_Modes( cl ));
385                 }
386                 if( chan )
387                 {
388                         /* Channel-Modes oder Channel-User-Mode */
389                         if( chan_cl )
390                         {
391                                 /* Channel-User-Mode */
392                                 if( Client_Type( Client ) == CLIENT_SERVER )
393                                 {
394                                         /* Modes an andere Server und Channel-User forwarden */
395                                         IRC_WriteStrServersPrefix( Client, prefix, "MODE %s %s :%s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
396                                         IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s %s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
397                                 }
398                                 else
399                                 {
400                                         /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
401                                         ok = IRC_WriteStrClientPrefix( Client, prefix, "MODE %s %s %s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
402                                         IRC_WriteStrServersPrefix( Client, prefix, "MODE %s %s :%s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
403                                         IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s %s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
404                                 }
405                                 Log( LOG_DEBUG, "User \"%s\" on %s: Mode change, now \"%s\".", Client_Mask( chan_cl), Channel_Name( chan ), Channel_UserModes( chan, chan_cl ));
406                         }
407                         else
408                         {
409                                 /* Channel-Mode */
410                                 if( Client_Type( Client ) == CLIENT_SERVER )
411                                 {
412                                         /* Modes an andere Server und Channel-User forwarden */
413                                         IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Channel_Name( chan ), the_modes );
414                                         IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s", Channel_Name( chan ), the_modes );
415                                 }
416                                 else
417                                 {
418                                         /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
419                                         ok = IRC_WriteStrClientPrefix( Client, prefix, "MODE %s %s", Channel_Name( chan ), the_modes );
420                                         IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Channel_Name( chan ), the_modes );
421                                         IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s", Channel_Name( chan ), the_modes );
422                                 }
423                                 Log( LOG_DEBUG, "Channel \"%s\": Mode change, now \"%s\".", Channel_Name( chan ), Channel_Modes( chan ));
424                         }
425                 }
426         }
427
428         return ok;
429 } /* IRC_MODE */
430
431
432 GLOBAL BOOLEAN
433 IRC_AWAY( CLIENT *Client, REQUEST *Req )
434 {
435         assert( Client != NULL );
436         assert( Req != NULL );
437
438         /* Falsche Anzahl Parameter? */
439         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
440
441         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
442         {
443                 /* AWAY setzen */
444                 Client_SetAway( Client, Req->argv[0] );
445                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
446                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
447         }
448         else
449         {
450                 /* AWAY loeschen */
451                 Client_SetAway( Client, NULL );
452                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
453                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
454         }
455 } /* IRC_AWAY */
456
457
458 LOCAL BOOLEAN
459 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
460 {
461         CHAR *mask;
462
463         assert( Client != NULL );
464         assert( Channel != NULL );
465         assert( Pattern != NULL );
466
467         mask = Lists_MakeMask( Pattern );
468
469         if( ! Lists_AddInvited( Prefix, mask, Channel, FALSE )) return CONNECTED;
470         return Send_ListChange( "+I", Prefix, Client, Channel, mask );
471 } /* Add_Invite */
472
473
474 LOCAL BOOLEAN
475 Add_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
476 {
477         CHAR *mask;
478
479         assert( Client != NULL );
480         assert( Channel != NULL );
481         assert( Pattern != NULL );
482
483         mask = Lists_MakeMask( Pattern );
484
485         if( ! Lists_AddBanned( Prefix, mask, Channel )) return CONNECTED;
486         return Send_ListChange( "+b", Prefix, Client, Channel, mask );
487 } /* Add_Ban */
488
489
490 LOCAL BOOLEAN
491 Del_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
492 {
493         CHAR *mask;
494
495         assert( Client != NULL );
496         assert( Channel != NULL );
497         assert( Pattern != NULL );
498
499         mask = Lists_MakeMask( Pattern );
500         Lists_DelInvited( mask, Channel );
501         return Send_ListChange( "-I", Prefix, Client, Channel, mask );
502 } /* Del_Invite */
503
504
505 LOCAL BOOLEAN
506 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
507 {
508         CHAR *mask;
509
510         assert( Client != NULL );
511         assert( Channel != NULL );
512         assert( Pattern != NULL );
513
514         mask = Lists_MakeMask( Pattern );
515         Lists_DelBanned( mask, Channel );
516         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
517 } /* Del_Ban */
518
519
520 LOCAL BOOLEAN
521 Send_ListChange( CHAR *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Mask )
522 {
523         /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
524
525         BOOLEAN ok;
526
527         if( Client_Type( Client ) == CLIENT_USER )
528         {
529                 /* Bestaetigung an Client */
530                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
531         }
532         else ok = TRUE;
533
534         /* an andere Server */
535         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
536
537         /* und lokale User im Channel */
538         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, FALSE, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
539         
540         return ok;
541 } /* Send_ListChange */
542
543
544 /* -eof- */