]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services.cpp
fixed some indentation and spacing in modules
[user/henk/code/inspircd.git] / src / modules / m_services.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 "inspircd.h"
15
16 static bool kludgeme = false;
17
18 /* $ModDesc: Povides support for services +r user/chan modes and more */
19
20 /** Channel mode +r - mark a channel as identified
21  */
22 class Channel_r : public ModeHandler
23 {
24
25  public:
26         Channel_r(InspIRCd* Instance) : ModeHandler(Instance, 'r', 0, 0, false, MODETYPE_CHANNEL, false) { }
27
28         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
29         {
30                 // only a u-lined server may add or remove the +r mode.
31                 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server || (source->nick.find('.') != std::string::npos)))
32                 {
33                         channel->SetMode('r',adding);
34                         return MODEACTION_ALLOW;
35                 }
36                 else
37                 {
38                         source->WriteNumeric(500, "%s :Only a server may modify the +r channel mode", source->nick.c_str());
39                         return MODEACTION_DENY;
40                 }
41         }
42 };
43
44 /** User mode +r - mark a user as identified
45  */
46 class User_r : public ModeHandler
47 {
48
49  public:
50         User_r(InspIRCd* Instance) : ModeHandler(Instance, 'r', 0, 0, false, MODETYPE_USER, false) { }
51
52         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
53         {
54                 if ((kludgeme) || (ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server || (source->nick.find('.') != std::string::npos)))
55                 {
56                         if ((adding && !dest->IsModeSet('r')) || (!adding && dest->IsModeSet('r')))
57                         {
58                                 dest->SetMode('r',adding);
59                                 return MODEACTION_ALLOW;
60                         }
61                         return MODEACTION_DENY;
62                 }
63                 else
64                 {
65                         source->WriteNumeric(500, "%s :Only a server may modify the +r user mode", source->nick.c_str());
66                         return MODEACTION_DENY;
67                 }
68         }
69 };
70
71 /** Channel mode +R - registered users only
72  */
73 class Channel_R : public SimpleChannelModeHandler
74 {
75  public:
76         Channel_R(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'R') { }
77 };
78
79 /** User mode +R - only allow PRIVMSG and NOTICE from registered users
80  */
81 class User_R : public SimpleUserModeHandler
82 {
83  public:
84         User_R(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'R') { }
85 };
86
87 /** Channel mode +M - only allow privmsg and notice to channel from registered users
88  */
89 class Channel_M : public SimpleChannelModeHandler
90 {
91  public:
92         Channel_M(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'M') { }
93 };
94
95 /** Dreamnforge-like services support
96  */
97 class ModuleServices : public Module
98 {
99
100         Channel_r* m1;
101         Channel_R* m2;
102         Channel_M* m3;
103         User_r* m4;
104         User_R* m5;
105  public:
106         ModuleServices(InspIRCd* Me)
107                 : Module(Me)
108         {
109
110                 m1 = new Channel_r(ServerInstance);
111                 m2 = new Channel_R(ServerInstance);
112                 m3 = new Channel_M(ServerInstance);
113                 m4 = new User_r(ServerInstance);
114                 m5 = new User_R(ServerInstance);
115
116                 if (!ServerInstance->Modes->AddMode(m1) || !ServerInstance->Modes->AddMode(m2) || !ServerInstance->Modes->AddMode(m3)
117                         || !ServerInstance->Modes->AddMode(m4) || !ServerInstance->Modes->AddMode(m5))
118                 {
119                         throw ModuleException("Could not add user and channel modes!");
120                 }
121
122                 kludgeme = false;
123                 Implementation eventlist[] = { I_OnWhois, I_OnUserPostNick, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin };
124                 ServerInstance->Modules->Attach(eventlist, this, 5);
125         }
126
127         /* <- :stitch.chatspike.net 307 w00t w00t :is a registered nick */
128         virtual void OnWhois(User* source, User* dest)
129         {
130                 if (dest->IsModeSet('r'))
131                 {
132                         /* user is registered */
133                         ServerInstance->SendWhoisLine(source, dest, 307, "%s %s :is a registered nick", source->nick.c_str(), dest->nick.c_str());
134                 }
135         }
136
137
138         virtual void OnUserPostNick(User* user, const std::string &oldnick)
139         {
140                 /* On nickchange, if they have +r, remove it */
141                 if (user->IsModeSet('r') && assign(user->nick) != oldnick)
142                 {
143                         std::vector<std::string> modechange;
144                         modechange.push_back(user->nick);
145                         modechange.push_back("-r");
146                         kludgeme = true;
147                         ServerInstance->SendMode(modechange,user);
148                         kludgeme = false;
149                 }
150         }
151
152         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
153         {
154                 if (!IS_LOCAL(user))
155                         return 0;
156
157                 if (target_type == TYPE_CHANNEL)
158                 {
159                         Channel* c = (Channel*)dest;
160                         if ((c->IsModeSet('M')) && (!user->IsModeSet('r')))
161                         {
162                                 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
163                                 {
164                                         // user is ulined, can speak regardless
165                                         return 0;
166                                 }
167                                 // user messaging a +M channel and is not registered
168                                 user->WriteNumeric(477, "%s %s :You need a registered nickname to speak on this channel", user->nick.c_str(), c->name.c_str());
169                                 return 1;
170                         }
171                 }
172                 if (target_type == TYPE_USER)
173                 {
174                         User* u = (User*)dest;
175                         if ((u->IsModeSet('R')) && (!user->IsModeSet('r')))
176                         {
177                                 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
178                                 {
179                                         // user is ulined, can speak regardless
180                                         return 0;
181                                 }
182                                 // user messaging a +R user and is not registered
183                                 user->WriteNumeric(477, "%s %s :You need a registered nickname to message this user", user->nick.c_str(), u->nick.c_str());
184                                 return 1;
185                         }
186                 }
187                 return 0;
188         }
189
190         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
191         {
192                 return OnUserPreMessage(user,dest,target_type,text,status, exempt_list);
193         }
194
195         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
196         {
197                 if (chan)
198                 {
199                         if (chan->IsModeSet('R'))
200                         {
201                                 if (!user->IsModeSet('r'))
202                                 {
203                                         if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
204                                         {
205                                                 // user is ulined, won't be stopped from joining
206                                                 return 0;
207                                         }
208                                         // joining a +R channel and not identified
209                                         user->WriteNumeric(477, "%s %s :You need a registered nickname to join this channel", user->nick.c_str(), chan->name.c_str());
210                                         return 1;
211                                 }
212                         }
213                 }
214                 return 0;
215         }
216
217         virtual ~ModuleServices()
218         {
219                 kludgeme = true;
220                 ServerInstance->Modes->DelMode(m1);
221                 ServerInstance->Modes->DelMode(m2);
222                 ServerInstance->Modes->DelMode(m3);
223                 ServerInstance->Modes->DelMode(m4);
224                 ServerInstance->Modes->DelMode(m5);
225                 delete m1;
226                 delete m2;
227                 delete m3;
228                 delete m4;
229                 delete m5;
230         }
231
232         virtual Version GetVersion()
233         {
234                 return Version(1,2,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
235         }
236 };
237
238
239 MODULE_INIT(ModuleServices)