]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcolor.cpp
Review and optimize
[user/henk/code/inspircd.git] / src / modules / m_blockcolor.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 <sstream>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24 #include "helperfuncs.h"
25
26 /* $ModDesc: Provides support for unreal-style channel mode +c */
27
28 class ModuleBlockColor : public Module
29 {
30         Server *Srv;
31  public:
32  
33         ModuleBlockColor(Server* Me)
34                 : Module::Module(Me)
35         {
36                 Srv = Me;
37                 Srv->AddExtendedMode('c',MT_CHANNEL,false,0,0);
38         }
39
40         virtual void On005Numeric(std::string &output)
41         {
42                 // we don't really have a limit...
43                 std::stringstream line(output);
44                 std::string temp1, temp2;
45                 while (!line.eof())
46                 {
47                         line >> temp1;
48                         if (temp1.substr(0,10) == "CHANMODES=")
49                         {
50                                 // append the chanmode to the end
51                                 temp1 = temp1.substr(10,temp1.length());
52                                 temp1 = "CHANMODES=" + temp1 + "c";
53                         }
54                         temp2 = temp2 + temp1 + " ";
55                 }
56                 if (temp2.length())
57                         output = temp2.substr(0,temp2.length()-1);
58         }
59         
60         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
61         {
62                 if (target_type == TYPE_CHANNEL)
63                 {
64                         chanrec* c = (chanrec*)dest;
65                         char ctext[MAXBUF];
66                         char *ctptr = ctext;
67                         snprintf(ctext,MAXBUF,"%s",text.c_str());
68
69
70                         if (c->IsCustomModeSet('c'))
71                         {
72                                 /* Instead of using strchr() here, do our own loop. Hopefully faster. --w00t */
73                                 while (ctptr && *ctptr)
74                                 {
75                                         switch (*ctptr)
76                                         {
77                                                 case 2:
78                                                 case 3:
79                                                 case 31:
80                                                         WriteServ(user->fd,"404 %s %s :Can't send colors to channel (+c set)",user->nick, c->name);
81                                                         return 1;
82                                                         break;
83                                         }
84
85                                         *ctptr++;
86                                 }
87                         }
88                 }
89                 return 0;
90         }
91         
92         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
93         {
94                 if (target_type == TYPE_CHANNEL)
95                 {
96                         chanrec* c = (chanrec*)dest;
97                         char ctext[MAXBUF];
98                         char *ctptr = ctext;
99                         snprintf(ctext,MAXBUF,"%s",text.c_str());
100
101
102                         if (c->IsCustomModeSet('c'))
103                         {
104                                 /* Instead of using strchr() here, do our own loop. Hopefully faster. --w00t */
105                                 while (ctptr && *ctptr)
106                                 {
107                                         switch (*ctptr)
108                                         {
109                                                 case 2:
110                                                 case 3:
111                                                 case 31:
112                                                         WriteServ(user->fd,"404 %s %s :Can't send colors to channel (+c set)",user->nick, c->name);
113                                                         return 1;
114                                                         break;
115                                         }
116
117                                         ctptr++;
118                                 }
119                         }
120                 }
121                 return 0;
122         }
123         
124         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
125         {
126                 // check if this is our mode character...
127                 if ((modechar == 'c') && (type == MT_CHANNEL))
128                 {
129                         log(DEBUG,"Allowing c change");
130                         return 1;
131                 }
132                 else
133                 {
134                         return 0;
135                 }
136         }
137
138         virtual ~ModuleBlockColor()
139         {
140         }
141         
142         virtual Version GetVersion()
143         {
144                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
145         }
146 };
147
148
149 class ModuleBlockColorFactory : public ModuleFactory
150 {
151  public:
152         ModuleBlockColorFactory()
153         {
154         }
155         
156         ~ModuleBlockColorFactory()
157         {
158         }
159         
160         virtual Module * CreateModule(Server* Me)
161         {
162                 return new ModuleBlockColor(Me);
163         }
164         
165 };
166
167
168 extern "C" void * init_module( void )
169 {
170         return new ModuleBlockColorFactory;
171 }
172