1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 * ---------------------------------------------------
20 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
22 /** Channel mode +R - unidentified users cannot join
24 class AChannel_R : public ModeHandler
27 AChannel_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_CHANNEL, false) { }
29 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
33 if (!channel->IsModeSet('R'))
35 channel->SetMode('R',true);
36 return MODEACTION_ALLOW;
41 if (channel->IsModeSet('R'))
43 channel->SetMode('R',false);
44 return MODEACTION_ALLOW;
48 return MODEACTION_DENY;
52 /** User mode +R - unidentified users cannot message
54 class AUser_R : public ModeHandler
57 AUser_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_USER, false) { }
59 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
63 if (!dest->IsModeSet('R'))
65 dest->SetMode('R',true);
66 return MODEACTION_ALLOW;
71 if (dest->IsModeSet('R'))
73 dest->SetMode('R',false);
74 return MODEACTION_ALLOW;
78 return MODEACTION_DENY;
82 /** Channel mode +M - unidentified users cannot message channel
84 class AChannel_M : public ModeHandler
87 AChannel_M(InspIRCd* Instance) : ModeHandler(Instance, 'M', 0, 0, false, MODETYPE_CHANNEL, false) { }
89 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
93 if (!channel->IsModeSet('M'))
95 channel->SetMode('M',true);
96 return MODEACTION_ALLOW;
101 if (channel->IsModeSet('M'))
103 channel->SetMode('M',false);
104 return MODEACTION_ALLOW;
108 return MODEACTION_DENY;
112 class ModuleServicesAccount : public Module
119 ModuleServicesAccount(InspIRCd* Me) : Module(Me)
122 m1 = new AChannel_R(ServerInstance);
123 m2 = new AChannel_M(ServerInstance);
124 m3 = new AUser_R(ServerInstance);
125 if (!ServerInstance->AddMode(m1, 'R') || !ServerInstance->AddMode(m2, 'M') || !ServerInstance->AddMode(m3, 'R'))
126 throw ModuleException("Could not add new modes!");
129 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
130 virtual void OnWhois(userrec* source, userrec* dest)
132 std::string *account;
133 dest->GetExt("accountname", account);
137 ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
141 void Implements(char* List)
143 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
144 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
147 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
149 std::string *account;
154 user->GetExt("accountname", account);
156 if (target_type == TYPE_CHANNEL)
158 chanrec* c = (chanrec*)dest;
160 if ((c->IsModeSet('M')) && (!account))
162 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
164 // user is ulined, can speak regardless
168 // user messaging a +M channel and is not registered
169 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
173 if (target_type == TYPE_USER)
175 userrec* u = (userrec*)dest;
177 if ((u->modes['R'-65]) && (!account))
179 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
181 // user is ulined, can speak regardless
185 // user messaging a +R user and is not registered
186 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
193 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
195 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
198 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
200 std::string *account;
201 user->GetExt("accountname", account);
205 if (chan->IsModeSet('R'))
209 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
211 // user is ulined, won't be stopped from joining
214 // joining a +R channel and not identified
215 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
223 // Whenever the linking module wants to send out data, but doesnt know what the data
224 // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
225 // this method is called. We should use the ProtoSendMetaData function after we've
226 // corrected decided how the data should look, to send the metadata on its way if
228 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
230 // check if the linking module wants to know about OUR metadata
231 if (extname == "accountname")
233 // check if this user has an swhois field to send
234 std::string* account;
235 user->GetExt("accountname", account);
238 // remove any accidental leading/trailing spaces
241 // call this function in the linking module, let it format the data how it
242 // sees fit, and send it on its way. We dont need or want to know how.
243 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
248 // when a user quits, tidy up their metadata
249 virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
251 std::string* account;
252 user->GetExt("accountname", account);
255 user->Shrink("accountname");
260 // if the module is unloaded, tidy up all our dangling metadata
261 virtual void OnCleanup(int target_type, void* item)
263 if (target_type == TYPE_USER)
265 userrec* user = (userrec*)item;
266 std::string* account;
267 user->GetExt("accountname", account);
270 user->Shrink("accountname");
276 // Whenever the linking module receives metadata from another server and doesnt know what
277 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
278 // module in turn to figure out if this metadata key belongs to them, and what they want
280 // In our case we're only sending a single string around, so we just construct a std::string.
281 // Some modules will probably get much more complex and format more detailed structs and classes
282 // in a textual way for sending over the link.
283 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
285 // check if its our metadata key, and its associated with a user
286 if ((target_type == TYPE_USER) && (extname == "accountname"))
288 userrec* dest = (userrec*)target;
290 /* logging them out? */
293 std::string* account;
294 dest->GetExt("accountname", account);
297 dest->Shrink("accountname");
303 // if they dont already have an accountname field, accept the remote server's
305 if (!dest->GetExt("accountname", text))
307 text = new std::string(extdata);
308 // remove any accidental leading/trailing spaces
310 dest->Extend("accountname", text);
316 virtual ~ModuleServicesAccount()
318 ServerInstance->Modes->DelMode(m1);
319 ServerInstance->Modes->DelMode(m2);
320 ServerInstance->Modes->DelMode(m3);
326 virtual Version GetVersion()
328 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
332 MODULE_INIT(ModuleServicesAccount)