]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_stripcolor.cpp
Re-added required parts of connection.h
[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()
34         {
35                 Srv = new Server;
36
37                 Srv->AddExtendedMode('S',MT_CHANNEL,false,0,0);
38                 Srv->AddExtendedMode('S',MT_CLIENT,false,0,0);
39         }
40
41         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
42         {
43                 // check if this is our mode character...
44                 if (modechar == 'S')
45                 {
46                         return 1;
47                 }
48                 else
49                 {
50                         return 0;
51                 }
52         }
53
54         virtual void On005Numeric(std::string &output)
55         {
56                 std::stringstream line(output);
57                 std::string temp1, temp2;
58                 while (!line.eof())
59                 {
60                         line >> temp1;
61                         if (temp1.substr(0,10) == "CHANMODES=")
62                         {
63                                 // append the chanmode to the end
64                                 temp1 = temp1.substr(10,temp1.length());
65                                 temp1 = "CHANMODES=" + temp1 + "S";
66                         }
67                         temp2 = temp2 + temp1 + " ";
68                 }
69                 if (temp2.length())
70                         output = temp2.substr(0,temp2.length()-1);
71         }
72         
73         virtual ~ModuleStripColor()
74         {
75                 delete Srv;
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                                 len -= remove;
122
123                                 for (a = i; a <= len; a++)
124                                         sentence[a] = sentence[a + remove];
125                                 i--;
126                         }
127                 }
128                 
129                 text = sentence;
130         }
131         
132         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
133         {
134                 bool active = false;
135                 if (target_type == TYPE_USER)
136                 {
137                         userrec* t = (userrec*)dest;
138                         active = (strchr(t->modes,'S') > 0);
139                 }
140                 else if (target_type == TYPE_CHANNEL)
141                 {
142                         chanrec* t = (chanrec*)dest;
143                         active = (t->IsCustomModeSet('S'));
144                 }
145                 if (active)
146                 {
147                         this->ReplaceLine(text);
148                 }
149                 return 0;
150         }
151         
152         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
153         {
154                 bool active = false;
155                 if (target_type == TYPE_USER)
156                 {
157                         userrec* t = (userrec*)dest;
158                         active = (strchr(t->modes,'S') > 0);
159                 }
160                 else if (target_type == TYPE_CHANNEL)
161                 {
162                         chanrec* t = (chanrec*)dest;
163                         active = (t->IsCustomModeSet('S'));
164                 }
165                 if (active)
166                 {
167                         this->ReplaceLine(text);
168                 }
169                 return 0;
170         }
171         
172         virtual Version GetVersion()
173         {
174                 // This is version 2 because version 1.x is the unreleased unrealircd module
175                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
176         }
177         
178 };
179
180 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
181
182 class ModuleStripColorFactory : public ModuleFactory
183 {
184  public:
185         ModuleStripColorFactory()
186         {
187         }
188         
189         ~ModuleStripColorFactory()
190         {
191         }
192         
193         virtual Module * CreateModule()
194         {
195                 return new ModuleStripColor;
196         }
197         
198 };
199
200
201 extern "C" void * init_module( void )
202 {
203         return new ModuleStripColorFactory;
204 }
205