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