]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Review and optimize
[user/henk/code/inspircd.git] / src / modules / m_stripcolor.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24
25 /* $ModDesc: Provides channel +S mode (strip ansi colour) */
26
27 class ModuleStripColor : public Module
28 {
29  Server *Srv;
30  ConfigReader *Conf, *MyConf;
31  
32  public:
33         ModuleStripColor(Server* Me)
34                 : Module::Module(Me)
35         {
36                 Srv = Me;
37
38                 Srv->AddExtendedMode('S',MT_CHANNEL,false,0,0);
39                 Srv->AddExtendedMode('S',MT_CLIENT,false,0,0);
40         }
41
42         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
43         {
44                 // check if this is our mode character...
45                 if (modechar == 'S')
46                 {
47                         return 1;
48                 }
49                 else
50                 {
51                         return 0;
52                 }
53         }
54
55         virtual void On005Numeric(std::string &output)
56         {
57                 std::stringstream line(output);
58                 std::string temp1, temp2;
59                 while (!line.eof())
60                 {
61                         line >> temp1;
62                         if (temp1.substr(0,10) == "CHANMODES=")
63                         {
64                                 // append the chanmode to the end
65                                 temp1 = temp1.substr(10,temp1.length());
66                                 temp1 = "CHANMODES=" + temp1 + "S";
67                         }
68                         temp2 = temp2 + temp1 + " ";
69                 }
70                 if (temp2.length())
71                         output = temp2.substr(0,temp2.length()-1);
72         }
73         
74         virtual ~ModuleStripColor()
75         {
76         }
77         
78         // ANSI colour stripping by Doc (Peter Wood)
79         virtual void ReplaceLine(std::string &text)
80         {
81                 int i, a, len, remove;
82                 char sentence[MAXBUF];
83                 strncpy(sentence,text.c_str(),MAXBUF);
84   
85                 len = strlen(sentence);
86
87                 for (i = 0; i < len; i++)
88                 {
89                         remove = 0;
90
91                         switch (sentence[i])
92                         {
93                                 case 2:
94                                 case 15:
95                                 case 22:
96                                 case 21:
97                                 case 31:
98                                         remove++;
99                                 break;
100
101                                 case 3:
102                                         remove = 1;
103
104                                         if (isdigit(sentence[i + remove]))
105                                                 remove++;
106
107                                         if (isdigit(sentence[i + remove]))
108                                                 remove++;
109
110                                         if (sentence[i + remove] == ',')
111                                         {
112                                                 remove += 2;
113
114                                                 if (isdigit(sentence[i + remove]))
115                                                         remove++;
116                                         }
117                                 break;
118                         }
119
120                         if (remove != 0)
121                         {
122                                 len -= remove;
123
124                                 for (a = i; a <= len; a++)
125                                         sentence[a] = sentence[a + remove];
126                                 i--;
127                         }
128                 }
129                 
130                 text = sentence;
131         }
132         
133         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
134         {
135                 bool active = false;
136                 if (target_type == TYPE_USER)
137                 {
138                         userrec* t = (userrec*)dest;
139                         active = (strchr(t->modes,'S') > 0);
140                 }
141                 else if (target_type == TYPE_CHANNEL)
142                 {
143                         chanrec* t = (chanrec*)dest;
144                         active = (t->IsCustomModeSet('S'));
145                 }
146                 if (active)
147                 {
148                         this->ReplaceLine(text);
149                 }
150                 return 0;
151         }
152         
153         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
154         {
155                 bool active = false;
156                 if (target_type == TYPE_USER)
157                 {
158                         userrec* t = (userrec*)dest;
159                         active = (strchr(t->modes,'S') > 0);
160                 }
161                 else if (target_type == TYPE_CHANNEL)
162                 {
163                         chanrec* t = (chanrec*)dest;
164                         active = (t->IsCustomModeSet('S'));
165                 }
166                 if (active)
167                 {
168                         this->ReplaceLine(text);
169                 }
170                 return 0;
171         }
172         
173         virtual Version GetVersion()
174         {
175                 // This is version 2 because version 1.x is the unreleased unrealircd module
176                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
177         }
178         
179 };
180
181 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
182
183 class ModuleStripColorFactory : public ModuleFactory
184 {
185  public:
186         ModuleStripColorFactory()
187         {
188         }
189         
190         ~ModuleStripColorFactory()
191         {
192         }
193         
194         virtual Module * CreateModule(Server* Me)
195         {
196                 return new ModuleStripColor(Me);
197         }
198         
199 };
200
201
202 extern "C" void * init_module( void )
203 {
204         return new ModuleStripColorFactory;
205 }
206