]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
976be2b5c34cfff5e9847e3db975bb19ae65e194
[user/henk/code/inspircd.git] / src / modules / m_stripcolor.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22 #include "inspircd.h"
23
24 /* $ModDesc: Provides channel +S mode (strip ansi colour) */
25
26 /** Handles channel mode +S
27  */
28 class ChannelStripColor : public ModeHandler
29 {
30  public:
31         ChannelStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_CHANNEL, false) { }
32
33         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
34         {
35                 /* Only opers can change other users modes */
36                 if ((source != dest) && (!*source->oper))
37                         return MODEACTION_DENY;
38
39                 if (adding)
40                 {
41                         if (!channel->IsModeSet('S'))
42                         {
43                                 channel->SetMode('S',true);
44                                 return MODEACTION_ALLOW;
45                         }
46                 }
47                 else
48                 {
49                         if (channel->IsModeSet('S'))
50                         {
51                                 channel->SetMode('S',false);
52                                 return MODEACTION_ALLOW;
53                         }
54                 }
55
56                 return MODEACTION_DENY;
57         }
58 };
59
60 /** Handles user mode +S
61  */
62 class UserStripColor : public ModeHandler
63 {
64  public:
65         UserStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_USER, false) { }
66
67         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
68         {
69                 /* Only opers can change other users modes */
70                 if ((source != dest) && (!*source->oper))
71                         return MODEACTION_DENY;
72
73                 if (adding)
74                 {
75                         if (!dest->IsModeSet('S'))
76                         {
77                                 dest->SetMode('S',true);
78                                 return MODEACTION_ALLOW;
79                         }
80                 }
81                 else
82                 {
83                         if (dest->IsModeSet('S'))
84                         {
85                                 dest->SetMode('S',false);
86                                 return MODEACTION_ALLOW;
87                         }
88                 }
89
90                 return MODEACTION_DENY;
91         }
92 };
93
94
95 class ModuleStripColor : public Module
96 {
97  
98  ConfigReader *Conf, *MyConf;
99  ChannelStripColor *csc;
100  UserStripColor *usc;
101  
102  public:
103         ModuleStripColor(InspIRCd* Me)
104                 : Module::Module(Me)
105         {
106                 
107
108                 usc = new UserStripColor(ServerInstance);
109                 csc = new ChannelStripColor(ServerInstance);
110
111                 ServerInstance->AddMode(usc, 'S');
112                 ServerInstance->AddMode(csc, 'S');
113         }
114
115         void Implements(char* List)
116         {
117                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
118         }
119
120         virtual ~ModuleStripColor()
121         {
122                 ServerInstance->Modes->DelMode(usc);
123                 ServerInstance->Modes->DelMode(csc);
124                 DELETE(usc);
125                 DELETE(csc);
126         }
127         
128         // ANSI colour stripping by Doc (Peter Wood)
129         virtual void ReplaceLine(std::string &text)
130         {
131                 int i, a, len, remove;
132                 char sentence[MAXBUF];
133                 strlcpy(sentence,text.c_str(),MAXBUF);
134   
135                 len = text.length();
136
137                 for (i = 0; i < len; i++)
138                 {
139                         remove = 0;
140
141                         switch (sentence[i])
142                         {
143                                 case 2:
144                                 case 15:
145                                 case 22:
146                                 case 21:
147                                 case 31:
148                                         remove++;
149                                 break;
150
151                                 case 3:
152                                         remove = 1;
153
154                                         if (isdigit(sentence[i + remove]))
155                                                 remove++;
156
157                                         if (isdigit(sentence[i + remove]))
158                                                 remove++;
159
160                                         if (sentence[i + remove] == ',')
161                                         {
162                                                 remove += 2;
163
164                                                 if (isdigit(sentence[i + remove]))
165                                                         remove++;
166                                         }
167                                 break;
168                         }
169
170                         if (remove != 0)
171                         {
172                                 len -= remove;
173
174                                 for (a = i; a <= len; a++)
175                                         sentence[a] = sentence[a + remove];
176                                 i--;
177                         }
178                 }
179                 
180                 text = sentence;
181         }
182         
183         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
184         {
185                 bool active = false;
186                 if (target_type == TYPE_USER)
187                 {
188                         userrec* t = (userrec*)dest;
189                         active = t->modes['S'-65];
190                 }
191                 else if (target_type == TYPE_CHANNEL)
192                 {
193                         chanrec* t = (chanrec*)dest;
194                         active = (t->IsModeSet('S'));
195                 }
196                 if (active)
197                 {
198                         this->ReplaceLine(text);
199                 }
200                 return 0;
201         }
202         
203         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
204         {
205                 return OnUserPreMessage(user,dest,target_type,text,status);
206         }
207         
208         virtual Version GetVersion()
209         {
210                 return Version(1, 0, 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