]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
eee4d881a5d19b55f262feef5bc34b4ac92fc06b
[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
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         void Implements(char* List)
143         {
144                 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
145                 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
146         }
147
148         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
149         {
150                 std::string *account;
151
152                 if (!IS_LOCAL(user))
153                         return 0;
154
155                 user->GetExt("accountname", account);
156                 
157                 if (target_type == TYPE_CHANNEL)
158                 {
159                         Channel* c = (Channel*)dest;
160                         
161                         if ((c->IsModeSet('M')) && (!account))
162                         {
163                                 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
164                                 {
165                                         // user is ulined, can speak regardless
166                                         return 0;
167                                 }
168
169                                 // user messaging a +M channel and is not registered
170                                 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
171                                 return 1;
172                         }
173                 }
174                 if (target_type == TYPE_USER)
175                 {
176                         User* u = (User*)dest;
177                         
178                         if ((u->modes['R'-65]) && (!account))
179                         {
180                                 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
181                                 {
182                                         // user is ulined, can speak regardless
183                                         return 0;
184                                 }
185
186                                 // user messaging a +R user and is not registered
187                                 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
188                                 return 1;
189                         }
190                 }
191                 return 0;
192         }
193          
194         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
195         {
196                 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
197         }
198          
199         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs)
200         {
201                 std::string *account;
202                 user->GetExt("accountname", account);
203                 
204                 if (chan)
205                 {
206                         if (chan->IsModeSet('R'))
207                         {
208                                 if (!account)
209                                 {
210                                         if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
211                                         {
212                                                 // user is ulined, won't be stopped from joining
213                                                 return 0;
214                                         }
215                                         // joining a +R channel and not identified
216                                         user->WriteServ("477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
217                                         return 1;
218                                 }
219                         }
220                 }
221                 return 0;
222         }
223         
224         // Whenever the linking module wants to send out data, but doesnt know what the data
225         // represents (e.g. it is metadata, added to a User or Channel by a module) then
226         // this method is called. We should use the ProtoSendMetaData function after we've
227         // corrected decided how the data should look, to send the metadata on its way if
228         // it is ours.
229         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
230         {
231                 // check if the linking module wants to know about OUR metadata
232                 if (extname == "accountname")
233                 {
234                         // check if this user has an swhois field to send
235                         std::string* account;
236                         user->GetExt("accountname", account);
237                         if (account)
238                         {
239                                 // remove any accidental leading/trailing spaces
240                                 trim(*account);
241
242                                 // call this function in the linking module, let it format the data how it
243                                 // sees fit, and send it on its way. We dont need or want to know how.
244                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
245                         }
246                 }
247         }
248
249         // when a user quits, tidy up their metadata
250         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
251         {
252                 std::string* account;
253                 user->GetExt("accountname", account);
254                 if (account)
255                 {
256                         user->Shrink("accountname");
257                         delete account;
258                 }
259         }
260
261         // if the module is unloaded, tidy up all our dangling metadata
262         virtual void OnCleanup(int target_type, void* item)
263         {
264                 if (target_type == TYPE_USER)
265                 {
266                         User* user = (User*)item;
267                         std::string* account;
268                         user->GetExt("accountname", account);
269                         if (account)
270                         {
271                                 user->Shrink("accountname");
272                                 delete account;
273                         }
274                 }
275         }
276
277         // Whenever the linking module receives metadata from another server and doesnt know what
278         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
279         // module in turn to figure out if this metadata key belongs to them, and what they want
280         // to do with it.
281         // In our case we're only sending a single string around, so we just construct a std::string.
282         // Some modules will probably get much more complex and format more detailed structs and classes
283         // in a textual way for sending over the link.
284         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
285         {
286                 // check if its our metadata key, and its associated with a user
287                 if ((target_type == TYPE_USER) && (extname == "accountname"))
288                 {       
289                         User* dest = (User*)target;
290                         
291                         /* logging them out? */
292                         if (extdata.empty())
293                         {
294                                 std::string* account;
295                                 dest->GetExt("accountname", account);
296                                 if (account)
297                                 {
298                                         dest->Shrink("accountname");
299                                         delete account;
300                                 }
301                         }
302                         else
303                         {
304                                 // if they dont already have an accountname field, accept the remote server's
305                                 std::string* text;
306                                 if (!dest->GetExt("accountname", text))
307                                 {
308                                         text = new std::string(extdata);
309                                         // remove any accidental leading/trailing spaces
310                                         trim(*text);
311                                         dest->Extend("accountname", text);
312                                 }
313                         }
314                 }
315         }
316
317         virtual ~ModuleServicesAccount()
318         {
319                 ServerInstance->Modes->DelMode(m1);
320                 ServerInstance->Modes->DelMode(m2);
321                 ServerInstance->Modes->DelMode(m3);
322                 delete m1;
323                 delete m2;
324                 delete m3;
325         }
326         
327         virtual Version GetVersion()
328         {
329                 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
330         }
331 };
332
333 MODULE_INIT(ModuleServicesAccount)