]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
e3a1c39da362ce4d4af73de347ddc64342b1c647
[user/henk/code/inspircd.git] / src / modules / m_stripcolor.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.h"
18
19 /* $ModDesc: Provides channel +S mode (strip ansi colour) */
20
21 /** Handles channel mode +S
22  */
23 class ChannelStripColor : public ModeHandler
24 {
25  public:
26         ChannelStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_CHANNEL, false) { }
27
28         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
29         {
30                 if (adding)
31                 {
32                         if (!channel->IsModeSet('S'))
33                         {
34                                 channel->SetMode('S',true);
35                                 return MODEACTION_ALLOW;
36                         }
37                 }
38                 else
39                 {
40                         if (channel->IsModeSet('S'))
41                         {
42                                 channel->SetMode('S',false);
43                                 return MODEACTION_ALLOW;
44                         }
45                 }
46
47                 return MODEACTION_DENY;
48         }
49 };
50
51 /** Handles user mode +S
52  */
53 class UserStripColor : public ModeHandler
54 {
55  public:
56         UserStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_USER, false) { }
57
58         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
59         {
60                 /* Only opers can change other users modes */
61                 if ((source != dest) && (!*source->oper))
62                         return MODEACTION_DENY;
63
64                 if (adding)
65                 {
66                         if (!dest->IsModeSet('S'))
67                         {
68                                 dest->SetMode('S',true);
69                                 return MODEACTION_ALLOW;
70                         }
71                 }
72                 else
73                 {
74                         if (dest->IsModeSet('S'))
75                         {
76                                 dest->SetMode('S',false);
77                                 return MODEACTION_ALLOW;
78                         }
79                 }
80
81                 return MODEACTION_DENY;
82         }
83 };
84
85
86 class ModuleStripColor : public Module
87 {
88         bool AllowChanOps;
89         ChannelStripColor *csc;
90         UserStripColor *usc;
91  
92  public:
93         ModuleStripColor(InspIRCd* Me) : Module::Module(Me)
94         {
95                 OnRehash(NULL, "");
96
97                 usc = new UserStripColor(ServerInstance);
98                 csc = new ChannelStripColor(ServerInstance);
99
100                 if (!ServerInstance->AddMode(usc, 'S') || !ServerInstance->AddMode(csc, 'S'))
101                         throw ModuleException("Could not add new modes!");
102         }
103
104         void Implements(char* List)
105         {
106                 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
107         }
108
109         virtual ~ModuleStripColor()
110         {
111                 ServerInstance->Modes->DelMode(usc);
112                 ServerInstance->Modes->DelMode(csc);
113                 DELETE(usc);
114                 DELETE(csc);
115         }
116         
117         // ANSI colour stripping based on C example by Doc (Peter Wood)
118         virtual void ReplaceLine(std::string &sentence)
119         {
120                 int i, a, len, remove;
121                 len = sentence.length();
122
123                 for (i = 0; i < len; i++)
124                 {
125                         remove = 0;
126
127                         switch (sentence[i])
128                         {
129                                 case 2:
130                                 case 15:
131                                 case 22:
132                                 case 21:
133                                 case 31:
134                                         remove++;
135                                 break;
136
137                                 case 3:
138                                         remove = 1;
139
140                                         if (isdigit(sentence[i + remove]))
141                                                 remove++;
142
143                                         if (isdigit(sentence[i + remove]))
144                                                 remove++;
145
146                                         if (sentence[i + remove] == ',')
147                                         {
148                                                 remove += 2;
149
150                                                 if (isdigit(sentence[i + remove]))
151                                                         remove++;
152                                         }
153                                 break;
154                         }
155
156                         if (remove != 0)
157                         {
158                                 len -= remove;
159
160                                 for (a = i; a <= len; a++)
161                                         sentence[a] = sentence[a + remove];
162                                 i--;
163                         }
164                 }
165         }
166
167         virtual void OnRehash(userrec* user, const std::string &parameter)
168         {
169                 ConfigReader Conf(ServerInstance);
170
171                 AllowChanOps = Conf.ReadFlag("stripcolor", "allowchanops", 0);
172         }
173         
174         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
175         {
176                 if (!IS_LOCAL(user))
177                         return 0;
178
179                 bool active = false;
180                 if (target_type == TYPE_USER)
181                 {
182                         userrec* t = (userrec*)dest;
183                         active = t->IsModeSet('S');
184                 }
185                 else if (target_type == TYPE_CHANNEL)
186                 {
187                         chanrec* t = (chanrec*)dest;
188
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');
193                 }
194
195                 if (active)
196                 {
197                         this->ReplaceLine(text);
198                 }
199
200                 return 0;
201         }
202         
203         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
204         {
205                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
206         }
207         
208         virtual Version GetVersion()
209         {
210                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
211         }
212         
213 };
214
215 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
216
217 class ModuleStripColorFactory : public ModuleFactory
218 {
219  public:
220         ModuleStripColorFactory()
221         {
222         }
223         
224         ~ModuleStripColorFactory()
225         {
226         }
227         
228         virtual Module * CreateModule(InspIRCd* Me)
229         {
230                 return new ModuleStripColor(Me);
231         }
232         
233 };
234
235
236 extern "C" void * init_module( void )
237 {
238         return new ModuleStripColorFactory;
239 }
240