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 ModeHandler
24 AChannel_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_CHANNEL, false) { }
26 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
30 if (!channel->IsModeSet('R'))
32 channel->SetMode('R',true);
33 return MODEACTION_ALLOW;
38 if (channel->IsModeSet('R'))
40 channel->SetMode('R',false);
41 return MODEACTION_ALLOW;
45 return MODEACTION_DENY;
49 /** User mode +R - unidentified users cannot message
51 class AUser_R : public ModeHandler
54 AUser_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_USER, false) { }
56 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
60 if (!dest->IsModeSet('R'))
62 dest->SetMode('R',true);
63 return MODEACTION_ALLOW;
68 if (dest->IsModeSet('R'))
70 dest->SetMode('R',false);
71 return MODEACTION_ALLOW;
75 return MODEACTION_DENY;
79 /** Channel mode +M - unidentified users cannot message channel
81 class AChannel_M : public ModeHandler
84 AChannel_M(InspIRCd* Instance) : ModeHandler(Instance, 'M', 0, 0, false, MODETYPE_CHANNEL, false) { }
86 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
90 if (!channel->IsModeSet('M'))
92 channel->SetMode('M',true);
93 return MODEACTION_ALLOW;
98 if (channel->IsModeSet('M'))
100 channel->SetMode('M',false);
101 return MODEACTION_ALLOW;
105 return MODEACTION_DENY;
109 class ModuleServicesAccount : public Module
116 ModuleServicesAccount(InspIRCd* Me) : Module(Me)
119 m1 = new AChannel_R(ServerInstance);
120 m2 = new AChannel_M(ServerInstance);
121 m3 = new AUser_R(ServerInstance);
122 if (!ServerInstance->Modes->AddMode(m1) || !ServerInstance->Modes->AddMode(m2) || !ServerInstance->Modes->AddMode(m3))
123 throw ModuleException("Could not add new modes!");
125 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin,
126 I_OnSyncUserMetaData, I_OnUserQuit, I_OnCleanup, I_OnDecodeMetaData };
128 ServerInstance->Modules->Attach(eventlist, this, 8);
131 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
132 virtual void OnWhois(User* source, User* dest)
134 std::string *account;
135 dest->GetExt("accountname", account);
139 ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
144 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
146 std::string *account;
151 user->GetExt("accountname", account);
153 if (target_type == TYPE_CHANNEL)
155 Channel* c = (Channel*)dest;
157 if ((c->IsModeSet('M')) && (!account))
159 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
161 // user is ulined, can speak regardless
165 // user messaging a +M channel and is not registered
166 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
170 if (target_type == TYPE_USER)
172 User* u = (User*)dest;
174 if ((u->modes['R'-65]) && (!account))
176 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
178 // user is ulined, can speak regardless
182 // user messaging a +R user and is not registered
183 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
190 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
192 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
195 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
197 std::string *account;
198 user->GetExt("accountname", account);
202 if (chan->IsModeSet('R'))
206 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
208 // user is ulined, won't be stopped from joining
211 // joining a +R channel and not identified
212 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
220 // Whenever the linking module wants to send out data, but doesnt know what the data
221 // represents (e.g. it is metadata, added to a User or Channel by a module) then
222 // this method is called. We should use the ProtoSendMetaData function after we've
223 // corrected decided how the data should look, to send the metadata on its way if
225 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
227 // check if the linking module wants to know about OUR metadata
228 if (extname == "accountname")
230 // check if this user has an swhois field to send
231 std::string* account;
232 user->GetExt("accountname", account);
235 // remove any accidental leading/trailing spaces
238 // call this function in the linking module, let it format the data how it
239 // sees fit, and send it on its way. We dont need or want to know how.
240 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
245 // when a user quits, tidy up their metadata
246 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
248 std::string* account;
249 user->GetExt("accountname", account);
252 user->Shrink("accountname");
257 // if the module is unloaded, tidy up all our dangling metadata
258 virtual void OnCleanup(int target_type, void* item)
260 if (target_type == TYPE_USER)
262 User* user = (User*)item;
263 std::string* account;
264 user->GetExt("accountname", account);
267 user->Shrink("accountname");
273 // Whenever the linking module receives metadata from another server and doesnt know what
274 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
275 // module in turn to figure out if this metadata key belongs to them, and what they want
277 // In our case we're only sending a single string around, so we just construct a std::string.
278 // Some modules will probably get much more complex and format more detailed structs and classes
279 // in a textual way for sending over the link.
280 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
282 // check if its our metadata key, and its associated with a user
283 if ((target_type == TYPE_USER) && (extname == "accountname"))
285 User* dest = (User*)target;
287 /* logging them out? */
290 std::string* account;
291 dest->GetExt("accountname", account);
294 dest->Shrink("accountname");
300 // if they dont already have an accountname field, accept the remote server's
302 if (!dest->GetExt("accountname", text))
304 text = new std::string(extdata);
305 // remove any accidental leading/trailing spaces
307 dest->Extend("accountname", text);
310 dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s", dest->nick, dest->GetFullHost(), text->c_str(), text->c_str());
315 Event n((char*)&ac, this, "account_login");
316 n.Send(ServerInstance);
322 virtual ~ModuleServicesAccount()
324 ServerInstance->Modes->DelMode(m1);
325 ServerInstance->Modes->DelMode(m2);
326 ServerInstance->Modes->DelMode(m3);
332 virtual Version GetVersion()
334 return Version(1,2,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
338 MODULE_INIT(ModuleServicesAccount)