]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Added <oper:swhois> to m_swhois, which will override <type:swhois> if specified
[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                 if (adding)
36                 {
37                         if (!channel->IsModeSet('S'))
38                         {
39                                 channel->SetMode('S',true);
40                                 return MODEACTION_ALLOW;
41                         }
42                 }
43                 else
44                 {
45                         if (channel->IsModeSet('S'))
46                         {
47                                 channel->SetMode('S',false);
48                                 return MODEACTION_ALLOW;
49                         }
50                 }
51
52                 return MODEACTION_DENY;
53         }
54 };
55
56 /** Handles user mode +S
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                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
207         }
208         
209 };
210
211 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
212
213 class ModuleStripColorFactory : public ModuleFactory
214 {
215  public:
216         ModuleStripColorFactory()
217         {
218         }
219         
220         ~ModuleStripColorFactory()
221         {
222         }
223         
224         virtual Module * CreateModule(InspIRCd* Me)
225         {
226                 return new ModuleStripColor(Me);
227         }
228         
229 };
230
231
232 extern "C" void * init_module( void )
233 {
234         return new ModuleStripColorFactory;
235 }
236