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 * ---------------------------------------------------
25 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
27 class AChannel_R : public ModeHandler
30 AChannel_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_CHANNEL, false) { }
32 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
36 if (!channel->IsModeSet('R'))
38 channel->SetMode('R',true);
39 return MODEACTION_ALLOW;
44 if (channel->IsModeSet('R'))
46 channel->SetMode('R',false);
47 return MODEACTION_ALLOW;
51 return MODEACTION_DENY;
55 class AUser_R : public ModeHandler
58 AUser_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_USER, false) { }
60 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
64 if (!dest->IsModeSet('R'))
66 dest->SetMode('R',true);
67 return MODEACTION_ALLOW;
72 if (dest->IsModeSet('R'))
74 dest->SetMode('R',false);
75 return MODEACTION_ALLOW;
79 return MODEACTION_DENY;
83 class AChannel_M : public ModeHandler
86 AChannel_M(InspIRCd* Instance) : ModeHandler(Instance, 'M', 0, 0, false, MODETYPE_CHANNEL, false) { }
88 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
92 if (!channel->IsModeSet('M'))
94 channel->SetMode('M',true);
95 return MODEACTION_ALLOW;
100 if (channel->IsModeSet('M'))
102 channel->SetMode('M',true);
103 return MODEACTION_ALLOW;
107 return MODEACTION_DENY;
111 class ModuleServicesAccount : public Module
118 ModuleServicesAccount(InspIRCd* Me) : Module::Module(Me)
121 m1 = new AChannel_R(ServerInstance);
122 m2 = new AChannel_M(ServerInstance);
123 m3 = new AUser_R(ServerInstance);
124 ServerInstance->AddMode(m1, 'R');
125 ServerInstance->AddMode(m2, 'M');
126 ServerInstance->AddMode(m3, 'R');
129 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
130 virtual void OnWhois(userrec* source, userrec* dest)
132 std::string *account;
133 dest->GetExt("accountname", account);
137 source->WriteServ("330 %s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
141 void Implements(char* List)
143 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
144 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
147 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
149 std::string *account;
150 user->GetExt("accountname", account);
152 if (target_type == TYPE_CHANNEL)
154 chanrec* c = (chanrec*)dest;
156 if ((c->IsModeSet('M')) && (!account))
158 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)) || (!strcmp(user->server,"")))
160 // user is ulined, can speak regardless
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");
169 if (target_type == TYPE_USER)
171 userrec* u = (userrec*)dest;
173 if ((u->modes['R'-65]) && (!account))
175 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
177 // user is ulined, can speak regardless
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");
189 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
191 return OnUserPreMessage(user, dest, target_type, text, status);
194 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
196 std::string *account;
197 user->GetExt("accountname", account);
201 if (chan->IsModeSet('R'))
205 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
207 // user is ulined, won't be stopped from joining
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");
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 userrec or chanrec 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
224 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
226 // check if the linking module wants to know about OUR metadata
227 if (extname == "accountname")
229 // check if this user has an swhois field to send
230 std::string* account;
231 user->GetExt("accountname", account);
234 // call this function in the linking module, let it format the data how it
235 // sees fit, and send it on its way. We dont need or want to know how.
236 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
241 // when a user quits, tidy up their metadata
242 virtual void OnUserQuit(userrec* user, const std::string &message)
244 std::string* account;
245 user->GetExt("accountname", account);
248 user->Shrink("accountname");
253 // if the module is unloaded, tidy up all our dangling metadata
254 virtual void OnCleanup(int target_type, void* item)
256 if (target_type == TYPE_USER)
258 userrec* user = (userrec*)item;
259 std::string* account;
260 user->GetExt("accountname", account);
263 user->Shrink("accountname");
269 // Whenever the linking module receives metadata from another server and doesnt know what
270 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
271 // module in turn to figure out if this metadata key belongs to them, and what they want
273 // In our case we're only sending a single string around, so we just construct a std::string.
274 // Some modules will probably get much more complex and format more detailed structs and classes
275 // in a textual way for sending over the link.
276 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
278 // check if its our metadata key, and its associated with a user
279 if ((target_type == TYPE_USER) && (extname == "accountname"))
281 userrec* dest = (userrec*)target;
282 /* logging them out? */
285 std::string* account;
286 dest->GetExt("accountname", account);
289 dest->Shrink("accountname");
295 // if they dont already have an accountname field, accept the remote server's
297 if (!dest->GetExt("accountname", text))
299 text = new std::string(extdata);
300 dest->Extend("accountname", text);
306 virtual ~ModuleServicesAccount()
308 ServerInstance->Modes->DelMode(m1);
309 ServerInstance->Modes->DelMode(m2);
310 ServerInstance->Modes->DelMode(m3);
316 virtual Version GetVersion()
318 return Version(1,0,0,0,VF_COMMON|VF_VENDOR);
323 class ModuleServicesAccountFactory : public ModuleFactory
326 ModuleServicesAccountFactory()
330 ~ModuleServicesAccountFactory()
334 virtual Module * CreateModule(InspIRCd* Me)
336 return new ModuleServicesAccount(Me);
342 extern "C" void * init_module( void )
344 return new ModuleServicesAccountFactory;