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