]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Fix problem where in certain cases a \0 would be sent out. Thx to network Hak5IRC...
[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         virtual void ReplaceLine(std::string &sentence)
116         {
117                 /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */
118                 int seq = 0;
119                 std::string::iterator i,safei;
120                 for (i = sentence.begin(); i != sentence.end(); ++i)
121                 {
122                         if (((*i == 31) || (*i == 3)))
123                         {
124                                 seq = 1;
125                         }
126                         else if (seq && ( (*i >= '0') && (*i <= '9') || (*i == ',') ) )
127                         {
128                                 seq++;
129                                 if ( (seq <= 4) && (*i == ',') )
130                                         seq = 1;
131                                 else if (seq > 3)
132                                         seq = 0;
133                         }
134                         else
135                                 seq = 0;
136                         
137                         if (seq)
138                         {
139                                 safei = i;
140                                 --i;
141                                 sentence.erase(safei);
142                         }
143                 }
144         }
145
146         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
147         {
148                 if (!IS_LOCAL(user))
149                         return 0;
150
151                 bool active = false;
152                 if (target_type == TYPE_USER)
153                 {
154                         userrec* t = (userrec*)dest;
155                         active = t->IsModeSet('S');
156                 }
157                 else if (target_type == TYPE_CHANNEL)
158                 {
159                         chanrec* t = (chanrec*)dest;
160
161                         // check if we allow ops to bypass filtering, if we do, check if they're opped accordingly.
162                         // note: short circut logic here, don't wreck it. -- w00t
163                         if (!CHANOPS_EXEMPT(ServerInstance, 'S') || CHANOPS_EXEMPT(ServerInstance, 'S') && t->GetStatus(user) != STATUS_OP)
164                                 active = t->IsModeSet('S');
165                 }
166
167                 if (active)
168                 {
169                         this->ReplaceLine(text);
170                 }
171
172                 return 0;
173         }
174         
175         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
176         {
177                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
178         }
179         
180         virtual Version GetVersion()
181         {
182                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
183         }
184         
185 };
186
187 MODULE_INIT(ModuleStripColor);