]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
Restore <options:exemptchanops> with long names
[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 = ServerInstance->OnCheckExemption(user,c,"regmoderated");
179
180                         if (c->IsModeSet('M') && !is_registered && res != MOD_RES_ALLOW)
181                         {
182                                 // user messaging a +M channel and is not registered
183                                 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
184                                 return MOD_RES_DENY;
185                         }
186                 }
187                 else if (target_type == TYPE_USER)
188                 {
189                         User* u = (User*)dest;
190
191                         if (u->IsModeSet('R') && !is_registered)
192                         {
193                                 // user messaging a +R user and is not registered
194                                 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
195                                 return MOD_RES_DENY;
196                         }
197                 }
198                 return MOD_RES_PASSTHRU;
199         }
200
201         ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask)
202         {
203                 if (mask[0] == 'R' && mask[1] == ':')
204                 {
205                         std::string *account = accountname.get(user);
206                         if (account && InspIRCd::Match(*account, mask.substr(2)))
207                                 return MOD_RES_DENY;
208                 }
209                 return MOD_RES_PASSTHRU;
210         }
211
212         ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
213         {
214                 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
215         }
216
217         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
218         {
219                 if (!IS_LOCAL(user))
220                         return MOD_RES_PASSTHRU;
221
222                 std::string *account = accountname.get(user);
223                 bool is_registered = account && !account->empty();
224
225                 if (chan)
226                 {
227                         if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
228                         {
229                                 // user is ulined, won't be stopped from joining
230                                 return MOD_RES_PASSTHRU;
231                         }
232
233                         if (chan->IsModeSet('R'))
234                         {
235                                 if (!is_registered)
236                                 {
237                                         // joining a +R channel and not identified
238                                         user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
239                                         return MOD_RES_DENY;
240                                 }
241                         }
242                 }
243                 return MOD_RES_PASSTHRU;
244         }
245
246         // Whenever the linking module receives metadata from another server and doesnt know what
247         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
248         // module in turn to figure out if this metadata key belongs to them, and what they want
249         // to do with it.
250         // In our case we're only sending a single string around, so we just construct a std::string.
251         // Some modules will probably get much more complex and format more detailed structs and classes
252         // in a textual way for sending over the link.
253         void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
254         {
255                 User* dest = dynamic_cast<User*>(target);
256                 // check if its our metadata key, and its associated with a user
257                 if (dest && (extname == "accountname"))
258                 {
259                         std::string *account = accountname.get(dest);
260                         if (account && !account->empty())
261                         {
262                                 trim(*account);
263
264                                 if (IS_LOCAL(dest))
265                                         dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
266                                                 dest->nick.c_str(), dest->GetFullHost().c_str(), account->c_str(), account->c_str());
267
268                                 AccountEvent(this, dest, *account).Send();
269                         }
270                 }
271         }
272
273         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
274         {
275                 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
276                         return MOD_RES_DENY;
277                 return MOD_RES_PASSTHRU;
278         }
279
280         Version GetVersion()
281         {
282                 return Version("Povides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
283         }
284 };
285
286 MODULE_INIT(ModuleServicesAccount)