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