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 * ---------------------------------------------------
16 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
18 /** Channel mode +R - unidentified users cannot join
20 class AChannel_R : public ModeHandler
23 AChannel_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_CHANNEL, false) { }
25 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
29 if (!channel->IsModeSet('R'))
31 channel->SetMode('R',true);
32 return MODEACTION_ALLOW;
37 if (channel->IsModeSet('R'))
39 channel->SetMode('R',false);
40 return MODEACTION_ALLOW;
44 return MODEACTION_DENY;
48 /** User mode +R - unidentified users cannot message
50 class AUser_R : public ModeHandler
53 AUser_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_USER, false) { }
55 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
59 if (!dest->IsModeSet('R'))
61 dest->SetMode('R',true);
62 return MODEACTION_ALLOW;
67 if (dest->IsModeSet('R'))
69 dest->SetMode('R',false);
70 return MODEACTION_ALLOW;
74 return MODEACTION_DENY;
78 /** Channel mode +M - unidentified users cannot message channel
80 class AChannel_M : public ModeHandler
83 AChannel_M(InspIRCd* Instance) : ModeHandler(Instance, 'M', 0, 0, false, MODETYPE_CHANNEL, false) { }
85 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
89 if (!channel->IsModeSet('M'))
91 channel->SetMode('M',true);
92 return MODEACTION_ALLOW;
97 if (channel->IsModeSet('M'))
99 channel->SetMode('M',false);
100 return MODEACTION_ALLOW;
104 return MODEACTION_DENY;
108 class ModuleServicesAccount : public Module
115 ModuleServicesAccount(InspIRCd* Me) : Module(Me)
118 m1 = new AChannel_R(ServerInstance);
119 m2 = new AChannel_M(ServerInstance);
120 m3 = new AUser_R(ServerInstance);
121 if (!ServerInstance->AddMode(m1, 'R') || !ServerInstance->AddMode(m2, 'M') || !ServerInstance->AddMode(m3, 'R'))
122 throw ModuleException("Could not add new modes!");
125 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
126 virtual void OnWhois(User* source, User* dest)
128 std::string *account;
129 dest->GetExt("accountname", account);
133 ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
137 void Implements(char* List)
139 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
140 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
143 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
145 std::string *account;
150 user->GetExt("accountname", account);
152 if (target_type == TYPE_CHANNEL)
154 Channel* c = (Channel*)dest;
156 if ((c->IsModeSet('M')) && (!account))
158 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
160 // user is ulined, can speak regardless
164 // user messaging a +M channel and is not registered
165 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
169 if (target_type == TYPE_USER)
171 User* u = (User*)dest;
173 if ((u->modes['R'-65]) && (!account))
175 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
177 // user is ulined, can speak regardless
181 // user messaging a +R user and is not registered
182 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
189 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
191 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
194 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs)
196 std::string *account;
197 user->GetExt("accountname", account);
201 if (chan->IsModeSet('R'))
205 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
207 // user is ulined, won't be stopped from joining
210 // joining a +R channel and not identified
211 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
219 // Whenever the linking module wants to send out data, but doesnt know what the data
220 // represents (e.g. it is metadata, added to a User or Channel by a module) then
221 // this method is called. We should use the ProtoSendMetaData function after we've
222 // corrected decided how the data should look, to send the metadata on its way if
224 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
226 // check if the linking module wants to know about OUR metadata
227 if (extname == "accountname")
229 // check if this user has an swhois field to send
230 std::string* account;
231 user->GetExt("accountname", account);
234 // remove any accidental leading/trailing spaces
237 // call this function in the linking module, let it format the data how it
238 // sees fit, and send it on its way. We dont need or want to know how.
239 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
244 // when a user quits, tidy up their metadata
245 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
247 std::string* account;
248 user->GetExt("accountname", account);
251 user->Shrink("accountname");
256 // if the module is unloaded, tidy up all our dangling metadata
257 virtual void OnCleanup(int target_type, void* item)
259 if (target_type == TYPE_USER)
261 User* user = (User*)item;
262 std::string* account;
263 user->GetExt("accountname", account);
266 user->Shrink("accountname");
272 // Whenever the linking module receives metadata from another server and doesnt know what
273 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
274 // module in turn to figure out if this metadata key belongs to them, and what they want
276 // In our case we're only sending a single string around, so we just construct a std::string.
277 // Some modules will probably get much more complex and format more detailed structs and classes
278 // in a textual way for sending over the link.
279 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
281 // check if its our metadata key, and its associated with a user
282 if ((target_type == TYPE_USER) && (extname == "accountname"))
284 User* dest = (User*)target;
286 /* logging them out? */
289 std::string* account;
290 dest->GetExt("accountname", account);
293 dest->Shrink("accountname");
299 // if they dont already have an accountname field, accept the remote server's
301 if (!dest->GetExt("accountname", text))
303 text = new std::string(extdata);
304 // remove any accidental leading/trailing spaces
306 dest->Extend("accountname", text);
312 virtual ~ModuleServicesAccount()
314 ServerInstance->Modes->DelMode(m1);
315 ServerInstance->Modes->DelMode(m2);
316 ServerInstance->Modes->DelMode(m3);
322 virtual Version GetVersion()
324 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
328 MODULE_INIT(ModuleServicesAccount)