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