]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
OOPS! We try again, since I'm smoking craq. LF is 0x0a NOT CR.
[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 == 3))
123                                 seq = 1;
124                         else if (seq && ( (*i >= '0') && (*i <= '9') || (*i == ',') ) )
125                         {
126                                 seq++;
127                                 if ( (seq <= 4) && (*i == ',') )
128                                         seq = 1;
129                                 else if (seq > 3)
130                                         seq = 0;
131                         }
132                         else
133                                 seq = 0;
134                         
135                         if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31)))
136                         {
137                                 safei = i;
138                                 --i;
139                                 sentence.erase(safei);
140                         }
141                 }
142         }
143
144         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
145         {
146                 if (!IS_LOCAL(user))
147                         return 0;
148
149                 bool active = false;
150                 if (target_type == TYPE_USER)
151                 {
152                         userrec* t = (userrec*)dest;
153                         active = t->IsModeSet('S');
154                 }
155                 else if (target_type == TYPE_CHANNEL)
156                 {
157                         chanrec* t = (chanrec*)dest;
158
159                         // check if we allow ops to bypass filtering, if we do, check if they're opped accordingly.
160                         // note: short circut logic here, don't wreck it. -- w00t
161                         if (!CHANOPS_EXEMPT(ServerInstance, 'S') || CHANOPS_EXEMPT(ServerInstance, 'S') && t->GetStatus(user) != STATUS_OP)
162                                 active = t->IsModeSet('S');
163                 }
164
165                 if (active)
166                 {
167                         this->ReplaceLine(text);
168                 }
169
170                 return 0;
171         }
172         
173         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
174         {
175                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
176         }
177         
178         virtual Version GetVersion()
179         {
180                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
181         }
182         
183 };
184
185 MODULE_INIT(ModuleStripColor)