]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Someone is getting slapped for this; the new hidesplits/hidebans behavior doesn't...
[user/henk/code/inspircd.git] / src / modules / m_stripcolor.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.h"
18
19 /* $ModDesc: Provides channel +S mode (strip ansi colour) */
20
21 /** Handles channel mode +S
22  */
23 class ChannelStripColor : public ModeHandler
24 {
25  public:
26         ChannelStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_CHANNEL, false) { }
27
28         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
29         {
30                 if (adding)
31                 {
32                         if (!channel->IsModeSet('S'))
33                         {
34                                 channel->SetMode('S',true);
35                                 return MODEACTION_ALLOW;
36                         }
37                 }
38                 else
39                 {
40                         if (channel->IsModeSet('S'))
41                         {
42                                 channel->SetMode('S',false);
43                                 return MODEACTION_ALLOW;
44                         }
45                 }
46
47                 return MODEACTION_DENY;
48         }
49 };
50
51 /** Handles user mode +S
52  */
53 class UserStripColor : public ModeHandler
54 {
55  public:
56         UserStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_USER, false) { }
57
58         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
59         {
60                 /* Only opers can change other users modes */
61                 if ((source != dest) && (!*source->oper))
62                         return MODEACTION_DENY;
63
64                 if (adding)
65                 {
66                         if (!dest->IsModeSet('S'))
67                         {
68                                 dest->SetMode('S',true);
69                                 return MODEACTION_ALLOW;
70                         }
71                 }
72                 else
73                 {
74                         if (dest->IsModeSet('S'))
75                         {
76                                 dest->SetMode('S',false);
77                                 return MODEACTION_ALLOW;
78                         }
79                 }
80
81                 return MODEACTION_DENY;
82         }
83 };
84
85
86 class ModuleStripColor : public Module
87 {
88  
89  ConfigReader *Conf, *MyConf;
90  ChannelStripColor *csc;
91  UserStripColor *usc;
92  
93  public:
94         ModuleStripColor(InspIRCd* Me)
95                 : Module::Module(Me)
96         {
97                 
98
99                 usc = new UserStripColor(ServerInstance);
100                 csc = new ChannelStripColor(ServerInstance);
101
102                 if (!ServerInstance->AddMode(usc, 'S') || !ServerInstance->AddMode(csc, 'S'))
103                         throw ModuleException("Could not add new modes!");
104         }
105
106         void Implements(char* List)
107         {
108                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
109         }
110
111         virtual ~ModuleStripColor()
112         {
113                 ServerInstance->Modes->DelMode(usc);
114                 ServerInstance->Modes->DelMode(csc);
115                 DELETE(usc);
116                 DELETE(csc);
117         }
118         
119         // ANSI colour stripping based on C example by Doc (Peter Wood)
120         virtual void ReplaceLine(std::string &sentence)
121         {
122                 int i, a, len, remove;
123                 len = sentence.length();
124
125                 for (i = 0; i < len; i++)
126                 {
127                         remove = 0;
128
129                         switch (sentence[i])
130                         {
131                                 case 2:
132                                 case 15:
133                                 case 22:
134                                 case 21:
135                                 case 31:
136                                         remove++;
137                                 break;
138
139                                 case 3:
140                                         remove = 1;
141
142                                         if (isdigit(sentence[i + remove]))
143                                                 remove++;
144
145                                         if (isdigit(sentence[i + remove]))
146                                                 remove++;
147
148                                         if (sentence[i + remove] == ',')
149                                         {
150                                                 remove += 2;
151
152                                                 if (isdigit(sentence[i + remove]))
153                                                         remove++;
154                                         }
155                                 break;
156                         }
157
158                         if (remove != 0)
159                         {
160                                 len -= remove;
161
162                                 for (a = i; a <= len; a++)
163                                         sentence[a] = sentence[a + remove];
164                                 i--;
165                         }
166                 }
167         }
168         
169         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
170         {
171                 if (!IS_LOCAL(user))
172                         return 0;
173
174                 bool active = false;
175                 if (target_type == TYPE_USER)
176                 {
177                         userrec* t = (userrec*)dest;
178                         active = t->IsModeSet('S');
179                 }
180                 else if (target_type == TYPE_CHANNEL)
181                 {
182                         chanrec* t = (chanrec*)dest;
183                         active = t->IsModeSet('S');
184                 }
185                 if (active)
186                 {
187                         this->ReplaceLine(text);
188                 }
189                 return 0;
190         }
191         
192         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
193         {
194                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
195         }
196         
197         virtual Version GetVersion()
198         {
199                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
200         }
201         
202 };
203
204 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
205
206 class ModuleStripColorFactory : public ModuleFactory
207 {
208  public:
209         ModuleStripColorFactory()
210         {
211         }
212         
213         ~ModuleStripColorFactory()
214         {
215         }
216         
217         virtual Module * CreateModule(InspIRCd* Me)
218         {
219                 return new ModuleStripColor(Me);
220         }
221         
222 };
223
224
225 extern "C" void * init_module( void )
226 {
227         return new ModuleStripColorFactory;
228 }
229