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