1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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))
63 log(DEFAULT,"*** BUG *** GiveOps was given an invalid parameter");
69 log(DEFAULT,"the target nickname given to GiveOps was invalid");
70 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
76 log(DEFAULT,"the target nickname given to GiveOps couldnt be found");
77 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
84 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
86 if (MOD_RESULT == ACR_DENY)
88 if (MOD_RESULT == ACR_DEFAULT)
90 if ((status < STATUS_OP) && (!is_uline(user->server)))
92 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
93 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
99 for (unsigned int i = 0; i < d->chans.size(); i++)
101 if ((d->chans[i].channel != NULL) && (chan != NULL))
102 if (!strcasecmp(d->chans[i].channel->name,chan->name))
104 if (d->chans[i].uc_modes & UCMODE_OP)
106 /* mode already set on user, dont allow multiple */
107 log(DEFAULT,"The target user given to GiveOps was already opped on the channel");
110 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
111 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
115 log(DEFAULT,"The target channel given to GiveOps was not in the users mode list");
120 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
124 if ((!user) || (!dest) || (!chan))
126 log(DEFAULT,"*** BUG *** GiveHops was given an invalid parameter");
133 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
138 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
144 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
146 if (MOD_RESULT == ACR_DENY)
148 if (MOD_RESULT == ACR_DEFAULT)
150 if ((status < STATUS_OP) && (!is_uline(user->server)))
152 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
157 for (unsigned int i = 0; i < d->chans.size(); i++)
159 if ((d->chans[i].channel != NULL) && (chan != NULL))
160 if (!strcasecmp(d->chans[i].channel->name,chan->name))
162 if (d->chans[i].uc_modes & UCMODE_HOP)
164 /* mode already set on user, dont allow multiple */
167 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
168 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
176 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
180 if ((!user) || (!dest) || (!chan))
182 log(DEFAULT,"*** BUG *** GiveVoice was given an invalid parameter");
189 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
194 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
200 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
202 if (MOD_RESULT == ACR_DENY)
204 if (MOD_RESULT == ACR_DEFAULT)
206 if ((status < STATUS_HOP) && (!is_uline(user->server)))
208 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
213 for (unsigned int i = 0; i < d->chans.size(); i++)
215 if ((d->chans[i].channel != NULL) && (chan != NULL))
216 if (!strcasecmp(d->chans[i].channel->name,chan->name))
218 if (d->chans[i].uc_modes & UCMODE_VOICE)
220 /* mode already set on user, dont allow multiple */
223 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
224 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
232 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
236 if ((!user) || (!dest) || (!chan))
238 log(DEFAULT,"*** BUG *** TakeOps was given an invalid parameter");
245 log(DEBUG,"TakeOps was given an invalid target nickname of %s",dest);
246 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
251 log(DEBUG,"TakeOps couldnt resolve the target nickname: %s",dest);
252 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
258 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
260 if (MOD_RESULT == ACR_DENY)
262 if (MOD_RESULT == ACR_DEFAULT)
264 if ((status < STATUS_OP) && (!is_uline(user->server)))
266 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
271 for (unsigned int i = 0; i < d->chans.size(); i++)
273 if ((d->chans[i].channel != NULL) && (chan != NULL))
274 if (!strcasecmp(d->chans[i].channel->name,chan->name))
276 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
278 /* mode already set on user, dont allow multiple */
281 d->chans[i].uc_modes ^= UCMODE_OP;
282 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
286 log(DEBUG,"TakeOps couldnt locate the target channel in the target users list");
291 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
295 if ((!user) || (!dest) || (!chan))
297 log(DEFAULT,"*** BUG *** TakeHops was given an invalid parameter");
304 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
309 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
315 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
317 if (MOD_RESULT == ACR_DENY)
319 if (MOD_RESULT == ACR_DEFAULT)
321 if ((status < STATUS_OP) && (!is_uline(user->server)))
323 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
328 for (unsigned int i = 0; i < d->chans.size(); i++)
330 if ((d->chans[i].channel != NULL) && (chan != NULL))
331 if (!strcasecmp(d->chans[i].channel->name,chan->name))
333 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
335 /* mode already set on user, dont allow multiple */
338 d->chans[i].uc_modes ^= UCMODE_HOP;
339 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
347 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
351 if ((!user) || (!dest) || (!chan))
353 log(DEFAULT,"*** BUG *** TakeVoice was given an invalid parameter");
360 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
365 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
371 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
373 if (MOD_RESULT == ACR_DENY)
375 if (MOD_RESULT == ACR_DEFAULT)
377 if ((status < STATUS_HOP) && (!is_uline(user->server)))
379 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
384 for (unsigned int i = 0; i < d->chans.size(); i++)
386 if ((d->chans[i].channel != NULL) && (chan != NULL))
387 if (!strcasecmp(d->chans[i].channel->name,chan->name))
389 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
391 /* mode already set on user, dont allow multiple */
394 d->chans[i].uc_modes ^= UCMODE_VOICE;
395 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
403 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
405 if ((!user) || (!dest) || (!chan)) {
406 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
411 if ((!user) || (!dest) || (!chan))
413 unsigned int l = strlen(dest);
414 if (strchr(dest,'!')==0)
416 if (strchr(dest,'@')==0)
418 for (unsigned int i = 0; i < l; i++)
421 for (unsigned int i = 0; i < l; i++)
425 for (unsigned int i = 0; i < l; i++)
431 for (unsigned int i = 0; i < l; i++)
437 long maxbans = GetMaxBans(chan->name);
438 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
440 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);
444 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
447 FOREACH_RESULT(OnAddBan(user,chan,dest));
452 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
454 if (!strcasecmp(i->data,dest))
456 // dont allow a user to set the same ban twice
462 strncpy(b.data,dest,MAXBUF);
463 strncpy(b.set_by,user->nick,NICKMAX);
464 chan->bans.push_back(b);
468 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
470 if ((!user) || (!dest) || (!chan)) {
471 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
475 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
476 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
478 if (!strcasecmp(i->data,dest))
481 FOREACH_RESULT(OnDelBan(user,chan,dest));
491 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
492 // a section further down the chain tidies up the +-+- crap.
493 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
497 memset(counts,0,sizeof(counts));
498 memset(active,0,sizeof(active));
499 for (unsigned int i = 0; i < modes.length(); i++)
501 if ((modes[i] == '+') || (modes[i] == '-'))
505 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
507 log(DEBUG,"Tidy mode %c",modes[i]);
508 counts[(unsigned int)modes[i]]++;
509 active[(unsigned int)modes[i]] = true;
514 log(DEBUG,"Tidy mode %c",modes[i]);
515 counts[(unsigned int)modes[i]]++;
516 active[(unsigned int)modes[i]] = true;
519 for (int j = 65; j < 127; j++)
521 if ((counts[j] > 1) && (active[j] == true))
524 v[0] = (unsigned char)j;
526 std::string mode_str = v;
527 std::string::size_type pos = modes.find(mode_str);
528 if (pos != std::string::npos)
530 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
531 while (modes.find(mode_str) != std::string::npos)
532 modes.erase(modes.find(mode_str),1);
533 log(DEBUG,"New mode line: %s",modes.c_str());
540 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
543 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
547 char modelist[MAXBUF];
548 char outlist[MAXBUF];
550 char outpars[32][MAXBUF];
556 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
564 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
566 if (MOD_RESULT == ACR_DENY)
569 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
571 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
572 * parameters[2] onwards are parameters for
573 * modes that require them :) */
574 strlcpy(outlist,"+",MAXBUF);
577 log(DEBUG,"process_modes: modelist: %s",modelist);
579 std::string tidied = this->CompressModes(modelist,true);
580 strlcpy(modelist,tidied.c_str(),MAXBUF);
582 int len = strlen(modelist);
583 while (modelist[len-1] == ' ')
584 modelist[--len] = '\0';
585 for (ptr = 0; ptr < len; ptr++)
590 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
592 char modechar = modelist[ptr];
593 switch (modelist[ptr])
598 int t = strlen(outlist)-1;
599 if ((outlist[t] == '+') || (outlist[t] == '-'))
615 int t = strlen(outlist)-1;
616 if ((outlist[t] == '+') || (outlist[t] == '-'))
630 if ((param >= pcnt)) break;
631 log(DEBUG,"Enough parameters left");
635 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
638 log(DEBUG,"calling GiveOps");
639 r = GiveOps(user,parameters[param++],chan,status);
646 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
649 log(DEBUG,"calling TakeOps");
650 r = TakeOps(user,parameters[param++],chan,status);
656 strlcat(outlist,"o",MAXBUF);
657 strlcpy(outpars[pc++],r,MAXBUF);
662 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
666 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
669 r = GiveHops(user,parameters[param++],chan,status);
676 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
679 r = TakeHops(user,parameters[param++],chan,status);
685 strlcat(outlist,"h",MAXBUF);
686 strlcpy(outpars[pc++],r,MAXBUF);
692 if ((param >= pcnt)) break;
696 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
699 r = GiveVoice(user,parameters[param++],chan,status);
706 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
709 r = TakeVoice(user,parameters[param++],chan,status);
715 strlcat(outlist,"v",MAXBUF);
716 strlcpy(outpars[pc++],r,MAXBUF);
721 if ((param >= pcnt)) break;
725 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
728 r = AddBan(user,parameters[param++],chan,status);
735 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
738 r = TakeBan(user,parameters[param++],chan,status);
744 strlcat(outlist,"b",MAXBUF);
745 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
759 if (previously_unset_k)
761 previously_set_k = true;
763 if (!strcmp(chan->key,""))
766 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
771 strlcpy(key,parameters[param++],32);
772 strlcpy(outpars[pc++],key,MAXBUF);
773 strlcpy(chan->key,key,MAXBUF);
781 /* checks on -k are case sensitive and only accurate to the
782 first 32 characters */
783 if (previously_set_k)
785 previously_unset_k = true;
789 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
792 strlcpy(key,parameters[param++],32);
793 /* only allow -k if correct key given */
794 if (!strcmp(chan->key,key))
796 strlcat(outlist,"k",MAXBUF);
797 strlcpy(chan->key,"",MAXBUF);
798 strlcpy(outpars[pc++],key,MAXBUF);
808 if (previously_set_l)
810 previously_unset_l = true;
812 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
823 if ((param >= pcnt)) break;
828 if (previously_unset_l)
830 previously_set_l = true;
831 bool invalid = false;
832 for (unsigned int i = 0; i < strlen(parameters[param]); i++)
834 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
839 if (atoi(parameters[param]) < 1)
848 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
852 chan->limit = atoi(parameters[param]);
854 // reported by mech: large values cause underflow
856 chan->limit = 0x7FFF;
861 strlcat(outlist,"l",MAXBUF);
862 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
870 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
875 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
876 chan->binarymodes |= CM_INVITEONLY;
880 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
881 chan->binarymodes &= ~CM_INVITEONLY;
888 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
893 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
894 chan->binarymodes |= CM_TOPICLOCK;
898 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"t",MAXBUF);
899 chan->binarymodes &= ~CM_TOPICLOCK;
906 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
911 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
912 chan->binarymodes |= CM_NOEXTERNAL;
916 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
917 chan->binarymodes &= ~CM_NOEXTERNAL;
924 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
929 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
930 chan->binarymodes |= CM_MODERATED;
934 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
935 chan->binarymodes &= ~CM_MODERATED;
942 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
947 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
948 chan->binarymodes |= CM_SECRET;
949 if (chan->binarymodes & CM_PRIVATE)
951 chan->binarymodes &= ~CM_PRIVATE;
954 strlcat(outlist,"-p+",MAXBUF);
960 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
961 chan->binarymodes &= ~CM_SECRET;
968 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
973 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
974 chan->binarymodes |= CM_PRIVATE;
975 if (chan->binarymodes & CM_SECRET)
977 chan->binarymodes &= ~CM_SECRET;
980 strlcat(outlist,"-s+",MAXBUF);
986 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
987 chan->binarymodes &= ~CM_PRIVATE;
993 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
996 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
998 if (!ModeIsListMode(modechar,MT_CHANNEL))
1000 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1004 if (ModeDefined(modechar,MT_CHANNEL))
1006 log(DEBUG,"A module has claimed this mode");
1009 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1011 p.push_back(parameters[param]);
1013 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1015 p.push_back(parameters[param]);
1018 bool handled = false;
1021 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1022 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1024 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1030 // BIG ASS IDIOTIC CODER WARNING!
1031 // Using OnRawMode on another modules mode's behavour
1032 // will confuse the crap out of admins! just because you CAN
1033 // do it, doesnt mean you SHOULD!
1035 std::string para = "";
1038 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1041 for (int i = 0; i <= MODCOUNT; i++)
1045 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1048 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1049 char app[] = {modechar, 0};
1050 if (ModeIsListMode(modechar,MT_CHANNEL))
1061 strlcat(outlist, app,MAXBUF);
1063 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1070 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1072 strlcat(outlist, app,MAXBUF);
1074 else if (!strchr(outlist,modechar))
1076 strlcat(outlist, app,MAXBUF);
1079 chan->SetCustomMode(modechar,mdir);
1080 // include parameters in output if mode has them
1081 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1083 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1084 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1087 // break, because only one module can handle the mode.
1096 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1104 /* this ensures only the *valid* modes are sent out onto the network */
1105 int xt = strlen(outlist)-1;
1106 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1109 xt = strlen(outlist)-1;
1113 strlcpy(outstr,outlist,MAXBUF);
1114 for (ptr = 0; ptr < pc; ptr++)
1116 strlcat(outstr," ",MAXBUF);
1117 strlcat(outstr,outpars[ptr],MAXBUF);
1121 log(DEBUG,"Local mode change");
1122 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1123 FOREACH_MOD OnMode(user, chan, TYPE_CHANNEL, outstr);
1131 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outstr);
1139 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1140 FOREACH_MOD OnMode(user, chan, TYPE_CHANNEL, outstr);
1147 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1149 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1151 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1152 // Servers can +o and -o arbitrarily
1153 if ((serveroverride == true) && (umode == 'o'))
1157 // RFC1459 specified modes
1158 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1160 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1164 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1165 if ((strchr(sourcemodes,'o')) && (!adding))
1167 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1170 else if (umode == 'o')
1172 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1176 // process any module-defined modes that need oper
1177 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1179 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1183 if (ModeDefined(umode,MT_CLIENT))
1185 // process any module-defined modes that don't need oper
1186 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1187 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1189 // no, this mode needs oper, and this user 'aint got what it takes!
1195 // anything else - return false.
1196 log(DEBUG,"umode %c not known by any ruleset",umode);
1200 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1207 strlcpy(s2->nick,Config->ServerName,NICKMAX);
1208 strlcpy(s2->modes,"o",52);
1215 if (ModeDefined(umode,MT_CLIENT))
1217 for (int i = 0; i <= MODCOUNT; i++)
1219 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1221 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1225 log(DEBUG,"No module claims umode %c",umode);
1244 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1250 char outpars[MAXBUF];
1252 dest = Find(parameters[0]);
1259 if ((dest) && (pcnt == 1))
1261 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1265 if ((dest) && (pcnt > 1))
1267 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1268 parameters[1] = (char*)tidied.c_str();
1270 char dmodes[MAXBUF];
1271 strlcpy(dmodes,dest->modes,52);
1272 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1277 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1288 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1292 strcpy(outpars,"+");
1295 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1298 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1300 if (parameters[1][i] == ' ')
1302 if (parameters[1][i] == '+')
1306 int t = strlen(outpars)-1;
1307 if ((outpars[t] == '+') || (outpars[t] == '-'))
1313 strcat(outpars,"+");
1319 if (parameters[1][i] == '-')
1323 int t = strlen(outpars)-1;
1324 if ((outpars[t] == '+') || (outpars[t] == '-'))
1330 strcat(outpars,"-");
1338 if (strchr(user->modes,'o'))
1344 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,direction,false)))
1353 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,false)))
1355 char umode = parameters[1][i];
1356 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1358 int q = strlen(dmodes);
1359 int r = strlen(outpars);
1361 dmodes[q] = parameters[1][i];
1363 outpars[r] = parameters[1][i];
1364 if (parameters[1][i] == 'o')
1366 FOREACH_MOD OnGlobalOper(dest);
1373 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
1375 char umode = parameters[1][i];
1376 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1382 unsigned int r = strlen(outpars);
1384 outpars[r] = parameters[1][i];
1387 for (q = 0; q < strlen(dmodes); q++)
1389 if (dmodes[q] != parameters[1][i])
1393 strlcat(temp,moo,MAXBUF);
1396 strlcpy(dmodes,temp,52);
1409 strlcpy(b,"",MAXBUF);
1412 while (i < strlen (outpars))
1414 b[z++] = outpars[i++];
1416 if (i<strlen(outpars)-1)
1418 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1420 // someones playing silly buggers and trying
1421 // to put a +- or -+ into the line...
1425 if (i == strlen(outpars)-1)
1427 if ((outpars[i] == '-') || (outpars[i] == '+'))
1435 if ((b[z] == '-') || (b[z] == '+'))
1438 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1443 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1444 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
1447 if (strlen(dmodes)>MAXMODES)
1449 dmodes[MAXMODES-1] = '\0';
1451 log(DEBUG,"Stripped mode line");
1452 log(DEBUG,"Line dest is now %s",dmodes);
1453 strlcpy(dest->modes,dmodes,52);
1460 Ptr = FindChan(parameters[0]);
1465 /* just /modes #channel */
1466 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1467 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1473 char* mode = parameters[1];
1477 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1483 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1485 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1487 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1490 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1492 // list of items for an extmode
1493 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1494 FOREACH_MOD OnSendList(user,Ptr,*mode);
1500 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)))
1502 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1509 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1511 if (MOD_RESULT == ACR_DENY)
1513 if (MOD_RESULT == ACR_DEFAULT)
1515 if (cstatus(user,Ptr) < STATUS_HOP)
1517 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1522 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1527 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1534 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1540 char outpars[MAXBUF];
1542 dest = Find(parameters[0]);
1544 // fix: ChroNiCk found this - we cant use this as debug if its null!
1547 log(DEBUG,"server_mode on %s",dest->nick);
1550 if ((dest) && (pcnt > 1))
1552 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1553 parameters[1] = (char*)tidied.c_str();
1555 char dmodes[MAXBUF];
1556 strlcpy(dmodes,dest->modes,52);
1558 strcpy(outpars,"+");
1561 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1564 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1566 if (parameters[1][i] == ' ')
1568 if (parameters[1][i] == '+')
1572 int t = strlen(outpars)-1;
1573 if ((outpars[t] == '+') || (outpars[t] == '-'))
1579 strcat(outpars,"+");
1585 if (parameters[1][i] == '-')
1589 int t = strlen(outpars)-1;
1590 if ((outpars[t] == '+') || (outpars[t] == '-'))
1596 strcat(outpars,"-");
1603 log(DEBUG,"begin mode processing entry");
1609 log(DEBUG,"umode %c being added",parameters[1][i]);
1610 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,true)))
1612 char umode = parameters[1][i];
1613 log(DEBUG,"umode %c is an allowed umode",umode);
1614 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1616 int v1 = strlen(dmodes);
1617 int v2 = strlen(outpars);
1619 dmodes[v1] = parameters[1][i];
1621 outpars[v2] = parameters[1][i];
1627 // can only remove a mode they already have
1628 log(DEBUG,"umode %c being removed",parameters[1][i]);
1629 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,true)) && (strchr(dmodes,parameters[1][i])))
1631 char umode = parameters[1][i];
1632 log(DEBUG,"umode %c is an allowed umode",umode);
1633 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1639 unsigned int v1 = strlen(outpars);
1641 outpars[v1] = parameters[1][i];
1644 for (q = 0; q < strlen(dmodes); q++)
1646 if (dmodes[q] != parameters[1][i])
1650 strlcat(temp,moo,MAXBUF);
1653 strlcpy(dmodes,temp,52);
1663 strlcpy(b,"",MAXBUF);
1666 while (i < strlen (outpars))
1668 b[z++] = outpars[i++];
1670 if (i<strlen(outpars)-1)
1672 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1674 // someones playing silly buggers and trying
1675 // to put a +- or -+ into the line...
1679 if (i == strlen(outpars)-1)
1681 if ((outpars[i] == '-') || (outpars[i] == '+'))
1689 if ((b[z] == '-') || (b[z] == '+'))
1692 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1697 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1698 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
1701 if (strlen(dmodes)>MAXMODES)
1703 dmodes[MAXMODES-1] = '\0';
1705 log(DEBUG,"Stripped mode line");
1706 log(DEBUG,"Line dest is now %s",dmodes);
1707 strlcpy(dest->modes,dmodes,MAXMODES);
1714 Ptr = FindChan(parameters[0]);
1717 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1721 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);