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 /** Channel mode +R - unidentified users cannot join
29 class AChannel_R : public ModeHandler
32 AChannel_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_CHANNEL, false) { }
34 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
38 if (!channel->IsModeSet('R'))
40 channel->SetMode('R',true);
41 return MODEACTION_ALLOW;
46 if (channel->IsModeSet('R'))
48 channel->SetMode('R',false);
49 return MODEACTION_ALLOW;
53 return MODEACTION_DENY;
57 /** User mode +R - unidentified users cannot message
59 class AUser_R : public ModeHandler
62 AUser_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_USER, false) { }
64 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
68 if (!dest->IsModeSet('R'))
70 dest->SetMode('R',true);
71 return MODEACTION_ALLOW;
76 if (dest->IsModeSet('R'))
78 dest->SetMode('R',false);
79 return MODEACTION_ALLOW;
83 return MODEACTION_DENY;
87 /** Channel mode +M - unidentified users cannot message channel
89 class AChannel_M : public ModeHandler
92 AChannel_M(InspIRCd* Instance) : ModeHandler(Instance, 'M', 0, 0, false, MODETYPE_CHANNEL, false) { }
94 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
98 if (!channel->IsModeSet('M'))
100 channel->SetMode('M',true);
101 return MODEACTION_ALLOW;
106 if (channel->IsModeSet('M'))
108 channel->SetMode('M',false);
109 return MODEACTION_ALLOW;
113 return MODEACTION_DENY;
117 class ModuleServicesAccount : public Module
124 ModuleServicesAccount(InspIRCd* Me) : Module::Module(Me)
127 m1 = new AChannel_R(ServerInstance);
128 m2 = new AChannel_M(ServerInstance);
129 m3 = new AUser_R(ServerInstance);
130 ServerInstance->AddMode(m1, 'R');
131 ServerInstance->AddMode(m2, 'M');
132 ServerInstance->AddMode(m3, 'R');
135 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
136 virtual void OnWhois(userrec* source, userrec* dest)
138 std::string *account;
139 dest->GetExt("accountname", account);
143 source->WriteServ("330 %s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
147 void Implements(char* List)
149 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
150 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
153 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
155 std::string *account;
156 user->GetExt("accountname", account);
158 if (target_type == TYPE_CHANNEL)
160 chanrec* c = (chanrec*)dest;
162 if ((c->IsModeSet('M')) && (!account))
164 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)) || (!strcmp(user->server,"")))
166 // user is ulined, can speak regardless
170 // user messaging a +M channel and is not registered
171 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
175 if (target_type == TYPE_USER)
177 userrec* u = (userrec*)dest;
179 if ((u->modes['R'-65]) && (!account))
181 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
183 // user is ulined, can speak regardless
187 // user messaging a +R user and is not registered
188 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
195 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
197 return OnUserPreMessage(user, dest, target_type, text, status);
200 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
202 std::string *account;
203 user->GetExt("accountname", account);
207 if (chan->IsModeSet('R'))
211 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
213 // user is ulined, won't be stopped from joining
216 // joining a +R channel and not identified
217 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
225 // Whenever the linking module wants to send out data, but doesnt know what the data
226 // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
227 // this method is called. We should use the ProtoSendMetaData function after we've
228 // corrected decided how the data should look, to send the metadata on its way if
230 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
232 // check if the linking module wants to know about OUR metadata
233 if (extname == "accountname")
235 // check if this user has an swhois field to send
236 std::string* account;
237 user->GetExt("accountname", account);
240 // call this function in the linking module, let it format the data how it
241 // sees fit, and send it on its way. We dont need or want to know how.
242 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
247 // when a user quits, tidy up their metadata
248 virtual void OnUserQuit(userrec* user, const std::string &message)
250 std::string* account;
251 user->GetExt("accountname", account);
254 user->Shrink("accountname");
259 // if the module is unloaded, tidy up all our dangling metadata
260 virtual void OnCleanup(int target_type, void* item)
262 if (target_type == TYPE_USER)
264 userrec* user = (userrec*)item;
265 std::string* account;
266 user->GetExt("accountname", account);
269 user->Shrink("accountname");
275 // Whenever the linking module receives metadata from another server and doesnt know what
276 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
277 // module in turn to figure out if this metadata key belongs to them, and what they want
279 // In our case we're only sending a single string around, so we just construct a std::string.
280 // Some modules will probably get much more complex and format more detailed structs and classes
281 // in a textual way for sending over the link.
282 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
284 // check if its our metadata key, and its associated with a user
285 if ((target_type == TYPE_USER) && (extname == "accountname"))
287 userrec* dest = (userrec*)target;
288 /* logging them out? */
291 std::string* account;
292 dest->GetExt("accountname", account);
295 dest->Shrink("accountname");
301 // if they dont already have an accountname field, accept the remote server's
303 if (!dest->GetExt("accountname", text))
305 text = new std::string(extdata);
306 dest->Extend("accountname", text);
312 virtual ~ModuleServicesAccount()
314 ServerInstance->Modes->DelMode(m1);
315 ServerInstance->Modes->DelMode(m2);
316 ServerInstance->Modes->DelMode(m3);
322 virtual Version GetVersion()
324 return Version(1,0,0,0,VF_COMMON|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;