]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Add Base64 encode/decode functions to the core
[user/henk/code/inspircd.git] / src / modules / m_stripcolor.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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, "u_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         }
45
46         void init()
47         {
48                 ServerInstance->Modules->AddService(usc);
49                 ServerInstance->Modules->AddService(csc);
50                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_On005Numeric };
51                 ServerInstance->Modules->Attach(eventlist, this, 3);
52         }
53
54         virtual ~ModuleStripColor()
55         {
56         }
57
58         virtual void On005Numeric(std::string &output)
59         {
60                 ServerInstance->AddExtBanChar('S');
61         }
62
63         virtual void ReplaceLine(std::string &sentence)
64         {
65                 /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */
66                 int seq = 0;
67                 std::string::iterator i,safei;
68                 for (i = sentence.begin(); i != sentence.end();)
69                 {
70                         if ((*i == 3))
71                                 seq = 1;
72                         else if (seq && (( ((*i >= '0') && (*i <= '9')) || (*i == ',') ) ))
73                         {
74                                 seq++;
75                                 if ( (seq <= 4) && (*i == ',') )
76                                         seq = 1;
77                                 else if (seq > 3)
78                                         seq = 0;
79                         }
80                         else
81                                 seq = 0;
82
83                         if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31)))
84                         {
85                                 if (i != sentence.begin())
86                                 {
87                                         safei = i;
88                                         --i;
89                                         sentence.erase(safei);
90                                         ++i;
91                                 }
92                                 else
93                                 {
94                                         sentence.erase(i);
95                                         i = sentence.begin();
96                                 }
97                         }
98                         else
99                                 ++i;
100                 }
101         }
102
103         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
104         {
105                 if (!IS_LOCAL(user))
106                         return MOD_RES_PASSTHRU;
107
108                 bool active = false;
109                 if (target_type == TYPE_USER)
110                 {
111                         User* t = (User*)dest;
112                         active = t->IsModeSet('S');
113                 }
114                 else if (target_type == TYPE_CHANNEL)
115                 {
116                         Channel* t = (Channel*)dest;
117                         ModResult res = ServerInstance->OnCheckExemption(user,t,"stripcolor");
118
119                         if (res == MOD_RES_ALLOW)
120                                 return MOD_RES_PASSTHRU;
121
122                         active = !t->GetExtBanStatus(user, 'S').check(!t->IsModeSet('S'));
123                 }
124
125                 if (active)
126                 {
127                         this->ReplaceLine(text);
128                 }
129
130                 return MOD_RES_PASSTHRU;
131         }
132
133         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
134         {
135                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
136         }
137
138         virtual Version GetVersion()
139         {
140                 return Version("Provides channel +S mode (strip ansi colour)", VF_VENDOR);
141         }
142
143 };
144
145 MODULE_INIT(ModuleStripColor)