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