]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/mode.cpp
Aaaand, correct all the obligitary typos
[user/henk/code/inspircd.git] / src / mode.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "configreader.h"
22 #include <unistd.h>
23 #include <sys/errno.h>
24 #include <time.h>
25 #include <string>
26 #ifdef GCC3
27 #include <ext/hash_map>
28 #else
29 #include <hash_map>
30 #endif
31 #include <map>
32 #include <sstream>
33 #include <vector>
34 #include <deque>
35 #include "connection.h"
36 #include "users.h"
37 #include "ctables.h"
38 #include "globals.h"
39 #include "modules.h"
40 #include "dynamic.h"
41 #include "wildcard.h"
42 #include "message.h"
43 #include "commands.h"
44 #include "xline.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
47 #include "mode.h"
48
49 #include "modes/cmode_s.h"
50 #include "modes/cmode_p.h"
51
52 extern int MODCOUNT;
53 extern std::vector<Module*> modules;
54 extern std::vector<ircd_module*> factory;
55 extern InspIRCd* ServerInstance;
56 extern ServerConfig* Config;
57
58 extern time_t TIME;
59
60 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly) : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
61 {
62 }
63
64 ModeHandler::~ModeHandler()
65 {
66 }
67
68 bool ModeHandler::IsListMode()
69 {
70         return list;
71 }
72
73 ModeType ModeHandler::GetModeType()
74 {
75         return m_type;
76 }
77
78 bool ModeHandler::NeedsOper()
79 {
80         return oper;
81 }
82
83 int ModeHandler::GetNumParams(bool adding)
84 {
85         return adding ? n_params_on : n_params_off;
86 }
87
88 char ModeHandler::GetModeChar()
89 {
90         return mode;
91 }
92
93 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
94 {
95         return MODEACTION_DENY;
96 }
97
98 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
99 {
100 }
101
102 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
103 {
104         return (ours < theirs);
105 }
106
107 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
108 {
109 }
110
111 ModeWatcher::~ModeWatcher()
112 {
113 }
114
115 char ModeWatcher::GetModeChar()
116 {
117         return mode;
118 }
119
120 ModeType ModeWatcher::GetModeType()
121 {
122         return m_type;
123 }
124
125 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding, ModeType type)
126 {
127         return true;
128 }
129
130 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter, bool adding, ModeType type)
131 {
132 }
133
134 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
135 {
136         userrec *d;
137         if ((!user) || (!dest) || (!chan) || (!*dest))
138         {
139                 return NULL;
140         }
141         d = Find(dest);
142         if (!d)
143         {
144                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
145                 return NULL;
146         }
147         return d;
148 }
149
150 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
151 {
152         if (!chan)
153                 return NULL;
154
155         for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
156         {
157                 if (((ucrec*)(*i))->channel == chan)
158                 {
159                         if (((ucrec*)(*i))->uc_modes & MASK)
160                         {
161                                 return NULL;
162                         }
163                         ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
164                         switch (MASK)
165                         {
166                                 case UCMODE_OP:
167                                         ((ucrec*)(*i))->channel->AddOppedUser(d);
168                                 break;
169                                 case UCMODE_HOP:
170                                         ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
171                                 break;
172                                 case UCMODE_VOICE:
173                                         ((ucrec*)(*i))->channel->AddVoicedUser(d);
174                                 break;
175                         }
176                         log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
177                         return d->nick;
178                 }
179         }
180         return NULL;
181 }
182
183 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
184 {
185         if (!chan)
186                 return NULL;
187
188         for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
189         {
190                 if (((ucrec*)(*i))->channel == chan)
191                 {
192                         if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
193                         {
194                                 return NULL;
195                         }
196                         ((ucrec*)(*i))->uc_modes ^= MASK;
197                         switch (MASK)
198                         {
199                                 case UCMODE_OP:
200                                         ((ucrec*)(*i))->channel->DelOppedUser(d);
201                                 break;
202                                 case UCMODE_HOP:
203                                         ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
204                                 break;
205                                 case UCMODE_VOICE:
206                                         ((ucrec*)(*i))->channel->DelVoicedUser(d);
207                                 break;
208                         }
209                         log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
210                         return d->nick;
211                 }
212         }
213         return NULL;
214 }
215
216 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
217 {
218         userrec *d = this->SanityChecks(user,dest,chan,status);
219         
220         if (d)
221         {
222                 if (IS_LOCAL(user))
223                 {
224                         int MOD_RESULT = 0;
225                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
226                         
227                         if (MOD_RESULT == ACR_DENY)
228                                 return NULL;
229                         if (MOD_RESULT == ACR_DEFAULT)
230                         {
231                                 if ((status < STATUS_OP) && (!is_uline(user->server)))
232                                 {
233                                         WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
234                                         return NULL;
235                                 }
236                         }
237                 }
238
239                 return this->Grant(d,chan,UCMODE_OP);
240         }
241         return NULL;
242 }
243
244 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
245 {
246         userrec *d = this->SanityChecks(user,dest,chan,status);
247         
248         if (d)
249         {
250                 if (IS_LOCAL(user))
251                 {
252                         int MOD_RESULT = 0;
253                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
254                 
255                         if (MOD_RESULT == ACR_DENY)
256                                 return NULL;
257                         if (MOD_RESULT == ACR_DEFAULT)
258                         {
259                                 if ((status < STATUS_OP) && (!is_uline(user->server)))
260                                 {
261                                         WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
262                                         return NULL;
263                                 }
264                         }
265                 }
266
267                 return this->Grant(d,chan,UCMODE_HOP);
268         }
269         return NULL;
270 }
271
272 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
273 {
274         userrec *d = this->SanityChecks(user,dest,chan,status);
275         
276         if (d)
277         {
278                 if (IS_LOCAL(user))
279                 {
280                         int MOD_RESULT = 0;
281                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
282                         
283                         if (MOD_RESULT == ACR_DENY)
284                                 return NULL;
285                         if (MOD_RESULT == ACR_DEFAULT)
286                         {
287                                 if ((status < STATUS_HOP) && (!is_uline(user->server)))
288                                 {
289                                         WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
290                                         return NULL;
291                                 }
292                         }
293                 }
294
295                 return this->Grant(d,chan,UCMODE_VOICE);
296         }
297         return NULL;
298 }
299
300 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
301 {
302         userrec *d = this->SanityChecks(user,dest,chan,status);
303         
304         if (d)
305         {
306                 if (IS_LOCAL(user))
307                 {
308                         int MOD_RESULT = 0;
309                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
310                         
311                         if (MOD_RESULT == ACR_DENY)
312                                 return NULL;
313                         if (MOD_RESULT == ACR_DEFAULT)
314                         {
315                                 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
316                                 {
317                                         WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
318                                         return NULL;
319                                 }
320                         }
321                 }
322
323                 return this->Revoke(d,chan,UCMODE_OP);
324         }
325         return NULL;
326 }
327
328 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
329 {
330         userrec *d = this->SanityChecks(user,dest,chan,status);
331         
332         if (d)
333         {
334                 if (IS_LOCAL(user))
335                 {
336                         int MOD_RESULT = 0;
337                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
338                         
339                         if (MOD_RESULT == ACR_DENY)
340                                 return NULL;
341                         if (MOD_RESULT == ACR_DEFAULT)
342                         {
343                                 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
344                                 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
345                                 {
346                                         WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
347                                         return NULL;
348                                 }
349                         }
350                 }
351
352                 return this->Revoke(d,chan,UCMODE_HOP);
353         }
354         return NULL;
355 }
356
357 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
358 {
359         userrec *d = this->SanityChecks(user,dest,chan,status);
360
361         if (d)  
362         {
363                 if (IS_LOCAL(user))
364                 {
365                         int MOD_RESULT = 0;
366                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
367                         
368                         if (MOD_RESULT == ACR_DENY)
369                                 return NULL;
370                         if (MOD_RESULT == ACR_DEFAULT)
371                         {
372                                 if ((status < STATUS_HOP) && (!is_uline(user->server)))
373                                 {
374                                         WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
375                                         return NULL;
376                                 }
377                         }
378                 }
379
380                 return this->Revoke(d,chan,UCMODE_VOICE);
381         }
382         return NULL;
383 }
384
385 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
386 {
387         BanItem b;
388         int toomanyexclamation = 0;
389         int toomanyat = 0;
390
391         if ((!user) || (!dest) || (!chan) || (!*dest))
392         {
393                 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
394                 return NULL;
395         }
396
397         for (char* i = dest; *i; i++)
398         {
399                 if ((*i < 32) || (*i > 126))
400                 {
401                         return NULL;
402                 }
403                 else if (*i == '!')
404                 {
405                         toomanyexclamation++;
406                 }
407                 else if (*i == '@')
408                 {
409                         toomanyat++;
410                 }
411         }
412
413         if (toomanyexclamation != 1 || toomanyat != 1)
414                 /*
415                  * this stops sillyness like n!u!u!u@h, though note that most
416                  * ircds don't actually verify banmask validity. --w00t
417                  */
418                 return NULL;
419
420         long maxbans = GetMaxBans(chan->name);
421         if ((unsigned)chan->bans.size() > (unsigned)maxbans)
422         {
423                 WriteServ(user->fd,"478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
424                 return NULL;
425         }
426
427         log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
428
429         int MOD_RESULT = 0;
430         FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
431         if (MOD_RESULT)
432                 return NULL;
433
434         TidyBan(dest);
435         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
436         {
437                 if (!strcasecmp(i->data,dest))
438                 {
439                         // dont allow a user to set the same ban twice
440                         return NULL;
441                 }
442         }
443
444         b.set_time = TIME;
445         strlcpy(b.data,dest,MAXBUF);
446         if (*user->nick)
447         {
448                 strlcpy(b.set_by,user->nick,NICKMAX-1);
449         }
450         else
451         {
452                 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
453         }
454         chan->bans.push_back(b);
455         return dest;
456 }
457
458 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
459 {
460         if ((!user) || (!dest) || (!chan) || (!*dest)) {
461                 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
462                 return 0;
463         }
464
465         log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
466         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
467         {
468                 if (!strcasecmp(i->data,dest))
469                 {
470                         int MOD_RESULT = 0;
471                         FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
472                         if (MOD_RESULT)
473                                 return NULL;
474                         chan->bans.erase(i);
475                         return dest;
476                 }
477         }
478         return NULL;
479 }
480
481 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
482 {
483         std::string target = parameters[0];
484         ModeType type = MODETYPE_USER;
485         unsigned char mask = 0;
486         chanrec* targetchannel = FindChan(parameters[0]);
487         userrec* targetuser  = Find(parameters[0]);
488
489         log(DEBUG,"ModeParser::Process start");
490
491         if (pcnt > 1)
492         {
493                 if (targetchannel)
494                 {
495                         log(DEBUG,"Target type is CHANNEL");
496                         type = MODETYPE_CHANNEL;
497                         mask = MASK_CHANNEL;
498                 }
499                 else if (targetuser)
500                 {
501                         log(DEBUG,"Target type is USER");
502                         type = MODETYPE_USER;
503                         mask = MASK_USER;
504                 }
505                 else
506                 {
507                         /* No such nick/channel */
508                         log(DEBUG,"Target type is UNKNOWN, bailing");
509                         return;
510                 }
511                 std::string mode_sequence = parameters[1];
512                 std::string parameter = "";
513                 std::ostringstream parameter_list;
514                 std::string output_sequence = "";
515                 bool adding = true, state_change = false;
516                 int handler_id = 0;
517                 int parameter_counter = 2; /* Index of first parameter */
518
519                 for (std::string::const_iterator modeletter = mode_sequence.begin(); modeletter != mode_sequence.end(); modeletter++)
520                 {
521                         switch (*modeletter)
522                         {
523
524                                 log(DEBUG,"Iterate mode letter %c",*modeletter);
525
526                                 /* NB:
527                                  * For + and - mode characters, we don't just stick the character into the output sequence.
528                                  * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
529                                  * appearing in the output sequence, we store a flag which says there was a state change,
530                                  * which is set on any + or -, however, the + or - that we finish on is only appended to
531                                  * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
532                                  */
533                                 case '+':
534                                         /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
535                                          * however, will allow the + if it is the first item in the sequence, regardless.
536                                          */
537                                         if ((!adding) || (!output_sequence.length()))
538                                                 state_change = true;
539                                         adding = true;
540                                         continue;
541                                 break;
542                                 case '-':
543                                         if ((adding) || (!output_sequence.length()))
544                                                 state_change = true;
545                                         adding = false;
546                                         continue;
547                                 break;
548                                 default:
549
550                                         /**
551                                          * Watch carefully for the sleight of hand trick.
552                                          * 65 is the ascii value of 'A'. We take this from
553                                          * the char we're looking at to get a number between
554                                          * 1 and 127. We then logic-or it to get the hashed
555                                          * position, dependent on wether its a channel or
556                                          * a user mode. This is a little stranger, but a lot
557                                          * faster, than using a map of pairs.
558                                          */
559                                         handler_id = (*modeletter - 65) | mask;
560
561                                         if (modehandlers[handler_id])
562                                         {
563                                                 bool abort = false;
564
565                                                 log(DEBUG,"Found a ModeHandler* for mode %c",*modeletter);
566
567                                                 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
568                                                 {
569                                                         log(DEBUG,"Call a ModeWatcher*");
570                                                         if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
571                                                                 abort = true;
572                                                 }
573                                                 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
574                                                 {
575                                                         log(DEBUG,"Modetype match, calling handler");
576
577                                                         if (modehandlers[handler_id]->GetNumParams(adding))
578                                                         {
579                                                                 log(DEBUG,"ModeHandler* for this mode says it has parameters");
580
581                                                                 if (pcnt < parameter_counter)
582                                                                 {
583                                                                         parameter = parameters[parameter_counter++];
584                                                                 }
585                                                                 else
586                                                                 {
587                                                                         parameter = "";
588                                                                 }
589                                                         }
590                                                         ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
591                                                         if (ma == MODEACTION_ALLOW)
592                                                         {
593                                                                 log(DEBUG,"ModeAction was allow");
594
595                                                                 /* We're about to output a valid mode letter - was there previously a pending state-change? */
596                                                                 if (state_change)
597                                                                 {
598                                                                         log(DEBUG,"Appending state change");
599                                                                         output_sequence.append(adding ? "+" : "-");
600                                                                 }
601                                                                 
602                                                                 /* Add the mode letter */
603                                                                 output_sequence = output_sequence + *modeletter;
604                                                                 log(DEBUG,"Added mode letter to output sequence, sequence now: '%s'",output_sequence.c_str());
605
606                                                                 /* Is there a valid parameter for this mode? If so add it to the parameter list */
607                                                                 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
608                                                                 {
609                                                                         log(DEBUG,"Added parameter to parameter_list, list now: '%s'",parameter_list.str().c_str());
610                                                                         parameter_list << " " << parameter;
611                                                                 }
612
613                                                                 /* Call all the AfterMode events in the mode watchers for this mode */
614                                                                 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
615                                                                 {
616                                                                         log(DEBUG,"Called a ModeWatcher* after event");
617                                                                         (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
618                                                                 }
619
620                                                                 /* Reset the state change flag */
621                                                                 state_change = false;
622                                                         }
623                                                 }
624                                         }
625                                 break;
626                         }
627                 }
628                 /* Was there at least one valid mode in the sequence? */
629                 if (output_sequence != "")
630                 {
631                         if (servermode)
632                         {
633                                 if (type == MODETYPE_CHANNEL)
634                                 {
635                                         WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
636                                 }
637                         }
638                         else
639                         {
640                                 if (type == MODETYPE_CHANNEL)
641                                 {
642                                         log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
643                                         WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
644                                         FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
645                                 }
646                         }
647                 }
648         }
649 }
650
651
652 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
653 {
654         chanrec* chan;
655         int MOD_RESULT;
656         int can_change;
657         int direction = 1;
658         char outpars[MAXBUF];
659         bool next_ok = true;
660
661         if (!user)
662                 return;
663
664         ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
665
666         return;
667 }
668
669 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
670 {
671         unsigned char mask = 0;
672         mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
673         modehandlers[(modeletter-65) | mask] = mh;
674         return true;
675 }
676
677 ModeParser::ModeParser()
678 {
679         /* Initialise the RFC mode letters */
680         this->AddMode(new ModeChannelSecret, 's');
681         this->AddMode(new ModeChannelPrivate, 'p');
682 }