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