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"
27 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
29 class AChannel_R : public ModeHandler
32 AChannel_R() : ModeHandler('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 class AUser_R : public ModeHandler
60 AUser_R() : ModeHandler('R', 0, 0, false, MODETYPE_USER, false) { }
62 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
66 if (!dest->IsModeSet('R'))
68 dest->SetMode('R',true);
69 return MODEACTION_ALLOW;
74 if (dest->IsModeSet('R'))
76 dest->SetMode('R',false);
77 return MODEACTION_ALLOW;
81 return MODEACTION_DENY;
85 class AChannel_M : public ModeHandler
88 AChannel_M() : ModeHandler('M', 0, 0, false, MODETYPE_CHANNEL, false) { }
90 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
94 if (!channel->IsModeSet('M'))
96 channel->SetMode('M',true);
97 return MODEACTION_ALLOW;
102 if (channel->IsModeSet('M'))
104 channel->SetMode('M',true);
105 return MODEACTION_ALLOW;
109 return MODEACTION_DENY;
113 class ModuleServicesAccount : public Module
120 ModuleServicesAccount(Server* Me) : Module::Module(Me)
123 m1 = new AChannel_R();
124 m2 = new AChannel_M();
126 Srv->AddMode(m1, 'R');
127 Srv->AddMode(m2, 'M');
128 Srv->AddMode(m3, 'R');
131 virtual void On005Numeric(std::string &output)
133 InsertMode(output, "RM", 4);
136 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
137 virtual void OnWhois(userrec* source, userrec* dest)
139 char *account = dest->GetExt("accountname");
143 std::string* accountn = (std::string*)account;
144 WriteServ(source->fd, "330 %s %s %s :is logged in as", source->nick, dest->nick, accountn->c_str());
148 void Implements(char* List)
150 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
151 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
154 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
156 char *account = user->GetExt("accountname");
158 if (target_type == TYPE_CHANNEL)
160 chanrec* c = (chanrec*)dest;
162 if ((c->IsModeSet('M')) && (!account))
164 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
166 // user is ulined, can speak regardless
170 // user messaging a +M channel and is not registered
171 Srv->SendServ(user->fd,"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 ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
183 // user is ulined, can speak regardless
187 // user messaging a +R user and is not registered
188 Srv->SendServ(user->fd,"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)
202 char *account = user->GetExt("accountname");
206 if (chan->IsModeSet('R'))
210 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
212 // user is ulined, won't be stopped from joining
215 // joining a +R channel and not identified
216 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
224 // Whenever the linking module wants to send out data, but doesnt know what the data
225 // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
226 // this method is called. We should use the ProtoSendMetaData function after we've
227 // corrected decided how the data should look, to send the metadata on its way if
229 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
231 // check if the linking module wants to know about OUR metadata
232 if (extname == "accountname")
234 // check if this user has an swhois field to send
235 char* field = user->GetExt("accountname");
238 // get our extdata out with a cast
239 std::string* account = (std::string*)field;
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 char* field = user->GetExt("accountname");
253 std::string* account = (std::string*)field;
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 char* field = user->GetExt("accountname");
268 std::string* account = (std::string*)field;
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 char* field = dest->GetExt("accountname");
294 std::string* account = (std::string*)field;
295 dest->Shrink("accountname");
301 // if they dont already have an accountname field, accept the remote server's
302 if (!dest->GetExt("accountname"))
304 std::string* text = new std::string(extdata);
305 dest->Extend("accountname",(char*)text);
311 virtual ~ModuleServicesAccount()
318 virtual Version GetVersion()
320 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
325 class ModuleServicesAccountFactory : public ModuleFactory
328 ModuleServicesAccountFactory()
332 ~ModuleServicesAccountFactory()
336 virtual Module * CreateModule(Server* Me)
338 return new ModuleServicesAccount(Me);
344 extern "C" void * init_module( void )
346 return new ModuleServicesAccountFactory;