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"
47 #include "inspstring.h"
48 #include "helperfuncs.h"
51 extern std::vector<Module*> modules;
52 extern std::vector<ircd_module*> factory;
53 extern std::vector<std::string> module_names;
57 extern char ServerName[MAXBUF];
58 extern char Network[MAXBUF];
59 extern char ServerDesc[MAXBUF];
60 extern char AdminName[MAXBUF];
61 extern char AdminEmail[MAXBUF];
62 extern char AdminNick[MAXBUF];
63 extern char diepass[MAXBUF];
64 extern char restartpass[MAXBUF];
65 extern char motd[MAXBUF];
66 extern char rules[MAXBUF];
67 extern char list[MAXBUF];
68 extern char PrefixQuit[MAXBUF];
69 extern char DieValue[MAXBUF];
71 extern bool AllowHalfop;
72 extern bool AllowProtect;
73 extern bool AllowFounder;
77 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
81 if ((!user) || (!dest) || (!chan))
83 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
89 log(DEFAULT,"the target nickname given to give_ops was invalid");
90 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
96 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
97 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
104 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
106 if (MOD_RESULT == ACR_DENY)
108 if (MOD_RESULT == ACR_DEFAULT)
110 if ((status < STATUS_OP) && (!is_uline(user->server)))
112 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
113 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
119 for (unsigned int i = 0; i != MAXCHANS; i++)
121 if ((d->chans[i].channel != NULL) && (chan != NULL))
122 if (!strcasecmp(d->chans[i].channel->name,chan->name))
124 if (d->chans[i].uc_modes & UCMODE_OP)
126 /* mode already set on user, dont allow multiple */
127 log(DEFAULT,"The target user given to give_ops was already opped on the channel");
130 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
131 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
135 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
140 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
144 if ((!user) || (!dest) || (!chan))
146 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
153 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
158 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
164 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
166 if (MOD_RESULT == ACR_DENY)
168 if (MOD_RESULT == ACR_DEFAULT)
170 if ((status < STATUS_OP) && (!is_uline(user->server)))
172 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
177 for (unsigned int i = 0; i != MAXCHANS; i++)
179 if ((d->chans[i].channel != NULL) && (chan != NULL))
180 if (!strcasecmp(d->chans[i].channel->name,chan->name))
182 if (d->chans[i].uc_modes & UCMODE_HOP)
184 /* mode already set on user, dont allow multiple */
187 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
188 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
196 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
200 if ((!user) || (!dest) || (!chan))
202 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
209 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
214 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
220 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
222 if (MOD_RESULT == ACR_DENY)
224 if (MOD_RESULT == ACR_DEFAULT)
226 if ((status < STATUS_HOP) && (!is_uline(user->server)))
228 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
233 for (unsigned int i = 0; i != MAXCHANS; i++)
235 if ((d->chans[i].channel != NULL) && (chan != NULL))
236 if (!strcasecmp(d->chans[i].channel->name,chan->name))
238 if (d->chans[i].uc_modes & UCMODE_VOICE)
240 /* mode already set on user, dont allow multiple */
243 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
244 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
252 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
256 if ((!user) || (!dest) || (!chan))
258 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
265 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
266 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
271 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
272 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
278 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
280 if (MOD_RESULT == ACR_DENY)
282 if (MOD_RESULT == ACR_DEFAULT)
284 if ((status < STATUS_OP) && (!is_uline(user->server)))
286 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
291 for (unsigned int i = 0; i != MAXCHANS; i++)
293 if ((d->chans[i].channel != NULL) && (chan != NULL))
294 if (!strcasecmp(d->chans[i].channel->name,chan->name))
296 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
298 /* mode already set on user, dont allow multiple */
301 d->chans[i].uc_modes ^= UCMODE_OP;
302 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
306 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
311 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
315 if ((!user) || (!dest) || (!chan))
317 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
324 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
329 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
335 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
337 if (MOD_RESULT == ACR_DENY)
339 if (MOD_RESULT == ACR_DEFAULT)
341 if ((status < STATUS_OP) && (!is_uline(user->server)))
343 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
348 for (unsigned int i = 0; i != MAXCHANS; i++)
350 if ((d->chans[i].channel != NULL) && (chan != NULL))
351 if (!strcasecmp(d->chans[i].channel->name,chan->name))
353 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
355 /* mode already set on user, dont allow multiple */
358 d->chans[i].uc_modes ^= UCMODE_HOP;
359 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
367 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
371 if ((!user) || (!dest) || (!chan))
373 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
380 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
385 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
391 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
393 if (MOD_RESULT == ACR_DENY)
395 if (MOD_RESULT == ACR_DEFAULT)
397 if ((status < STATUS_HOP) && (!is_uline(user->server)))
399 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
404 for (unsigned int i = 0; i != MAXCHANS; i++)
406 if ((d->chans[i].channel != NULL) && (chan != NULL))
407 if (!strcasecmp(d->chans[i].channel->name,chan->name))
409 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
411 /* mode already set on user, dont allow multiple */
414 d->chans[i].uc_modes ^= UCMODE_VOICE;
415 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
423 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
425 if ((!user) || (!dest) || (!chan)) {
426 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
431 if ((!user) || (!dest) || (!chan))
433 unsigned int l = strlen(dest);
434 if (strchr(dest,'!')==0)
436 if (strchr(dest,'@')==0)
438 for (unsigned int i = 0; i < l; i++)
441 for (unsigned int i = 0; i < l; i++)
445 for (unsigned int i = 0; i < l; i++)
451 for (unsigned int i = 0; i < l; i++)
457 long maxbans = GetMaxBans(chan->name);
458 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
460 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);
464 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
467 FOREACH_RESULT(OnAddBan(user,chan,dest));
472 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
474 if (!strcasecmp(i->data,dest))
476 // dont allow a user to set the same ban twice
482 strncpy(b.data,dest,MAXBUF);
483 strncpy(b.set_by,user->nick,NICKMAX);
484 chan->bans.push_back(b);
488 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
490 if ((!user) || (!dest) || (!chan)) {
491 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
495 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
496 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
498 if (!strcasecmp(i->data,dest))
501 FOREACH_RESULT(OnDelBan(user,chan,dest));
511 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
512 // a section further down the chain tidies up the +-+- crap.
513 std::string compress_modes(std::string modes,bool channelmodes)
517 memset(counts,0,sizeof(counts));
518 memset(active,0,sizeof(active));
519 log(DEBUG,"compress_modes: %s",modes.c_str());
520 for (unsigned int i = 0; i < modes.length(); i++)
522 if ((modes[i] == '+') || (modes[i] == '-'))
526 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
528 log(DEBUG,"Tidy mode %c",modes[i]);
529 counts[(unsigned int)modes[i]]++;
530 active[(unsigned int)modes[i]] = true;
535 log(DEBUG,"Tidy mode %c",modes[i]);
536 counts[(unsigned int)modes[i]]++;
537 active[(unsigned int)modes[i]] = true;
540 for (int j = 65; j < 127; j++)
542 if ((counts[j] > 1) && (active[j] == true))
545 v[0] = (unsigned char)j;
547 std::string mode_str = v;
548 std::string::size_type pos = modes.find(mode_str);
549 if (pos != std::string::npos)
551 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
552 while (modes.find(mode_str) != std::string::npos)
553 modes.erase(modes.find(mode_str),1);
554 log(DEBUG,"New mode line: %s",modes.c_str());
561 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
564 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
568 char modelist[MAXBUF];
569 char outlist[MAXBUF];
571 char outpars[32][MAXBUF];
577 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
585 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
587 if (MOD_RESULT == ACR_DENY)
590 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
592 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
593 * parameters[2] onwards are parameters for
594 * modes that require them :) */
595 strlcpy(outlist,"+",MAXBUF);
598 log(DEBUG,"process_modes: modelist: %s",modelist);
600 std::string tidied = compress_modes(modelist,true);
601 strlcpy(modelist,tidied.c_str(),MAXBUF);
603 int len = strlen(modelist);
604 while (modelist[len-1] == ' ')
605 modelist[--len] = '\0';
606 for (ptr = 0; ptr < len; ptr++)
611 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
613 char modechar = modelist[ptr];
614 switch (modelist[ptr])
619 int t = strlen(outlist)-1;
620 if ((outlist[t] == '+') || (outlist[t] == '-'))
636 int t = strlen(outlist)-1;
637 if ((outlist[t] == '+') || (outlist[t] == '-'))
651 if ((param >= pcnt)) break;
652 log(DEBUG,"Enough parameters left");
656 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
659 log(DEBUG,"calling give_ops");
660 r = give_ops(user,parameters[param++],chan,status);
667 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
670 log(DEBUG,"calling take_ops");
671 r = take_ops(user,parameters[param++],chan,status);
677 strlcat(outlist,"o",MAXBUF);
678 strlcpy(outpars[pc++],r,MAXBUF);
683 if (((param >= pcnt)) || (!AllowHalfop)) break;
687 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
690 r = give_hops(user,parameters[param++],chan,status);
697 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
700 r = take_hops(user,parameters[param++],chan,status);
706 strlcat(outlist,"h",MAXBUF);
707 strlcpy(outpars[pc++],r,MAXBUF);
713 if ((param >= pcnt)) break;
717 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
720 r = give_voice(user,parameters[param++],chan,status);
727 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
730 r = take_voice(user,parameters[param++],chan,status);
736 strlcat(outlist,"v",MAXBUF);
737 strlcpy(outpars[pc++],r,MAXBUF);
742 if ((param >= pcnt)) break;
746 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
749 r = add_ban(user,parameters[param++],chan,status);
756 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
759 r = take_ban(user,parameters[param++],chan,status);
765 strlcat(outlist,"b",MAXBUF);
766 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
780 if (previously_unset_k)
782 previously_set_k = true;
784 if (!strcmp(chan->key,""))
787 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
792 strlcpy(key,parameters[param++],32);
793 strlcpy(outpars[pc++],key,MAXBUF);
794 strlcpy(chan->key,key,MAXBUF);
802 /* checks on -k are case sensitive and only accurate to the
803 first 32 characters */
804 if (previously_set_k)
806 previously_unset_k = true;
810 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
813 strlcpy(key,parameters[param++],32);
814 /* only allow -k if correct key given */
815 if (!strcmp(chan->key,key))
817 strlcat(outlist,"k",MAXBUF);
818 strlcpy(chan->key,"",MAXBUF);
819 strlcpy(outpars[pc++],key,MAXBUF);
829 if (previously_set_l)
831 previously_unset_l = true;
833 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
844 if ((param >= pcnt)) break;
849 if (previously_unset_l)
851 previously_set_l = true;
852 bool invalid = false;
853 for (unsigned int i = 0; i < strlen(parameters[param]); i++)
855 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
860 if (atoi(parameters[param]) < 1)
869 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
873 chan->limit = atoi(parameters[param]);
875 // reported by mech: large values cause underflow
877 chan->limit = 0x7FFF;
882 strlcat(outlist,"l",MAXBUF);
883 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
891 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
896 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
897 chan->binarymodes |= CM_INVITEONLY;
901 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
902 chan->binarymodes &= ~CM_INVITEONLY;
909 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
914 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
915 chan->binarymodes |= CM_TOPICLOCK;
919 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"t",MAXBUF);
920 chan->binarymodes &= ~CM_TOPICLOCK;
927 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
932 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
933 chan->binarymodes |= CM_NOEXTERNAL;
937 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
938 chan->binarymodes &= ~CM_NOEXTERNAL;
945 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
950 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
951 chan->binarymodes |= CM_MODERATED;
955 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
956 chan->binarymodes &= ~CM_MODERATED;
963 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
968 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
969 chan->binarymodes |= CM_SECRET;
970 if (chan->binarymodes & CM_PRIVATE)
972 chan->binarymodes &= ~CM_PRIVATE;
975 strlcat(outlist,"-p+",MAXBUF);
981 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
982 chan->binarymodes &= ~CM_SECRET;
989 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
994 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
995 chan->binarymodes |= CM_PRIVATE;
996 if (chan->binarymodes & CM_SECRET)
998 chan->binarymodes &= ~CM_SECRET;
1001 strlcat(outlist,"-s+",MAXBUF);
1007 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
1008 chan->binarymodes &= ~CM_PRIVATE;
1014 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
1017 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
1019 if (!ModeIsListMode(modechar,MT_CHANNEL))
1021 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1025 if (ModeDefined(modechar,MT_CHANNEL))
1027 log(DEBUG,"A module has claimed this mode");
1030 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1032 p.push_back(parameters[param]);
1034 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1036 p.push_back(parameters[param]);
1039 bool handled = false;
1042 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1043 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1045 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1051 // BIG ASS IDIOTIC CODER WARNING!
1052 // Using OnRawMode on another modules mode's behavour
1053 // will confuse the crap out of admins! just because you CAN
1054 // do it, doesnt mean you SHOULD!
1056 std::string para = "";
1059 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1062 for (int i = 0; i <= MODCOUNT; i++)
1066 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1069 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1070 char app[] = {modechar, 0};
1071 if (ModeIsListMode(modechar,MT_CHANNEL))
1082 strlcat(outlist, app,MAXBUF);
1084 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1091 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1093 strlcat(outlist, app,MAXBUF);
1095 else if (!strchr(outlist,modechar))
1097 strlcat(outlist, app,MAXBUF);
1100 chan->SetCustomMode(modechar,mdir);
1101 // include parameters in output if mode has them
1102 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1104 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1105 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1108 // break, because only one module can handle the mode.
1117 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1125 /* this ensures only the *valid* modes are sent out onto the network */
1126 int xt = strlen(outlist)-1;
1127 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1130 xt = strlen(outlist)-1;
1134 strlcpy(outstr,outlist,MAXBUF);
1135 for (ptr = 0; ptr < pc; ptr++)
1137 strlcat(outstr," ",MAXBUF);
1138 strlcat(outstr,outpars[ptr],MAXBUF);
1142 log(DEBUG,"Local mode change");
1143 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1151 WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1152 // M token for a usermode must go to all servers
1153 char buffer[MAXBUF];
1154 snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1155 NetSendToAll(buffer);
1163 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1164 // M token for a usermode must go to all servers
1165 char buffer[MAXBUF];
1166 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1167 NetSendToAll(buffer);
1174 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1176 bool allowed_umode(char umode, char* sourcemodes,bool adding)
1178 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1179 // RFC1459 specified modes
1180 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1182 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1186 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1187 if ((strchr(sourcemodes,'o')) && (!adding))
1189 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1192 else if (umode == 'o')
1194 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1198 // process any module-defined modes that need oper
1199 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1201 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1205 if (ModeDefined(umode,MT_CLIENT))
1207 // process any module-defined modes that don't need oper
1208 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1209 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1211 // no, this mode needs oper, and this user 'aint got what it takes!
1217 // anything else - return false.
1218 log(DEBUG,"umode %c not known by any ruleset",umode);
1222 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1229 strlcpy(s2->nick,ServerName,NICKMAX);
1230 strlcpy(s2->modes,"o",52);
1237 if (ModeDefined(umode,MT_CLIENT))
1239 for (int i = 0; i <= MODCOUNT; i++)
1241 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1243 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1247 log(DEBUG,"No module claims umode %c",umode);
1266 void handle_mode(char **parameters, int pcnt, userrec *user)
1272 char outpars[MAXBUF];
1274 dest = Find(parameters[0]);
1281 if ((dest) && (pcnt == 1))
1283 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1287 if ((dest) && (pcnt > 1))
1289 std::string tidied = compress_modes(parameters[1],false);
1290 parameters[1] = (char*)tidied.c_str();
1292 char dmodes[MAXBUF];
1293 strlcpy(dmodes,dest->modes,52);
1294 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1299 if (strchr(user->modes,'o'))
1310 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1314 strcpy(outpars,"+");
1317 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1320 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1322 if (parameters[1][i] == ' ')
1324 if (parameters[1][i] == '+')
1328 int t = strlen(outpars)-1;
1329 if ((outpars[t] == '+') || (outpars[t] == '-'))
1335 strcat(outpars,"+");
1341 if (parameters[1][i] == '-')
1345 int t = strlen(outpars)-1;
1346 if ((outpars[t] == '+') || (outpars[t] == '-'))
1352 strcat(outpars,"-");
1360 if (strchr(user->modes,'o'))
1366 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1375 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1377 char umode = parameters[1][i];
1378 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1380 int q = strlen(dmodes);
1381 int r = strlen(outpars);
1383 dmodes[q] = parameters[1][i];
1385 outpars[r] = parameters[1][i];
1386 if (parameters[1][i] == 'o')
1388 FOREACH_MOD OnGlobalOper(dest);
1395 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1397 char umode = parameters[1][i];
1398 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1404 unsigned int r = strlen(outpars);
1406 outpars[r] = parameters[1][i];
1409 for (q = 0; q < strlen(dmodes); q++)
1411 if (dmodes[q] != parameters[1][i])
1415 strlcat(temp,moo,MAXBUF);
1418 strlcpy(dmodes,temp,52);
1431 strlcpy(b,"",MAXBUF);
1434 while (i < strlen (outpars))
1436 b[z++] = outpars[i++];
1438 if (i<strlen(outpars)-1)
1440 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1442 // someones playing silly buggers and trying
1443 // to put a +- or -+ into the line...
1447 if (i == strlen(outpars)-1)
1449 if ((outpars[i] == '-') || (outpars[i] == '+'))
1457 if ((b[z] == '-') || (b[z] == '+'))
1460 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1465 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1466 // M token for a usermode must go to all servers
1467 char buffer[MAXBUF];
1468 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1469 NetSendToAll(buffer);
1472 if (strlen(dmodes)>MAXMODES)
1474 dmodes[MAXMODES-1] = '\0';
1476 log(DEBUG,"Stripped mode line");
1477 log(DEBUG,"Line dest is now %s",dmodes);
1478 strlcpy(dest->modes,dmodes,52);
1485 Ptr = FindChan(parameters[0]);
1490 /* just /modes #channel */
1491 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1492 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1498 char* mode = parameters[1];
1502 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1508 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1510 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1512 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1515 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1517 // list of items for an extmode
1518 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1519 FOREACH_MOD OnSendList(user,Ptr,*mode);
1525 if ((Ptr) && (!has_channel(user,Ptr)))
1527 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1534 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1536 if (MOD_RESULT == ACR_DENY)
1538 if (MOD_RESULT == ACR_DEFAULT)
1540 if (cstatus(user,Ptr) < STATUS_HOP)
1542 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1547 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1552 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1559 void server_mode(char **parameters, int pcnt, userrec *user)
1565 char outpars[MAXBUF];
1567 dest = Find(parameters[0]);
1569 // fix: ChroNiCk found this - we cant use this as debug if its null!
1572 log(DEBUG,"server_mode on %s",dest->nick);
1575 if ((dest) && (pcnt > 1))
1577 std::string tidied = compress_modes(parameters[1],false);
1578 parameters[1] = (char*)tidied.c_str();
1580 char dmodes[MAXBUF];
1581 strlcpy(dmodes,dest->modes,52);
1583 strcpy(outpars,"+");
1586 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1589 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1591 if (parameters[1][i] == ' ')
1593 if (parameters[1][i] == '+')
1597 int t = strlen(outpars)-1;
1598 if ((outpars[t] == '+') || (outpars[t] == '-'))
1604 strcat(outpars,"+");
1610 if (parameters[1][i] == '-')
1614 int t = strlen(outpars)-1;
1615 if ((outpars[t] == '+') || (outpars[t] == '-'))
1621 strcat(outpars,"-");
1628 log(DEBUG,"begin mode processing entry");
1634 log(DEBUG,"umode %c being added",parameters[1][i]);
1635 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1637 char umode = parameters[1][i];
1638 log(DEBUG,"umode %c is an allowed umode",umode);
1639 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1641 int v1 = strlen(dmodes);
1642 int v2 = strlen(outpars);
1644 dmodes[v1] = parameters[1][i];
1646 outpars[v2] = parameters[1][i];
1652 // can only remove a mode they already have
1653 log(DEBUG,"umode %c being removed",parameters[1][i]);
1654 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1656 char umode = parameters[1][i];
1657 log(DEBUG,"umode %c is an allowed umode",umode);
1658 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1664 unsigned int v1 = strlen(outpars);
1666 outpars[v1] = parameters[1][i];
1669 for (q = 0; q < strlen(dmodes); q++)
1671 if (dmodes[q] != parameters[1][i])
1675 strlcat(temp,moo,MAXBUF);
1678 strlcpy(dmodes,temp,52);
1688 strlcpy(b,"",MAXBUF);
1691 while (i < strlen (outpars))
1693 b[z++] = outpars[i++];
1695 if (i<strlen(outpars)-1)
1697 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1699 // someones playing silly buggers and trying
1700 // to put a +- or -+ into the line...
1704 if (i == strlen(outpars)-1)
1706 if ((outpars[i] == '-') || (outpars[i] == '+'))
1714 if ((b[z] == '-') || (b[z] == '+'))
1717 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1722 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1723 // M token for a usermode must go to all servers
1724 char buffer[MAXBUF];
1725 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1726 NetSendToAll(buffer);
1729 if (strlen(dmodes)>MAXMODES)
1731 dmodes[MAXMODES-1] = '\0';
1733 log(DEBUG,"Stripped mode line");
1734 log(DEBUG,"Line dest is now %s",dmodes);
1735 strlcpy(dest->modes,dmodes,MAXMODES);
1742 Ptr = FindChan(parameters[0]);
1745 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1749 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1755 void merge_mode(char **parameters, int pcnt)
1761 char outpars[MAXBUF];
1763 dest = Find(parameters[0]);
1765 // fix: ChroNiCk found this - we cant use this as debug if its null!
1768 log(DEBUG,"merge_mode on %s",dest->nick);
1771 if ((dest) && (pcnt > 1))
1773 std::string tidied = compress_modes(parameters[1],false);
1774 parameters[1] = (char*)tidied.c_str();
1776 char dmodes[MAXBUF];
1777 strlcpy(dmodes,dest->modes,52);
1779 strcpy(outpars,"+");
1782 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1785 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1787 if (parameters[1][i] == ' ')
1789 if (parameters[1][i] == '+')
1793 int t = strlen(outpars)-1;
1794 if ((outpars[t] == '+') || (outpars[t] == '-'))
1800 strcat(outpars,"+");
1806 if (parameters[1][i] == '-')
1810 int t = strlen(outpars)-1;
1811 if ((outpars[t] == '+') || (outpars[t] == '-'))
1817 strcat(outpars,"-");
1824 log(DEBUG,"begin mode processing entry");
1830 log(DEBUG,"umode %c being added",parameters[1][i]);
1831 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1833 char umode = parameters[1][i];
1834 log(DEBUG,"umode %c is an allowed umode",umode);
1835 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1837 int v1 = strlen(dmodes);
1838 int v2 = strlen(outpars);
1840 dmodes[v1] = parameters[1][i];
1842 outpars[v2] = parameters[1][i];
1848 // can only remove a mode they already have
1849 log(DEBUG,"umode %c being removed",parameters[1][i]);
1850 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1852 char umode = parameters[1][i];
1853 log(DEBUG,"umode %c is an allowed umode",umode);
1854 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1860 unsigned int v1 = strlen(outpars);
1862 outpars[v1] = parameters[1][i];
1865 for (q = 0; q < strlen(dmodes); q++)
1867 if (dmodes[q] != parameters[1][i])
1871 strlcat(temp,moo,MAXBUF);
1874 strlcpy(dmodes,temp,52);
1887 while (i < strlen (outpars))
1889 b[z++] = outpars[i++];
1891 if (i<strlen(outpars)-1)
1893 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1895 // someones playing silly buggers and trying
1896 // to put a +- or -+ into the line...
1900 if (i == strlen(outpars)-1)
1902 if ((outpars[i] == '-') || (outpars[i] == '+'))
1910 if ((b[z] == '-') || (b[z] == '+'))
1913 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1916 if (strlen(dmodes)>MAXMODES)
1918 dmodes[MAXMODES-1] = '\0';
1920 log(DEBUG,"Stripped mode line");
1921 log(DEBUG,"Line dest is now %s",dmodes);
1922 strlcpy(dest->modes,dmodes,MAXMODES);
1929 Ptr = FindChan(parameters[0]);
1933 strlcpy(s2.nick,ServerName,NICKMAX);
1934 strcpy(s2.modes,"o");
1936 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1941 void merge_mode2(char **parameters, int pcnt, userrec* user)
1947 char outpars[MAXBUF];
1949 dest = Find(parameters[0]);
1951 // fix: ChroNiCk found this - we cant use this as debug if its null!
1954 log(DEBUG,"merge_mode2 on %s",dest->nick);
1957 if ((dest) && (pcnt > 1))
1959 std::string tidied = compress_modes(parameters[1],false);
1960 parameters[1] = (char*)tidied.c_str();
1962 char dmodes[MAXBUF];
1963 strlcpy(dmodes,dest->modes,52);
1965 strcpy(outpars,"+");
1968 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1970 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1971 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1973 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1976 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1978 if (parameters[1][i] == ' ')
1980 if (parameters[1][i] == '+')
1984 int t = strlen(outpars)-1;
1985 if ((outpars[t] == '+') || (outpars[t] == '-'))
1991 strcat(outpars,"+");
1997 if (parameters[1][i] == '-')
2001 int t = strlen(outpars)-1;
2002 if ((outpars[t] == '+') || (outpars[t] == '-'))
2008 strcat(outpars,"-");
2015 log(DEBUG,"begin mode processing entry");
2021 log(DEBUG,"umode %c being added",parameters[1][i]);
2022 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
2024 char umode = parameters[1][i];
2025 log(DEBUG,"umode %c is an allowed umode",umode);
2026 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2028 int v1 = strlen(dmodes);
2029 int v2 = strlen(outpars);
2031 dmodes[v1] = parameters[1][i];
2033 outpars[v2] = parameters[1][i];
2034 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2040 // can only remove a mode they already have
2041 log(DEBUG,"umode %c being removed",parameters[1][i]);
2042 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
2044 char umode = parameters[1][i];
2045 log(DEBUG,"umode %c is an allowed umode",umode);
2046 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2052 unsigned int v1 = strlen(outpars);
2054 outpars[v1] = parameters[1][i];
2057 for (q = 0; q < strlen(dmodes); q++)
2059 if (dmodes[q] != parameters[1][i])
2063 strlcat(temp,moo,MAXBUF);
2066 strlcpy(dmodes,temp,52);
2073 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2080 while (i < strlen (outpars))
2082 b[z++] = outpars[i++];
2084 if (i<strlen(outpars)-1)
2086 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2088 // someones playing silly buggers and trying
2089 // to put a +- or -+ into the line...
2093 if (i == strlen(outpars)-1)
2095 if ((outpars[i] == '-') || (outpars[i] == '+'))
2103 if ((b[z] == '-') || (b[z] == '+'))
2107 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
2112 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2113 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2116 if (strlen(dmodes)>MAXMODES)
2118 dmodes[MAXMODES-1] = '\0';
2120 log(DEBUG,"Stripped mode line");
2121 log(DEBUG,"Line dest is now %s",dmodes);
2122 strlcpy(dest->modes,dmodes,MAXMODES);
2128 Ptr = FindChan(parameters[0]);
2131 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2134 if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2138 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);