]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
25d2f5a495e9c6553bbc25e6193af0e4ae71359a
[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 "inspircd.h"
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.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)
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         bool AllowChanOps;
89         ChannelStripColor *csc;
90         UserStripColor *usc;
91  
92  public:
93         ModuleStripColor(InspIRCd* Me) : Module(Me)
94         {
95                 usc = new UserStripColor(ServerInstance);
96                 csc = new ChannelStripColor(ServerInstance);
97
98                 if (!ServerInstance->AddMode(usc, 'S') || !ServerInstance->AddMode(csc, 'S'))
99                         throw ModuleException("Could not add new modes!");
100         }
101
102         void Implements(char* List)
103         {
104                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
105         }
106
107         virtual ~ModuleStripColor()
108         {
109                 ServerInstance->Modes->DelMode(usc);
110                 ServerInstance->Modes->DelMode(csc);
111                 DELETE(usc);
112                 DELETE(csc);
113         }
114         
115         // ANSI colour stripping based on C example by Doc (Peter Wood)
116         virtual void ReplaceLine(std::string &sentence)
117         {
118                 int i, a, len, remove;
119                 len = sentence.length();
120
121                 for (i = 0; i < len; i++)
122                 {
123                         remove = 0;
124
125                         switch (sentence[i])
126                         {
127                                 case 2:
128                                 case 15:
129                                 case 22:
130                                 case 21:
131                                 case 31:
132                                         remove++;
133                                 break;
134
135                                 case 3:
136                                         remove = 1;
137
138                                         if (isdigit(sentence[i + remove]))
139                                                 remove++;
140
141                                         if (isdigit(sentence[i + remove]))
142                                                 remove++;
143
144                                         if (sentence[i + remove] == ',')
145                                         {
146                                                 remove += 2;
147
148                                                 if (isdigit(sentence[i + remove]))
149                                                         remove++;
150                                         }
151                                 break;
152                         }
153
154                         if (remove != 0)
155                         {
156                                 len -= remove;
157
158                                 for (a = i; a <= len; a++)
159                                         sentence[a] = sentence[a + remove];
160                                 i--;
161                         }
162                 }
163         }
164
165         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
166         {
167                 if (!IS_LOCAL(user))
168                         return 0;
169
170                 bool active = false;
171                 if (target_type == TYPE_USER)
172                 {
173                         userrec* t = (userrec*)dest;
174                         active = t->IsModeSet('S');
175                 }
176                 else if (target_type == TYPE_CHANNEL)
177                 {
178                         chanrec* t = (chanrec*)dest;
179
180                         // check if we allow ops to bypass filtering, if we do, check if they're opped accordingly.
181                         // note: short circut logic here, don't wreck it. -- w00t
182                         if (!CHANOPS_EXEMPT(ServerInstance, 'S') || CHANOPS_EXEMPT(ServerInstance, 'S') && t->GetStatus(user) != STATUS_OP)
183                                 active = t->IsModeSet('S');
184                 }
185
186                 if (active)
187                 {
188                         this->ReplaceLine(text);
189                 }
190
191                 return 0;
192         }
193         
194         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
195         {
196                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
197         }
198         
199         virtual Version GetVersion()
200         {
201                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
202         }
203         
204 };
205
206 MODULE_INIT(ModuleStripColor);