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 * ---------------------------------------------------
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20 #include "inspircd_config.h"
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
30 #include <ext/hash_map>
42 #include "connection.h"
53 #include "inspstring.h"
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<std::string> module_names;
64 extern char ServerName[MAXBUF];
65 extern char Network[MAXBUF];
66 extern char ServerDesc[MAXBUF];
67 extern char AdminName[MAXBUF];
68 extern char AdminEmail[MAXBUF];
69 extern char AdminNick[MAXBUF];
70 extern char diepass[MAXBUF];
71 extern char restartpass[MAXBUF];
72 extern char motd[MAXBUF];
73 extern char rules[MAXBUF];
74 extern char list[MAXBUF];
75 extern char PrefixQuit[MAXBUF];
76 extern char DieValue[MAXBUF];
78 extern bool AllowHalfop;
79 extern bool AllowProtect;
80 extern bool AllowFounder;
84 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
88 if ((!user) || (!dest) || (!chan))
90 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
96 log(DEFAULT,"the target nickname given to give_ops was invalid");
97 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
103 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
104 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
111 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
113 if (MOD_RESULT == ACR_DENY)
115 if (MOD_RESULT == ACR_DEFAULT)
117 if ((status < STATUS_OP) && (!is_uline(user->server)))
119 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
120 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
126 for (int i = 0; i != MAXCHANS; i++)
128 if ((d->chans[i].channel != NULL) && (chan != NULL))
129 if (!strcasecmp(d->chans[i].channel->name,chan->name))
131 if (d->chans[i].uc_modes & UCMODE_OP)
133 /* mode already set on user, dont allow multiple */
134 log(DEFAULT,"The target user given to give_ops was already opped on the channel");
137 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
138 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
142 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
147 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
151 if ((!user) || (!dest) || (!chan))
153 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
160 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
165 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
171 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
173 if (MOD_RESULT == ACR_DENY)
175 if (MOD_RESULT == ACR_DEFAULT)
177 if ((status < STATUS_OP) && (!is_uline(user->server)))
179 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
184 for (int i = 0; i != MAXCHANS; i++)
186 if ((d->chans[i].channel != NULL) && (chan != NULL))
187 if (!strcasecmp(d->chans[i].channel->name,chan->name))
189 if (d->chans[i].uc_modes & UCMODE_HOP)
191 /* mode already set on user, dont allow multiple */
194 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
195 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
203 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
207 if ((!user) || (!dest) || (!chan))
209 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
216 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
221 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
227 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
229 if (MOD_RESULT == ACR_DENY)
231 if (MOD_RESULT == ACR_DEFAULT)
233 if ((status < STATUS_HOP) && (!is_uline(user->server)))
235 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
240 for (int i = 0; i != MAXCHANS; i++)
242 if ((d->chans[i].channel != NULL) && (chan != NULL))
243 if (!strcasecmp(d->chans[i].channel->name,chan->name))
245 if (d->chans[i].uc_modes & UCMODE_VOICE)
247 /* mode already set on user, dont allow multiple */
250 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
251 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
259 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
263 if ((!user) || (!dest) || (!chan))
265 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
272 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
273 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
278 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
279 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
285 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
287 if (MOD_RESULT == ACR_DENY)
289 if (MOD_RESULT == ACR_DEFAULT)
291 if ((status < STATUS_OP) && (!is_uline(user->server)))
293 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
298 for (int i = 0; i != MAXCHANS; i++)
300 if ((d->chans[i].channel != NULL) && (chan != NULL))
301 if (!strcasecmp(d->chans[i].channel->name,chan->name))
303 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
305 /* mode already set on user, dont allow multiple */
308 d->chans[i].uc_modes ^= UCMODE_OP;
309 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
313 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
318 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
322 if ((!user) || (!dest) || (!chan))
324 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
331 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
336 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
342 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
344 if (MOD_RESULT == ACR_DENY)
346 if (MOD_RESULT == ACR_DEFAULT)
348 if ((status < STATUS_OP) && (!is_uline(user->server)))
350 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
355 for (int i = 0; i != MAXCHANS; i++)
357 if ((d->chans[i].channel != NULL) && (chan != NULL))
358 if (!strcasecmp(d->chans[i].channel->name,chan->name))
360 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
362 /* mode already set on user, dont allow multiple */
365 d->chans[i].uc_modes ^= UCMODE_HOP;
366 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
374 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
378 if ((!user) || (!dest) || (!chan))
380 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
387 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
392 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
398 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
400 if (MOD_RESULT == ACR_DENY)
402 if (MOD_RESULT == ACR_DEFAULT)
404 if ((status < STATUS_HOP) && (!is_uline(user->server)))
406 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
411 for (int i = 0; i != MAXCHANS; i++)
413 if ((d->chans[i].channel != NULL) && (chan != NULL))
414 if (!strcasecmp(d->chans[i].channel->name,chan->name))
416 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
418 /* mode already set on user, dont allow multiple */
421 d->chans[i].uc_modes ^= UCMODE_VOICE;
422 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
430 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
432 if ((!user) || (!dest) || (!chan)) {
433 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
438 if ((!user) || (!dest) || (!chan))
440 int l = strlen(dest);
441 if (strchr(dest,'!')==0)
443 if (strchr(dest,'@')==0)
445 for (int i = 0; i < l; i++)
448 for (int i = 0; i < l; i++)
452 for (int i = 0; i < l; i++)
458 for (int i = 0; i < l; i++)
464 long maxbans = GetMaxBans(chan->name);
465 if (chan->bans.size() > maxbans)
467 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);
471 log(DEBUG,"add_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))
478 // dont allow a user to set the same ban twice
484 strncpy(b.data,dest,MAXBUF);
485 strncpy(b.set_by,user->nick,NICKMAX);
486 chan->bans.push_back(b);
490 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
492 if ((!user) || (!dest) || (!chan)) {
493 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
497 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
498 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
500 if (!strcasecmp(i->data,dest))
509 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
510 // a section further down the chain tidies up the +-+- crap.
511 std::string compress_modes(std::string modes,bool channelmodes)
516 memset(counts,0,sizeof(counts));
517 memset(active,0,sizeof(active));
518 log(DEBUG,"compress_modes: %s",modes.c_str());
519 for (int i = 0; i < modes.length(); i++)
521 if ((modes[i] == '+') || (modes[i] == '-'))
525 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
527 log(DEBUG,"Tidy mode %c",modes[i]);
528 counts[(unsigned int)modes[i]]++;
529 active[(unsigned int)modes[i]] = true;
534 log(DEBUG,"Tidy mode %c",modes[i]);
535 counts[(unsigned int)modes[i]]++;
536 active[(unsigned int)modes[i]] = true;
539 for (int j = 65; j < 127; j++)
541 if ((counts[j] > 1) && (active[j] == true))
544 v[0] = (unsigned char)j;
546 std::string::size_type pos = modes.find(std::string(v));
547 if (pos != std::string::npos)
549 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
550 while (modes.find(std::string(v)) != std::string::npos)
551 modes.erase(modes.find(std::string(v)),1);
552 log(DEBUG,"New mode line: %s",modes.c_str());
559 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
562 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
566 char modelist[MAXBUF];
567 char outlist[MAXBUF];
569 char outpars[32][MAXBUF];
575 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
583 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
585 if (MOD_RESULT == ACR_DENY)
588 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
590 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
591 * parameters[2] onwards are parameters for
592 * modes that require them :) */
593 strlcpy(outlist,"+",MAXBUF);
596 log(DEBUG,"process_modes: modelist: %s",modelist);
598 std::string tidied = compress_modes(modelist,true);
599 strlcpy(modelist,tidied.c_str(),MAXBUF);
601 int len = strlen(modelist);
602 while (modelist[len-1] == ' ')
603 modelist[--len] = '\0';
604 for (ptr = 0; ptr < len; ptr++)
609 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
611 char modechar = modelist[ptr];
612 switch (modelist[ptr])
617 int t = strlen(outlist)-1;
618 if ((outlist[t] == '+') || (outlist[t] == '-'))
634 int t = strlen(outlist)-1;
635 if ((outlist[t] == '+') || (outlist[t] == '-'))
649 if ((param >= pcnt)) break;
650 log(DEBUG,"Enough parameters left");
654 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
657 log(DEBUG,"calling give_ops");
658 r = give_ops(user,parameters[param++],chan,status);
665 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
668 log(DEBUG,"calling take_ops");
669 r = take_ops(user,parameters[param++],chan,status);
675 strlcat(outlist,"o",MAXBUF);
676 strlcpy(outpars[pc++],r,MAXBUF);
681 if (((param >= pcnt)) || (!AllowHalfop)) break;
685 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
688 r = give_hops(user,parameters[param++],chan,status);
695 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
698 r = take_hops(user,parameters[param++],chan,status);
704 strlcat(outlist,"h",MAXBUF);
705 strlcpy(outpars[pc++],r,MAXBUF);
711 if ((param >= pcnt)) break;
715 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
718 r = give_voice(user,parameters[param++],chan,status);
725 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
728 r = take_voice(user,parameters[param++],chan,status);
734 strlcat(outlist,"v",MAXBUF);
735 strlcpy(outpars[pc++],r,MAXBUF);
740 if ((param >= pcnt)) break;
744 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
747 r = add_ban(user,parameters[param++],chan,status);
754 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
757 r = take_ban(user,parameters[param++],chan,status);
763 strlcat(outlist,"b",MAXBUF);
764 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
778 if (previously_unset_k)
780 previously_set_k = true;
782 if (!strcmp(chan->key,""))
785 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
790 strlcpy(key,parameters[param++],32);
791 strlcpy(outpars[pc++],key,MAXBUF);
792 strlcpy(chan->key,key,MAXBUF);
800 /* checks on -k are case sensitive and only accurate to the
801 first 32 characters */
802 if (previously_set_k)
804 previously_unset_k = true;
808 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
811 strlcpy(key,parameters[param++],32);
812 /* only allow -k if correct key given */
813 if (!strcmp(chan->key,key))
815 strlcat(outlist,"k",MAXBUF);
816 strlcpy(chan->key,"",MAXBUF);
817 strlcpy(outpars[pc++],key,MAXBUF);
827 if (previously_set_l)
829 previously_unset_l = true;
831 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
842 if ((param >= pcnt)) break;
847 if (previously_unset_l)
849 previously_set_l = true;
850 bool invalid = false;
851 for (int i = 0; i < strlen(parameters[param]); i++)
853 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
858 if (atoi(parameters[param]) < 1)
867 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
871 chan->limit = atoi(parameters[param]);
873 // reported by mech: large values cause underflow
875 chan->limit = 0x7FFFFF;
880 strlcat(outlist,"l",MAXBUF);
881 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
889 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
892 if (chan->inviteonly != mdir)
894 strlcat(outlist,"i",MAXBUF);
896 chan->inviteonly = mdir;
902 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
905 if (chan->topiclock != mdir)
907 strlcat(outlist,"t",MAXBUF);
909 chan->topiclock = mdir;
915 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
918 if (chan->noexternal != mdir)
920 strlcat(outlist,"n",MAXBUF);
922 chan->noexternal = mdir;
928 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
931 if (chan->moderated != mdir)
933 strlcat(outlist,"m",MAXBUF);
935 chan->moderated = mdir;
941 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
944 if (chan->secret != mdir)
952 strlcat(outlist,"-p+",MAXBUF);
956 strlcat(outlist,"+p-",MAXBUF);
966 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
969 if (chan->c_private != mdir)
971 strlcat(outlist,"p",MAXBUF);
977 strlcat(outlist,"-s+",MAXBUF);
981 strlcat(outlist,"+s-",MAXBUF);
985 chan->c_private = mdir;
990 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
993 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
995 if (!ModeIsListMode(modechar,MT_CHANNEL))
997 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1001 if (ModeDefined(modechar,MT_CHANNEL))
1003 log(DEBUG,"A module has claimed this mode");
1006 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1008 p.push_back(parameters[param]);
1010 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1012 p.push_back(parameters[param]);
1015 bool handled = false;
1018 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1019 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1021 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1027 // BIG ASS IDIOTIC CODER WARNING!
1028 // Using OnRawMode on another modules mode's behavour
1029 // will confuse the crap out of admins! just because you CAN
1030 // do it, doesnt mean you SHOULD!
1032 std::string para = "";
1035 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1038 for (int i = 0; i <= MODCOUNT; i++)
1042 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1045 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1046 char app[] = {modechar, 0};
1047 if (ModeIsListMode(modechar,MT_CHANNEL))
1057 strlcat(outlist, app,MAXBUF);
1059 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1066 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1068 strlcat(outlist, app,MAXBUF);
1070 else if (!strchr(outlist,modechar))
1072 strlcat(outlist, app,MAXBUF);
1075 chan->SetCustomMode(modechar,mdir);
1076 // include parameters in output if mode has them
1077 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1079 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1080 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1083 // break, because only one module can handle the mode.
1092 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1100 /* this ensures only the *valid* modes are sent out onto the network */
1101 int xt = strlen(outlist)-1;
1102 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1105 xt = strlen(outlist)-1;
1109 strlcpy(outstr,outlist,MAXBUF);
1110 for (ptr = 0; ptr < pc; ptr++)
1112 strlcat(outstr," ",MAXBUF);
1113 strlcat(outstr,outpars[ptr],MAXBUF);
1117 log(DEBUG,"Local mode change");
1118 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1126 WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1127 // M token for a usermode must go to all servers
1128 char buffer[MAXBUF];
1129 snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1130 NetSendToAll(buffer);
1138 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1139 // M token for a usermode must go to all servers
1140 char buffer[MAXBUF];
1141 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1142 NetSendToAll(buffer);
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)
1153 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1154 // RFC1459 specified modes
1155 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1157 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1161 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1162 if ((strchr(sourcemodes,'o')) && (!adding))
1164 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1167 else if (umode == 'o')
1169 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1173 // process any module-defined modes that need oper
1174 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1176 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1180 if (ModeDefined(umode,MT_CLIENT))
1182 // process any module-defined modes that don't need oper
1183 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1184 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1186 // no, this mode needs oper, and this user 'aint got what it takes!
1192 // anything else - return false.
1193 log(DEBUG,"umode %c not known by any ruleset",umode);
1197 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1204 strlcpy(s2->nick,ServerName,NICKMAX);
1205 strcpy(s2->modes,"o");
1212 if (ModeDefined(umode,MT_CLIENT))
1214 for (int i = 0; i <= MODCOUNT; i++)
1216 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1218 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1222 log(DEBUG,"No module claims umode %c",umode);
1241 void handle_mode(char **parameters, int pcnt, userrec *user)
1247 char outpars[MAXBUF];
1249 dest = Find(parameters[0]);
1256 if ((dest) && (pcnt == 1))
1258 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1262 if ((dest) && (pcnt > 1))
1264 std::string tidied = compress_modes(parameters[1],false);
1265 parameters[1] = (char*)tidied.c_str();
1267 char dmodes[MAXBUF];
1268 strlcpy(dmodes,dest->modes,MAXBUF);
1269 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1274 if (strchr(user->modes,'o'))
1285 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1289 strcpy(outpars,"+");
1292 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1295 for (int i = 0; i < strlen(parameters[1]); i++)
1297 if (parameters[1][i] == ' ')
1299 if (parameters[1][i] == '+')
1303 int t = strlen(outpars)-1;
1304 if ((outpars[t] == '+') || (outpars[t] == '-'))
1310 strcat(outpars,"+");
1316 if (parameters[1][i] == '-')
1320 int t = strlen(outpars)-1;
1321 if ((outpars[t] == '+') || (outpars[t] == '-'))
1327 strcat(outpars,"-");
1335 if (strchr(user->modes,'o'))
1341 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1350 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1352 char umode = parameters[1][i];
1353 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1355 int q = strlen(dmodes);
1356 int r = strlen(outpars);
1358 dmodes[q] = parameters[1][i];
1360 outpars[r] = parameters[1][i];
1361 if (parameters[1][i] == 'o')
1363 FOREACH_MOD OnGlobalOper(dest);
1370 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1372 char umode = parameters[1][i];
1373 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1379 int r = strlen(outpars);
1381 outpars[r] = parameters[1][i];
1384 for (q = 0; q < strlen(dmodes); q++)
1386 if (dmodes[q] != parameters[1][i])
1390 strlcat(temp,moo,MAXBUF);
1393 strlcpy(dmodes,temp,MAXBUF);
1406 strlcpy(b,"",MAXBUF);
1409 while (i < strlen (outpars))
1411 b[z++] = outpars[i++];
1413 if (i<strlen(outpars)-1)
1415 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1417 // someones playing silly buggers and trying
1418 // to put a +- or -+ into the line...
1422 if (i == strlen(outpars)-1)
1424 if ((outpars[i] == '-') || (outpars[i] == '+'))
1432 if ((b[z] == '-') || (b[z] == '+'))
1435 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1438 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1440 // M token for a usermode must go to all servers
1441 char buffer[MAXBUF];
1442 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1443 NetSendToAll(buffer);
1445 if (strlen(dmodes)>MAXMODES)
1447 dmodes[MAXMODES-1] = '\0';
1449 log(DEBUG,"Stripped mode line");
1450 log(DEBUG,"Line dest is now %s",dmodes);
1451 strlcpy(dest->modes,dmodes,MAXMODES);
1458 Ptr = FindChan(parameters[0]);
1463 /* just /modes #channel */
1464 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1465 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1471 char* mode = parameters[1];
1475 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1481 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1483 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1485 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1488 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1490 // list of items for an extmode
1491 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1492 FOREACH_MOD OnSendList(user,Ptr,*mode);
1498 if ((Ptr) && (!has_channel(user,Ptr)))
1500 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1507 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1509 if (MOD_RESULT == ACR_DENY)
1511 if (MOD_RESULT == ACR_DEFAULT)
1513 if (cstatus(user,Ptr) < STATUS_HOP)
1515 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1520 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1525 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1532 void server_mode(char **parameters, int pcnt, userrec *user)
1538 char outpars[MAXBUF];
1540 dest = Find(parameters[0]);
1542 // fix: ChroNiCk found this - we cant use this as debug if its null!
1545 log(DEBUG,"server_mode on %s",dest->nick);
1548 if ((dest) && (pcnt > 1))
1550 std::string tidied = compress_modes(parameters[1],false);
1551 parameters[1] = (char*)tidied.c_str();
1553 char dmodes[MAXBUF];
1554 strlcpy(dmodes,dest->modes,MAXBUF);
1556 strcpy(outpars,"+");
1559 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1562 for (int i = 0; i < strlen(parameters[1]); i++)
1564 if (parameters[1][i] == ' ')
1566 if (parameters[1][i] == '+')
1570 int t = strlen(outpars)-1;
1571 if ((outpars[t] == '+') || (outpars[t] == '-'))
1577 strcat(outpars,"+");
1583 if (parameters[1][i] == '-')
1587 int t = strlen(outpars)-1;
1588 if ((outpars[t] == '+') || (outpars[t] == '-'))
1594 strcat(outpars,"-");
1601 log(DEBUG,"begin mode processing entry");
1607 log(DEBUG,"umode %c being added",parameters[1][i]);
1608 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1610 char umode = parameters[1][i];
1611 log(DEBUG,"umode %c is an allowed umode",umode);
1612 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1614 int v1 = strlen(dmodes);
1615 int v2 = strlen(outpars);
1617 dmodes[v1] = parameters[1][i];
1619 outpars[v2] = parameters[1][i];
1625 // can only remove a mode they already have
1626 log(DEBUG,"umode %c being removed",parameters[1][i]);
1627 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1629 char umode = parameters[1][i];
1630 log(DEBUG,"umode %c is an allowed umode",umode);
1631 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1637 int v1 = strlen(outpars);
1639 outpars[v1] = parameters[1][i];
1642 for (q = 0; q < strlen(dmodes); q++)
1644 if (dmodes[q] != parameters[1][i])
1648 strlcat(temp,moo,MAXBUF);
1651 strlcpy(dmodes,temp,MAXBUF);
1661 strlcpy(b,"",MAXBUF);
1664 while (i < strlen (outpars))
1666 b[z++] = outpars[i++];
1668 if (i<strlen(outpars)-1)
1670 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1672 // someones playing silly buggers and trying
1673 // to put a +- or -+ into the line...
1677 if (i == strlen(outpars)-1)
1679 if ((outpars[i] == '-') || (outpars[i] == '+'))
1687 if ((b[z] == '-') || (b[z] == '+'))
1690 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1693 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1695 // M token for a usermode must go to all servers
1696 char buffer[MAXBUF];
1697 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1698 NetSendToAll(buffer);
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,MAXBUF);
1750 strcpy(outpars,"+");
1753 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1756 for (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)))
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)) && (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 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,MAXBUF);
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,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,MAXBUF);
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 (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)))
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)) && (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 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,MAXBUF);
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] == '+'))
2077 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2081 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2082 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2084 if (strlen(dmodes)>MAXMODES)
2086 dmodes[MAXMODES-1] = '\0';
2088 log(DEBUG,"Stripped mode line");
2089 log(DEBUG,"Line dest is now %s",dmodes);
2090 strlcpy(dest->modes,dmodes,MAXMODES);
2097 Ptr = FindChan(parameters[0]);
2100 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2103 if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2107 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);