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 class AChannel_R : public ModeHandler
33 AChannel_R() : ModeHandler('R', 0, 0, false, MODETYPE_CHANNEL, false) { }
35 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
39 if (!channel->IsModeSet('R'))
41 channel->SetMode('R',true);
42 return MODEACTION_ALLOW;
47 if (channel->IsModeSet('R'))
49 channel->SetMode('R',false);
50 return MODEACTION_ALLOW;
54 return MODEACTION_DENY;
58 class AUser_R : public ModeHandler
61 AUser_R() : ModeHandler('R', 0, 0, false, MODETYPE_USER, false) { }
63 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
67 if (!dest->IsModeSet('R'))
69 dest->SetMode('R',true);
70 return MODEACTION_ALLOW;
75 if (dest->IsModeSet('R'))
77 dest->SetMode('R',false);
78 return MODEACTION_ALLOW;
82 return MODEACTION_DENY;
86 class AChannel_M : public ModeHandler
89 AChannel_M() : ModeHandler('M', 0, 0, false, MODETYPE_CHANNEL, false) { }
91 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
95 if (!channel->IsModeSet('M'))
97 channel->SetMode('M',true);
98 return MODEACTION_ALLOW;
103 if (channel->IsModeSet('M'))
105 channel->SetMode('M',true);
106 return MODEACTION_ALLOW;
110 return MODEACTION_DENY;
114 class ModuleServicesAccount : public Module
121 ModuleServicesAccount(Server* Me) : Module::Module(Me)
124 m1 = new AChannel_R();
125 m2 = new AChannel_M();
127 Srv->AddMode(m1, 'R');
128 Srv->AddMode(m2, 'M');
129 Srv->AddMode(m3, 'R');
132 virtual void On005Numeric(std::string &output)
134 InsertMode(output, "RM", 4);
137 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
138 virtual void OnWhois(userrec* source, userrec* dest)
140 std::string *account;
141 dest->GetExt("accountname", account);
145 source->WriteServ("330 %s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
149 void Implements(char* List)
151 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
152 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
155 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
157 std::string *account;
158 user->GetExt("accountname", account);
160 if (target_type == TYPE_CHANNEL)
162 chanrec* c = (chanrec*)dest;
164 if ((c->IsModeSet('M')) && (!account))
166 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
168 // user is ulined, can speak regardless
172 // user messaging a +M channel and is not registered
173 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
177 if (target_type == TYPE_USER)
179 userrec* u = (userrec*)dest;
181 if ((u->modes['R'-65]) && (!account))
183 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
185 // user is ulined, can speak regardless
189 // user messaging a +R user and is not registered
190 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
197 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
199 return OnUserPreMessage(user, dest, target_type, text, status);
202 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
204 std::string *account;
205 user->GetExt("accountname", account);
209 if (chan->IsModeSet('R'))
213 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
215 // user is ulined, won't be stopped from joining
218 // joining a +R channel and not identified
219 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
227 // Whenever the linking module wants to send out data, but doesnt know what the data
228 // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
229 // this method is called. We should use the ProtoSendMetaData function after we've
230 // corrected decided how the data should look, to send the metadata on its way if
232 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
234 // check if the linking module wants to know about OUR metadata
235 if (extname == "accountname")
237 // check if this user has an swhois field to send
238 std::string* account;
239 user->GetExt("accountname", account);
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);
249 // when a user quits, tidy up their metadata
250 virtual void OnUserQuit(userrec* user, const std::string &message)
252 std::string* account;
253 user->GetExt("accountname", account);
256 user->Shrink("accountname");
261 // if the module is unloaded, tidy up all our dangling metadata
262 virtual void OnCleanup(int target_type, void* item)
264 if (target_type == TYPE_USER)
266 userrec* user = (userrec*)item;
267 std::string* account;
268 user->GetExt("accountname", account);
271 user->Shrink("accountname");
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
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)
286 // check if its our metadata key, and its associated with a user
287 if ((target_type == TYPE_USER) && (extname == "accountname"))
289 userrec* dest = (userrec*)target;
290 /* logging them out? */
293 std::string* account;
294 dest->GetExt("accountname", account);
297 dest->Shrink("accountname");
303 // if they dont already have an accountname field, accept the remote server's
305 if (!dest->GetExt("accountname", text))
307 text = new std::string(extdata);
308 dest->Extend("accountname", text);
314 virtual ~ModuleServicesAccount()
321 virtual Version GetVersion()
323 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
328 class ModuleServicesAccountFactory : public ModuleFactory
331 ModuleServicesAccountFactory()
335 ~ModuleServicesAccountFactory()
339 virtual Module * CreateModule(Server* Me)
341 return new ModuleServicesAccount(Me);
347 extern "C" void * init_module( void )
349 return new ModuleServicesAccountFactory;