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