]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcolor.cpp
Change to use std::string::iterator rather than making a copy of the pointer and...
[user/henk/code/inspircd.git] / src / modules / m_blockcolor.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <stdio.h>
15 #include <sstream>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "inspircd.h"
20
21 /* $ModDesc: Provides support for unreal-style channel mode +c */
22
23 /** Handles the +c channel mode
24  */
25 class BlockColor : public ModeHandler
26 {
27  public:
28         BlockColor(InspIRCd* Instance) : ModeHandler(Instance, 'c', 0, 0, false, MODETYPE_CHANNEL, false) { }
29
30         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
31         {
32                 if (adding)
33                 {
34                         if (!channel->IsModeSet('c'))
35                         {
36                                 channel->SetMode('c',true);
37                                 return MODEACTION_ALLOW;
38                         }
39                 }
40                 else
41                 {
42                         if (channel->IsModeSet('c'))
43                         {
44                                 channel->SetMode('c',false);
45                                 return MODEACTION_ALLOW;
46                         }
47                 }
48
49                 return MODEACTION_DENY;
50         }
51 };
52
53 class ModuleBlockColour : public Module
54 {
55         
56         BlockColor *bc;
57  public:
58  
59         ModuleBlockColour(InspIRCd* Me) : Module::Module(Me)
60         {
61                 
62                 bc = new BlockColor(ServerInstance);
63                 ServerInstance->AddMode(bc, 'c');
64         }
65
66         void Implements(char* List)
67         {
68                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
69         }
70
71         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
72         {
73                 if (target_type == TYPE_CHANNEL)
74                 {
75                         chanrec* c = (chanrec*)dest;
76                         
77                         if(c->IsModeSet('c'))
78                         {
79                                 /* Replace a strlcpy(), which ran even when +c wasn't set, with this (no copies at all) -- Om */
80                                 for (std::string::iterator i = text.begin(); i != text.end(); i++)
81                                 {
82                                         switch (*i)
83                                         {
84                                                 case 2:
85                                                 case 3:
86                                                 case 15:
87                                                 case 21:
88                                                 case 22:
89                                                 case 31:
90                                                         user->WriteServ("404 %s %s :Can't send colours to channel (+c set)",user->nick, c->name);
91                                                         return 1;
92                                                 break;
93                                         }
94                                 }
95                         }
96                 }
97                 return 0;
98         }
99         
100         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
101         {
102                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
103         }
104
105         virtual ~ModuleBlockColour()
106         {
107                 ServerInstance->Modes->DelMode(bc);
108                 DELETE(bc);
109         }
110         
111         virtual Version GetVersion()
112         {
113                 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
114         }
115 };
116
117
118 class ModuleBlockColourFactory : public ModuleFactory
119 {
120  public:
121         ModuleBlockColourFactory()
122         {
123         }
124         
125         ~ModuleBlockColourFactory()
126         {
127         }
128         
129         virtual Module * CreateModule(InspIRCd* Me)
130         {
131                 return new ModuleBlockColour(Me);
132         }
133         
134 };
135
136
137 extern "C" void * init_module( void )
138 {
139         return new ModuleBlockColourFactory;
140 }