]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Merge pull request #92 from Robby-/insp20-headers
[user/henk/code/inspircd.git] / src / modules / m_stripcolor.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2004, 2008 Craig Edwards <craigedwards@brainbox.cc>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23
24 /* $ModDesc: Provides channel +S mode (strip ansi color) */
25
26 /** Handles channel mode +S
27  */
28 class ChannelStripColor : public SimpleChannelModeHandler
29 {
30  public:
31         ChannelStripColor(Module* Creator) : SimpleChannelModeHandler(Creator, "stripcolor", 'S') { }
32 };
33
34 /** Handles user mode +S
35  */
36 class UserStripColor : public SimpleUserModeHandler
37 {
38  public:
39         UserStripColor(Module* Creator) : SimpleUserModeHandler(Creator, "u_stripcolor", 'S') { }
40 };
41
42
43 class ModuleStripColor : public Module
44 {
45         bool AllowChanOps;
46         ChannelStripColor csc;
47         UserStripColor usc;
48
49  public:
50         ModuleStripColor() : csc(this), usc(this)
51         {
52         }
53
54         void init()
55         {
56                 ServerInstance->Modules->AddService(usc);
57                 ServerInstance->Modules->AddService(csc);
58                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_On005Numeric };
59                 ServerInstance->Modules->Attach(eventlist, this, 3);
60         }
61
62         virtual ~ModuleStripColor()
63         {
64         }
65
66         virtual void On005Numeric(std::string &output)
67         {
68                 ServerInstance->AddExtBanChar('S');
69         }
70
71         virtual void ReplaceLine(std::string &sentence)
72         {
73                 /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */
74                 int seq = 0;
75                 std::string::iterator i,safei;
76                 for (i = sentence.begin(); i != sentence.end();)
77                 {
78                         if ((*i == 3))
79                                 seq = 1;
80                         else if (seq && (( ((*i >= '0') && (*i <= '9')) || (*i == ',') ) ))
81                         {
82                                 seq++;
83                                 if ( (seq <= 4) && (*i == ',') )
84                                         seq = 1;
85                                 else if (seq > 3)
86                                         seq = 0;
87                         }
88                         else
89                                 seq = 0;
90
91                         if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31)))
92                         {
93                                 if (i != sentence.begin())
94                                 {
95                                         safei = i;
96                                         --i;
97                                         sentence.erase(safei);
98                                         ++i;
99                                 }
100                                 else
101                                 {
102                                         sentence.erase(i);
103                                         i = sentence.begin();
104                                 }
105                         }
106                         else
107                                 ++i;
108                 }
109         }
110
111         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
112         {
113                 if (!IS_LOCAL(user))
114                         return MOD_RES_PASSTHRU;
115
116                 bool active = false;
117                 if (target_type == TYPE_USER)
118                 {
119                         User* t = (User*)dest;
120                         active = t->IsModeSet('S');
121                 }
122                 else if (target_type == TYPE_CHANNEL)
123                 {
124                         Channel* t = (Channel*)dest;
125                         ModResult res = ServerInstance->OnCheckExemption(user,t,"stripcolor");
126
127                         if (res == MOD_RES_ALLOW)
128                                 return MOD_RES_PASSTHRU;
129
130                         active = !t->GetExtBanStatus(user, 'S').check(!t->IsModeSet('S'));
131                 }
132
133                 if (active)
134                 {
135                         this->ReplaceLine(text);
136                 }
137
138                 return MOD_RES_PASSTHRU;
139         }
140
141         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
142         {
143                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
144         }
145
146         virtual Version GetVersion()
147         {
148                 return Version("Provides channel +S mode (strip ansi color)", VF_VENDOR);
149         }
150
151 };
152
153 MODULE_INIT(ModuleStripColor)