]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
Gah, im forgetting to SetMode!
[user/henk/code/inspircd.git] / src / modules / m_services_account.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23 #include <string>
24 #include "helperfuncs.h"
25 #include "hashcomp.h"
26
27 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
28
29 class ModuleServicesAccount : public Module
30 {
31         Server *Srv; 
32
33  public:
34         ModuleServicesAccount(Server* Me) : Module::Module(Me)
35         {
36                 Srv = Me;
37
38                 Srv->AddExtendedMode('R',MT_CHANNEL,false,0,0);
39                 Srv->AddExtendedMode('R',MT_CLIENT,false,0,0);
40                 Srv->AddExtendedMode('M',MT_CHANNEL,false,0,0);
41         }
42
43         virtual void On005Numeric(std::string &output)
44         {
45                 InsertMode(output, "RM", 4);
46         }
47
48         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
49         virtual void OnWhois(userrec* source, userrec* dest)
50         {
51                 char *account = dest->GetExt("accountname");
52
53                 if (account)
54                 {
55                         std::string* accountn = (std::string*)account;
56                         WriteServ(source->fd, "330 %s %s %s :is logged in as", source->nick, dest->nick, accountn->c_str());
57                 }
58         }
59
60         void Implements(char* List)
61         {
62                 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_OnExtendedMode] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
63                 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
64         }
65
66         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
67         {
68                 if (modechar == 'R')
69                 {
70                         return 1;
71                 }
72                 else if (modechar == 'M')
73                 {
74                         if (type == MT_CHANNEL)
75                         {
76                                 return 1;
77                         }
78                 }
79
80                 return 0;
81         }
82
83         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
84         {
85                 char *account = user->GetExt("accountname");
86                 
87                 if (target_type == TYPE_CHANNEL)
88                 {
89                         chanrec* c = (chanrec*)dest;
90                         
91                         if ((c->IsModeSet('M')) && (!account))
92                         {
93                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
94                                 {
95                                         // user is ulined, can speak regardless
96                                         return 0;
97                                 }
98
99                                 // user messaging a +M channel and is not registered
100                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
101                                 return 1;
102                         }
103                 }
104                 if (target_type == TYPE_USER)
105                 {
106                         userrec* u = (userrec*)dest;
107                         
108                         if ((u->modes['R'-65]) && (!account))
109                         {
110                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
111                                 {
112                                         // user is ulined, can speak regardless
113                                         return 0;
114                                 }
115
116                                 // user messaging a +R user and is not registered
117                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
118                                 return 1;
119                         }
120                 }
121                 return 0;
122         }
123          
124         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
125         {
126                 return OnUserPreMessage(user, dest, target_type, text, status);
127         }
128          
129         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
130         {
131                 char *account = user->GetExt("accountname");
132                 
133                 if (chan)
134                 {
135                         if (chan->IsModeSet('R'))
136                         {
137                                 if (!account)
138                                 {
139                                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
140                                         {
141                                                 // user is ulined, won't be stopped from joining
142                                                 return 0;
143                                         }
144                                         // joining a +R channel and not identified
145                                         Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
146                                         return 1;
147                                 }
148                         }
149                 }
150                 return 0;
151         }
152         
153         // Whenever the linking module wants to send out data, but doesnt know what the data
154         // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
155         // this method is called. We should use the ProtoSendMetaData function after we've
156         // corrected decided how the data should look, to send the metadata on its way if
157         // it is ours.
158         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
159         {
160                 // check if the linking module wants to know about OUR metadata
161                 if (extname == "accountname")
162                 {
163                         // check if this user has an swhois field to send
164                         char* field = user->GetExt("accountname");
165                         if (field)
166                         {
167                                 // get our extdata out with a cast
168                                 std::string* account = (std::string*)field;
169                                 // call this function in the linking module, let it format the data how it
170                                 // sees fit, and send it on its way. We dont need or want to know how.
171                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
172                         }
173                 }
174         }
175
176         // when a user quits, tidy up their metadata
177         virtual void OnUserQuit(userrec* user, const std::string &message)
178         {
179                 char* field = user->GetExt("accountname");
180                 if (field)
181                 {
182                         std::string* account = (std::string*)field;
183                         user->Shrink("accountname");
184                         delete account;
185                 }
186         }
187
188         // if the module is unloaded, tidy up all our dangling metadata
189         virtual void OnCleanup(int target_type, void* item)
190         {
191                 if (target_type == TYPE_USER)
192                 {
193                         userrec* user = (userrec*)item;
194                         char* field = user->GetExt("accountname");
195                         if (field)
196                         {
197                                 std::string* account = (std::string*)field;
198                                 user->Shrink("accountname");
199                                 delete account;
200                         }
201                 }
202         }
203
204         // Whenever the linking module receives metadata from another server and doesnt know what
205         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
206         // module in turn to figure out if this metadata key belongs to them, and what they want
207         // to do with it.
208         // In our case we're only sending a single string around, so we just construct a std::string.
209         // Some modules will probably get much more complex and format more detailed structs and classes
210         // in a textual way for sending over the link.
211         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
212         {
213                 // check if its our metadata key, and its associated with a user
214                 if ((target_type == TYPE_USER) && (extname == "accountname"))
215                 {       
216                         userrec* dest = (userrec*)target;
217                         /* logging them out? */
218                         if (extdata == "")
219                         {
220                                 char* field = dest->GetExt("accountname");
221                                 if (field)
222                                 {
223                                         std::string* account = (std::string*)field;
224                                         dest->Shrink("accountname");
225                                         delete account;
226                                 }
227                         }
228                         else
229                         {
230                                 // if they dont already have an accountname field, accept the remote server's
231                                 if (!dest->GetExt("accountname"))
232                                 {
233                                         std::string* text = new std::string(extdata);
234                                         dest->Extend("accountname",(char*)text);
235                                 }
236                         }
237                 }
238         }
239
240         virtual ~ModuleServicesAccount()
241         {
242         }
243         
244         virtual Version GetVersion()
245         {
246                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
247         }
248 };
249
250
251 class ModuleServicesAccountFactory : public ModuleFactory
252 {
253  public:
254         ModuleServicesAccountFactory()
255         {
256         }
257         
258         ~ModuleServicesAccountFactory()
259         {
260         }
261         
262         virtual Module * CreateModule(Server* Me)
263         {
264                 return new ModuleServicesAccount(Me);
265         }
266         
267 };
268
269
270 extern "C" void * init_module( void )
271 {
272         return new ModuleServicesAccountFactory;
273 }