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"
22 #include "inspircd_util.h"
24 #include <sys/errno.h>
28 #include <ext/hash_map>
36 #include "connection.h"
46 #include "inspstring.h"
47 #include "helperfuncs.h"
50 extern std::vector<Module*> modules;
51 extern std::vector<ircd_module*> factory;
52 extern ServerConfig* Config;
56 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
60 if ((!user) || (!dest) || (!chan))
62 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
68 log(DEFAULT,"the target nickname given to give_ops was invalid");
69 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
75 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
76 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
83 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
85 if (MOD_RESULT == ACR_DENY)
87 if (MOD_RESULT == ACR_DEFAULT)
89 if ((status < STATUS_OP) && (!is_uline(user->server)))
91 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
92 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
98 for (unsigned int i = 0; i < d->chans.size(); i++)
100 if ((d->chans[i].channel != NULL) && (chan != NULL))
101 if (!strcasecmp(d->chans[i].channel->name,chan->name))
103 if (d->chans[i].uc_modes & UCMODE_OP)
105 /* mode already set on user, dont allow multiple */
106 log(DEFAULT,"The target user given to give_ops was already opped on the channel");
109 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
110 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
114 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
119 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
123 if ((!user) || (!dest) || (!chan))
125 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
132 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
137 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
143 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
145 if (MOD_RESULT == ACR_DENY)
147 if (MOD_RESULT == ACR_DEFAULT)
149 if ((status < STATUS_OP) && (!is_uline(user->server)))
151 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
156 for (unsigned int i = 0; i < d->chans.size(); i++)
158 if ((d->chans[i].channel != NULL) && (chan != NULL))
159 if (!strcasecmp(d->chans[i].channel->name,chan->name))
161 if (d->chans[i].uc_modes & UCMODE_HOP)
163 /* mode already set on user, dont allow multiple */
166 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
167 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
175 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
179 if ((!user) || (!dest) || (!chan))
181 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
188 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
193 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
199 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
201 if (MOD_RESULT == ACR_DENY)
203 if (MOD_RESULT == ACR_DEFAULT)
205 if ((status < STATUS_HOP) && (!is_uline(user->server)))
207 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
212 for (unsigned int i = 0; i < d->chans.size(); i++)
214 if ((d->chans[i].channel != NULL) && (chan != NULL))
215 if (!strcasecmp(d->chans[i].channel->name,chan->name))
217 if (d->chans[i].uc_modes & UCMODE_VOICE)
219 /* mode already set on user, dont allow multiple */
222 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
223 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
231 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
235 if ((!user) || (!dest) || (!chan))
237 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
244 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
245 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
250 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
251 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
257 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
259 if (MOD_RESULT == ACR_DENY)
261 if (MOD_RESULT == ACR_DEFAULT)
263 if ((status < STATUS_OP) && (!is_uline(user->server)))
265 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
270 for (unsigned int i = 0; i < d->chans.size(); i++)
272 if ((d->chans[i].channel != NULL) && (chan != NULL))
273 if (!strcasecmp(d->chans[i].channel->name,chan->name))
275 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
277 /* mode already set on user, dont allow multiple */
280 d->chans[i].uc_modes ^= UCMODE_OP;
281 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
285 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
290 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
294 if ((!user) || (!dest) || (!chan))
296 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
303 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
308 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
314 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
316 if (MOD_RESULT == ACR_DENY)
318 if (MOD_RESULT == ACR_DEFAULT)
320 if ((status < STATUS_OP) && (!is_uline(user->server)))
322 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
327 for (unsigned int i = 0; i < d->chans.size(); i++)
329 if ((d->chans[i].channel != NULL) && (chan != NULL))
330 if (!strcasecmp(d->chans[i].channel->name,chan->name))
332 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
334 /* mode already set on user, dont allow multiple */
337 d->chans[i].uc_modes ^= UCMODE_HOP;
338 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
346 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
350 if ((!user) || (!dest) || (!chan))
352 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
359 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
364 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
370 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
372 if (MOD_RESULT == ACR_DENY)
374 if (MOD_RESULT == ACR_DEFAULT)
376 if ((status < STATUS_HOP) && (!is_uline(user->server)))
378 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
383 for (unsigned int i = 0; i < d->chans.size(); i++)
385 if ((d->chans[i].channel != NULL) && (chan != NULL))
386 if (!strcasecmp(d->chans[i].channel->name,chan->name))
388 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
390 /* mode already set on user, dont allow multiple */
393 d->chans[i].uc_modes ^= UCMODE_VOICE;
394 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
402 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
404 if ((!user) || (!dest) || (!chan)) {
405 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
410 if ((!user) || (!dest) || (!chan))
412 unsigned int l = strlen(dest);
413 if (strchr(dest,'!')==0)
415 if (strchr(dest,'@')==0)
417 for (unsigned int i = 0; i < l; i++)
420 for (unsigned int i = 0; i < l; i++)
424 for (unsigned int i = 0; i < l; i++)
430 for (unsigned int i = 0; i < l; i++)
436 long maxbans = GetMaxBans(chan->name);
437 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
439 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);
443 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
446 FOREACH_RESULT(OnAddBan(user,chan,dest));
451 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
453 if (!strcasecmp(i->data,dest))
455 // dont allow a user to set the same ban twice
461 strncpy(b.data,dest,MAXBUF);
462 strncpy(b.set_by,user->nick,NICKMAX);
463 chan->bans.push_back(b);
467 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
469 if ((!user) || (!dest) || (!chan)) {
470 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
474 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
475 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
477 if (!strcasecmp(i->data,dest))
480 FOREACH_RESULT(OnDelBan(user,chan,dest));
490 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
491 // a section further down the chain tidies up the +-+- crap.
492 std::string compress_modes(std::string modes,bool channelmodes)
496 memset(counts,0,sizeof(counts));
497 memset(active,0,sizeof(active));
498 log(DEBUG,"compress_modes: %s",modes.c_str());
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 process_modes(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 = compress_modes(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 give_ops");
639 r = give_ops(user,parameters[param++],chan,status);
646 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
649 log(DEBUG,"calling take_ops");
650 r = take_ops(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 = give_hops(user,parameters[param++],chan,status);
676 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
679 r = take_hops(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 = give_voice(user,parameters[param++],chan,status);
706 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
709 r = take_voice(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 = add_ban(user,parameters[param++],chan,status);
735 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
738 r = take_ban(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 allowed_umode(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 process_module_umode(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 handle_mode(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 = compress_modes(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') || (allowed_umode(parameters[1][i],user->modes,direction,false)))
1353 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,false)))
1355 char umode = parameters[1][i];
1356 if ((process_module_umode(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 ((allowed_umode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
1375 char umode = parameters[1][i];
1376 if ((process_module_umode(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 process_modes(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 server_mode(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 = compress_modes(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])) && (allowed_umode(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 ((process_module_umode(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 ((allowed_umode(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 ((process_module_umode(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 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1721 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1727 void merge_mode(char **parameters, int pcnt)
1733 char outpars[MAXBUF];
1735 dest = Find(parameters[0]);
1737 // fix: ChroNiCk found this - we cant use this as debug if its null!
1740 log(DEBUG,"merge_mode on %s",dest->nick);
1743 if ((dest) && (pcnt > 1))
1745 std::string tidied = compress_modes(parameters[1],false);
1746 parameters[1] = (char*)tidied.c_str();
1748 char dmodes[MAXBUF];
1749 strlcpy(dmodes,dest->modes,52);
1751 strcpy(outpars,"+");
1754 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1757 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1759 if (parameters[1][i] == ' ')
1761 if (parameters[1][i] == '+')
1765 int t = strlen(outpars)-1;
1766 if ((outpars[t] == '+') || (outpars[t] == '-'))
1772 strcat(outpars,"+");
1778 if (parameters[1][i] == '-')
1782 int t = strlen(outpars)-1;
1783 if ((outpars[t] == '+') || (outpars[t] == '-'))
1789 strcat(outpars,"-");
1796 log(DEBUG,"begin mode processing entry");
1802 log(DEBUG,"umode %c being added",parameters[1][i]);
1803 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true,true)))
1805 char umode = parameters[1][i];
1806 log(DEBUG,"umode %c is an allowed umode",umode);
1807 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1809 int v1 = strlen(dmodes);
1810 int v2 = strlen(outpars);
1812 dmodes[v1] = parameters[1][i];
1814 outpars[v2] = parameters[1][i];
1820 // can only remove a mode they already have
1821 log(DEBUG,"umode %c being removed",parameters[1][i]);
1822 if ((allowed_umode(parameters[1][i],"o",false,true)) && (strchr(dmodes,parameters[1][i])))
1824 char umode = parameters[1][i];
1825 log(DEBUG,"umode %c is an allowed umode",umode);
1826 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1832 unsigned int v1 = strlen(outpars);
1834 outpars[v1] = parameters[1][i];
1837 for (q = 0; q < strlen(dmodes); q++)
1839 if (dmodes[q] != parameters[1][i])
1843 strlcat(temp,moo,MAXBUF);
1846 strlcpy(dmodes,temp,52);
1859 while (i < strlen (outpars))
1861 b[z++] = outpars[i++];
1863 if (i<strlen(outpars)-1)
1865 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1867 // someones playing silly buggers and trying
1868 // to put a +- or -+ into the line...
1872 if (i == strlen(outpars)-1)
1874 if ((outpars[i] == '-') || (outpars[i] == '+'))
1882 if ((b[z] == '-') || (b[z] == '+'))
1885 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1888 if (strlen(dmodes)>MAXMODES)
1890 dmodes[MAXMODES-1] = '\0';
1892 log(DEBUG,"Stripped mode line");
1893 log(DEBUG,"Line dest is now %s",dmodes);
1894 strlcpy(dest->modes,dmodes,MAXMODES);
1901 Ptr = FindChan(parameters[0]);
1905 strlcpy(s2.nick,Config->ServerName,NICKMAX);
1906 strcpy(s2.modes,"o");
1908 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1913 void merge_mode2(char **parameters, int pcnt, userrec* user)
1919 char outpars[MAXBUF];
1921 dest = Find(parameters[0]);
1923 // fix: ChroNiCk found this - we cant use this as debug if its null!
1926 log(DEBUG,"merge_mode2 on %s",dest->nick);
1929 if ((dest) && (pcnt > 1))
1931 std::string tidied = compress_modes(parameters[1],false);
1932 parameters[1] = (char*)tidied.c_str();
1934 char dmodes[MAXBUF];
1935 strlcpy(dmodes,dest->modes,52);
1937 strcpy(outpars,"+");
1940 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1942 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1943 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1945 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1948 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1950 if (parameters[1][i] == ' ')
1952 if (parameters[1][i] == '+')
1956 int t = strlen(outpars)-1;
1957 if ((outpars[t] == '+') || (outpars[t] == '-'))
1963 strcat(outpars,"+");
1969 if (parameters[1][i] == '-')
1973 int t = strlen(outpars)-1;
1974 if ((outpars[t] == '+') || (outpars[t] == '-'))
1980 strcat(outpars,"-");
1987 log(DEBUG,"begin mode processing entry");
1993 log(DEBUG,"umode %c being added",parameters[1][i]);
1994 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,false)))
1996 char umode = parameters[1][i];
1997 log(DEBUG,"umode %c is an allowed umode",umode);
1998 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2000 int v1 = strlen(dmodes);
2001 int v2 = strlen(outpars);
2003 dmodes[v1] = parameters[1][i];
2005 outpars[v2] = parameters[1][i];
2006 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2012 // can only remove a mode they already have
2013 log(DEBUG,"umode %c being removed",parameters[1][i]);
2014 if ((allowed_umode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
2016 char umode = parameters[1][i];
2017 log(DEBUG,"umode %c is an allowed umode",umode);
2018 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2024 unsigned int v1 = strlen(outpars);
2026 outpars[v1] = parameters[1][i];
2029 for (q = 0; q < strlen(dmodes); q++)
2031 if (dmodes[q] != parameters[1][i])
2035 strlcat(temp,moo,MAXBUF);
2038 strlcpy(dmodes,temp,52);
2045 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2052 while (i < strlen (outpars))
2054 b[z++] = outpars[i++];
2056 if (i<strlen(outpars)-1)
2058 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2060 // someones playing silly buggers and trying
2061 // to put a +- or -+ into the line...
2065 if (i == strlen(outpars)-1)
2067 if ((outpars[i] == '-') || (outpars[i] == '+'))
2075 if ((b[z] == '-') || (b[z] == '+'))
2079 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
2084 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2085 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
2088 if (strlen(dmodes)>MAXMODES)
2090 dmodes[MAXMODES-1] = '\0';
2092 log(DEBUG,"Stripped mode line");
2093 log(DEBUG,"Line dest is now %s",dmodes);
2094 strlcpy(dest->modes,dmodes,MAXMODES);
2100 Ptr = FindChan(parameters[0]);
2103 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2106 //if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2110 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);