1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
23 #include <sys/errno.h>
27 #include <ext/hash_map>
35 #include "connection.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
50 extern std::vector<Module*> modules;
51 extern std::vector<ircd_module*> factory;
52 extern InspIRCd* ServerInstance;
53 extern ServerConfig* Config;
57 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
61 if ((!user) || (!dest) || (!chan) || (!*dest))
63 log(DEFAULT,"*** BUG *** GiveOps was given an invalid parameter");
69 log(DEFAULT,"the target nickname given to GiveOps couldnt be found");
70 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
77 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
79 if (MOD_RESULT == ACR_DENY)
81 if (MOD_RESULT == ACR_DEFAULT)
83 if ((status < STATUS_OP) && (!is_uline(user->server)))
85 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
86 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
92 for (unsigned int i = 0; i < d->chans.size(); i++)
94 if ((d->chans[i].channel != NULL) && (chan != NULL))
95 if (!strcasecmp(d->chans[i].channel->name,chan->name))
97 if (d->chans[i].uc_modes & UCMODE_OP)
99 /* mode already set on user, dont allow multiple */
100 log(DEFAULT,"The target user given to GiveOps was already opped on the channel");
103 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
104 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
108 log(DEFAULT,"The target channel given to GiveOps was not in the users mode list");
113 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
117 if ((!user) || (!dest) || (!chan) || (!*dest))
119 log(DEFAULT,"*** BUG *** GiveHops was given an invalid parameter");
126 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
132 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
134 if (MOD_RESULT == ACR_DENY)
136 if (MOD_RESULT == ACR_DEFAULT)
138 if ((status < STATUS_OP) && (!is_uline(user->server)))
140 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
145 for (unsigned int i = 0; i < d->chans.size(); i++)
147 if ((d->chans[i].channel != NULL) && (chan != NULL))
148 if (!strcasecmp(d->chans[i].channel->name,chan->name))
150 if (d->chans[i].uc_modes & UCMODE_HOP)
152 /* mode already set on user, dont allow multiple */
155 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
156 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
164 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
168 if ((!user) || (!dest) || (!chan) || (!*dest))
170 log(DEFAULT,"*** BUG *** GiveVoice was given an invalid parameter");
177 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
183 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
185 if (MOD_RESULT == ACR_DENY)
187 if (MOD_RESULT == ACR_DEFAULT)
189 if ((status < STATUS_HOP) && (!is_uline(user->server)))
191 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
196 for (unsigned int i = 0; i < d->chans.size(); i++)
198 if ((d->chans[i].channel != NULL) && (chan != NULL))
199 if (!strcasecmp(d->chans[i].channel->name,chan->name))
201 if (d->chans[i].uc_modes & UCMODE_VOICE)
203 /* mode already set on user, dont allow multiple */
206 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
207 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
215 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
219 if ((!user) || (!dest) || (!chan) || (!*dest))
221 log(DEFAULT,"*** BUG *** TakeOps was given an invalid parameter");
228 log(DEBUG,"TakeOps couldnt resolve the target nickname: %s",dest);
229 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
235 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
237 if (MOD_RESULT == ACR_DENY)
239 if (MOD_RESULT == ACR_DEFAULT)
241 if ((status < STATUS_OP) && (!is_uline(user->server)))
243 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
248 for (unsigned int i = 0; i < d->chans.size(); i++)
250 if ((d->chans[i].channel != NULL) && (chan != NULL))
251 if (!strcasecmp(d->chans[i].channel->name,chan->name))
253 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
255 /* mode already set on user, dont allow multiple */
258 d->chans[i].uc_modes ^= UCMODE_OP;
259 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
263 log(DEBUG,"TakeOps couldnt locate the target channel in the target users list");
268 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
272 if ((!user) || (!dest) || (!chan) || (!*dest))
274 log(DEFAULT,"*** BUG *** TakeHops was given an invalid parameter");
281 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
287 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
289 if (MOD_RESULT == ACR_DENY)
291 if (MOD_RESULT == ACR_DEFAULT)
293 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
294 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
296 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
301 for (unsigned int i = 0; i < d->chans.size(); i++)
303 if ((d->chans[i].channel != NULL) && (chan != NULL))
304 if (!strcasecmp(d->chans[i].channel->name,chan->name))
306 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
308 /* mode already set on user, dont allow multiple */
311 d->chans[i].uc_modes ^= UCMODE_HOP;
312 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
320 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
324 if ((!user) || (!dest) || (!chan) || (!*dest))
326 log(DEFAULT,"*** BUG *** TakeVoice was given an invalid parameter");
333 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
339 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
341 if (MOD_RESULT == ACR_DENY)
343 if (MOD_RESULT == ACR_DEFAULT)
345 if ((status < STATUS_HOP) && (!is_uline(user->server)))
347 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
352 for (unsigned int i = 0; i < d->chans.size(); i++)
354 if ((d->chans[i].channel != NULL) && (chan != NULL))
355 if (!strcasecmp(d->chans[i].channel->name,chan->name))
357 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
359 /* mode already set on user, dont allow multiple */
362 d->chans[i].uc_modes ^= UCMODE_VOICE;
363 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
371 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
374 int toomanyexclamation = 0;
377 if ((!user) || (!dest) || (!chan) || (!*dest))
379 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
383 for (char* i = dest; *i; i++)
385 if ((*i < 32) || (*i > 126))
391 toomanyexclamation++;
399 if (toomanyexclamation != 1 || toomanyat != 1)
401 * this stops sillyness like n!u!u!u@h, though note that most
402 * ircds don't actually verify banmask validity. --w00t
406 long maxbans = GetMaxBans(chan->name);
407 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
409 WriteServ(user->fd,"478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
413 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
416 FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
421 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
423 if (!strcasecmp(i->data,dest))
425 // dont allow a user to set the same ban twice
431 strlcpy(b.data,dest,MAXBUF);
432 strlcpy(b.set_by,user->nick,NICKMAX);
433 chan->bans.push_back(b);
437 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
439 if ((!user) || (!dest) || (!chan) || (!*dest)) {
440 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
444 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
445 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
447 if (!strcasecmp(i->data,dest))
450 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
460 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
461 // a section further down the chain tidies up the +-+- crap.
462 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
466 memset(counts,0,sizeof(counts));
467 memset(active,0,sizeof(active));
468 for (unsigned int i = 0; i < modes.length(); i++)
470 if ((modes[i] == '+') || (modes[i] == '-'))
474 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
476 log(DEBUG,"Tidy mode %c",modes[i]);
477 counts[(unsigned int)modes[i]]++;
478 active[(unsigned int)modes[i]] = true;
483 log(DEBUG,"Tidy mode %c",modes[i]);
484 counts[(unsigned int)modes[i]]++;
485 active[(unsigned int)modes[i]] = true;
488 for (int j = 65; j < 127; j++)
490 if ((counts[j] > 1) && (active[j] == true))
493 v[0] = (unsigned char)j;
495 std::string mode_str = v;
496 std::string::size_type pos = modes.find(mode_str);
497 if (pos != std::string::npos)
499 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
500 while (modes.find(mode_str) != std::string::npos)
501 modes.erase(modes.find(mode_str),1);
502 log(DEBUG,"New mode line: %s",modes.c_str());
509 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
512 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
516 char modelist[MAXBUF];
517 char outlist[MAXBUF];
519 char outpars[32][MAXBUF];
525 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
533 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
535 if (MOD_RESULT == ACR_DENY)
538 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
540 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
541 * parameters[2] onwards are parameters for
542 * modes that require them :) */
547 log(DEBUG,"process_modes: modelist: %s",modelist);
549 std::string tidied = this->CompressModes(modelist,true);
550 strlcpy(modelist,tidied.c_str(),MAXBUF);
552 int len = tidied.length();
553 while (modelist[len-1] == ' ')
554 modelist[--len] = '\0';
556 for (char* modechar = modelist; *modechar; ptr++, modechar++)
566 int t = strlen(outlist)-1;
567 if ((outlist[t] == '+') || (outlist[t] == '-'))
583 int t = strlen(outlist)-1;
584 if ((outlist[t] == '+') || (outlist[t] == '-'))
598 if ((param >= pcnt)) break;
599 log(DEBUG,"Enough parameters left");
603 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
606 log(DEBUG,"calling GiveOps");
607 r = GiveOps(user,parameters[param++],chan,status);
614 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
617 log(DEBUG,"calling TakeOps");
618 r = TakeOps(user,parameters[param++],chan,status);
624 strlcat(outlist,"o",MAXBUF);
625 strlcpy(outpars[pc++],r,MAXBUF);
630 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
634 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
637 r = GiveHops(user,parameters[param++],chan,status);
644 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
647 r = TakeHops(user,parameters[param++],chan,status);
653 strlcat(outlist,"h",MAXBUF);
654 strlcpy(outpars[pc++],r,MAXBUF);
660 if ((param >= pcnt)) break;
664 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
667 r = GiveVoice(user,parameters[param++],chan,status);
674 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
677 r = TakeVoice(user,parameters[param++],chan,status);
683 strlcat(outlist,"v",MAXBUF);
684 strlcpy(outpars[pc++],r,MAXBUF);
689 if ((param >= pcnt)) break;
693 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
696 r = AddBan(user,parameters[param++],chan,status);
703 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
706 r = TakeBan(user,parameters[param++],chan,status);
712 strlcat(outlist,"b",MAXBUF);
713 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
727 if (previously_unset_k)
729 previously_set_k = true;
734 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
739 strlcpy(key,parameters[param++],32);
740 strlcpy(outpars[pc++],key,MAXBUF);
741 strlcpy(chan->key,key,MAXBUF);
749 /* checks on -k are case sensitive and only accurate to the
750 first 32 characters */
751 if (previously_set_k)
753 previously_unset_k = true;
757 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
760 strlcpy(key,parameters[param++],32);
761 /* only allow -k if correct key given */
762 if (!strcmp(chan->key,key))
764 strlcat(outlist,"k",MAXBUF);
765 strlcpy(chan->key,"",MAXBUF);
766 strlcpy(outpars[pc++],key,MAXBUF);
776 if (previously_set_l)
778 previously_unset_l = true;
780 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
791 if ((param >= pcnt)) break;
796 if (previously_unset_l)
798 previously_set_l = true;
799 bool invalid = false;
800 for (char* f = parameters[param]; *f; f++)
802 if ((*f < '0') || (*f > '9'))
807 /* If the limit is < 1, or the new limit is the current limit, dont allow */
808 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
817 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
821 chan->limit = atoi(parameters[param]);
823 // reported by mech: large values cause underflow
825 chan->limit = 0x7FFF;
830 strlcat(outlist,"l",MAXBUF);
831 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
839 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
844 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
845 chan->binarymodes |= CM_INVITEONLY;
849 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
850 chan->binarymodes &= ~CM_INVITEONLY;
857 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
862 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
863 chan->binarymodes |= CM_TOPICLOCK;
867 if (chan->binarymodes & CM_TOPICLOCK) strlcat(outlist,"t",MAXBUF);
868 chan->binarymodes &= ~CM_TOPICLOCK;
875 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
880 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
881 chan->binarymodes |= CM_NOEXTERNAL;
885 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
886 chan->binarymodes &= ~CM_NOEXTERNAL;
893 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
898 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
899 chan->binarymodes |= CM_MODERATED;
903 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
904 chan->binarymodes &= ~CM_MODERATED;
911 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
916 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
917 chan->binarymodes |= CM_SECRET;
918 if (chan->binarymodes & CM_PRIVATE)
920 chan->binarymodes &= ~CM_PRIVATE;
923 strlcat(outlist,"-p+",MAXBUF);
929 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
930 chan->binarymodes &= ~CM_SECRET;
937 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
942 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
943 chan->binarymodes |= CM_PRIVATE;
944 if (chan->binarymodes & CM_SECRET)
946 chan->binarymodes &= ~CM_SECRET;
949 strlcat(outlist,"-s+",MAXBUF);
955 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
956 chan->binarymodes &= ~CM_PRIVATE;
962 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",*modechar,chan->custom_modes);
965 if (((!strchr(chan->custom_modes,*modechar)) && (!mdir)) || ((strchr(chan->custom_modes,*modechar)) && (mdir)))
967 if (!ModeIsListMode(*modechar,MT_CHANNEL))
969 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
973 if (ModeDefined(*modechar,MT_CHANNEL))
975 log(DEBUG,"A module has claimed this mode");
978 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
980 p.push_back(parameters[param]);
982 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
984 p.push_back(parameters[param]);
987 bool handled = false;
990 // we're supposed to have a parameter, but none was given... so dont handle the mode.
991 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
993 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
999 // BIG ASS IDIOTIC CODER WARNING!
1000 // Using OnRawMode on another modules mode's behavour
1001 // will confuse the crap out of admins! just because you CAN
1002 // do it, doesnt mean you SHOULD!
1004 std::string para = "";
1007 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
1010 for (int i = 0; i <= MODCOUNT; i++)
1014 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
1017 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1018 char app[] = {*modechar, 0};
1019 if (ModeIsListMode(*modechar,MT_CHANNEL))
1030 strlcat(outlist, app,MAXBUF);
1032 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1039 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1041 strlcat(outlist, app,MAXBUF);
1043 else if (!strchr(outlist,*modechar))
1045 strlcat(outlist, app,MAXBUF);
1048 chan->SetCustomMode(*modechar,mdir);
1049 // include parameters in output if mode has them
1050 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1052 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1053 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1056 // break, because only one module can handle the mode.
1065 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
1073 /* this ensures only the *valid* modes are sent out onto the network */
1074 int xt = strlen(outlist)-1;
1075 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1078 xt = strlen(outlist)-1;
1082 strlcpy(outstr,outlist,MAXBUF);
1083 for (ptr = 0; ptr < pc; ptr++)
1085 strlcat(outstr," ",MAXBUF);
1086 strlcat(outstr,outpars[ptr],MAXBUF);
1090 log(DEBUG,"Local mode change");
1091 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1092 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outstr));
1100 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outstr);
1108 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1109 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outstr));
1116 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1118 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1120 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1121 // Servers can +o and -o arbitrarily
1122 if ((serveroverride == true) && (umode == 'o'))
1126 // RFC1459 specified modes
1127 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1129 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1133 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1134 if ((strchr(sourcemodes,'o')) && (!adding))
1136 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1139 else if (umode == 'o')
1141 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1145 // process any module-defined modes that need oper
1146 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1148 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1152 if (ModeDefined(umode,MT_CLIENT))
1154 // process any module-defined modes that don't need oper
1155 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1156 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1158 // no, this mode needs oper, and this user 'aint got what it takes!
1164 // anything else - return false.
1165 log(DEBUG,"umode %c not known by any ruleset",umode);
1169 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1176 strlcpy(s2->nick,Config->ServerName,NICKMAX);
1177 strlcpy(s2->modes,"o",52);
1184 if (ModeDefined(umode,MT_CLIENT))
1186 for (int i = 0; i <= MODCOUNT; i++)
1188 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1190 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1194 log(DEBUG,"No module claims umode %c",umode);
1213 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1219 char outpars[MAXBUF];
1221 dest = Find(parameters[0]);
1228 if ((dest) && (pcnt == 1))
1230 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1234 if ((dest) && (pcnt > 1))
1236 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1237 parameters[1] = (char*)tidied.c_str();
1239 char dmodes[MAXBUF];
1240 strlcpy(dmodes,dest->modes,52);
1241 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1246 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1257 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1265 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1268 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1270 if (parameters[1][i] == ' ')
1272 if (parameters[1][i] == '+')
1276 int t = strlen(outpars)-1;
1277 if ((outpars[t] == '+') || (outpars[t] == '-'))
1283 strcat(outpars,"+");
1289 if (parameters[1][i] == '-')
1293 int t = strlen(outpars)-1;
1294 if ((outpars[t] == '+') || (outpars[t] == '-'))
1300 strcat(outpars,"-");
1308 if (strchr(user->modes,'o'))
1314 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,direction,false)))
1323 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,false)))
1325 char umode = parameters[1][i];
1326 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1328 int q = strlen(dmodes);
1329 int r = strlen(outpars);
1331 dmodes[q] = parameters[1][i];
1333 outpars[r] = parameters[1][i];
1334 if (parameters[1][i] == 'o')
1336 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1343 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
1345 char umode = parameters[1][i];
1346 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1352 unsigned int r = strlen(outpars);
1354 outpars[r] = parameters[1][i];
1357 for (q = 0; q < strlen(dmodes); q++)
1359 if (dmodes[q] != parameters[1][i])
1363 strlcat(temp,moo,MAXBUF);
1366 strlcpy(dmodes,temp,52);
1382 strlcpy(b,"",MAXBUF);
1385 while (i < strlen (outpars))
1387 b[z++] = outpars[i++];
1389 if (i<strlen(outpars)-1)
1391 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1393 // someones playing silly buggers and trying
1394 // to put a +- or -+ into the line...
1398 if (i == strlen(outpars)-1)
1400 if ((outpars[i] == '-') || (outpars[i] == '+'))
1408 if ((b[z] == '-') || (b[z] == '+'))
1411 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1416 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1417 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1420 if (strlen(dmodes)>MAXMODES)
1422 dmodes[MAXMODES-1] = '\0';
1424 log(DEBUG,"Stripped mode line");
1425 log(DEBUG,"Line dest is now %s",dmodes);
1426 strlcpy(dest->modes,dmodes,52);
1433 Ptr = FindChan(parameters[0]);
1438 /* just /modes #channel */
1439 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1440 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1446 char* mode = parameters[1];
1450 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, Ptr, *mode, "", false, 0));
1456 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1458 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1460 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1463 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1465 // list of items for an extmode
1466 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1467 FOREACH_MOD(I_OnSendList,OnSendList(user,Ptr,*mode));
1473 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)))
1475 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1482 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1484 if (MOD_RESULT == ACR_DENY)
1486 if (MOD_RESULT == ACR_DEFAULT)
1488 if (cstatus(user,Ptr) < STATUS_HOP)
1490 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1495 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1500 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1507 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1513 char outpars[MAXBUF];
1515 dest = Find(parameters[0]);
1517 // fix: ChroNiCk found this - we cant use this as debug if its null!
1520 log(DEBUG,"server_mode on %s",dest->nick);
1523 if ((dest) && (pcnt > 1))
1525 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1526 parameters[1] = (char*)tidied.c_str();
1528 char dmodes[MAXBUF];
1529 strlcpy(dmodes,dest->modes,52);
1535 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1538 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1540 if (parameters[1][i] == ' ')
1542 if (parameters[1][i] == '+')
1546 int t = strlen(outpars)-1;
1547 if ((outpars[t] == '+') || (outpars[t] == '-'))
1553 strcat(outpars,"+");
1559 if (parameters[1][i] == '-')
1563 int t = strlen(outpars)-1;
1564 if ((outpars[t] == '+') || (outpars[t] == '-'))
1570 strcat(outpars,"-");
1577 log(DEBUG,"begin mode processing entry");
1583 log(DEBUG,"umode %c being added",parameters[1][i]);
1584 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,true)))
1586 char umode = parameters[1][i];
1587 log(DEBUG,"umode %c is an allowed umode",umode);
1588 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1590 int v1 = strlen(dmodes);
1591 int v2 = strlen(outpars);
1593 dmodes[v1] = parameters[1][i];
1595 outpars[v2] = parameters[1][i];
1601 // can only remove a mode they already have
1602 log(DEBUG,"umode %c being removed",parameters[1][i]);
1603 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,true)) && (strchr(dmodes,parameters[1][i])))
1605 char umode = parameters[1][i];
1606 log(DEBUG,"umode %c is an allowed umode",umode);
1607 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1613 unsigned int v1 = strlen(outpars);
1615 outpars[v1] = parameters[1][i];
1617 for (q = 0; q < strlen(dmodes); q++)
1619 if (dmodes[q] != parameters[1][i])
1623 strlcat(temp,moo,MAXBUF);
1626 strlcpy(dmodes,temp,52);
1636 strlcpy(b,"",MAXBUF);
1639 while (i < strlen (outpars))
1641 b[z++] = outpars[i++];
1643 if (i<strlen(outpars)-1)
1645 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1647 // someones playing silly buggers and trying
1648 // to put a +- or -+ into the line...
1652 if (i == strlen(outpars)-1)
1654 if ((outpars[i] == '-') || (outpars[i] == '+'))
1662 if ((b[z] == '-') || (b[z] == '+'))
1665 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1670 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1671 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1674 if (strlen(dmodes)>MAXMODES)
1676 dmodes[MAXMODES-1] = '\0';
1678 log(DEBUG,"Stripped mode line");
1679 log(DEBUG,"Line dest is now %s",dmodes);
1680 strlcpy(dest->modes,dmodes,MAXMODES);
1687 Ptr = FindChan(parameters[0]);
1690 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1694 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);