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