]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Change to use std::string::iterator rather than making a copy of the pointer and...
[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  
89  ConfigReader *Conf, *MyConf;
90  ChannelStripColor *csc;
91  UserStripColor *usc;
92  
93  public:
94         ModuleStripColor(InspIRCd* Me)
95                 : Module::Module(Me)
96         {
97                 
98
99                 usc = new UserStripColor(ServerInstance);
100                 csc = new ChannelStripColor(ServerInstance);
101
102                 ServerInstance->AddMode(usc, 'S');
103                 ServerInstance->AddMode(csc, 'S');
104         }
105
106         void Implements(char* List)
107         {
108                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
109         }
110
111         virtual ~ModuleStripColor()
112         {
113                 ServerInstance->Modes->DelMode(usc);
114                 ServerInstance->Modes->DelMode(csc);
115                 DELETE(usc);
116                 DELETE(csc);
117         }
118         
119         // ANSI colour stripping by Doc (Peter Wood)
120         virtual void ReplaceLine(std::string &text)
121         {
122                 int i, a, len, remove;
123                 char sentence[MAXBUF];
124                 strlcpy(sentence,text.c_str(),MAXBUF);
125   
126                 len = text.length();
127
128                 for (i = 0; i < len; i++)
129                 {
130                         remove = 0;
131
132                         switch (sentence[i])
133                         {
134                                 case 2:
135                                 case 15:
136                                 case 22:
137                                 case 21:
138                                 case 31:
139                                         remove++;
140                                 break;
141
142                                 case 3:
143                                         remove = 1;
144
145                                         if (isdigit(sentence[i + remove]))
146                                                 remove++;
147
148                                         if (isdigit(sentence[i + remove]))
149                                                 remove++;
150
151                                         if (sentence[i + remove] == ',')
152                                         {
153                                                 remove += 2;
154
155                                                 if (isdigit(sentence[i + remove]))
156                                                         remove++;
157                                         }
158                                 break;
159                         }
160
161                         if (remove != 0)
162                         {
163                                 len -= remove;
164
165                                 for (a = i; a <= len; a++)
166                                         sentence[a] = sentence[a + remove];
167                                 i--;
168                         }
169                 }
170                 
171                 text = sentence;
172         }
173         
174         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
175         {
176                 bool active = false;
177                 if (target_type == TYPE_USER)
178                 {
179                         userrec* t = (userrec*)dest;
180                         active = t->modes['S'-65];
181                 }
182                 else if (target_type == TYPE_CHANNEL)
183                 {
184                         chanrec* t = (chanrec*)dest;
185                         active = (t->IsModeSet('S'));
186                 }
187                 if (active)
188                 {
189                         this->ReplaceLine(text);
190                 }
191                 return 0;
192         }
193         
194         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
195         {
196                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
197         }
198         
199         virtual Version GetVersion()
200         {
201                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
202         }
203         
204 };
205
206 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
207
208 class ModuleStripColorFactory : public ModuleFactory
209 {
210  public:
211         ModuleStripColorFactory()
212         {
213         }
214         
215         ~ModuleStripColorFactory()
216         {
217         }
218         
219         virtual Module * CreateModule(InspIRCd* Me)
220         {
221                 return new ModuleStripColor(Me);
222         }
223         
224 };
225
226
227 extern "C" void * init_module( void )
228 {
229         return new ModuleStripColorFactory;
230 }
231