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