]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Make OnChannelRestrictionApply take a User* instead of a Membership* [jackmcbarn]
[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, "stripcolor", 'S') { }
24 };
25
26 /** Handles user mode +S
27  */
28 class UserStripColor : public SimpleUserModeHandler
29 {
30  public:
31         UserStripColor(Module* Creator) : SimpleUserModeHandler(Creator, "stripcolor", '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         }
53
54         virtual void On005Numeric(std::string &output)
55         {
56                 ServerInstance->AddExtBanChar('S');
57         }
58
59         virtual void ReplaceLine(std::string &sentence)
60         {
61                 /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */
62                 int seq = 0;
63                 std::string::iterator i,safei;
64                 for (i = sentence.begin(); i != sentence.end();)
65                 {
66                         if ((*i == 3))
67                                 seq = 1;
68                         else if (seq && (( ((*i >= '0') && (*i <= '9')) || (*i == ',') ) ))
69                         {
70                                 seq++;
71                                 if ( (seq <= 4) && (*i == ',') )
72                                         seq = 1;
73                                 else if (seq > 3)
74                                         seq = 0;
75                         }
76                         else
77                                 seq = 0;
78
79                         if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31)))
80                         {
81                                 if (i != sentence.begin())
82                                 {
83                                         safei = i;
84                                         --i;
85                                         sentence.erase(safei);
86                                         ++i;
87                                 }
88                                 else
89                                 {
90                                         sentence.erase(i);
91                                         i = sentence.begin();
92                                 }
93                         }
94                         else
95                                 ++i;
96                 }
97         }
98
99         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
100         {
101                 if (!IS_LOCAL(user))
102                         return MOD_RES_PASSTHRU;
103
104                 bool active = false;
105                 if (target_type == TYPE_USER)
106                 {
107                         User* t = (User*)dest;
108                         active = t->IsModeSet('S');
109                 }
110                 else if (target_type == TYPE_CHANNEL)
111                 {
112                         Channel* t = (Channel*)dest;
113                         ModResult res;
114                         FIRST_MOD_RESULT(OnChannelRestrictionApply, res, (user,t,"stripcolor"));
115
116                         if (res == MOD_RES_ALLOW)
117                                 return MOD_RES_PASSTHRU;
118
119                         active = !t->GetExtBanStatus(user, 'S').check(!t->IsModeSet('S'));
120                 }
121
122                 if (active)
123                 {
124                         this->ReplaceLine(text);
125                 }
126
127                 return MOD_RES_PASSTHRU;
128         }
129
130         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
131         {
132                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
133         }
134
135         virtual Version GetVersion()
136         {
137                 return Version("Provides channel +S mode (strip ansi colour)", VF_COMMON | VF_VENDOR, API_VERSION);
138         }
139
140 };
141
142 MODULE_INIT(ModuleStripColor)