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 * ---------------------------------------------------
28 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
32 class AChannel_R : public ModeHandler
35 AChannel_R(InspIRCd* Instance) : ModeHandler(Instance, '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(InspIRCd* Instance) : ModeHandler(Instance, '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(InspIRCd* Instance) : ModeHandler(Instance, '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(InspIRCd* Me) : Module::Module(Me)
126 m1 = new AChannel_R(ServerInstance);
127 m2 = new AChannel_M(ServerInstance);
128 m3 = new AUser_R(ServerInstance);
129 ServerInstance->AddMode(m1, 'R');
130 ServerInstance->AddMode(m2, 'M');
131 ServerInstance->AddMode(m3, 'R');
134 virtual void On005Numeric(std::string &output)
138 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
139 virtual void OnWhois(userrec* source, userrec* dest)
141 std::string *account;
142 dest->GetExt("accountname", account);
146 source->WriteServ("330 %s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
150 void Implements(char* List)
152 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
153 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
156 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
158 std::string *account;
159 user->GetExt("accountname", account);
161 if (target_type == TYPE_CHANNEL)
163 chanrec* c = (chanrec*)dest;
165 if ((c->IsModeSet('M')) && (!account))
167 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)) || (!strcmp(user->server,"")))
169 // user is ulined, can speak regardless
173 // user messaging a +M channel and is not registered
174 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
178 if (target_type == TYPE_USER)
180 userrec* u = (userrec*)dest;
182 if ((u->modes['R'-65]) && (!account))
184 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
186 // user is ulined, can speak regardless
190 // user messaging a +R user and is not registered
191 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
198 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
200 return OnUserPreMessage(user, dest, target_type, text, status);
203 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
205 std::string *account;
206 user->GetExt("accountname", account);
210 if (chan->IsModeSet('R'))
214 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
216 // user is ulined, won't be stopped from joining
219 // joining a +R channel and not identified
220 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
228 // Whenever the linking module wants to send out data, but doesnt know what the data
229 // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
230 // this method is called. We should use the ProtoSendMetaData function after we've
231 // corrected decided how the data should look, to send the metadata on its way if
233 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
235 // check if the linking module wants to know about OUR metadata
236 if (extname == "accountname")
238 // check if this user has an swhois field to send
239 std::string* account;
240 user->GetExt("accountname", account);
243 // call this function in the linking module, let it format the data how it
244 // sees fit, and send it on its way. We dont need or want to know how.
245 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
250 // when a user quits, tidy up their metadata
251 virtual void OnUserQuit(userrec* user, const std::string &message)
253 std::string* account;
254 user->GetExt("accountname", account);
257 user->Shrink("accountname");
262 // if the module is unloaded, tidy up all our dangling metadata
263 virtual void OnCleanup(int target_type, void* item)
265 if (target_type == TYPE_USER)
267 userrec* user = (userrec*)item;
268 std::string* account;
269 user->GetExt("accountname", account);
272 user->Shrink("accountname");
278 // Whenever the linking module receives metadata from another server and doesnt know what
279 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
280 // module in turn to figure out if this metadata key belongs to them, and what they want
282 // In our case we're only sending a single string around, so we just construct a std::string.
283 // Some modules will probably get much more complex and format more detailed structs and classes
284 // in a textual way for sending over the link.
285 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
287 // check if its our metadata key, and its associated with a user
288 if ((target_type == TYPE_USER) && (extname == "accountname"))
290 userrec* dest = (userrec*)target;
291 /* logging them out? */
294 std::string* account;
295 dest->GetExt("accountname", account);
298 dest->Shrink("accountname");
304 // if they dont already have an accountname field, accept the remote server's
306 if (!dest->GetExt("accountname", text))
308 text = new std::string(extdata);
309 dest->Extend("accountname", text);
315 virtual ~ModuleServicesAccount()
322 virtual Version GetVersion()
324 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
329 class ModuleServicesAccountFactory : public ModuleFactory
332 ModuleServicesAccountFactory()
336 ~ModuleServicesAccountFactory()
340 virtual Module * CreateModule(InspIRCd* Me)
342 return new ModuleServicesAccount(Me);
348 extern "C" void * init_module( void )
350 return new ModuleServicesAccountFactory;