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 std::vector<std::string> module_names;
54 extern ServerConfig* Config;
58 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
62 if ((!user) || (!dest) || (!chan))
64 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
70 log(DEFAULT,"the target nickname given to give_ops was invalid");
71 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
77 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
78 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
85 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
87 if (MOD_RESULT == ACR_DENY)
89 if (MOD_RESULT == ACR_DEFAULT)
91 if ((status < STATUS_OP) && (!is_uline(user->server)))
93 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
94 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
100 for (unsigned int i = 0; i < d->chans.size(); i++)
102 if ((d->chans[i].channel != NULL) && (chan != NULL))
103 if (!strcasecmp(d->chans[i].channel->name,chan->name))
105 if (d->chans[i].uc_modes & UCMODE_OP)
107 /* mode already set on user, dont allow multiple */
108 log(DEFAULT,"The target user given to give_ops was already opped on the channel");
111 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
112 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
116 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
121 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
125 if ((!user) || (!dest) || (!chan))
127 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
134 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
139 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
145 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
147 if (MOD_RESULT == ACR_DENY)
149 if (MOD_RESULT == ACR_DEFAULT)
151 if ((status < STATUS_OP) && (!is_uline(user->server)))
153 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
158 for (unsigned int i = 0; i < d->chans.size(); i++)
160 if ((d->chans[i].channel != NULL) && (chan != NULL))
161 if (!strcasecmp(d->chans[i].channel->name,chan->name))
163 if (d->chans[i].uc_modes & UCMODE_HOP)
165 /* mode already set on user, dont allow multiple */
168 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
169 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
177 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
181 if ((!user) || (!dest) || (!chan))
183 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
190 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
195 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
201 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
203 if (MOD_RESULT == ACR_DENY)
205 if (MOD_RESULT == ACR_DEFAULT)
207 if ((status < STATUS_HOP) && (!is_uline(user->server)))
209 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
214 for (unsigned int i = 0; i < d->chans.size(); i++)
216 if ((d->chans[i].channel != NULL) && (chan != NULL))
217 if (!strcasecmp(d->chans[i].channel->name,chan->name))
219 if (d->chans[i].uc_modes & UCMODE_VOICE)
221 /* mode already set on user, dont allow multiple */
224 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
225 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
233 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
237 if ((!user) || (!dest) || (!chan))
239 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
246 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
247 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
252 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
253 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
259 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
261 if (MOD_RESULT == ACR_DENY)
263 if (MOD_RESULT == ACR_DEFAULT)
265 if ((status < STATUS_OP) && (!is_uline(user->server)))
267 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
272 for (unsigned int i = 0; i < d->chans.size(); i++)
274 if ((d->chans[i].channel != NULL) && (chan != NULL))
275 if (!strcasecmp(d->chans[i].channel->name,chan->name))
277 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
279 /* mode already set on user, dont allow multiple */
282 d->chans[i].uc_modes ^= UCMODE_OP;
283 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
287 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
292 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
296 if ((!user) || (!dest) || (!chan))
298 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
305 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
310 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
316 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
318 if (MOD_RESULT == ACR_DENY)
320 if (MOD_RESULT == ACR_DEFAULT)
322 if ((status < STATUS_OP) && (!is_uline(user->server)))
324 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
329 for (unsigned int i = 0; i < d->chans.size(); i++)
331 if ((d->chans[i].channel != NULL) && (chan != NULL))
332 if (!strcasecmp(d->chans[i].channel->name,chan->name))
334 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
336 /* mode already set on user, dont allow multiple */
339 d->chans[i].uc_modes ^= UCMODE_HOP;
340 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
348 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
352 if ((!user) || (!dest) || (!chan))
354 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
361 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
366 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
372 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
374 if (MOD_RESULT == ACR_DENY)
376 if (MOD_RESULT == ACR_DEFAULT)
378 if ((status < STATUS_HOP) && (!is_uline(user->server)))
380 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
385 for (unsigned int i = 0; i < d->chans.size(); i++)
387 if ((d->chans[i].channel != NULL) && (chan != NULL))
388 if (!strcasecmp(d->chans[i].channel->name,chan->name))
390 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
392 /* mode already set on user, dont allow multiple */
395 d->chans[i].uc_modes ^= UCMODE_VOICE;
396 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
404 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
406 if ((!user) || (!dest) || (!chan)) {
407 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
412 if ((!user) || (!dest) || (!chan))
414 unsigned int l = strlen(dest);
415 if (strchr(dest,'!')==0)
417 if (strchr(dest,'@')==0)
419 for (unsigned int i = 0; i < l; i++)
422 for (unsigned int i = 0; i < l; i++)
426 for (unsigned int i = 0; i < l; i++)
432 for (unsigned int i = 0; i < l; i++)
438 long maxbans = GetMaxBans(chan->name);
439 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
441 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);
445 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
448 FOREACH_RESULT(OnAddBan(user,chan,dest));
453 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
455 if (!strcasecmp(i->data,dest))
457 // dont allow a user to set the same ban twice
463 strncpy(b.data,dest,MAXBUF);
464 strncpy(b.set_by,user->nick,NICKMAX);
465 chan->bans.push_back(b);
469 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
471 if ((!user) || (!dest) || (!chan)) {
472 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
476 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
477 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
479 if (!strcasecmp(i->data,dest))
482 FOREACH_RESULT(OnDelBan(user,chan,dest));
492 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
493 // a section further down the chain tidies up the +-+- crap.
494 std::string compress_modes(std::string modes,bool channelmodes)
498 memset(counts,0,sizeof(counts));
499 memset(active,0,sizeof(active));
500 log(DEBUG,"compress_modes: %s",modes.c_str());
501 for (unsigned int i = 0; i < modes.length(); i++)
503 if ((modes[i] == '+') || (modes[i] == '-'))
507 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
509 log(DEBUG,"Tidy mode %c",modes[i]);
510 counts[(unsigned int)modes[i]]++;
511 active[(unsigned int)modes[i]] = true;
516 log(DEBUG,"Tidy mode %c",modes[i]);
517 counts[(unsigned int)modes[i]]++;
518 active[(unsigned int)modes[i]] = true;
521 for (int j = 65; j < 127; j++)
523 if ((counts[j] > 1) && (active[j] == true))
526 v[0] = (unsigned char)j;
528 std::string mode_str = v;
529 std::string::size_type pos = modes.find(mode_str);
530 if (pos != std::string::npos)
532 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
533 while (modes.find(mode_str) != std::string::npos)
534 modes.erase(modes.find(mode_str),1);
535 log(DEBUG,"New mode line: %s",modes.c_str());
542 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
545 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
549 char modelist[MAXBUF];
550 char outlist[MAXBUF];
552 char outpars[32][MAXBUF];
558 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
566 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
568 if (MOD_RESULT == ACR_DENY)
571 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
573 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
574 * parameters[2] onwards are parameters for
575 * modes that require them :) */
576 strlcpy(outlist,"+",MAXBUF);
579 log(DEBUG,"process_modes: modelist: %s",modelist);
581 std::string tidied = compress_modes(modelist,true);
582 strlcpy(modelist,tidied.c_str(),MAXBUF);
584 int len = strlen(modelist);
585 while (modelist[len-1] == ' ')
586 modelist[--len] = '\0';
587 for (ptr = 0; ptr < len; ptr++)
592 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
594 char modechar = modelist[ptr];
595 switch (modelist[ptr])
600 int t = strlen(outlist)-1;
601 if ((outlist[t] == '+') || (outlist[t] == '-'))
617 int t = strlen(outlist)-1;
618 if ((outlist[t] == '+') || (outlist[t] == '-'))
632 if ((param >= pcnt)) break;
633 log(DEBUG,"Enough parameters left");
637 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
640 log(DEBUG,"calling give_ops");
641 r = give_ops(user,parameters[param++],chan,status);
648 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
651 log(DEBUG,"calling take_ops");
652 r = take_ops(user,parameters[param++],chan,status);
658 strlcat(outlist,"o",MAXBUF);
659 strlcpy(outpars[pc++],r,MAXBUF);
664 if (((param >= pcnt)) || (!AllowHalfop)) break;
668 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
671 r = give_hops(user,parameters[param++],chan,status);
678 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
681 r = take_hops(user,parameters[param++],chan,status);
687 strlcat(outlist,"h",MAXBUF);
688 strlcpy(outpars[pc++],r,MAXBUF);
694 if ((param >= pcnt)) break;
698 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
701 r = give_voice(user,parameters[param++],chan,status);
708 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
711 r = take_voice(user,parameters[param++],chan,status);
717 strlcat(outlist,"v",MAXBUF);
718 strlcpy(outpars[pc++],r,MAXBUF);
723 if ((param >= pcnt)) break;
727 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
730 r = add_ban(user,parameters[param++],chan,status);
737 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
740 r = take_ban(user,parameters[param++],chan,status);
746 strlcat(outlist,"b",MAXBUF);
747 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
761 if (previously_unset_k)
763 previously_set_k = true;
765 if (!strcmp(chan->key,""))
768 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
773 strlcpy(key,parameters[param++],32);
774 strlcpy(outpars[pc++],key,MAXBUF);
775 strlcpy(chan->key,key,MAXBUF);
783 /* checks on -k are case sensitive and only accurate to the
784 first 32 characters */
785 if (previously_set_k)
787 previously_unset_k = true;
791 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
794 strlcpy(key,parameters[param++],32);
795 /* only allow -k if correct key given */
796 if (!strcmp(chan->key,key))
798 strlcat(outlist,"k",MAXBUF);
799 strlcpy(chan->key,"",MAXBUF);
800 strlcpy(outpars[pc++],key,MAXBUF);
810 if (previously_set_l)
812 previously_unset_l = true;
814 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
825 if ((param >= pcnt)) break;
830 if (previously_unset_l)
832 previously_set_l = true;
833 bool invalid = false;
834 for (unsigned int i = 0; i < strlen(parameters[param]); i++)
836 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
841 if (atoi(parameters[param]) < 1)
850 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
854 chan->limit = atoi(parameters[param]);
856 // reported by mech: large values cause underflow
858 chan->limit = 0x7FFF;
863 strlcat(outlist,"l",MAXBUF);
864 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
872 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
877 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
878 chan->binarymodes |= CM_INVITEONLY;
882 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
883 chan->binarymodes &= ~CM_INVITEONLY;
890 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
895 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
896 chan->binarymodes |= CM_TOPICLOCK;
900 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"t",MAXBUF);
901 chan->binarymodes &= ~CM_TOPICLOCK;
908 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
913 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
914 chan->binarymodes |= CM_NOEXTERNAL;
918 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
919 chan->binarymodes &= ~CM_NOEXTERNAL;
926 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
931 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
932 chan->binarymodes |= CM_MODERATED;
936 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
937 chan->binarymodes &= ~CM_MODERATED;
944 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
949 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
950 chan->binarymodes |= CM_SECRET;
951 if (chan->binarymodes & CM_PRIVATE)
953 chan->binarymodes &= ~CM_PRIVATE;
956 strlcat(outlist,"-p+",MAXBUF);
962 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
963 chan->binarymodes &= ~CM_SECRET;
970 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
975 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
976 chan->binarymodes |= CM_PRIVATE;
977 if (chan->binarymodes & CM_SECRET)
979 chan->binarymodes &= ~CM_SECRET;
982 strlcat(outlist,"-s+",MAXBUF);
988 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
989 chan->binarymodes &= ~CM_PRIVATE;
995 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
998 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
1000 if (!ModeIsListMode(modechar,MT_CHANNEL))
1002 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1006 if (ModeDefined(modechar,MT_CHANNEL))
1008 log(DEBUG,"A module has claimed this mode");
1011 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1013 p.push_back(parameters[param]);
1015 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1017 p.push_back(parameters[param]);
1020 bool handled = false;
1023 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1024 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1026 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1032 // BIG ASS IDIOTIC CODER WARNING!
1033 // Using OnRawMode on another modules mode's behavour
1034 // will confuse the crap out of admins! just because you CAN
1035 // do it, doesnt mean you SHOULD!
1037 std::string para = "";
1040 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1043 for (int i = 0; i <= MODCOUNT; i++)
1047 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1050 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1051 char app[] = {modechar, 0};
1052 if (ModeIsListMode(modechar,MT_CHANNEL))
1063 strlcat(outlist, app,MAXBUF);
1065 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1072 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1074 strlcat(outlist, app,MAXBUF);
1076 else if (!strchr(outlist,modechar))
1078 strlcat(outlist, app,MAXBUF);
1081 chan->SetCustomMode(modechar,mdir);
1082 // include parameters in output if mode has them
1083 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1085 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1086 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1089 // break, because only one module can handle the mode.
1098 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1106 /* this ensures only the *valid* modes are sent out onto the network */
1107 int xt = strlen(outlist)-1;
1108 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1111 xt = strlen(outlist)-1;
1115 strlcpy(outstr,outlist,MAXBUF);
1116 for (ptr = 0; ptr < pc; ptr++)
1118 strlcat(outstr," ",MAXBUF);
1119 strlcat(outstr,outpars[ptr],MAXBUF);
1123 log(DEBUG,"Local mode change");
1124 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1125 FOREACH_MOD OnMode(user, chan, TYPE_CHANNEL, outstr);
1133 WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1141 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1142 FOREACH_MOD OnMode(user, chan, TYPE_CHANNEL, outstr);
1149 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1151 bool allowed_umode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1153 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1154 // Servers can +o and -o arbitrarily
1155 if ((serveroverride == true) && (umode == 'o'))
1159 // RFC1459 specified modes
1160 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1162 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1166 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1167 if ((strchr(sourcemodes,'o')) && (!adding))
1169 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1172 else if (umode == 'o')
1174 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1178 // process any module-defined modes that need oper
1179 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1181 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1185 if (ModeDefined(umode,MT_CLIENT))
1187 // process any module-defined modes that don't need oper
1188 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1189 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1191 // no, this mode needs oper, and this user 'aint got what it takes!
1197 // anything else - return false.
1198 log(DEBUG,"umode %c not known by any ruleset",umode);
1202 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1209 strlcpy(s2->nick,ServerName,NICKMAX);
1210 strlcpy(s2->modes,"o",52);
1217 if (ModeDefined(umode,MT_CLIENT))
1219 for (int i = 0; i <= MODCOUNT; i++)
1221 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1223 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1227 log(DEBUG,"No module claims umode %c",umode);
1246 void handle_mode(char **parameters, int pcnt, userrec *user)
1252 char outpars[MAXBUF];
1254 dest = Find(parameters[0]);
1261 if ((dest) && (pcnt == 1))
1263 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1267 if ((dest) && (pcnt > 1))
1269 std::string tidied = compress_modes(parameters[1],false);
1270 parameters[1] = (char*)tidied.c_str();
1272 char dmodes[MAXBUF];
1273 strlcpy(dmodes,dest->modes,52);
1274 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1279 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1290 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1294 strcpy(outpars,"+");
1297 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1300 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1302 if (parameters[1][i] == ' ')
1304 if (parameters[1][i] == '+')
1308 int t = strlen(outpars)-1;
1309 if ((outpars[t] == '+') || (outpars[t] == '-'))
1315 strcat(outpars,"+");
1321 if (parameters[1][i] == '-')
1325 int t = strlen(outpars)-1;
1326 if ((outpars[t] == '+') || (outpars[t] == '-'))
1332 strcat(outpars,"-");
1340 if (strchr(user->modes,'o'))
1346 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction,false)))
1355 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,false)))
1357 char umode = parameters[1][i];
1358 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1360 int q = strlen(dmodes);
1361 int r = strlen(outpars);
1363 dmodes[q] = parameters[1][i];
1365 outpars[r] = parameters[1][i];
1366 if (parameters[1][i] == 'o')
1368 FOREACH_MOD OnGlobalOper(dest);
1375 if ((allowed_umode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
1377 char umode = parameters[1][i];
1378 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1384 unsigned int r = strlen(outpars);
1386 outpars[r] = parameters[1][i];
1389 for (q = 0; q < strlen(dmodes); q++)
1391 if (dmodes[q] != parameters[1][i])
1395 strlcat(temp,moo,MAXBUF);
1398 strlcpy(dmodes,temp,52);
1411 strlcpy(b,"",MAXBUF);
1414 while (i < strlen (outpars))
1416 b[z++] = outpars[i++];
1418 if (i<strlen(outpars)-1)
1420 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1422 // someones playing silly buggers and trying
1423 // to put a +- or -+ into the line...
1427 if (i == strlen(outpars)-1)
1429 if ((outpars[i] == '-') || (outpars[i] == '+'))
1437 if ((b[z] == '-') || (b[z] == '+'))
1440 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1445 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1446 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
1449 if (strlen(dmodes)>MAXMODES)
1451 dmodes[MAXMODES-1] = '\0';
1453 log(DEBUG,"Stripped mode line");
1454 log(DEBUG,"Line dest is now %s",dmodes);
1455 strlcpy(dest->modes,dmodes,52);
1462 Ptr = FindChan(parameters[0]);
1467 /* just /modes #channel */
1468 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1469 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1475 char* mode = parameters[1];
1479 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1485 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1487 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1489 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1492 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1494 // list of items for an extmode
1495 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1496 FOREACH_MOD OnSendList(user,Ptr,*mode);
1502 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)))
1504 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1511 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1513 if (MOD_RESULT == ACR_DENY)
1515 if (MOD_RESULT == ACR_DEFAULT)
1517 if (cstatus(user,Ptr) < STATUS_HOP)
1519 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1524 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1529 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1536 void server_mode(char **parameters, int pcnt, userrec *user)
1542 char outpars[MAXBUF];
1544 dest = Find(parameters[0]);
1546 // fix: ChroNiCk found this - we cant use this as debug if its null!
1549 log(DEBUG,"server_mode on %s",dest->nick);
1552 if ((dest) && (pcnt > 1))
1554 std::string tidied = compress_modes(parameters[1],false);
1555 parameters[1] = (char*)tidied.c_str();
1557 char dmodes[MAXBUF];
1558 strlcpy(dmodes,dest->modes,52);
1560 strcpy(outpars,"+");
1563 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1566 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1568 if (parameters[1][i] == ' ')
1570 if (parameters[1][i] == '+')
1574 int t = strlen(outpars)-1;
1575 if ((outpars[t] == '+') || (outpars[t] == '-'))
1581 strcat(outpars,"+");
1587 if (parameters[1][i] == '-')
1591 int t = strlen(outpars)-1;
1592 if ((outpars[t] == '+') || (outpars[t] == '-'))
1598 strcat(outpars,"-");
1605 log(DEBUG,"begin mode processing entry");
1611 log(DEBUG,"umode %c being added",parameters[1][i]);
1612 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,true)))
1614 char umode = parameters[1][i];
1615 log(DEBUG,"umode %c is an allowed umode",umode);
1616 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1618 int v1 = strlen(dmodes);
1619 int v2 = strlen(outpars);
1621 dmodes[v1] = parameters[1][i];
1623 outpars[v2] = parameters[1][i];
1629 // can only remove a mode they already have
1630 log(DEBUG,"umode %c being removed",parameters[1][i]);
1631 if ((allowed_umode(parameters[1][i],user->modes,false,true)) && (strchr(dmodes,parameters[1][i])))
1633 char umode = parameters[1][i];
1634 log(DEBUG,"umode %c is an allowed umode",umode);
1635 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1641 unsigned int v1 = strlen(outpars);
1643 outpars[v1] = parameters[1][i];
1646 for (q = 0; q < strlen(dmodes); q++)
1648 if (dmodes[q] != parameters[1][i])
1652 strlcat(temp,moo,MAXBUF);
1655 strlcpy(dmodes,temp,52);
1665 strlcpy(b,"",MAXBUF);
1668 while (i < strlen (outpars))
1670 b[z++] = outpars[i++];
1672 if (i<strlen(outpars)-1)
1674 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1676 // someones playing silly buggers and trying
1677 // to put a +- or -+ into the line...
1681 if (i == strlen(outpars)-1)
1683 if ((outpars[i] == '-') || (outpars[i] == '+'))
1691 if ((b[z] == '-') || (b[z] == '+'))
1694 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1699 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1700 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
1703 if (strlen(dmodes)>MAXMODES)
1705 dmodes[MAXMODES-1] = '\0';
1707 log(DEBUG,"Stripped mode line");
1708 log(DEBUG,"Line dest is now %s",dmodes);
1709 strlcpy(dest->modes,dmodes,MAXMODES);
1716 Ptr = FindChan(parameters[0]);
1719 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1723 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1729 void merge_mode(char **parameters, int pcnt)
1735 char outpars[MAXBUF];
1737 dest = Find(parameters[0]);
1739 // fix: ChroNiCk found this - we cant use this as debug if its null!
1742 log(DEBUG,"merge_mode on %s",dest->nick);
1745 if ((dest) && (pcnt > 1))
1747 std::string tidied = compress_modes(parameters[1],false);
1748 parameters[1] = (char*)tidied.c_str();
1750 char dmodes[MAXBUF];
1751 strlcpy(dmodes,dest->modes,52);
1753 strcpy(outpars,"+");
1756 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1759 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1761 if (parameters[1][i] == ' ')
1763 if (parameters[1][i] == '+')
1767 int t = strlen(outpars)-1;
1768 if ((outpars[t] == '+') || (outpars[t] == '-'))
1774 strcat(outpars,"+");
1780 if (parameters[1][i] == '-')
1784 int t = strlen(outpars)-1;
1785 if ((outpars[t] == '+') || (outpars[t] == '-'))
1791 strcat(outpars,"-");
1798 log(DEBUG,"begin mode processing entry");
1804 log(DEBUG,"umode %c being added",parameters[1][i]);
1805 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true,true)))
1807 char umode = parameters[1][i];
1808 log(DEBUG,"umode %c is an allowed umode",umode);
1809 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1811 int v1 = strlen(dmodes);
1812 int v2 = strlen(outpars);
1814 dmodes[v1] = parameters[1][i];
1816 outpars[v2] = parameters[1][i];
1822 // can only remove a mode they already have
1823 log(DEBUG,"umode %c being removed",parameters[1][i]);
1824 if ((allowed_umode(parameters[1][i],"o",false,true)) && (strchr(dmodes,parameters[1][i])))
1826 char umode = parameters[1][i];
1827 log(DEBUG,"umode %c is an allowed umode",umode);
1828 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1834 unsigned int v1 = strlen(outpars);
1836 outpars[v1] = parameters[1][i];
1839 for (q = 0; q < strlen(dmodes); q++)
1841 if (dmodes[q] != parameters[1][i])
1845 strlcat(temp,moo,MAXBUF);
1848 strlcpy(dmodes,temp,52);
1861 while (i < strlen (outpars))
1863 b[z++] = outpars[i++];
1865 if (i<strlen(outpars)-1)
1867 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1869 // someones playing silly buggers and trying
1870 // to put a +- or -+ into the line...
1874 if (i == strlen(outpars)-1)
1876 if ((outpars[i] == '-') || (outpars[i] == '+'))
1884 if ((b[z] == '-') || (b[z] == '+'))
1887 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1890 if (strlen(dmodes)>MAXMODES)
1892 dmodes[MAXMODES-1] = '\0';
1894 log(DEBUG,"Stripped mode line");
1895 log(DEBUG,"Line dest is now %s",dmodes);
1896 strlcpy(dest->modes,dmodes,MAXMODES);
1903 Ptr = FindChan(parameters[0]);
1907 strlcpy(s2.nick,ServerName,NICKMAX);
1908 strcpy(s2.modes,"o");
1910 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1915 void merge_mode2(char **parameters, int pcnt, userrec* user)
1921 char outpars[MAXBUF];
1923 dest = Find(parameters[0]);
1925 // fix: ChroNiCk found this - we cant use this as debug if its null!
1928 log(DEBUG,"merge_mode2 on %s",dest->nick);
1931 if ((dest) && (pcnt > 1))
1933 std::string tidied = compress_modes(parameters[1],false);
1934 parameters[1] = (char*)tidied.c_str();
1936 char dmodes[MAXBUF];
1937 strlcpy(dmodes,dest->modes,52);
1939 strcpy(outpars,"+");
1942 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1944 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1945 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1947 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1950 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1952 if (parameters[1][i] == ' ')
1954 if (parameters[1][i] == '+')
1958 int t = strlen(outpars)-1;
1959 if ((outpars[t] == '+') || (outpars[t] == '-'))
1965 strcat(outpars,"+");
1971 if (parameters[1][i] == '-')
1975 int t = strlen(outpars)-1;
1976 if ((outpars[t] == '+') || (outpars[t] == '-'))
1982 strcat(outpars,"-");
1989 log(DEBUG,"begin mode processing entry");
1995 log(DEBUG,"umode %c being added",parameters[1][i]);
1996 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,false)))
1998 char umode = parameters[1][i];
1999 log(DEBUG,"umode %c is an allowed umode",umode);
2000 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2002 int v1 = strlen(dmodes);
2003 int v2 = strlen(outpars);
2005 dmodes[v1] = parameters[1][i];
2007 outpars[v2] = parameters[1][i];
2008 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2014 // can only remove a mode they already have
2015 log(DEBUG,"umode %c being removed",parameters[1][i]);
2016 if ((allowed_umode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
2018 char umode = parameters[1][i];
2019 log(DEBUG,"umode %c is an allowed umode",umode);
2020 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2026 unsigned int v1 = strlen(outpars);
2028 outpars[v1] = parameters[1][i];
2031 for (q = 0; q < strlen(dmodes); q++)
2033 if (dmodes[q] != parameters[1][i])
2037 strlcat(temp,moo,MAXBUF);
2040 strlcpy(dmodes,temp,52);
2047 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2054 while (i < strlen (outpars))
2056 b[z++] = outpars[i++];
2058 if (i<strlen(outpars)-1)
2060 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2062 // someones playing silly buggers and trying
2063 // to put a +- or -+ into the line...
2067 if (i == strlen(outpars)-1)
2069 if ((outpars[i] == '-') || (outpars[i] == '+'))
2077 if ((b[z] == '-') || (b[z] == '+'))
2081 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
2086 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2087 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
2090 if (strlen(dmodes)>MAXMODES)
2092 dmodes[MAXMODES-1] = '\0';
2094 log(DEBUG,"Stripped mode line");
2095 log(DEBUG,"Line dest is now %s",dmodes);
2096 strlcpy(dest->modes,dmodes,MAXMODES);
2102 Ptr = FindChan(parameters[0]);
2105 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2108 //if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2112 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);