]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
Dump sendq before closing socket
[user/henk/code/inspircd.git] / src / modules / m_services_account.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 #include "account.h"
16
17 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
18
19 /** Channel mode +r - mark a channel as identified
20  */
21 class Channel_r : public ModeHandler
22 {
23  public:
24         Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
25
26         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
27         {
28                 // only a u-lined server may add or remove the +r mode.
29                 if (!IS_LOCAL(source) || ServerInstance->ULine(source->nick.c_str()) || ServerInstance->ULine(source->server))
30                 {
31                         // Only change the mode if it's not redundant
32                         if ((adding && !channel->IsModeSet('r')) || (!adding && channel->IsModeSet('r')))
33                         {
34                                 channel->SetMode('r',adding);
35                                 return MODEACTION_ALLOW;
36                         }
37
38                         return MODEACTION_DENY;
39                 }
40                 else
41                 {
42                         source->WriteNumeric(500, "%s :Only a server may modify the +r channel mode", source->nick.c_str());
43                         return MODEACTION_DENY;
44                 }
45         }
46 };
47
48 /** User mode +r - mark a user as identified
49  */
50 class User_r : public ModeHandler
51 {
52
53  public:
54         User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
55
56         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
57         {
58                 if (!IS_LOCAL(source) || ServerInstance->ULine(source->nick.c_str()) || ServerInstance->ULine(source->server))
59                 {
60                         if ((adding && !dest->IsModeSet('r')) || (!adding && dest->IsModeSet('r')))
61                         {
62                                 dest->SetMode('r',adding);
63                                 return MODEACTION_ALLOW;
64                         }
65                         return MODEACTION_DENY;
66                 }
67                 else
68                 {
69                         source->WriteNumeric(500, "%s :Only a server may modify the +r user mode", source->nick.c_str());
70                         return MODEACTION_DENY;
71                 }
72         }
73 };
74
75 /** Channel mode +R - unidentified users cannot join
76  */
77 class AChannel_R : public SimpleChannelModeHandler
78 {
79  public:
80         AChannel_R(Module* Creator) : SimpleChannelModeHandler(Creator, "reginvite", 'R') { }
81 };
82
83 /** User mode +R - unidentified users cannot message
84  */
85 class AUser_R : public SimpleUserModeHandler
86 {
87  public:
88         AUser_R(Module* Creator) : SimpleUserModeHandler(Creator, "regdeaf", 'R') { }
89 };
90
91 /** Channel mode +M - unidentified users cannot message channel
92  */
93 class AChannel_M : public SimpleChannelModeHandler
94 {
95  public:
96         AChannel_M(Module* Creator) : SimpleChannelModeHandler(Creator, "regmoderated", 'M') { }
97 };
98
99 class ModuleServicesAccount : public Module
100 {
101         AChannel_R m1;
102         AChannel_M m2;
103         AUser_R m3;
104         Channel_r m4;
105         User_r m5;
106         AccountExtItem accountname;
107  public:
108         ModuleServicesAccount() : m1(this), m2(this), m3(this), m4(this), m5(this),
109                 accountname("accountname", this)
110         {
111         }
112
113         void init()
114         {
115                 ServerInstance->Modules->AddService(m1);
116                 ServerInstance->Modules->AddService(m2);
117                 ServerInstance->Modules->AddService(m3);
118                 ServerInstance->Modules->AddService(m4);
119                 ServerInstance->Modules->AddService(m5);
120                 ServerInstance->Modules->AddService(accountname);
121                 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin, I_OnCheckBan,
122                         I_OnDecodeMetaData, I_On005Numeric, I_OnUserPostNick, I_OnSetConnectClass };
123
124                 ServerInstance->Modules->Attach(eventlist, this, 9);
125         }
126
127         void On005Numeric(std::string &t)
128         {
129                 ServerInstance->AddExtBanChar('R');
130         }
131
132         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
133         void OnWhois(User* source, User* dest)
134         {
135                 std::string *account = accountname.get(dest);
136
137                 if (account)
138                 {
139                         ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick.c_str(), dest->nick.c_str(), account->c_str());
140                 }
141
142                 if (dest->IsModeSet('r'))
143                 {
144                         /* user is registered */
145                         ServerInstance->SendWhoisLine(source, dest, 307, "%s %s :is a registered nick", source->nick.c_str(), dest->nick.c_str());
146                 }
147         }
148
149         void OnUserPostNick(User* user, const std::string &oldnick)
150         {
151                 /* On nickchange, if they have +r, remove it */
152                 if (user->IsModeSet('r') && assign(user->nick) != oldnick)
153                 {
154                         std::vector<std::string> modechange;
155                         modechange.push_back(user->nick);
156                         modechange.push_back("-r");
157                         ServerInstance->SendMode(modechange, user);
158                 }
159         }
160
161         ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
162         {
163                 if (!IS_LOCAL(user))
164                         return MOD_RES_PASSTHRU;
165
166                 std::string *account = accountname.get(user);
167                 bool is_registered = account && !account->empty();
168
169                 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
170                 {
171                         // user is ulined, can speak regardless
172                         return MOD_RES_PASSTHRU;
173                 }
174
175                 if (target_type == TYPE_CHANNEL)
176                 {
177                         Channel* c = (Channel*)dest;
178                         ModResult res;
179                         FIRST_MOD_RESULT(OnChannelRestrictionApply, res, (user,c,"regmoderated"));
180
181                         if (c->IsModeSet('M') && !is_registered && res != MOD_RES_ALLOW)
182                         {
183                                 // user messaging a +M channel and is not registered
184                                 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
185                                 return MOD_RES_DENY;
186                         }
187                 }
188                 else if (target_type == TYPE_USER)
189                 {
190                         User* u = (User*)dest;
191
192                         if (u->IsModeSet('R') && !is_registered)
193                         {
194                                 // user messaging a +R user and is not registered
195                                 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
196                                 return MOD_RES_DENY;
197                         }
198                 }
199                 return MOD_RES_PASSTHRU;
200         }
201
202         ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask)
203         {
204                 if (mask[0] == 'R' && mask[1] == ':')
205                 {
206                         std::string *account = accountname.get(user);
207                         if (account && InspIRCd::Match(*account, mask.substr(2)))
208                                 return MOD_RES_DENY;
209                 }
210                 return MOD_RES_PASSTHRU;
211         }
212
213         ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
214         {
215                 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
216         }
217
218         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
219         {
220                 if (!IS_LOCAL(user))
221                         return MOD_RES_PASSTHRU;
222
223                 std::string *account = accountname.get(user);
224                 bool is_registered = account && !account->empty();
225
226                 if (chan)
227                 {
228                         if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
229                         {
230                                 // user is ulined, won't be stopped from joining
231                                 return MOD_RES_PASSTHRU;
232                         }
233
234                         if (chan->IsModeSet('R'))
235                         {
236                                 if (!is_registered)
237                                 {
238                                         // joining a +R channel and not identified
239                                         user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
240                                         return MOD_RES_DENY;
241                                 }
242                         }
243                 }
244                 return MOD_RES_PASSTHRU;
245         }
246
247         // Whenever the linking module receives metadata from another server and doesnt know what
248         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
249         // module in turn to figure out if this metadata key belongs to them, and what they want
250         // to do with it.
251         // In our case we're only sending a single string around, so we just construct a std::string.
252         // Some modules will probably get much more complex and format more detailed structs and classes
253         // in a textual way for sending over the link.
254         void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
255         {
256                 User* dest = dynamic_cast<User*>(target);
257                 // check if its our metadata key, and its associated with a user
258                 if (dest && (extname == "accountname"))
259                 {
260                         std::string *account = accountname.get(dest);
261                         if (account && !account->empty())
262                         {
263                                 trim(*account);
264
265                                 if (IS_LOCAL(dest))
266                                         dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
267                                                 dest->nick.c_str(), dest->GetFullHost().c_str(), account->c_str(), account->c_str());
268
269                                 AccountEvent(this, dest, *account).Send();
270                         }
271                 }
272         }
273
274         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
275         {
276                 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
277                         return MOD_RES_DENY;
278                 return MOD_RES_PASSTHRU;
279         }
280
281         Version GetVersion()
282         {
283                 return Version("Povides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
284         }
285 };
286
287 MODULE_INIT(ModuleServicesAccount)