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