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