]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
6e0100b46ecdd796144a6b5a5600ade44b937b9d
[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         }
98
99         void Implements(char* List)
100         {
101                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
102         }
103
104         virtual ~ModuleStripColor()
105         {
106                 ServerInstance->Modes->DelMode(usc);
107                 ServerInstance->Modes->DelMode(csc);
108                 DELETE(usc);
109                 DELETE(csc);
110         }
111         
112         virtual void ReplaceLine(std::string &sentence)
113         {
114                 /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */
115                 int seq = 0;
116                 std::string::iterator i,safei;
117                 for (i = sentence.begin(); i != sentence.end(); ++i)
118                 {
119                         if ((*i == 3))
120                                 seq = 1;
121                         else if (seq && ( (*i >= '0') && (*i <= '9') || (*i == ',') ) )
122                         {
123                                 seq++;
124                                 if ( (seq <= 4) && (*i == ',') )
125                                         seq = 1;
126                                 else if (seq > 3)
127                                         seq = 0;
128                         }
129                         else
130                                 seq = 0;
131                         
132                         if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31)))
133                         {
134                                 safei = i;
135                                 --i;
136                                 sentence.erase(safei);
137                         }
138                 }
139         }
140
141         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
142         {
143                 if (!IS_LOCAL(user))
144                         return 0;
145
146                 bool active = false;
147                 if (target_type == TYPE_USER)
148                 {
149                         User* t = (User*)dest;
150                         active = t->IsModeSet('S');
151                 }
152                 else if (target_type == TYPE_CHANNEL)
153                 {
154                         Channel* t = (Channel*)dest;
155
156                         // check if we allow ops to bypass filtering, if we do, check if they're opped accordingly.
157                         // note: short circut logic here, don't wreck it. -- w00t
158                         if (!CHANOPS_EXEMPT(ServerInstance, 'S') || CHANOPS_EXEMPT(ServerInstance, 'S') && t->GetStatus(user) != STATUS_OP)
159                                 active = t->IsModeSet('S');
160                 }
161
162                 if (active)
163                 {
164                         this->ReplaceLine(text);
165                 }
166
167                 return 0;
168         }
169         
170         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
171         {
172                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
173         }
174         
175         virtual Version GetVersion()
176         {
177                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
178         }
179         
180 };
181
182 MODULE_INIT(ModuleStripColor)