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