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, I_On005Numeric };
62 ServerInstance->Modules->Attach(eventlist, this, 9);
65 virtual void On005Numeric(std::string &t)
67 ServerInstance->AddExtBanChar('R');
68 ServerInstance->AddExtBanChar('M');
71 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
72 virtual void OnWhois(User* source, User* dest)
75 dest->GetExt("accountname", account);
79 ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick.c_str(), dest->nick.c_str(), account->c_str());
84 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
91 user->GetExt("accountname", account);
93 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
95 // user is ulined, can speak regardless
99 if (target_type == TYPE_CHANNEL)
101 Channel* c = (Channel*)dest;
103 if ((c->IsModeSet('M')) && (!account))
105 // user messaging a +M channel and is not registered
106 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
112 if (c->IsExtBanned(*account, 'M'))
115 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You may not speak in this channel");
121 if (target_type == TYPE_USER)
123 User* u = (User*)dest;
125 if ((u->modes['R'-65]) && (!account))
127 // user messaging a +R user and is not registered
128 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
135 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
137 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
140 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
142 std::string *account;
143 user->GetExt("accountname", account);
147 if (chan->IsModeSet('R'))
151 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
153 // user is ulined, won't be stopped from joining
156 // joining a +R channel and not identified
157 user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
164 if (chan->IsExtBanned(*account, 'R'))
167 user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s %s :Cannot join channel (You're banned)", user->nick.c_str(), chan->name.c_str());
175 // Whenever the linking module wants to send out data, but doesnt know what the data
176 // represents (e.g. it is metadata, added to a User or Channel by a module) then
177 // this method is called. We should use the ProtoSendMetaData function after we've
178 // corrected decided how the data should look, to send the metadata on its way if
180 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
182 // check if the linking module wants to know about OUR metadata
183 if (extname == "accountname")
185 // check if this user has an swhois field to send
186 std::string* account;
187 user->GetExt("accountname", account);
190 // remove any accidental leading/trailing spaces
193 // call this function in the linking module, let it format the data how it
194 // sees fit, and send it on its way. We dont need or want to know how.
195 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
200 // when a user quits, tidy up their metadata
201 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
203 std::string* account;
204 user->GetExt("accountname", account);
207 user->Shrink("accountname");
212 // if the module is unloaded, tidy up all our dangling metadata
213 virtual void OnCleanup(int target_type, void* item)
215 if (target_type == TYPE_USER)
217 User* user = (User*)item;
218 std::string* account;
219 user->GetExt("accountname", account);
222 user->Shrink("accountname");
228 // Whenever the linking module receives metadata from another server and doesnt know what
229 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
230 // module in turn to figure out if this metadata key belongs to them, and what they want
232 // In our case we're only sending a single string around, so we just construct a std::string.
233 // Some modules will probably get much more complex and format more detailed structs and classes
234 // in a textual way for sending over the link.
235 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
237 // check if its our metadata key, and its associated with a user
238 if ((target_type == TYPE_USER) && (extname == "accountname"))
240 User* dest = (User*)target;
242 /* logging them out? */
245 std::string* account;
246 dest->GetExt("accountname", account);
249 dest->Shrink("accountname");
255 // if they dont already have an accountname field, accept the remote server's
257 if (!dest->GetExt("accountname", text))
259 text = new std::string(extdata);
260 // remove any accidental leading/trailing spaces
262 dest->Extend("accountname", text);
265 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());
270 Event n((char*)&ac, this, "account_login");
271 n.Send(ServerInstance);
277 virtual ~ModuleServicesAccount()
279 ServerInstance->Modes->DelMode(m1);
280 ServerInstance->Modes->DelMode(m2);
281 ServerInstance->Modes->DelMode(m3);
287 virtual Version GetVersion()
289 return Version(1,2,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
293 MODULE_INIT(ModuleServicesAccount)