]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
fix some unitialised vectors and tidy up a bit.
[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 SimpleChannelModeHandler
22 {
23  public:
24         AChannel_R(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'R') { }
25 };
26
27 /** User mode +R - unidentified users cannot message
28  */
29 class AUser_R : public SimpleUserModeHandler
30 {
31  public:
32         AUser_R(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'R') { }
33 };
34
35 /** Channel mode +M - unidentified users cannot message channel
36  */
37 class AChannel_M : public SimpleChannelModeHandler
38 {
39  public:
40         AChannel_M(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'M') { }
41 };
42
43 class ModuleServicesAccount : public Module
44 {
45          
46         AChannel_R* m1;
47         AChannel_M* m2;
48         AUser_R* m3;
49  public:
50         ModuleServicesAccount(InspIRCd* Me) : Module(Me)
51         {
52                 
53                 m1 = new AChannel_R(ServerInstance);
54                 m2 = new AChannel_M(ServerInstance);
55                 m3 = new AUser_R(ServerInstance);
56                 if (!ServerInstance->Modes->AddMode(m1) || !ServerInstance->Modes->AddMode(m2) || !ServerInstance->Modes->AddMode(m3))
57                         throw ModuleException("Could not add new modes!");
58
59                 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin,
60                         I_OnSyncUserMetaData, I_OnUserQuit, I_OnCleanup, I_OnDecodeMetaData };
61
62                 ServerInstance->Modules->Attach(eventlist, this, 8);
63         }
64
65         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
66         virtual void OnWhois(User* source, User* dest)
67         {
68                 std::string *account;
69                 dest->GetExt("accountname", account);
70
71                 if (account)
72                 {
73                         ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
74                 }
75         }
76
77
78         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
79         {
80                 std::string *account;
81
82                 if (!IS_LOCAL(user))
83                         return 0;
84
85                 user->GetExt("accountname", account);
86                 
87                 if (target_type == TYPE_CHANNEL)
88                 {
89                         Channel* c = (Channel*)dest;
90                         
91                         if ((c->IsModeSet('M')) && (!account))
92                         {
93                                 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(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                                 user->WriteNumeric(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                         User* u = (User*)dest;
107                         
108                         if ((u->modes['R'-65]) && (!account))
109                         {
110                                 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(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                                 user->WriteNumeric(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(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
125         {
126                 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
127         }
128          
129         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
130         {
131                 std::string *account;
132                 user->GetExt("accountname", account);
133                 
134                 if (chan)
135                 {
136                         if (chan->IsModeSet('R'))
137                         {
138                                 if (!account)
139                                 {
140                                         if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
141                                         {
142                                                 // user is ulined, won't be stopped from joining
143                                                 return 0;
144                                         }
145                                         // joining a +R channel and not identified
146                                         user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
147                                         return 1;
148                                 }
149                         }
150                 }
151                 return 0;
152         }
153         
154         // Whenever the linking module wants to send out data, but doesnt know what the data
155         // represents (e.g. it is metadata, added to a User or Channel by a module) then
156         // this method is called. We should use the ProtoSendMetaData function after we've
157         // corrected decided how the data should look, to send the metadata on its way if
158         // it is ours.
159         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
160         {
161                 // check if the linking module wants to know about OUR metadata
162                 if (extname == "accountname")
163                 {
164                         // check if this user has an swhois field to send
165                         std::string* account;
166                         user->GetExt("accountname", account);
167                         if (account)
168                         {
169                                 // remove any accidental leading/trailing spaces
170                                 trim(*account);
171
172                                 // call this function in the linking module, let it format the data how it
173                                 // sees fit, and send it on its way. We dont need or want to know how.
174                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
175                         }
176                 }
177         }
178
179         // when a user quits, tidy up their metadata
180         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
181         {
182                 std::string* account;
183                 user->GetExt("accountname", account);
184                 if (account)
185                 {
186                         user->Shrink("accountname");
187                         delete account;
188                 }
189         }
190
191         // if the module is unloaded, tidy up all our dangling metadata
192         virtual void OnCleanup(int target_type, void* item)
193         {
194                 if (target_type == TYPE_USER)
195                 {
196                         User* user = (User*)item;
197                         std::string* account;
198                         user->GetExt("accountname", account);
199                         if (account)
200                         {
201                                 user->Shrink("accountname");
202                                 delete account;
203                         }
204                 }
205         }
206
207         // Whenever the linking module receives metadata from another server and doesnt know what
208         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
209         // module in turn to figure out if this metadata key belongs to them, and what they want
210         // to do with it.
211         // In our case we're only sending a single string around, so we just construct a std::string.
212         // Some modules will probably get much more complex and format more detailed structs and classes
213         // in a textual way for sending over the link.
214         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
215         {
216                 // check if its our metadata key, and its associated with a user
217                 if ((target_type == TYPE_USER) && (extname == "accountname"))
218                 {       
219                         User* dest = (User*)target;
220                         
221                         /* logging them out? */
222                         if (extdata.empty())
223                         {
224                                 std::string* account;
225                                 dest->GetExt("accountname", account);
226                                 if (account)
227                                 {
228                                         dest->Shrink("accountname");
229                                         delete account;
230                                 }
231                         }
232                         else
233                         {
234                                 // if they dont already have an accountname field, accept the remote server's
235                                 std::string* text;
236                                 if (!dest->GetExt("accountname", text))
237                                 {
238                                         text = new std::string(extdata);
239                                         // remove any accidental leading/trailing spaces
240                                         trim(*text);
241                                         dest->Extend("accountname", text);
242
243                                         if (IS_LOCAL(dest))
244                                                 dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s", dest->nick, dest->GetFullHost(), text->c_str(), text->c_str());
245
246                                         AccountData ac;
247                                         ac.user = dest;
248                                         ac.account = *text;
249                                         Event n((char*)&ac, this, "account_login");
250                                         n.Send(ServerInstance);
251                                 }
252                         }
253                 }
254         }
255
256         virtual ~ModuleServicesAccount()
257         {
258                 ServerInstance->Modes->DelMode(m1);
259                 ServerInstance->Modes->DelMode(m2);
260                 ServerInstance->Modes->DelMode(m3);
261                 delete m1;
262                 delete m2;
263                 delete m3;
264         }
265         
266         virtual Version GetVersion()
267         {
268                 return Version(1,2,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
269         }
270 };
271
272 MODULE_INIT(ModuleServicesAccount)