]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Tons of module versionflags stuff, and checks for it in /UNLOADMODULE
[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 void On005Numeric(std::string &output)
53         {
54                 std::stringstream line(output);
55                 std::string temp1, temp2;
56                 while (!line.eof())
57                 {
58                         line >> temp1;
59                         if (temp1.substr(0,10) == "CHANMODES=")
60                         {
61                                 // append the chanmode to the end
62                                 temp1 = temp1.substr(10,temp1.length());
63                                 temp1 = "CHANMODES=" + temp1 + "S";
64                         }
65                         temp2 = temp2 + temp1 + " ";
66                 }
67                 if (temp2.length())
68                         output = temp2.substr(0,temp2.length()-1);
69         }
70         
71         virtual ~ModuleStripColor()
72         {
73                 delete Srv;
74         }
75         
76         // ANSI colour stripping by Doc (Peter Wood)
77         virtual void ReplaceLine(std::string &text)
78         {
79                 int i, a, len, remove;
80                 char sentence[MAXBUF];
81                 strncpy(sentence,text.c_str(),MAXBUF);
82   
83                 len = strlen (sentence);
84
85                 for (i = 0; i < len; i++)
86                 {
87                         remove = 0;
88
89                         switch (sentence[i])
90                         {
91                                 case 2:
92                                 case 15:
93                                 case 22:
94                                 case 21:
95                                 case 31:
96                                         remove++;
97                                 break;
98
99                                 case 3:
100                                         remove = 1;
101
102                                         if (isdigit (sentence[i + remove]))
103                                                 remove++;
104
105                                         if (isdigit (sentence[i + remove]))
106                                                 remove++;
107
108                                         if (sentence[i + remove] == ',')
109                                         {
110                                                 remove += 2;
111
112                                                 if (isdigit (sentence[i + remove]))
113                                                 remove++;
114                                         }
115                                 break;
116                         }
117
118                         if (remove != 0) {
119                                 len -= remove;
120
121                                 for (a = i; a <= len; a++)
122                                         sentence[a] = sentence[a + remove];
123                                 i--;
124                         }
125                 }
126                 
127                 text = sentence;
128         }
129         
130         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
131         {
132                 bool active = false;
133                 if (target_type == TYPE_USER)
134                 {
135                         userrec* t = (userrec*)dest;
136                         active = (strchr(t->modes,'S') > 0);
137                 }
138                 else if (target_type == TYPE_CHANNEL)
139                 {
140                         chanrec* t = (chanrec*)dest;
141                         active = (t->IsCustomModeSet('S'));
142                 }
143                 if (active)
144                 {
145                         this->ReplaceLine(text);
146                 }
147                 return 0;
148         }
149         
150         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
151         {
152                 bool active = false;
153                 if (target_type == TYPE_USER)
154                 {
155                         userrec* t = (userrec*)dest;
156                         active = (strchr(t->modes,'S') > 0);
157                 }
158                 else if (target_type == TYPE_CHANNEL)
159                 {
160                         chanrec* t = (chanrec*)dest;
161                         active = (t->IsCustomModeSet('S'));
162                 }
163                 if (active)
164                 {
165                         this->ReplaceLine(text);
166                 }
167                 return 0;
168         }
169         
170         virtual Version GetVersion()
171         {
172                 // This is version 2 because version 1.x is the unreleased unrealircd module
173                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
174         }
175         
176 };
177
178 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
179
180 class ModuleStripColorFactory : public ModuleFactory
181 {
182  public:
183         ModuleStripColorFactory()
184         {
185         }
186         
187         ~ModuleStripColorFactory()
188         {
189         }
190         
191         virtual Module * CreateModule()
192         {
193                 return new ModuleStripColor;
194         }
195         
196 };
197
198
199 extern "C" void * init_module( void )
200 {
201         return new ModuleStripColorFactory;
202 }
203