1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
23 #include <sys/errno.h>
27 #include <ext/hash_map>
35 #include "connection.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
49 extern std::vector<Module*> modules;
50 extern std::vector<ircd_module*> factory;
51 extern ServerConfig* Config;
55 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
59 if ((!user) || (!dest) || (!chan))
61 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
67 log(DEFAULT,"the target nickname given to give_ops was invalid");
68 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
74 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
75 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
82 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
84 if (MOD_RESULT == ACR_DENY)
86 if (MOD_RESULT == ACR_DEFAULT)
88 if ((status < STATUS_OP) && (!is_uline(user->server)))
90 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
91 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
97 for (unsigned int i = 0; i < d->chans.size(); i++)
99 if ((d->chans[i].channel != NULL) && (chan != NULL))
100 if (!strcasecmp(d->chans[i].channel->name,chan->name))
102 if (d->chans[i].uc_modes & UCMODE_OP)
104 /* mode already set on user, dont allow multiple */
105 log(DEFAULT,"The target user given to give_ops was already opped on the channel");
108 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
109 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
113 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
118 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
122 if ((!user) || (!dest) || (!chan))
124 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
131 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
136 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
142 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
144 if (MOD_RESULT == ACR_DENY)
146 if (MOD_RESULT == ACR_DEFAULT)
148 if ((status < STATUS_OP) && (!is_uline(user->server)))
150 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
155 for (unsigned int i = 0; i < d->chans.size(); i++)
157 if ((d->chans[i].channel != NULL) && (chan != NULL))
158 if (!strcasecmp(d->chans[i].channel->name,chan->name))
160 if (d->chans[i].uc_modes & UCMODE_HOP)
162 /* mode already set on user, dont allow multiple */
165 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
166 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
174 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
178 if ((!user) || (!dest) || (!chan))
180 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
187 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
192 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
198 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
200 if (MOD_RESULT == ACR_DENY)
202 if (MOD_RESULT == ACR_DEFAULT)
204 if ((status < STATUS_HOP) && (!is_uline(user->server)))
206 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
211 for (unsigned int i = 0; i < d->chans.size(); i++)
213 if ((d->chans[i].channel != NULL) && (chan != NULL))
214 if (!strcasecmp(d->chans[i].channel->name,chan->name))
216 if (d->chans[i].uc_modes & UCMODE_VOICE)
218 /* mode already set on user, dont allow multiple */
221 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
222 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
230 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
234 if ((!user) || (!dest) || (!chan))
236 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
243 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
244 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
249 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
250 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
256 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
258 if (MOD_RESULT == ACR_DENY)
260 if (MOD_RESULT == ACR_DEFAULT)
262 if ((status < STATUS_OP) && (!is_uline(user->server)))
264 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
269 for (unsigned int i = 0; i < d->chans.size(); i++)
271 if ((d->chans[i].channel != NULL) && (chan != NULL))
272 if (!strcasecmp(d->chans[i].channel->name,chan->name))
274 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
276 /* mode already set on user, dont allow multiple */
279 d->chans[i].uc_modes ^= UCMODE_OP;
280 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
284 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
289 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
293 if ((!user) || (!dest) || (!chan))
295 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
302 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
307 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
313 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
315 if (MOD_RESULT == ACR_DENY)
317 if (MOD_RESULT == ACR_DEFAULT)
319 if ((status < STATUS_OP) && (!is_uline(user->server)))
321 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
326 for (unsigned int i = 0; i < d->chans.size(); i++)
328 if ((d->chans[i].channel != NULL) && (chan != NULL))
329 if (!strcasecmp(d->chans[i].channel->name,chan->name))
331 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
333 /* mode already set on user, dont allow multiple */
336 d->chans[i].uc_modes ^= UCMODE_HOP;
337 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
345 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
349 if ((!user) || (!dest) || (!chan))
351 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
358 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
363 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
369 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
371 if (MOD_RESULT == ACR_DENY)
373 if (MOD_RESULT == ACR_DEFAULT)
375 if ((status < STATUS_HOP) && (!is_uline(user->server)))
377 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
382 for (unsigned int i = 0; i < d->chans.size(); i++)
384 if ((d->chans[i].channel != NULL) && (chan != NULL))
385 if (!strcasecmp(d->chans[i].channel->name,chan->name))
387 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
389 /* mode already set on user, dont allow multiple */
392 d->chans[i].uc_modes ^= UCMODE_VOICE;
393 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
401 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
403 if ((!user) || (!dest) || (!chan)) {
404 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
409 if ((!user) || (!dest) || (!chan))
411 unsigned int l = strlen(dest);
412 if (strchr(dest,'!')==0)
414 if (strchr(dest,'@')==0)
416 for (unsigned int i = 0; i < l; i++)
419 for (unsigned int i = 0; i < l; i++)
423 for (unsigned int i = 0; i < l; i++)
429 for (unsigned int i = 0; i < l; i++)
435 long maxbans = GetMaxBans(chan->name);
436 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
438 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);
442 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
445 FOREACH_RESULT(OnAddBan(user,chan,dest));
450 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
452 if (!strcasecmp(i->data,dest))
454 // dont allow a user to set the same ban twice
460 strncpy(b.data,dest,MAXBUF);
461 strncpy(b.set_by,user->nick,NICKMAX);
462 chan->bans.push_back(b);
466 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
468 if ((!user) || (!dest) || (!chan)) {
469 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
473 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
474 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
476 if (!strcasecmp(i->data,dest))
479 FOREACH_RESULT(OnDelBan(user,chan,dest));
489 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
490 // a section further down the chain tidies up the +-+- crap.
491 std::string compress_modes(std::string modes,bool channelmodes)
495 memset(counts,0,sizeof(counts));
496 memset(active,0,sizeof(active));
497 log(DEBUG,"compress_modes: %s",modes.c_str());
498 for (unsigned int i = 0; i < modes.length(); i++)
500 if ((modes[i] == '+') || (modes[i] == '-'))
504 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
506 log(DEBUG,"Tidy mode %c",modes[i]);
507 counts[(unsigned int)modes[i]]++;
508 active[(unsigned int)modes[i]] = true;
513 log(DEBUG,"Tidy mode %c",modes[i]);
514 counts[(unsigned int)modes[i]]++;
515 active[(unsigned int)modes[i]] = true;
518 for (int j = 65; j < 127; j++)
520 if ((counts[j] > 1) && (active[j] == true))
523 v[0] = (unsigned char)j;
525 std::string mode_str = v;
526 std::string::size_type pos = modes.find(mode_str);
527 if (pos != std::string::npos)
529 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
530 while (modes.find(mode_str) != std::string::npos)
531 modes.erase(modes.find(mode_str),1);
532 log(DEBUG,"New mode line: %s",modes.c_str());
539 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
542 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
546 char modelist[MAXBUF];
547 char outlist[MAXBUF];
549 char outpars[32][MAXBUF];
555 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
563 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
565 if (MOD_RESULT == ACR_DENY)
568 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
570 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
571 * parameters[2] onwards are parameters for
572 * modes that require them :) */
573 strlcpy(outlist,"+",MAXBUF);
576 log(DEBUG,"process_modes: modelist: %s",modelist);
578 std::string tidied = compress_modes(modelist,true);
579 strlcpy(modelist,tidied.c_str(),MAXBUF);
581 int len = strlen(modelist);
582 while (modelist[len-1] == ' ')
583 modelist[--len] = '\0';
584 for (ptr = 0; ptr < len; ptr++)
589 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
591 char modechar = modelist[ptr];
592 switch (modelist[ptr])
597 int t = strlen(outlist)-1;
598 if ((outlist[t] == '+') || (outlist[t] == '-'))
614 int t = strlen(outlist)-1;
615 if ((outlist[t] == '+') || (outlist[t] == '-'))
629 if ((param >= pcnt)) break;
630 log(DEBUG,"Enough parameters left");
634 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
637 log(DEBUG,"calling give_ops");
638 r = give_ops(user,parameters[param++],chan,status);
645 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
648 log(DEBUG,"calling take_ops");
649 r = take_ops(user,parameters[param++],chan,status);
655 strlcat(outlist,"o",MAXBUF);
656 strlcpy(outpars[pc++],r,MAXBUF);
661 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
665 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
668 r = give_hops(user,parameters[param++],chan,status);
675 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
678 r = take_hops(user,parameters[param++],chan,status);
684 strlcat(outlist,"h",MAXBUF);
685 strlcpy(outpars[pc++],r,MAXBUF);
691 if ((param >= pcnt)) break;
695 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
698 r = give_voice(user,parameters[param++],chan,status);
705 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
708 r = take_voice(user,parameters[param++],chan,status);
714 strlcat(outlist,"v",MAXBUF);
715 strlcpy(outpars[pc++],r,MAXBUF);
720 if ((param >= pcnt)) break;
724 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
727 r = add_ban(user,parameters[param++],chan,status);
734 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
737 r = take_ban(user,parameters[param++],chan,status);
743 strlcat(outlist,"b",MAXBUF);
744 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
758 if (previously_unset_k)
760 previously_set_k = true;
762 if (!strcmp(chan->key,""))
765 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
770 strlcpy(key,parameters[param++],32);
771 strlcpy(outpars[pc++],key,MAXBUF);
772 strlcpy(chan->key,key,MAXBUF);
780 /* checks on -k are case sensitive and only accurate to the
781 first 32 characters */
782 if (previously_set_k)
784 previously_unset_k = true;
788 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
791 strlcpy(key,parameters[param++],32);
792 /* only allow -k if correct key given */
793 if (!strcmp(chan->key,key))
795 strlcat(outlist,"k",MAXBUF);
796 strlcpy(chan->key,"",MAXBUF);
797 strlcpy(outpars[pc++],key,MAXBUF);
807 if (previously_set_l)
809 previously_unset_l = true;
811 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
822 if ((param >= pcnt)) break;
827 if (previously_unset_l)
829 previously_set_l = true;
830 bool invalid = false;
831 for (unsigned int i = 0; i < strlen(parameters[param]); i++)
833 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
838 if (atoi(parameters[param]) < 1)
847 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
851 chan->limit = atoi(parameters[param]);
853 // reported by mech: large values cause underflow
855 chan->limit = 0x7FFF;
860 strlcat(outlist,"l",MAXBUF);
861 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
869 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
874 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
875 chan->binarymodes |= CM_INVITEONLY;
879 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
880 chan->binarymodes &= ~CM_INVITEONLY;
887 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
892 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
893 chan->binarymodes |= CM_TOPICLOCK;
897 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"t",MAXBUF);
898 chan->binarymodes &= ~CM_TOPICLOCK;
905 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
910 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
911 chan->binarymodes |= CM_NOEXTERNAL;
915 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
916 chan->binarymodes &= ~CM_NOEXTERNAL;
923 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
928 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
929 chan->binarymodes |= CM_MODERATED;
933 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
934 chan->binarymodes &= ~CM_MODERATED;
941 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
946 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
947 chan->binarymodes |= CM_SECRET;
948 if (chan->binarymodes & CM_PRIVATE)
950 chan->binarymodes &= ~CM_PRIVATE;
953 strlcat(outlist,"-p+",MAXBUF);
959 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
960 chan->binarymodes &= ~CM_SECRET;
967 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
972 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
973 chan->binarymodes |= CM_PRIVATE;
974 if (chan->binarymodes & CM_SECRET)
976 chan->binarymodes &= ~CM_SECRET;
979 strlcat(outlist,"-s+",MAXBUF);
985 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
986 chan->binarymodes &= ~CM_PRIVATE;
992 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
995 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
997 if (!ModeIsListMode(modechar,MT_CHANNEL))
999 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1003 if (ModeDefined(modechar,MT_CHANNEL))
1005 log(DEBUG,"A module has claimed this mode");
1008 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1010 p.push_back(parameters[param]);
1012 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1014 p.push_back(parameters[param]);
1017 bool handled = false;
1020 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1021 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1023 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1029 // BIG ASS IDIOTIC CODER WARNING!
1030 // Using OnRawMode on another modules mode's behavour
1031 // will confuse the crap out of admins! just because you CAN
1032 // do it, doesnt mean you SHOULD!
1034 std::string para = "";
1037 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1040 for (int i = 0; i <= MODCOUNT; i++)
1044 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1047 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1048 char app[] = {modechar, 0};
1049 if (ModeIsListMode(modechar,MT_CHANNEL))
1060 strlcat(outlist, app,MAXBUF);
1062 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1069 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1071 strlcat(outlist, app,MAXBUF);
1073 else if (!strchr(outlist,modechar))
1075 strlcat(outlist, app,MAXBUF);
1078 chan->SetCustomMode(modechar,mdir);
1079 // include parameters in output if mode has them
1080 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1082 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1083 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1086 // break, because only one module can handle the mode.
1095 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1103 /* this ensures only the *valid* modes are sent out onto the network */
1104 int xt = strlen(outlist)-1;
1105 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1108 xt = strlen(outlist)-1;
1112 strlcpy(outstr,outlist,MAXBUF);
1113 for (ptr = 0; ptr < pc; ptr++)
1115 strlcat(outstr," ",MAXBUF);
1116 strlcat(outstr,outpars[ptr],MAXBUF);
1120 log(DEBUG,"Local mode change");
1121 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1122 FOREACH_MOD OnMode(user, chan, TYPE_CHANNEL, outstr);
1130 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outstr);
1138 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1139 FOREACH_MOD OnMode(user, chan, TYPE_CHANNEL, outstr);
1146 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1148 bool allowed_umode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1150 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1151 // Servers can +o and -o arbitrarily
1152 if ((serveroverride == true) && (umode == 'o'))
1156 // RFC1459 specified modes
1157 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1159 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1163 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1164 if ((strchr(sourcemodes,'o')) && (!adding))
1166 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1169 else if (umode == 'o')
1171 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1175 // process any module-defined modes that need oper
1176 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1178 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1182 if (ModeDefined(umode,MT_CLIENT))
1184 // process any module-defined modes that don't need oper
1185 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1186 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1188 // no, this mode needs oper, and this user 'aint got what it takes!
1194 // anything else - return false.
1195 log(DEBUG,"umode %c not known by any ruleset",umode);
1199 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1206 strlcpy(s2->nick,Config->ServerName,NICKMAX);
1207 strlcpy(s2->modes,"o",52);
1214 if (ModeDefined(umode,MT_CLIENT))
1216 for (int i = 0; i <= MODCOUNT; i++)
1218 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1220 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1224 log(DEBUG,"No module claims umode %c",umode);
1243 void handle_mode(char **parameters, int pcnt, userrec *user)
1249 char outpars[MAXBUF];
1251 dest = Find(parameters[0]);
1258 if ((dest) && (pcnt == 1))
1260 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1264 if ((dest) && (pcnt > 1))
1266 std::string tidied = compress_modes(parameters[1],false);
1267 parameters[1] = (char*)tidied.c_str();
1269 char dmodes[MAXBUF];
1270 strlcpy(dmodes,dest->modes,52);
1271 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1276 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1287 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1291 strcpy(outpars,"+");
1294 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1297 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1299 if (parameters[1][i] == ' ')
1301 if (parameters[1][i] == '+')
1305 int t = strlen(outpars)-1;
1306 if ((outpars[t] == '+') || (outpars[t] == '-'))
1312 strcat(outpars,"+");
1318 if (parameters[1][i] == '-')
1322 int t = strlen(outpars)-1;
1323 if ((outpars[t] == '+') || (outpars[t] == '-'))
1329 strcat(outpars,"-");
1337 if (strchr(user->modes,'o'))
1343 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction,false)))
1352 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,false)))
1354 char umode = parameters[1][i];
1355 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1357 int q = strlen(dmodes);
1358 int r = strlen(outpars);
1360 dmodes[q] = parameters[1][i];
1362 outpars[r] = parameters[1][i];
1363 if (parameters[1][i] == 'o')
1365 FOREACH_MOD OnGlobalOper(dest);
1372 if ((allowed_umode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
1374 char umode = parameters[1][i];
1375 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1381 unsigned int r = strlen(outpars);
1383 outpars[r] = parameters[1][i];
1386 for (q = 0; q < strlen(dmodes); q++)
1388 if (dmodes[q] != parameters[1][i])
1392 strlcat(temp,moo,MAXBUF);
1395 strlcpy(dmodes,temp,52);
1408 strlcpy(b,"",MAXBUF);
1411 while (i < strlen (outpars))
1413 b[z++] = outpars[i++];
1415 if (i<strlen(outpars)-1)
1417 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1419 // someones playing silly buggers and trying
1420 // to put a +- or -+ into the line...
1424 if (i == strlen(outpars)-1)
1426 if ((outpars[i] == '-') || (outpars[i] == '+'))
1434 if ((b[z] == '-') || (b[z] == '+'))
1437 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1442 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1443 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
1446 if (strlen(dmodes)>MAXMODES)
1448 dmodes[MAXMODES-1] = '\0';
1450 log(DEBUG,"Stripped mode line");
1451 log(DEBUG,"Line dest is now %s",dmodes);
1452 strlcpy(dest->modes,dmodes,52);
1459 Ptr = FindChan(parameters[0]);
1464 /* just /modes #channel */
1465 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1466 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1472 char* mode = parameters[1];
1476 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1482 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1484 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1486 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1489 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1491 // list of items for an extmode
1492 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1493 FOREACH_MOD OnSendList(user,Ptr,*mode);
1499 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)))
1501 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1508 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1510 if (MOD_RESULT == ACR_DENY)
1512 if (MOD_RESULT == ACR_DEFAULT)
1514 if (cstatus(user,Ptr) < STATUS_HOP)
1516 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1521 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1526 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1533 void server_mode(char **parameters, int pcnt, userrec *user)
1539 char outpars[MAXBUF];
1541 dest = Find(parameters[0]);
1543 // fix: ChroNiCk found this - we cant use this as debug if its null!
1546 log(DEBUG,"server_mode on %s",dest->nick);
1549 if ((dest) && (pcnt > 1))
1551 std::string tidied = compress_modes(parameters[1],false);
1552 parameters[1] = (char*)tidied.c_str();
1554 char dmodes[MAXBUF];
1555 strlcpy(dmodes,dest->modes,52);
1557 strcpy(outpars,"+");
1560 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1563 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1565 if (parameters[1][i] == ' ')
1567 if (parameters[1][i] == '+')
1571 int t = strlen(outpars)-1;
1572 if ((outpars[t] == '+') || (outpars[t] == '-'))
1578 strcat(outpars,"+");
1584 if (parameters[1][i] == '-')
1588 int t = strlen(outpars)-1;
1589 if ((outpars[t] == '+') || (outpars[t] == '-'))
1595 strcat(outpars,"-");
1602 log(DEBUG,"begin mode processing entry");
1608 log(DEBUG,"umode %c being added",parameters[1][i]);
1609 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,true)))
1611 char umode = parameters[1][i];
1612 log(DEBUG,"umode %c is an allowed umode",umode);
1613 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1615 int v1 = strlen(dmodes);
1616 int v2 = strlen(outpars);
1618 dmodes[v1] = parameters[1][i];
1620 outpars[v2] = parameters[1][i];
1626 // can only remove a mode they already have
1627 log(DEBUG,"umode %c being removed",parameters[1][i]);
1628 if ((allowed_umode(parameters[1][i],user->modes,false,true)) && (strchr(dmodes,parameters[1][i])))
1630 char umode = parameters[1][i];
1631 log(DEBUG,"umode %c is an allowed umode",umode);
1632 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1638 unsigned int v1 = strlen(outpars);
1640 outpars[v1] = parameters[1][i];
1643 for (q = 0; q < strlen(dmodes); q++)
1645 if (dmodes[q] != parameters[1][i])
1649 strlcat(temp,moo,MAXBUF);
1652 strlcpy(dmodes,temp,52);
1662 strlcpy(b,"",MAXBUF);
1665 while (i < strlen (outpars))
1667 b[z++] = outpars[i++];
1669 if (i<strlen(outpars)-1)
1671 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1673 // someones playing silly buggers and trying
1674 // to put a +- or -+ into the line...
1678 if (i == strlen(outpars)-1)
1680 if ((outpars[i] == '-') || (outpars[i] == '+'))
1688 if ((b[z] == '-') || (b[z] == '+'))
1691 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1696 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1697 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
1700 if (strlen(dmodes)>MAXMODES)
1702 dmodes[MAXMODES-1] = '\0';
1704 log(DEBUG,"Stripped mode line");
1705 log(DEBUG,"Line dest is now %s",dmodes);
1706 strlcpy(dest->modes,dmodes,MAXMODES);
1713 Ptr = FindChan(parameters[0]);
1716 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1720 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1726 void merge_mode(char **parameters, int pcnt)
1732 char outpars[MAXBUF];
1734 dest = Find(parameters[0]);
1736 // fix: ChroNiCk found this - we cant use this as debug if its null!
1739 log(DEBUG,"merge_mode on %s",dest->nick);
1742 if ((dest) && (pcnt > 1))
1744 std::string tidied = compress_modes(parameters[1],false);
1745 parameters[1] = (char*)tidied.c_str();
1747 char dmodes[MAXBUF];
1748 strlcpy(dmodes,dest->modes,52);
1750 strcpy(outpars,"+");
1753 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1756 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1758 if (parameters[1][i] == ' ')
1760 if (parameters[1][i] == '+')
1764 int t = strlen(outpars)-1;
1765 if ((outpars[t] == '+') || (outpars[t] == '-'))
1771 strcat(outpars,"+");
1777 if (parameters[1][i] == '-')
1781 int t = strlen(outpars)-1;
1782 if ((outpars[t] == '+') || (outpars[t] == '-'))
1788 strcat(outpars,"-");
1795 log(DEBUG,"begin mode processing entry");
1801 log(DEBUG,"umode %c being added",parameters[1][i]);
1802 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true,true)))
1804 char umode = parameters[1][i];
1805 log(DEBUG,"umode %c is an allowed umode",umode);
1806 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1808 int v1 = strlen(dmodes);
1809 int v2 = strlen(outpars);
1811 dmodes[v1] = parameters[1][i];
1813 outpars[v2] = parameters[1][i];
1819 // can only remove a mode they already have
1820 log(DEBUG,"umode %c being removed",parameters[1][i]);
1821 if ((allowed_umode(parameters[1][i],"o",false,true)) && (strchr(dmodes,parameters[1][i])))
1823 char umode = parameters[1][i];
1824 log(DEBUG,"umode %c is an allowed umode",umode);
1825 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1831 unsigned int v1 = strlen(outpars);
1833 outpars[v1] = parameters[1][i];
1836 for (q = 0; q < strlen(dmodes); q++)
1838 if (dmodes[q] != parameters[1][i])
1842 strlcat(temp,moo,MAXBUF);
1845 strlcpy(dmodes,temp,52);
1858 while (i < strlen (outpars))
1860 b[z++] = outpars[i++];
1862 if (i<strlen(outpars)-1)
1864 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1866 // someones playing silly buggers and trying
1867 // to put a +- or -+ into the line...
1871 if (i == strlen(outpars)-1)
1873 if ((outpars[i] == '-') || (outpars[i] == '+'))
1881 if ((b[z] == '-') || (b[z] == '+'))
1884 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1887 if (strlen(dmodes)>MAXMODES)
1889 dmodes[MAXMODES-1] = '\0';
1891 log(DEBUG,"Stripped mode line");
1892 log(DEBUG,"Line dest is now %s",dmodes);
1893 strlcpy(dest->modes,dmodes,MAXMODES);
1900 Ptr = FindChan(parameters[0]);
1904 strlcpy(s2.nick,Config->ServerName,NICKMAX);
1905 strcpy(s2.modes,"o");
1907 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1912 void merge_mode2(char **parameters, int pcnt, userrec* user)
1918 char outpars[MAXBUF];
1920 dest = Find(parameters[0]);
1922 // fix: ChroNiCk found this - we cant use this as debug if its null!
1925 log(DEBUG,"merge_mode2 on %s",dest->nick);
1928 if ((dest) && (pcnt > 1))
1930 std::string tidied = compress_modes(parameters[1],false);
1931 parameters[1] = (char*)tidied.c_str();
1933 char dmodes[MAXBUF];
1934 strlcpy(dmodes,dest->modes,52);
1936 strcpy(outpars,"+");
1939 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1941 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1942 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1944 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1947 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1949 if (parameters[1][i] == ' ')
1951 if (parameters[1][i] == '+')
1955 int t = strlen(outpars)-1;
1956 if ((outpars[t] == '+') || (outpars[t] == '-'))
1962 strcat(outpars,"+");
1968 if (parameters[1][i] == '-')
1972 int t = strlen(outpars)-1;
1973 if ((outpars[t] == '+') || (outpars[t] == '-'))
1979 strcat(outpars,"-");
1986 log(DEBUG,"begin mode processing entry");
1992 log(DEBUG,"umode %c being added",parameters[1][i]);
1993 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,false)))
1995 char umode = parameters[1][i];
1996 log(DEBUG,"umode %c is an allowed umode",umode);
1997 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1999 int v1 = strlen(dmodes);
2000 int v2 = strlen(outpars);
2002 dmodes[v1] = parameters[1][i];
2004 outpars[v2] = parameters[1][i];
2005 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2011 // can only remove a mode they already have
2012 log(DEBUG,"umode %c being removed",parameters[1][i]);
2013 if ((allowed_umode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
2015 char umode = parameters[1][i];
2016 log(DEBUG,"umode %c is an allowed umode",umode);
2017 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2023 unsigned int v1 = strlen(outpars);
2025 outpars[v1] = parameters[1][i];
2028 for (q = 0; q < strlen(dmodes); q++)
2030 if (dmodes[q] != parameters[1][i])
2034 strlcat(temp,moo,MAXBUF);
2037 strlcpy(dmodes,temp,52);
2044 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2051 while (i < strlen (outpars))
2053 b[z++] = outpars[i++];
2055 if (i<strlen(outpars)-1)
2057 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2059 // someones playing silly buggers and trying
2060 // to put a +- or -+ into the line...
2064 if (i == strlen(outpars)-1)
2066 if ((outpars[i] == '-') || (outpars[i] == '+'))
2074 if ((b[z] == '-') || (b[z] == '+'))
2078 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
2083 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2084 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
2087 if (strlen(dmodes)>MAXMODES)
2089 dmodes[MAXMODES-1] = '\0';
2091 log(DEBUG,"Stripped mode line");
2092 log(DEBUG,"Line dest is now %s",dmodes);
2093 strlcpy(dest->modes,dmodes,MAXMODES);
2099 Ptr = FindChan(parameters[0]);
2102 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2105 //if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2109 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);