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);
75 if (user->server == d->server)
78 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
80 if (MOD_RESULT == ACR_DENY)
82 if (MOD_RESULT == ACR_DEFAULT)
84 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
86 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
87 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
94 for (unsigned int i = 0; i < d->chans.size(); i++)
96 if ((d->chans[i].channel != NULL) && (chan != NULL))
97 if (!strcasecmp(d->chans[i].channel->name,chan->name))
99 if (d->chans[i].uc_modes & UCMODE_OP)
101 /* mode already set on user, dont allow multiple */
102 log(DEFAULT,"The target user given to GiveOps was already opped on the channel");
105 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
106 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
110 log(DEFAULT,"The target channel given to GiveOps was not in the users mode list");
115 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
119 if ((!user) || (!dest) || (!chan) || (!*dest))
121 log(DEFAULT,"*** BUG *** GiveHops was given an invalid parameter");
128 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
133 if (user->server == d->server)
136 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
138 if (MOD_RESULT == ACR_DENY)
140 if (MOD_RESULT == ACR_DEFAULT)
142 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
144 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
150 for (unsigned int i = 0; i < d->chans.size(); i++)
152 if ((d->chans[i].channel != NULL) && (chan != NULL))
153 if (!strcasecmp(d->chans[i].channel->name,chan->name))
155 if (d->chans[i].uc_modes & UCMODE_HOP)
157 /* mode already set on user, dont allow multiple */
160 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
161 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
169 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
173 if ((!user) || (!dest) || (!chan) || (!*dest))
175 log(DEFAULT,"*** BUG *** GiveVoice was given an invalid parameter");
182 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
187 if (user->server == d->server)
190 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
192 if (MOD_RESULT == ACR_DENY)
194 if (MOD_RESULT == ACR_DEFAULT)
196 if ((status < STATUS_HOP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
198 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
204 for (unsigned int i = 0; i < d->chans.size(); i++)
206 if ((d->chans[i].channel != NULL) && (chan != NULL))
207 if (!strcasecmp(d->chans[i].channel->name,chan->name))
209 if (d->chans[i].uc_modes & UCMODE_VOICE)
211 /* mode already set on user, dont allow multiple */
214 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
215 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
223 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
227 if ((!user) || (!dest) || (!chan) || (!*dest))
229 log(DEFAULT,"*** BUG *** TakeOps was given an invalid parameter");
236 log(DEBUG,"TakeOps couldnt resolve the target nickname: %s",dest);
237 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
242 if (user->server == d->server)
245 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
247 if (MOD_RESULT == ACR_DENY)
249 if (MOD_RESULT == ACR_DEFAULT)
251 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)) && (IS_LOCAL(user)))
253 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
259 for (unsigned int i = 0; i < d->chans.size(); i++)
261 if ((d->chans[i].channel != NULL) && (chan != NULL))
262 if (!strcasecmp(d->chans[i].channel->name,chan->name))
264 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
266 /* mode already set on user, dont allow multiple */
269 d->chans[i].uc_modes ^= UCMODE_OP;
270 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
274 log(DEBUG,"TakeOps couldnt locate the target channel in the target users list");
279 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
283 if ((!user) || (!dest) || (!chan) || (!*dest))
285 log(DEFAULT,"*** BUG *** TakeHops was given an invalid parameter");
292 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
297 if (user->server == d->server)
300 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
302 if (MOD_RESULT == ACR_DENY)
304 if (MOD_RESULT == ACR_DEFAULT)
306 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
307 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))) && (IS_LOCAL(user)))
309 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
315 for (unsigned int i = 0; i < d->chans.size(); i++)
317 if ((d->chans[i].channel != NULL) && (chan != NULL))
318 if (!strcasecmp(d->chans[i].channel->name,chan->name))
320 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
322 /* mode already set on user, dont allow multiple */
325 d->chans[i].uc_modes ^= UCMODE_HOP;
326 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
334 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
338 if ((!user) || (!dest) || (!chan) || (!*dest))
340 log(DEFAULT,"*** BUG *** TakeVoice was given an invalid parameter");
347 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
352 if (user->server == d->server)
355 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
357 if (MOD_RESULT == ACR_DENY)
359 if (MOD_RESULT == ACR_DEFAULT)
361 if ((status < STATUS_HOP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
363 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
369 for (unsigned int i = 0; i < d->chans.size(); i++)
371 if ((d->chans[i].channel != NULL) && (chan != NULL))
372 if (!strcasecmp(d->chans[i].channel->name,chan->name))
374 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
376 /* mode already set on user, dont allow multiple */
379 d->chans[i].uc_modes ^= UCMODE_VOICE;
380 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
388 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
391 int toomanyexclamation = 0;
394 if ((!user) || (!dest) || (!chan) || (!*dest))
396 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
400 for (char* i = dest; *i; i++)
402 if ((*i < 32) || (*i > 126))
408 toomanyexclamation++;
416 if (toomanyexclamation != 1 || toomanyat != 1)
418 * this stops sillyness like n!u!u!u@h, though note that most
419 * ircds don't actually verify banmask validity. --w00t
423 long maxbans = GetMaxBans(chan->name);
424 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
426 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);
430 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
433 FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
438 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
440 if (!strcasecmp(i->data,dest))
442 // dont allow a user to set the same ban twice
448 strlcpy(b.data,dest,MAXBUF);
449 strlcpy(b.set_by,user->nick,NICKMAX);
450 chan->bans.push_back(b);
454 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
456 if ((!user) || (!dest) || (!chan) || (!*dest)) {
457 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
461 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
462 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
464 if (!strcasecmp(i->data,dest))
467 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
477 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
478 // a section further down the chain tidies up the +-+- crap.
479 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
483 memset(counts,0,sizeof(counts));
484 memset(active,0,sizeof(active));
485 for (unsigned int i = 0; i < modes.length(); i++)
487 if ((modes[i] == '+') || (modes[i] == '-'))
491 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
493 log(DEBUG,"Tidy mode %c",modes[i]);
494 counts[(unsigned int)modes[i]]++;
495 active[(unsigned int)modes[i]] = true;
500 log(DEBUG,"Tidy mode %c",modes[i]);
501 counts[(unsigned int)modes[i]]++;
502 active[(unsigned int)modes[i]] = true;
505 for (int j = 65; j < 127; j++)
507 if ((counts[j] > 1) && (active[j] == true))
510 v[0] = (unsigned char)j;
512 std::string mode_str = v;
513 std::string::size_type pos = modes.find(mode_str);
514 if (pos != std::string::npos)
516 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
517 while (modes.find(mode_str) != std::string::npos)
518 modes.erase(modes.find(mode_str),1);
519 log(DEBUG,"New mode line: %s",modes.c_str());
526 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
529 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
533 char modelist[MAXBUF];
534 char outlist[MAXBUF];
536 char outpars[32][MAXBUF];
542 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
553 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
554 if (MOD_RESULT == ACR_DENY)
558 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
560 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
561 * parameters[2] onwards are parameters for
562 * modes that require them :) */
567 log(DEBUG,"process_modes: modelist: %s",modelist);
569 std::string tidied = this->CompressModes(modelist,true);
570 strlcpy(modelist,tidied.c_str(),MAXBUF);
572 int len = tidied.length();
573 while (modelist[len-1] == ' ')
574 modelist[--len] = '\0';
576 for (char* modechar = modelist; *modechar; ptr++, modechar++)
586 int t = strlen(outlist)-1;
587 if ((outlist[t] == '+') || (outlist[t] == '-'))
603 int t = strlen(outlist)-1;
604 if ((outlist[t] == '+') || (outlist[t] == '-'))
618 if ((param >= pcnt)) break;
619 log(DEBUG,"Enough parameters left");
623 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
626 log(DEBUG,"calling GiveOps");
627 r = GiveOps(user,parameters[param++],chan,status);
634 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
637 log(DEBUG,"calling TakeOps");
638 r = TakeOps(user,parameters[param++],chan,status);
644 strlcat(outlist,"o",MAXBUF);
645 strlcpy(outpars[pc++],r,MAXBUF);
650 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
654 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
657 r = GiveHops(user,parameters[param++],chan,status);
664 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
667 r = TakeHops(user,parameters[param++],chan,status);
673 strlcat(outlist,"h",MAXBUF);
674 strlcpy(outpars[pc++],r,MAXBUF);
680 if ((param >= pcnt)) break;
684 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
687 r = GiveVoice(user,parameters[param++],chan,status);
694 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
697 r = TakeVoice(user,parameters[param++],chan,status);
703 strlcat(outlist,"v",MAXBUF);
704 strlcpy(outpars[pc++],r,MAXBUF);
709 if ((param >= pcnt)) break;
713 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
716 r = AddBan(user,parameters[param++],chan,status);
723 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
726 r = TakeBan(user,parameters[param++],chan,status);
732 strlcat(outlist,"b",MAXBUF);
733 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
747 if (previously_unset_k)
749 previously_set_k = true;
754 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
759 strlcpy(key,parameters[param++],32);
760 strlcpy(outpars[pc++],key,MAXBUF);
761 strlcpy(chan->key,key,MAXBUF);
769 /* checks on -k are case sensitive and only accurate to the
770 first 32 characters */
771 if (previously_set_k)
773 previously_unset_k = true;
777 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
780 strlcpy(key,parameters[param++],32);
781 /* only allow -k if correct key given */
782 if (!strcmp(chan->key,key))
784 strlcat(outlist,"k",MAXBUF);
785 strlcpy(chan->key,"",MAXBUF);
786 strlcpy(outpars[pc++],key,MAXBUF);
796 if (previously_set_l)
798 previously_unset_l = true;
800 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
811 if ((param >= pcnt)) break;
816 if (previously_unset_l)
818 previously_set_l = true;
819 bool invalid = false;
820 for (char* f = parameters[param]; *f; f++)
822 if ((*f < '0') || (*f > '9'))
827 /* If the limit is < 1, or the new limit is the current limit, dont allow */
828 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
837 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
841 chan->limit = atoi(parameters[param]);
843 // reported by mech: large values cause underflow
845 chan->limit = 0x7FFF;
850 strlcat(outlist,"l",MAXBUF);
851 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
859 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
864 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
865 chan->binarymodes |= CM_INVITEONLY;
869 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
870 chan->binarymodes &= ~CM_INVITEONLY;
877 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
882 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
883 chan->binarymodes |= CM_TOPICLOCK;
887 if (chan->binarymodes & CM_TOPICLOCK) strlcat(outlist,"t",MAXBUF);
888 chan->binarymodes &= ~CM_TOPICLOCK;
895 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
900 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
901 chan->binarymodes |= CM_NOEXTERNAL;
905 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
906 chan->binarymodes &= ~CM_NOEXTERNAL;
913 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
918 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
919 chan->binarymodes |= CM_MODERATED;
923 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
924 chan->binarymodes &= ~CM_MODERATED;
931 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
936 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
937 chan->binarymodes |= CM_SECRET;
938 if (chan->binarymodes & CM_PRIVATE)
940 chan->binarymodes &= ~CM_PRIVATE;
943 strlcat(outlist,"-p+",MAXBUF);
949 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
950 chan->binarymodes &= ~CM_SECRET;
957 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
962 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
963 chan->binarymodes |= CM_PRIVATE;
964 if (chan->binarymodes & CM_SECRET)
966 chan->binarymodes &= ~CM_SECRET;
969 strlcat(outlist,"-s+",MAXBUF);
975 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
976 chan->binarymodes &= ~CM_PRIVATE;
982 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",*modechar,chan->custom_modes);
985 if (((!strchr(chan->custom_modes,*modechar)) && (!mdir)) || ((strchr(chan->custom_modes,*modechar)) && (mdir)))
987 if (!ModeIsListMode(*modechar,MT_CHANNEL))
989 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
993 if (ModeDefined(*modechar,MT_CHANNEL))
995 log(DEBUG,"A module has claimed this mode");
998 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1000 p.push_back(parameters[param]);
1002 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
1004 p.push_back(parameters[param]);
1007 bool handled = false;
1010 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1011 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
1013 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
1019 // BIG ASS IDIOTIC CODER WARNING!
1020 // Using OnRawMode on another modules mode's behavour
1021 // will confuse the crap out of admins! just because you CAN
1022 // do it, doesnt mean you SHOULD!
1024 std::string para = "";
1027 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
1030 for (int i = 0; i <= MODCOUNT; i++)
1034 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
1037 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1038 char app[] = {*modechar, 0};
1039 if (ModeIsListMode(*modechar,MT_CHANNEL))
1050 strlcat(outlist, app,MAXBUF);
1052 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1059 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1061 strlcat(outlist, app,MAXBUF);
1063 else if (!strchr(outlist,*modechar))
1065 strlcat(outlist, app,MAXBUF);
1068 chan->SetCustomMode(*modechar,mdir);
1069 // include parameters in output if mode has them
1070 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1072 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1073 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1076 // break, because only one module can handle the mode.
1085 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
1093 /* this ensures only the *valid* modes are sent out onto the network */
1094 int xt = strlen(outlist)-1;
1095 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1098 xt = strlen(outlist)-1;
1102 strlcpy(outstr,outlist,MAXBUF);
1103 for (ptr = 0; ptr < pc; ptr++)
1105 strlcat(outstr," ",MAXBUF);
1106 strlcat(outstr,outpars[ptr],MAXBUF);
1110 log(DEBUG,"Local mode change");
1111 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1112 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outstr));
1120 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outstr);
1128 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1129 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outstr));
1136 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1138 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1140 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1141 // Servers can +o and -o arbitrarily
1142 if ((serveroverride == true) && (umode == 'o'))
1146 // RFC1459 specified modes
1147 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1149 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1153 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1154 if ((strchr(sourcemodes,'o')) && (!adding))
1156 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1159 else if (umode == 'o')
1161 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1165 // process any module-defined modes that need oper
1166 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1168 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1172 if (ModeDefined(umode,MT_CLIENT))
1174 // process any module-defined modes that don't need oper
1175 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1176 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1178 // no, this mode needs oper, and this user 'aint got what it takes!
1184 // anything else - return false.
1185 log(DEBUG,"umode %c not known by any ruleset",umode);
1189 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1196 strlcpy(s2->nick,Config->ServerName,NICKMAX);
1197 strlcpy(s2->modes,"o",52);
1204 if (ModeDefined(umode,MT_CLIENT))
1206 for (int i = 0; i <= MODCOUNT; i++)
1208 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1210 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1214 log(DEBUG,"No module claims umode %c",umode);
1233 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1239 char outpars[MAXBUF];
1241 dest = Find(parameters[0]);
1248 if ((dest) && (pcnt == 1))
1250 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1254 if ((dest) && (pcnt > 1))
1256 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1257 parameters[1] = (char*)tidied.c_str();
1259 char dmodes[MAXBUF];
1260 strlcpy(dmodes,dest->modes,52);
1261 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1266 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1277 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1285 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1288 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1290 if (parameters[1][i] == ' ')
1292 if (parameters[1][i] == '+')
1296 int t = strlen(outpars)-1;
1297 if ((outpars[t] == '+') || (outpars[t] == '-'))
1303 strcat(outpars,"+");
1309 if (parameters[1][i] == '-')
1313 int t = strlen(outpars)-1;
1314 if ((outpars[t] == '+') || (outpars[t] == '-'))
1320 strcat(outpars,"-");
1328 if (strchr(user->modes,'o'))
1334 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,direction,false)))
1343 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,false)))
1345 char umode = parameters[1][i];
1346 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1348 int q = strlen(dmodes);
1349 int r = strlen(outpars);
1351 dmodes[q] = parameters[1][i];
1353 outpars[r] = parameters[1][i];
1354 if (parameters[1][i] == 'o')
1356 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1363 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
1365 char umode = parameters[1][i];
1366 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1372 unsigned int r = strlen(outpars);
1374 outpars[r] = parameters[1][i];
1377 for (q = 0; q < strlen(dmodes); q++)
1379 if (dmodes[q] != parameters[1][i])
1383 strlcat(temp,moo,MAXBUF);
1386 strlcpy(dmodes,temp,52);
1402 strlcpy(b,"",MAXBUF);
1405 while (i < strlen (outpars))
1407 b[z++] = outpars[i++];
1409 if (i<strlen(outpars)-1)
1411 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1413 // someones playing silly buggers and trying
1414 // to put a +- or -+ into the line...
1418 if (i == strlen(outpars)-1)
1420 if ((outpars[i] == '-') || (outpars[i] == '+'))
1428 if ((b[z] == '-') || (b[z] == '+'))
1431 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1436 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1437 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1440 if (strlen(dmodes)>MAXMODES)
1442 dmodes[MAXMODES-1] = '\0';
1444 log(DEBUG,"Stripped mode line");
1445 log(DEBUG,"Line dest is now %s",dmodes);
1446 strlcpy(dest->modes,dmodes,52);
1453 Ptr = FindChan(parameters[0]);
1458 /* just /modes #channel */
1459 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr,has_channel(user,Ptr)));
1460 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1466 char* mode = parameters[1];
1470 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, Ptr, *mode, "", false, 0));
1476 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1478 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1480 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1483 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1485 // list of items for an extmode
1486 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1487 FOREACH_MOD(I_OnSendList,OnSendList(user,Ptr,*mode));
1493 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)) && (IS_LOCAL(user)))
1495 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1502 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1504 if (MOD_RESULT == ACR_DENY)
1506 if (MOD_RESULT == ACR_DEFAULT)
1508 if ((cstatus(user,Ptr) < STATUS_HOP) && (IS_LOCAL(user)))
1510 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1515 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1520 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1527 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1533 char outpars[MAXBUF];
1535 dest = Find(parameters[0]);
1537 // fix: ChroNiCk found this - we cant use this as debug if its null!
1540 log(DEBUG,"server_mode on %s",dest->nick);
1543 if ((dest) && (pcnt > 1))
1545 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1546 parameters[1] = (char*)tidied.c_str();
1548 char dmodes[MAXBUF];
1549 strlcpy(dmodes,dest->modes,52);
1555 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1558 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1560 if (parameters[1][i] == ' ')
1562 if (parameters[1][i] == '+')
1566 int t = strlen(outpars)-1;
1567 if ((outpars[t] == '+') || (outpars[t] == '-'))
1573 strcat(outpars,"+");
1579 if (parameters[1][i] == '-')
1583 int t = strlen(outpars)-1;
1584 if ((outpars[t] == '+') || (outpars[t] == '-'))
1590 strcat(outpars,"-");
1597 log(DEBUG,"begin mode processing entry");
1603 log(DEBUG,"umode %c being added",parameters[1][i]);
1604 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,true)))
1606 char umode = parameters[1][i];
1607 log(DEBUG,"umode %c is an allowed umode",umode);
1608 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1610 int v1 = strlen(dmodes);
1611 int v2 = strlen(outpars);
1613 dmodes[v1] = parameters[1][i];
1615 outpars[v2] = parameters[1][i];
1621 // can only remove a mode they already have
1622 log(DEBUG,"umode %c being removed",parameters[1][i]);
1623 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,true)) && (strchr(dmodes,parameters[1][i])))
1625 char umode = parameters[1][i];
1626 log(DEBUG,"umode %c is an allowed umode",umode);
1627 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1633 unsigned int v1 = strlen(outpars);
1635 outpars[v1] = parameters[1][i];
1637 for (q = 0; q < strlen(dmodes); q++)
1639 if (dmodes[q] != parameters[1][i])
1643 strlcat(temp,moo,MAXBUF);
1646 strlcpy(dmodes,temp,52);
1656 strlcpy(b,"",MAXBUF);
1659 while (i < strlen (outpars))
1661 b[z++] = outpars[i++];
1663 if (i<strlen(outpars)-1)
1665 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1667 // someones playing silly buggers and trying
1668 // to put a +- or -+ into the line...
1672 if (i == strlen(outpars)-1)
1674 if ((outpars[i] == '-') || (outpars[i] == '+'))
1682 if ((b[z] == '-') || (b[z] == '+'))
1685 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1690 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1691 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1694 if (strlen(dmodes)>MAXMODES)
1696 dmodes[MAXMODES-1] = '\0';
1698 log(DEBUG,"Stripped mode line");
1699 log(DEBUG,"Line dest is now %s",dmodes);
1700 strlcpy(dest->modes,dmodes,MAXMODES);
1707 Ptr = FindChan(parameters[0]);
1710 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1714 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);