]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcolor.cpp
a40d4ebd3f11439d8c98de462e5fd8648d98afd7
[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         bool AllowChanOps;      
56         BlockColor *bc;
57  public:
58  
59         ModuleBlockColour(InspIRCd* Me) : Module::Module(Me)
60         {
61                 bc = new BlockColor(ServerInstance);
62                 if (!ServerInstance->AddMode(bc, 'c'))
63                         throw ModuleException("Could not add new modes!");
64
65                 OnRehash(NULL, "");
66         }
67
68         void Implements(char* List)
69         {
70                 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
71         }
72
73
74         virtual void OnRehash(userrec* user, const std::string &parameter)
75         {
76                 ConfigReader Conf(ServerInstance);
77
78                 AllowChanOps = Conf.ReadFlag("blockcolor", "allowchanops", 0);
79         }
80
81         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
82         {
83                 if ((target_type == TYPE_CHANNEL) && (IS_LOCAL(user)))
84                 {
85                         chanrec* c = (chanrec*)dest;
86                         
87                         if(c->IsModeSet('c'))
88                         {
89                                 if (!AllowChanOps || AllowChanOps && c->GetStatus(user) != STATUS_OP)
90                                 {
91                                         for (std::string::iterator i = text.begin(); i != text.end(); i++)
92                                         {
93                                                 switch (*i)
94                                                 {
95                                                         case 2:
96                                                         case 3:
97                                                         case 15:
98                                                         case 21:
99                                                         case 22:
100                                                         case 31:
101                                                                 user->WriteServ("404 %s %s :Can't send colours to channel (+c set)",user->nick, c->name);
102                                                                 return 1;
103                                                         break;
104                                                 }
105                                         }
106                                 }
107                         }
108                 }
109                 return 0;
110         }
111         
112         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
113         {
114                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
115         }
116
117         virtual ~ModuleBlockColour()
118         {
119                 ServerInstance->Modes->DelMode(bc);
120                 DELETE(bc);
121         }
122         
123         virtual Version GetVersion()
124         {
125                 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
126         }
127 };
128
129
130 class ModuleBlockColourFactory : public ModuleFactory
131 {
132  public:
133         ModuleBlockColourFactory()
134         {
135         }
136         
137         ~ModuleBlockColourFactory()
138         {
139         }
140         
141         virtual Module * CreateModule(InspIRCd* Me)
142         {
143                 return new ModuleBlockColour(Me);
144         }
145         
146 };
147
148
149 extern "C" void * init_module( void )
150 {
151         return new ModuleBlockColourFactory;
152 }