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 ModuleServicesAccount : public Module
34 ModuleServicesAccount(Server* Me) : Module::Module(Me)
38 Srv->AddExtendedMode('R',MT_CHANNEL,false,0,0);
39 Srv->AddExtendedMode('R',MT_CLIENT,false,0,0);
40 Srv->AddExtendedMode('M',MT_CHANNEL,false,0,0);
43 virtual void On005Numeric(std::string &output)
45 InsertMode(output, "RM", 4);
48 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
49 virtual void OnWhois(userrec* source, userrec* dest)
51 char *account = dest->GetExt("accountname");
55 std::string* accountn = (std::string*)account;
56 WriteServ(source->fd, "330 %s %s %s :is logged in as", source->nick, dest->nick, accountn->c_str());
60 void Implements(char* List)
62 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_OnExtendedMode] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
63 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
66 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
72 else if (modechar == 'M')
74 if (type == MT_CHANNEL)
83 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
85 char *account = user->GetExt("accountname");
87 if (target_type == TYPE_CHANNEL)
89 chanrec* c = (chanrec*)dest;
91 if ((c->IsModeSet('M')) && (!account))
93 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
95 // user is ulined, can speak regardless
99 // user messaging a +M channel and is not registered
100 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");
104 if (target_type == TYPE_USER)
106 userrec* u = (userrec*)dest;
108 if ((strchr(u->modes,'R')) && (!account))
110 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
112 // user is ulined, can speak regardless
116 // user messaging a +R user and is not registered
117 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");
124 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
126 return OnUserPreMessage(user, dest, target_type, text, status);
129 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
131 char *account = user->GetExt("accountname");
135 if (chan->IsModeSet('R'))
139 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
141 // user is ulined, won't be stopped from joining
144 // joining a +R channel and not identified
145 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");
153 // Whenever the linking module wants to send out data, but doesnt know what the data
154 // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
155 // this method is called. We should use the ProtoSendMetaData function after we've
156 // corrected decided how the data should look, to send the metadata on its way if
158 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
160 // check if the linking module wants to know about OUR metadata
161 if (extname == "accountname")
163 // check if this user has an swhois field to send
164 char* field = user->GetExt("accountname");
167 // get our extdata out with a cast
168 std::string* account = (std::string*)field;
169 // call this function in the linking module, let it format the data how it
170 // sees fit, and send it on its way. We dont need or want to know how.
171 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
176 // when a user quits, tidy up their metadata
177 virtual void OnUserQuit(userrec* user, const std::string &message)
179 char* field = user->GetExt("accountname");
182 std::string* account = (std::string*)field;
183 user->Shrink("accountname");
188 // if the module is unloaded, tidy up all our dangling metadata
189 virtual void OnCleanup(int target_type, void* item)
191 if (target_type == TYPE_USER)
193 userrec* user = (userrec*)item;
194 char* field = user->GetExt("accountname");
197 std::string* account = (std::string*)field;
198 user->Shrink("accountname");
204 // Whenever the linking module receives metadata from another server and doesnt know what
205 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
206 // module in turn to figure out if this metadata key belongs to them, and what they want
208 // In our case we're only sending a single string around, so we just construct a std::string.
209 // Some modules will probably get much more complex and format more detailed structs and classes
210 // in a textual way for sending over the link.
211 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
213 // check if its our metadata key, and its associated with a user
214 if ((target_type == TYPE_USER) && (extname == "accountname"))
216 userrec* dest = (userrec*)target;
217 /* logging them out? */
220 char* field = dest->GetExt("accountname");
223 std::string* account = (std::string*)field;
224 dest->Shrink("accountname");
230 // if they dont already have an accountname field, accept the remote server's
231 if (!dest->GetExt("accountname"))
233 std::string* text = new std::string(extdata);
234 dest->Extend("accountname",(char*)text);
240 virtual ~ModuleServicesAccount()
244 virtual Version GetVersion()
246 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
251 class ModuleServicesAccountFactory : public ModuleFactory
254 ModuleServicesAccountFactory()
258 ~ModuleServicesAccountFactory()
262 virtual Module * CreateModule(Server* Me)
264 return new ModuleServicesAccount(Me);
270 extern "C" void * init_module( void )
272 return new ModuleServicesAccountFactory;