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