]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
30077e408f80715d0f03efeaac75348fc0bb48d6
[user/henk/code/inspircd.git] / src / modules / m_services_account.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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(InspIRCd* Instance, Module* Creator) : ModeHandler(Creator, '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_REMOTE(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(InspIRCd* Instance, Module* Creator) : ModeHandler(Creator, 'r', PARAM_NONE, MODETYPE_USER) { }
56
57         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
58         {
59                 if (IS_REMOTE(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(InspIRCd* Instance, Module* Creator) : SimpleChannelModeHandler(Instance, Creator, 'R') { }
82 };
83
84 /** User mode +R - unidentified users cannot message
85  */
86 class AUser_R : public SimpleUserModeHandler
87 {
88  public:
89         AUser_R(InspIRCd* Instance, Module* Creator) : SimpleUserModeHandler(Creator, '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(InspIRCd* Instance, Module* Creator) : SimpleChannelModeHandler(Instance, Creator, '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         StringExtItem accountname;
108  public:
109         ModuleServicesAccount(InspIRCd* Me) : Module(Me),
110                 m1(Me, this), m2(Me, this), m3(Me, this), m4(Me, this), m5(Me, this),
111                 accountname("accountname", this)
112         {
113
114                 if (!ServerInstance->Modes->AddMode(&m1) || !ServerInstance->Modes->AddMode(&m2) ||
115                         !ServerInstance->Modes->AddMode(&m3) || !ServerInstance->Modes->AddMode(&m4) ||
116                         !ServerInstance->Modes->AddMode(&m5))
117                         throw ModuleException("Some other module has claimed our modes!");
118
119                 Extensible::Register(&accountname);
120                 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin, I_OnCheckBan,
121                         I_OnSyncUser, I_OnUserQuit, I_OnCleanup, I_OnDecodeMetaData, I_On005Numeric, I_OnUserPostNick };
122
123                 ServerInstance->Modules->Attach(eventlist, this, 10);
124         }
125
126         virtual void On005Numeric(std::string &t)
127         {
128                 ServerInstance->AddExtBanChar('R');
129                 ServerInstance->AddExtBanChar('M');
130         }
131
132         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
133         virtual 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         virtual 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         virtual 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
179                         if (c->IsModeSet('M') && !is_registered)
180                         {
181                                 // user messaging a +M channel and is not registered
182                                 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
183                                 return MOD_RES_DENY;
184                         }
185
186                         if (account)
187                         {
188                                 if (c->GetExtBanStatus(*account, 'M') == MOD_RES_DENY)
189                                 {
190                                         // may not speak (text is deliberately vague, so they don't know which restriction to evade)
191                                         user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You may not speak in this channel");
192                                         return MOD_RES_DENY;
193                                 }
194                         }
195                 }
196                 else if (target_type == TYPE_USER)
197                 {
198                         User* u = (User*)dest;
199
200                         if (u->IsModeSet('R') && !is_registered)
201                         {
202                                 // user messaging a +R user and is not registered
203                                 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
204                                 return MOD_RES_DENY;
205                         }
206                 }
207                 return MOD_RES_PASSTHRU;
208         }
209
210         virtual ModResult OnCheckBan(User* user, Channel* chan)
211         {
212                 std::string *account = accountname.get(user);
213                 if (!account)
214                         return MOD_RES_PASSTHRU;
215                 return chan->GetExtBanStatus(*account, 'R');
216         }
217
218         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
219         {
220                 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
221         }
222
223         virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
224         {
225                 if (!IS_LOCAL(user))
226                         return MOD_RES_PASSTHRU;
227
228                 std::string *account = accountname.get(user);
229                 bool is_registered = account && !account->empty();
230
231                 if (chan)
232                 {
233                         if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
234                         {
235                                 // user is ulined, won't be stopped from joining
236                                 return MOD_RES_PASSTHRU;
237                         }
238
239                         if (chan->IsModeSet('R'))
240                         {
241                                 if (!is_registered)
242                                 {
243                                         // joining a +R channel and not identified
244                                         user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
245                                         return MOD_RES_DENY;
246                                 }
247                         }
248                 }
249                 return MOD_RES_PASSTHRU;
250         }
251
252         // Whenever the linking module receives metadata from another server and doesnt know what
253         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
254         // module in turn to figure out if this metadata key belongs to them, and what they want
255         // to do with it.
256         // In our case we're only sending a single string around, so we just construct a std::string.
257         // Some modules will probably get much more complex and format more detailed structs and classes
258         // in a textual way for sending over the link.
259         virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
260         {
261                 User* dest = dynamic_cast<User*>(target);
262                 // check if its our metadata key, and its associated with a user
263                 if (dest && (extname == "accountname"))
264                 {
265                         if (!extdata.empty())
266                         {
267                                 std::string *account = accountname.get(dest);
268                                 trim(*account);
269
270                                 if (IS_LOCAL(dest))
271                                         dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
272                                                 dest->nick.c_str(), dest->GetFullHost().c_str(), account->c_str(), account->c_str());
273
274                                 AccountData ac;
275                                 ac.user = dest;
276                                 ac.account = *account;
277                                 Event n((char*)&ac, this, "account_login");
278                                 n.Send(ServerInstance);
279                         }
280                 }
281         }
282
283         virtual ~ModuleServicesAccount()
284         {
285                 ServerInstance->Modes->DelMode(&m1);
286                 ServerInstance->Modes->DelMode(&m2);
287                 ServerInstance->Modes->DelMode(&m3);
288                 ServerInstance->Modes->DelMode(&m4);
289                 ServerInstance->Modes->DelMode(&m5);
290         }
291
292         virtual Version GetVersion()
293         {
294                 return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
295         }
296 };
297
298 MODULE_INIT(ModuleServicesAccount)