]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
420e2e6e293c1ad0413b86142b6dd2c7f1f82425
[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 };
50                 ServerInstance->Modules->Attach(eventlist, this, 2);
51         }
52
53
54         virtual ~ModuleStripColor()
55         {
56                 ServerInstance->Modes->DelMode(usc);
57                 ServerInstance->Modes->DelMode(csc);
58                 delete usc;
59                 delete csc;
60         }
61         
62         virtual void ReplaceLine(std::string &sentence)
63         {
64                 /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */
65                 int seq = 0;
66                 std::string::iterator i,safei;
67                 for (i = sentence.begin(); i != sentence.end();)
68                 {
69                         if ((*i == 3))
70                                 seq = 1;
71                         else if (seq && ( (*i >= '0') && (*i <= '9') || (*i == ',') ) )
72                         {
73                                 seq++;
74                                 if ( (seq <= 4) && (*i == ',') )
75                                         seq = 1;
76                                 else if (seq > 3)
77                                         seq = 0;
78                         }
79                         else
80                                 seq = 0;
81                         
82                         if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31)))
83                         {
84                                 if (i != sentence.begin())
85                                 {
86                                         safei = i;
87                                         --i;
88                                         sentence.erase(safei);
89                                         ++i;
90                                 }
91                                 else
92                                 {
93                                         sentence.erase(i);
94                                         i = sentence.begin();
95                                 }
96                         }
97                         else
98                                 ++i;
99                 }
100         }
101
102         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
103         {
104                 if (!IS_LOCAL(user))
105                         return 0;
106
107                 bool active = false;
108                 if (target_type == TYPE_USER)
109                 {
110                         User* t = (User*)dest;
111                         active = t->IsModeSet('S');
112                 }
113                 else if (target_type == TYPE_CHANNEL)
114                 {
115                         Channel* t = (Channel*)dest;
116
117                         // check if we allow ops to bypass filtering, if we do, check if they're opped accordingly.
118                         // note: short circut logic here, don't wreck it. -- w00t
119                         if (CHANOPS_EXEMPT(ServerInstance, 'S') && t->GetStatus(user) == STATUS_OP)
120                         {
121                                 return 0;
122                         }
123
124                         active = t->IsModeSet('S');
125                 }
126
127                 if (active)
128                 {
129                         this->ReplaceLine(text);
130                 }
131
132                 return 0;
133         }
134         
135         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
136         {
137                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
138         }
139         
140         virtual Version GetVersion()
141         {
142                 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
143         }
144         
145 };
146
147 MODULE_INIT(ModuleStripColor)