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