1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
19 /** Channel mode +R - unidentified users cannot join
21 class AChannel_R : public SimpleChannelModeHandler
24 AChannel_R(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'R') { }
27 /** User mode +R - unidentified users cannot message
29 class AUser_R : public SimpleUserModeHandler
32 AUser_R(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'R') { }
35 /** Channel mode +M - unidentified users cannot message channel
37 class AChannel_M : public SimpleChannelModeHandler
40 AChannel_M(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'M') { }
43 class ModuleServicesAccount : public Module
50 ModuleServicesAccount(InspIRCd* Me) : Module(Me)
53 m1 = new AChannel_R(ServerInstance);
54 m2 = new AChannel_M(ServerInstance);
55 m3 = new AUser_R(ServerInstance);
56 if (!ServerInstance->Modes->AddMode(m1) || !ServerInstance->Modes->AddMode(m2) || !ServerInstance->Modes->AddMode(m3))
57 throw ModuleException("Could not add new modes!");
59 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin,
60 I_OnSyncUserMetaData, I_OnUserQuit, I_OnCleanup, I_OnDecodeMetaData };
62 ServerInstance->Modules->Attach(eventlist, this, 8);
65 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
66 virtual void OnWhois(User* source, User* dest)
69 dest->GetExt("accountname", account);
73 ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick.c_str(), dest->nick.c_str(), account->c_str());
78 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
85 user->GetExt("accountname", account);
87 if (target_type == TYPE_CHANNEL)
89 Channel* c = (Channel*)dest;
91 if ((c->IsModeSet('M')) && (!account))
93 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
95 // user is ulined, can speak regardless
99 // user messaging a +M channel and is not registered
100 user->WriteNumeric(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 User* u = (User*)dest;
108 if ((u->modes['R'-65]) && (!account))
110 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
112 // user is ulined, can speak regardless
116 // user messaging a +R user and is not registered
117 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
124 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
126 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
129 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
131 std::string *account;
132 user->GetExt("accountname", account);
136 if (chan->IsModeSet('R'))
140 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
142 // user is ulined, won't be stopped from joining
145 // joining a +R channel and not identified
146 user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
154 // Whenever the linking module wants to send out data, but doesnt know what the data
155 // represents (e.g. it is metadata, added to a User or Channel by a module) then
156 // this method is called. We should use the ProtoSendMetaData function after we've
157 // corrected decided how the data should look, to send the metadata on its way if
159 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
161 // check if the linking module wants to know about OUR metadata
162 if (extname == "accountname")
164 // check if this user has an swhois field to send
165 std::string* account;
166 user->GetExt("accountname", account);
169 // remove any accidental leading/trailing spaces
172 // call this function in the linking module, let it format the data how it
173 // sees fit, and send it on its way. We dont need or want to know how.
174 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
179 // when a user quits, tidy up their metadata
180 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
182 std::string* account;
183 user->GetExt("accountname", account);
186 user->Shrink("accountname");
191 // if the module is unloaded, tidy up all our dangling metadata
192 virtual void OnCleanup(int target_type, void* item)
194 if (target_type == TYPE_USER)
196 User* user = (User*)item;
197 std::string* account;
198 user->GetExt("accountname", account);
201 user->Shrink("accountname");
207 // Whenever the linking module receives metadata from another server and doesnt know what
208 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
209 // module in turn to figure out if this metadata key belongs to them, and what they want
211 // In our case we're only sending a single string around, so we just construct a std::string.
212 // Some modules will probably get much more complex and format more detailed structs and classes
213 // in a textual way for sending over the link.
214 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
216 // check if its our metadata key, and its associated with a user
217 if ((target_type == TYPE_USER) && (extname == "accountname"))
219 User* dest = (User*)target;
221 /* logging them out? */
224 std::string* account;
225 dest->GetExt("accountname", account);
228 dest->Shrink("accountname");
234 // if they dont already have an accountname field, accept the remote server's
236 if (!dest->GetExt("accountname", text))
238 text = new std::string(extdata);
239 // remove any accidental leading/trailing spaces
241 dest->Extend("accountname", text);
244 dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s", dest->nick.c_str(), dest->GetFullHost().c_str(), text->c_str(), text->c_str());
249 Event n((char*)&ac, this, "account_login");
250 n.Send(ServerInstance);
256 virtual ~ModuleServicesAccount()
258 ServerInstance->Modes->DelMode(m1);
259 ServerInstance->Modes->DelMode(m2);
260 ServerInstance->Modes->DelMode(m3);
266 virtual Version GetVersion()
268 return Version(1,2,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
272 MODULE_INIT(ModuleServicesAccount)