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