]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
2be874f639caf8b07e07de4e34e20063ea36f2f6
[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
16 /* $ModDesc: Provides channel +S mode (strip ansi colour) */
17
18 /** Handles channel mode +S
19  */
20 class ChannelStripColor : public ModeHandler
21 {
22  public:
23         ChannelStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_CHANNEL, false) { }
24
25         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
26         {
27                 if (adding)
28                 {
29                         if (!channel->IsModeSet('S'))
30                         {
31                                 channel->SetMode('S',true);
32                                 return MODEACTION_ALLOW;
33                         }
34                 }
35                 else
36                 {
37                         if (channel->IsModeSet('S'))
38                         {
39                                 channel->SetMode('S',false);
40                                 return MODEACTION_ALLOW;
41                         }
42                 }
43
44                 return MODEACTION_DENY;
45         }
46 };
47
48 /** Handles user mode +S
49  */
50 class UserStripColor : public ModeHandler
51 {
52  public:
53         UserStripColor(InspIRCd* Instance) : ModeHandler(Instance, 'S', 0, 0, false, MODETYPE_USER, false) { }
54
55         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
56         {
57                 /* Only opers can change other users modes */
58                 if (source != dest)
59                         return MODEACTION_DENY;
60
61                 if (adding)
62                 {
63                         if (!dest->IsModeSet('S'))
64                         {
65                                 dest->SetMode('S',true);
66                                 return MODEACTION_ALLOW;
67                         }
68                 }
69                 else
70                 {
71                         if (dest->IsModeSet('S'))
72                         {
73                                 dest->SetMode('S',false);
74                                 return MODEACTION_ALLOW;
75                         }
76                 }
77
78                 return MODEACTION_DENY;
79         }
80 };
81
82
83 class ModuleStripColor : public Module
84 {
85         bool AllowChanOps;
86         ChannelStripColor *csc;
87         UserStripColor *usc;
88  
89  public:
90         ModuleStripColor(InspIRCd* Me) : Module(Me)
91         {
92                 usc = new UserStripColor(ServerInstance);
93                 csc = new ChannelStripColor(ServerInstance);
94
95                 if (!ServerInstance->AddMode(usc) || !ServerInstance->AddMode(csc))
96                         throw ModuleException("Could not add new modes!");
97                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice };
98                 ServerInstance->Modules->Attach(eventlist, this, 2);
99         }
100
101
102         virtual ~ModuleStripColor()
103         {
104                 ServerInstance->Modes->DelMode(usc);
105                 ServerInstance->Modes->DelMode(csc);
106                 delete usc;
107                 delete csc;
108         }
109         
110         virtual void ReplaceLine(std::string &sentence)
111         {
112                 /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */
113                 int seq = 0;
114                 std::string::iterator i,safei;
115                 for (i = sentence.begin(); i != sentence.end(); ++i)
116                 {
117                         if ((*i == 3))
118                                 seq = 1;
119                         else if (seq && ( (*i >= '0') && (*i <= '9') || (*i == ',') ) )
120                         {
121                                 seq++;
122                                 if ( (seq <= 4) && (*i == ',') )
123                                         seq = 1;
124                                 else if (seq > 3)
125                                         seq = 0;
126                         }
127                         else
128                                 seq = 0;
129                         
130                         if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31)))
131                         {
132                                 safei = i;
133                                 --i;
134                                 sentence.erase(safei);
135                         }
136                 }
137         }
138
139         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
140         {
141                 if (!IS_LOCAL(user))
142                         return 0;
143
144                 bool active = false;
145                 if (target_type == TYPE_USER)
146                 {
147                         User* t = (User*)dest;
148                         active = t->IsModeSet('S');
149                 }
150                 else if (target_type == TYPE_CHANNEL)
151                 {
152                         Channel* t = (Channel*)dest;
153
154                         // check if we allow ops to bypass filtering, if we do, check if they're opped accordingly.
155                         // note: short circut logic here, don't wreck it. -- w00t
156                         if (!CHANOPS_EXEMPT(ServerInstance, 'S') || CHANOPS_EXEMPT(ServerInstance, 'S') && t->GetStatus(user) != STATUS_OP)
157                                 active = t->IsModeSet('S');
158                 }
159
160                 if (active)
161                 {
162                         this->ReplaceLine(text);
163                 }
164
165                 return 0;
166         }
167         
168         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
169         {
170                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
171         }
172         
173         virtual Version GetVersion()
174         {
175                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
176         }
177         
178 };
179
180 MODULE_INIT(ModuleStripColor)