1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
24 #include "helperfuncs.h"
28 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
30 extern InspIRCd* ServerInstance;
32 class AChannel_R : public ModeHandler
35 AChannel_R() : ModeHandler('R', 0, 0, false, MODETYPE_CHANNEL, false) { }
37 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
41 if (!channel->IsModeSet('R'))
43 channel->SetMode('R',true);
44 return MODEACTION_ALLOW;
49 if (channel->IsModeSet('R'))
51 channel->SetMode('R',false);
52 return MODEACTION_ALLOW;
56 return MODEACTION_DENY;
60 class AUser_R : public ModeHandler
63 AUser_R() : ModeHandler('R', 0, 0, false, MODETYPE_USER, false) { }
65 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
69 if (!dest->IsModeSet('R'))
71 dest->SetMode('R',true);
72 return MODEACTION_ALLOW;
77 if (dest->IsModeSet('R'))
79 dest->SetMode('R',false);
80 return MODEACTION_ALLOW;
84 return MODEACTION_DENY;
88 class AChannel_M : public ModeHandler
91 AChannel_M() : ModeHandler('M', 0, 0, false, MODETYPE_CHANNEL, false) { }
93 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
97 if (!channel->IsModeSet('M'))
99 channel->SetMode('M',true);
100 return MODEACTION_ALLOW;
105 if (channel->IsModeSet('M'))
107 channel->SetMode('M',true);
108 return MODEACTION_ALLOW;
112 return MODEACTION_DENY;
116 class ModuleServicesAccount : public Module
123 ModuleServicesAccount(Server* Me) : Module::Module(Me)
126 m1 = new AChannel_R();
127 m2 = new AChannel_M();
129 Srv->AddMode(m1, 'R');
130 Srv->AddMode(m2, 'M');
131 Srv->AddMode(m3, 'R');
134 virtual void On005Numeric(std::string &output)
136 ServerInstance->ModeGrok->InsertMode(output, "RM", 4);
139 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
140 virtual void OnWhois(userrec* source, userrec* dest)
142 std::string *account;
143 dest->GetExt("accountname", account);
147 source->WriteServ("330 %s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
151 void Implements(char* List)
153 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
154 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
157 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
159 std::string *account;
160 user->GetExt("accountname", account);
162 if (target_type == TYPE_CHANNEL)
164 chanrec* c = (chanrec*)dest;
166 if ((c->IsModeSet('M')) && (!account))
168 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
170 // user is ulined, can speak regardless
174 // user messaging a +M channel and is not registered
175 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
179 if (target_type == TYPE_USER)
181 userrec* u = (userrec*)dest;
183 if ((u->modes['R'-65]) && (!account))
185 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
187 // user is ulined, can speak regardless
191 // user messaging a +R user and is not registered
192 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
199 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
201 return OnUserPreMessage(user, dest, target_type, text, status);
204 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
206 std::string *account;
207 user->GetExt("accountname", account);
211 if (chan->IsModeSet('R'))
215 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
217 // user is ulined, won't be stopped from joining
220 // joining a +R channel and not identified
221 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
229 // Whenever the linking module wants to send out data, but doesnt know what the data
230 // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
231 // this method is called. We should use the ProtoSendMetaData function after we've
232 // corrected decided how the data should look, to send the metadata on its way if
234 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
236 // check if the linking module wants to know about OUR metadata
237 if (extname == "accountname")
239 // check if this user has an swhois field to send
240 std::string* account;
241 user->GetExt("accountname", account);
244 // call this function in the linking module, let it format the data how it
245 // sees fit, and send it on its way. We dont need or want to know how.
246 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
251 // when a user quits, tidy up their metadata
252 virtual void OnUserQuit(userrec* user, const std::string &message)
254 std::string* account;
255 user->GetExt("accountname", account);
258 user->Shrink("accountname");
263 // if the module is unloaded, tidy up all our dangling metadata
264 virtual void OnCleanup(int target_type, void* item)
266 if (target_type == TYPE_USER)
268 userrec* user = (userrec*)item;
269 std::string* account;
270 user->GetExt("accountname", account);
273 user->Shrink("accountname");
279 // Whenever the linking module receives metadata from another server and doesnt know what
280 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
281 // module in turn to figure out if this metadata key belongs to them, and what they want
283 // In our case we're only sending a single string around, so we just construct a std::string.
284 // Some modules will probably get much more complex and format more detailed structs and classes
285 // in a textual way for sending over the link.
286 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
288 // check if its our metadata key, and its associated with a user
289 if ((target_type == TYPE_USER) && (extname == "accountname"))
291 userrec* dest = (userrec*)target;
292 /* logging them out? */
295 std::string* account;
296 dest->GetExt("accountname", account);
299 dest->Shrink("accountname");
305 // if they dont already have an accountname field, accept the remote server's
307 if (!dest->GetExt("accountname", text))
309 text = new std::string(extdata);
310 dest->Extend("accountname", text);
316 virtual ~ModuleServicesAccount()
323 virtual Version GetVersion()
325 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
330 class ModuleServicesAccountFactory : public ModuleFactory
333 ModuleServicesAccountFactory()
337 ~ModuleServicesAccountFactory()
341 virtual Module * CreateModule(Server* Me)
343 return new ModuleServicesAccount(Me);
349 extern "C" void * init_module( void )
351 return new ModuleServicesAccountFactory;