]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Fix typo opermoth -> opermotd. Thanks Ankit.
[user/henk/code/inspircd.git] / src / modules / m_stripcolor.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 SimpleChannelModeHandler
21 {
22  public:
23         ChannelStripColor(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'S') { }
24 };
25
26 /** Handles user mode +S
27  */
28 class UserStripColor : public SimpleUserModeHandler
29 {
30  public:
31         UserStripColor(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'S') { }
32 };
33
34
35 class ModuleStripColor : public Module
36 {
37         bool AllowChanOps;
38         ChannelStripColor *csc;
39         UserStripColor *usc;
40
41  public:
42         ModuleStripColor(InspIRCd* Me) : Module(Me)
43         {
44                 usc = new UserStripColor(ServerInstance);
45                 csc = new ChannelStripColor(ServerInstance);
46
47                 if (!ServerInstance->Modes->AddMode(usc) || !ServerInstance->Modes->AddMode(csc))
48                         throw ModuleException("Could not add new modes!");
49                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_On005Numeric };
50                 ServerInstance->Modules->Attach(eventlist, this, 3);
51         }
52
53         virtual ~ModuleStripColor()
54         {
55                 ServerInstance->Modes->DelMode(usc);
56                 ServerInstance->Modes->DelMode(csc);
57                 delete usc;
58                 delete csc;
59         }
60
61         virtual void On005Numeric(std::string &output)
62         {
63                 ServerInstance->AddExtBanChar('S');
64         }
65
66         virtual void ReplaceLine(std::string &sentence)
67         {
68                 /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */
69                 int seq = 0;
70                 std::string::iterator i,safei;
71                 for (i = sentence.begin(); i != sentence.end();)
72                 {
73                         if ((*i == 3))
74                                 seq = 1;
75                         else if (seq && (( ((*i >= '0') && (*i <= '9')) || (*i == ',') ) ))
76                         {
77                                 seq++;
78                                 if ( (seq <= 4) && (*i == ',') )
79                                         seq = 1;
80                                 else if (seq > 3)
81                                         seq = 0;
82                         }
83                         else
84                                 seq = 0;
85
86                         if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31)))
87                         {
88                                 if (i != sentence.begin())
89                                 {
90                                         safei = i;
91                                         --i;
92                                         sentence.erase(safei);
93                                         ++i;
94                                 }
95                                 else
96                                 {
97                                         sentence.erase(i);
98                                         i = sentence.begin();
99                                 }
100                         }
101                         else
102                                 ++i;
103                 }
104         }
105
106         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
107         {
108                 if (!IS_LOCAL(user))
109                         return 0;
110
111                 bool active = false;
112                 if (target_type == TYPE_USER)
113                 {
114                         User* t = (User*)dest;
115                         active = t->IsModeSet('S');
116                 }
117                 else if (target_type == TYPE_CHANNEL)
118                 {
119                         Channel* t = (Channel*)dest;
120
121                         // check if we allow ops to bypass filtering, if we do, check if they're opped accordingly.
122                         // note: short circut logic here, don't wreck it. -- w00t
123                         if (CHANOPS_EXEMPT(ServerInstance, 'S') && t->GetStatus(user) == STATUS_OP)
124                         {
125                                 return 0;
126                         }
127
128                         active = t->IsModeSet('S') || t->IsExtBanned(user, 'S');
129                 }
130
131                 if (active)
132                 {
133                         this->ReplaceLine(text);
134                 }
135
136                 return 0;
137         }
138
139         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
140         {
141                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
142         }
143
144         virtual Version GetVersion()
145         {
146                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
147         }
148
149 };
150
151 MODULE_INIT(ModuleStripColor)