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