]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Just to mess with om's head, remove helperfuncs.h from everywhere
[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 <stdio.h>
20 #include <string>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24
25 #include "inspircd.h"
26
27 /* $ModDesc: Provides channel +S mode (strip ansi colour) */
28
29
30
31 class ChannelStripColor : public ModeHandler
32 {
33  public:
34         ChannelStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_CHANNEL, false) { }
35
36         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
37         {
38                 /* Only opers can change other users modes */
39                 if ((source != dest) && (!*source->oper))
40                         return MODEACTION_DENY;
41
42                 if (adding)
43                 {
44                         if (!channel->IsModeSet('S'))
45                         {
46                                 channel->SetMode('S',true);
47                                 return MODEACTION_ALLOW;
48                         }
49                 }
50                 else
51                 {
52                         if (channel->IsModeSet('S'))
53                         {
54                                 channel->SetMode('S',false);
55                                 return MODEACTION_ALLOW;
56                         }
57                 }
58
59                 return MODEACTION_DENY;
60         }
61 };
62
63 class UserStripColor : public ModeHandler
64 {
65  public:
66         UserStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_USER, false) { }
67
68         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
69         {
70                 /* Only opers can change other users modes */
71                 if ((source != dest) && (!*source->oper))
72                         return MODEACTION_DENY;
73
74                 if (adding)
75                 {
76                         if (!dest->IsModeSet('S'))
77                         {
78                                 dest->SetMode('S',true);
79                                 return MODEACTION_ALLOW;
80                         }
81                 }
82                 else
83                 {
84                         if (dest->IsModeSet('S'))
85                         {
86                                 dest->SetMode('S',false);
87                                 return MODEACTION_ALLOW;
88                         }
89                 }
90
91                 return MODEACTION_DENY;
92         }
93 };
94
95
96 class ModuleStripColor : public Module
97 {
98  
99  ConfigReader *Conf, *MyConf;
100  ChannelStripColor *csc;
101  UserStripColor *usc;
102  
103  public:
104         ModuleStripColor(InspIRCd* Me)
105                 : Module::Module(Me)
106         {
107                 
108
109                 usc = new UserStripColor(ServerInstance);
110                 csc = new ChannelStripColor(ServerInstance);
111
112                 ServerInstance->AddMode(usc, 'S');
113                 ServerInstance->AddMode(csc, 'S');
114         }
115
116         void Implements(char* List)
117         {
118                 List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
119         }
120
121         virtual void On005Numeric(std::string &output)
122         {
123                 ServerInstance->Modes->InsertMode(output,"S",4);
124         }
125         
126         virtual ~ModuleStripColor()
127         {
128                 DELETE(usc);
129                 DELETE(csc);
130         }
131         
132         // ANSI colour stripping by Doc (Peter Wood)
133         virtual void ReplaceLine(std::string &text)
134         {
135                 int i, a, len, remove;
136                 char sentence[MAXBUF];
137                 strlcpy(sentence,text.c_str(),MAXBUF);
138   
139                 len = text.length();
140
141                 for (i = 0; i < len; i++)
142                 {
143                         remove = 0;
144
145                         switch (sentence[i])
146                         {
147                                 case 2:
148                                 case 15:
149                                 case 22:
150                                 case 21:
151                                 case 31:
152                                         remove++;
153                                 break;
154
155                                 case 3:
156                                         remove = 1;
157
158                                         if (isdigit(sentence[i + remove]))
159                                                 remove++;
160
161                                         if (isdigit(sentence[i + remove]))
162                                                 remove++;
163
164                                         if (sentence[i + remove] == ',')
165                                         {
166                                                 remove += 2;
167
168                                                 if (isdigit(sentence[i + remove]))
169                                                         remove++;
170                                         }
171                                 break;
172                         }
173
174                         if (remove != 0)
175                         {
176                                 len -= remove;
177
178                                 for (a = i; a <= len; a++)
179                                         sentence[a] = sentence[a + remove];
180                                 i--;
181                         }
182                 }
183                 
184                 text = sentence;
185         }
186         
187         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
188         {
189                 bool active = false;
190                 if (target_type == TYPE_USER)
191                 {
192                         userrec* t = (userrec*)dest;
193                         active = t->modes['S'-65];
194                 }
195                 else if (target_type == TYPE_CHANNEL)
196                 {
197                         chanrec* t = (chanrec*)dest;
198                         active = (t->IsModeSet('S'));
199                 }
200                 if (active)
201                 {
202                         this->ReplaceLine(text);
203                 }
204                 return 0;
205         }
206         
207         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
208         {
209                 return OnUserPreMessage(user,dest,target_type,text,status);
210         }
211         
212         virtual Version GetVersion()
213         {
214                 // This is version 2 because version 1.x is the unreleased unrealircd module
215                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
216         }
217         
218 };
219
220 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
221
222 class ModuleStripColorFactory : public ModuleFactory
223 {
224  public:
225         ModuleStripColorFactory()
226         {
227         }
228         
229         ~ModuleStripColorFactory()
230         {
231         }
232         
233         virtual Module * CreateModule(InspIRCd* Me)
234         {
235                 return new ModuleStripColor(Me);
236         }
237         
238 };
239
240
241 extern "C" void * init_module( void )
242 {
243         return new ModuleStripColorFactory;
244 }
245