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