]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
Only valid targets for encap are now server ids
[user/henk/code/inspircd.git] / src / modules / m_services_account.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 #include "account.h"
16
17 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
18
19 /** Channel mode +R - unidentified users cannot join
20  */
21 class AChannel_R : public ModeHandler
22 {
23  public:
24         AChannel_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_CHANNEL, false) { }
25
26         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
27         {
28                 if (adding)
29                 {
30                         if (!channel->IsModeSet('R'))
31                         {
32                                 channel->SetMode('R',true);
33                                 return MODEACTION_ALLOW;
34                         }
35                 }
36                 else
37                 {
38                         if (channel->IsModeSet('R'))
39                         {
40                                 channel->SetMode('R',false);
41                                 return MODEACTION_ALLOW;
42                         }
43                 }
44
45                 return MODEACTION_DENY;
46         }
47 };
48
49 /** User mode +R - unidentified users cannot message
50  */
51 class AUser_R : public ModeHandler
52 {
53  public:
54         AUser_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_USER, false) { }
55
56         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
57         {
58                 if (adding)
59                 {
60                         if (!dest->IsModeSet('R'))
61                         {
62                                 dest->SetMode('R',true);
63                                 return MODEACTION_ALLOW;
64                         }
65                 }
66                 else
67                 {
68                         if (dest->IsModeSet('R'))
69                         {
70                                 dest->SetMode('R',false);
71                                 return MODEACTION_ALLOW;
72                         }
73                 }
74
75                 return MODEACTION_DENY;
76         }
77 };
78
79 /** Channel mode +M - unidentified users cannot message channel
80  */
81 class AChannel_M : public ModeHandler
82 {
83  public:
84         AChannel_M(InspIRCd* Instance) : ModeHandler(Instance, 'M', 0, 0, false, MODETYPE_CHANNEL, false) { }
85
86         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
87         {
88                 if (adding)
89                 {
90                         if (!channel->IsModeSet('M'))
91                         {
92                                 channel->SetMode('M',true);
93                                 return MODEACTION_ALLOW;
94                         }
95                 }
96                 else
97                 {
98                         if (channel->IsModeSet('M'))
99                         {
100                                 channel->SetMode('M',false);
101                                 return MODEACTION_ALLOW;
102                         }
103                 }
104
105                 return MODEACTION_DENY;
106         }
107 };
108
109 class ModuleServicesAccount : public Module
110 {
111          
112         AChannel_R* m1;
113         AChannel_M* m2;
114         AUser_R* m3;
115  public:
116         ModuleServicesAccount(InspIRCd* Me) : Module(Me)
117         {
118                 
119                 m1 = new AChannel_R(ServerInstance);
120                 m2 = new AChannel_M(ServerInstance);
121                 m3 = new AUser_R(ServerInstance);
122                 if (!ServerInstance->Modes->AddMode(m1) || !ServerInstance->Modes->AddMode(m2) || !ServerInstance->Modes->AddMode(m3))
123                         throw ModuleException("Could not add new modes!");
124
125                 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin,
126                         I_OnSyncUserMetaData, I_OnUserQuit, I_OnCleanup, I_OnDecodeMetaData };
127
128                 ServerInstance->Modules->Attach(eventlist, this, 8);
129         }
130
131         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
132         virtual void OnWhois(User* source, User* dest)
133         {
134                 std::string *account;
135                 dest->GetExt("accountname", account);
136
137                 if (account)
138                 {
139                         ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
140                 }
141         }
142
143
144         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
145         {
146                 std::string *account;
147
148                 if (!IS_LOCAL(user))
149                         return 0;
150
151                 user->GetExt("accountname", account);
152                 
153                 if (target_type == TYPE_CHANNEL)
154                 {
155                         Channel* c = (Channel*)dest;
156                         
157                         if ((c->IsModeSet('M')) && (!account))
158                         {
159                                 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
160                                 {
161                                         // user is ulined, can speak regardless
162                                         return 0;
163                                 }
164
165                                 // user messaging a +M channel and is not registered
166                                 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
167                                 return 1;
168                         }
169                 }
170                 if (target_type == TYPE_USER)
171                 {
172                         User* u = (User*)dest;
173                         
174                         if ((u->modes['R'-65]) && (!account))
175                         {
176                                 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
177                                 {
178                                         // user is ulined, can speak regardless
179                                         return 0;
180                                 }
181
182                                 // user messaging a +R user and is not registered
183                                 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
184                                 return 1;
185                         }
186                 }
187                 return 0;
188         }
189          
190         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
191         {
192                 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
193         }
194          
195         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs)
196         {
197                 std::string *account;
198                 user->GetExt("accountname", account);
199                 
200                 if (chan)
201                 {
202                         if (chan->IsModeSet('R'))
203                         {
204                                 if (!account)
205                                 {
206                                         if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
207                                         {
208                                                 // user is ulined, won't be stopped from joining
209                                                 return 0;
210                                         }
211                                         // joining a +R channel and not identified
212                                         user->WriteServ("477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
213                                         return 1;
214                                 }
215                         }
216                 }
217                 return 0;
218         }
219         
220         // Whenever the linking module wants to send out data, but doesnt know what the data
221         // represents (e.g. it is metadata, added to a User or Channel by a module) then
222         // this method is called. We should use the ProtoSendMetaData function after we've
223         // corrected decided how the data should look, to send the metadata on its way if
224         // it is ours.
225         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
226         {
227                 // check if the linking module wants to know about OUR metadata
228                 if (extname == "accountname")
229                 {
230                         // check if this user has an swhois field to send
231                         std::string* account;
232                         user->GetExt("accountname", account);
233                         if (account)
234                         {
235                                 // remove any accidental leading/trailing spaces
236                                 trim(*account);
237
238                                 // call this function in the linking module, let it format the data how it
239                                 // sees fit, and send it on its way. We dont need or want to know how.
240                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
241                         }
242                 }
243         }
244
245         // when a user quits, tidy up their metadata
246         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
247         {
248                 std::string* account;
249                 user->GetExt("accountname", account);
250                 if (account)
251                 {
252                         user->Shrink("accountname");
253                         delete account;
254                 }
255         }
256
257         // if the module is unloaded, tidy up all our dangling metadata
258         virtual void OnCleanup(int target_type, void* item)
259         {
260                 if (target_type == TYPE_USER)
261                 {
262                         User* user = (User*)item;
263                         std::string* account;
264                         user->GetExt("accountname", account);
265                         if (account)
266                         {
267                                 user->Shrink("accountname");
268                                 delete account;
269                         }
270                 }
271         }
272
273         // Whenever the linking module receives metadata from another server and doesnt know what
274         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
275         // module in turn to figure out if this metadata key belongs to them, and what they want
276         // to do with it.
277         // In our case we're only sending a single string around, so we just construct a std::string.
278         // Some modules will probably get much more complex and format more detailed structs and classes
279         // in a textual way for sending over the link.
280         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
281         {
282                 // check if its our metadata key, and its associated with a user
283                 if ((target_type == TYPE_USER) && (extname == "accountname"))
284                 {       
285                         User* dest = (User*)target;
286                         
287                         /* logging them out? */
288                         if (extdata.empty())
289                         {
290                                 std::string* account;
291                                 dest->GetExt("accountname", account);
292                                 if (account)
293                                 {
294                                         dest->Shrink("accountname");
295                                         delete account;
296                                 }
297                         }
298                         else
299                         {
300                                 // if they dont already have an accountname field, accept the remote server's
301                                 std::string* text;
302                                 if (!dest->GetExt("accountname", text))
303                                 {
304                                         text = new std::string(extdata);
305                                         // remove any accidental leading/trailing spaces
306                                         trim(*text);
307                                         dest->Extend("accountname", text);
308
309                                         if (IS_LOCAL(dest))
310                                                 dest->WriteServ("900 %s %s %s :You are now logged in as %s", dest->nick, dest->GetFullHost(), text->c_str(), text->c_str());
311
312                                         AccountData ac;
313                                         ac.user = dest;
314                                         ac.account = *text;
315                                         Event n((char*)&ac, this, "account_login");
316                                         n.Send(ServerInstance);
317                                 }
318                         }
319                 }
320         }
321
322         virtual ~ModuleServicesAccount()
323         {
324                 ServerInstance->Modes->DelMode(m1);
325                 ServerInstance->Modes->DelMode(m2);
326                 ServerInstance->Modes->DelMode(m3);
327                 delete m1;
328                 delete m2;
329                 delete m3;
330         }
331         
332         virtual Version GetVersion()
333         {
334                 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
335         }
336 };
337
338 MODULE_INIT(ModuleServicesAccount)