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