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