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