1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 /* $ModDesc: Provides channel +S mode (strip ansi colour) */
21 /** Handles channel mode +S
23 class ChannelStripColor : public ModeHandler
26 ChannelStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_CHANNEL, false) { }
28 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
32 if (!channel->IsModeSet('S'))
34 channel->SetMode('S',true);
35 return MODEACTION_ALLOW;
40 if (channel->IsModeSet('S'))
42 channel->SetMode('S',false);
43 return MODEACTION_ALLOW;
47 return MODEACTION_DENY;
51 /** Handles user mode +S
53 class UserStripColor : public ModeHandler
56 UserStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_USER, false) { }
58 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
60 /* Only opers can change other users modes */
61 if ((source != dest) && (!*source->oper))
62 return MODEACTION_DENY;
66 if (!dest->IsModeSet('S'))
68 dest->SetMode('S',true);
69 return MODEACTION_ALLOW;
74 if (dest->IsModeSet('S'))
76 dest->SetMode('S',false);
77 return MODEACTION_ALLOW;
81 return MODEACTION_DENY;
86 class ModuleStripColor : public Module
89 ChannelStripColor *csc;
93 ModuleStripColor(InspIRCd* Me) : Module::Module(Me)
97 usc = new UserStripColor(ServerInstance);
98 csc = new ChannelStripColor(ServerInstance);
100 if (!ServerInstance->AddMode(usc, 'S') || !ServerInstance->AddMode(csc, 'S'))
101 throw ModuleException("Could not add new modes!");
104 void Implements(char* List)
106 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
109 virtual ~ModuleStripColor()
111 ServerInstance->Modes->DelMode(usc);
112 ServerInstance->Modes->DelMode(csc);
117 // ANSI colour stripping based on C example by Doc (Peter Wood)
118 virtual void ReplaceLine(std::string &sentence)
120 int i, a, len, remove;
121 len = sentence.length();
123 for (i = 0; i < len; i++)
140 if (isdigit(sentence[i + remove]))
143 if (isdigit(sentence[i + remove]))
146 if (sentence[i + remove] == ',')
150 if (isdigit(sentence[i + remove]))
160 for (a = i; a <= len; a++)
161 sentence[a] = sentence[a + remove];
167 virtual void OnRehash(userrec* user, const std::string ¶meter)
169 ConfigReader Conf(ServerInstance);
171 AllowChanOps = Conf.ReadFlag("stripcolor", "allowchanops", 0);
174 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
180 if (target_type == TYPE_USER)
182 userrec* t = (userrec*)dest;
183 active = t->IsModeSet('S');
185 else if (target_type == TYPE_CHANNEL)
187 chanrec* t = (chanrec*)dest;
189 // check if we allow ops to bypass filtering, if we do, check if they're opped accordingly.
190 // note: short circut logic here, don't wreck it. -- w00t
191 if (!AllowChanOps || AllowChanOps && t->GetStatus(user) != STATUS_OP)
192 active = t->IsModeSet('S');
197 this->ReplaceLine(text);
203 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
205 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
208 virtual Version GetVersion()
210 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
215 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
217 class ModuleStripColorFactory : public ModuleFactory
220 ModuleStripColorFactory()
224 ~ModuleStripColorFactory()
228 virtual Module * CreateModule(InspIRCd* Me)
230 return new ModuleStripColor(Me);
236 extern "C" void * init_module( void )
238 return new ModuleStripColorFactory;