2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2012 Attila Molnar <attilamolnar@hush.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /* $ModDesc: Provides support for extended-join, away-notify and account-notify CAP capabilities */
25 class ModuleIRCv3 : public Module
27 GenericCap cap_accountnotify;
28 GenericCap cap_awaynotify;
29 GenericCap cap_extendedjoin;
36 void WriteNeighboursWithExt(User* user, const std::string& line, const LocalIntExt& ext)
38 UserChanList chans(user->chans);
40 std::map<User*, bool> exceptions;
41 FOREACH_MOD(I_OnBuildNeighborList, OnBuildNeighborList(user, chans, exceptions));
43 // Send it to all local users who were explicitly marked as neighbours by modules and have the required ext
44 for (std::map<User*, bool>::const_iterator i = exceptions.begin(); i != exceptions.end(); ++i)
46 LocalUser* u = IS_LOCAL(i->first);
47 if ((u) && (i->second) && (ext.get(u)))
51 // Now consider sending it to all other users who has at least a common channel with the user
52 std::set<User*> already_sent;
53 for (UCListIter i = chans.begin(); i != chans.end(); ++i)
55 const UserMembList* userlist = (*i)->GetUsers();
56 for (UserMembList::const_iterator m = userlist->begin(); m != userlist->end(); ++m)
59 * Send the line if the channel member in question meets all of the following criteria:
61 * - not the user who is doing the action (i.e. whose channels we're iterating)
62 * - has the given extension
63 * - not on the except list built by modules
64 * - we haven't sent the line to the member yet
67 LocalUser* member = IS_LOCAL(m->first);
68 if ((member) && (member != user) && (ext.get(member)) && (exceptions.find(member) == exceptions.end()) && (already_sent.insert(member).second))
75 ModuleIRCv3() : cap_accountnotify(this, "account-notify"),
76 cap_awaynotify(this, "away-notify"),
77 cap_extendedjoin(this, "extended-join")
84 Implementation eventlist[] = { I_OnUserJoin, I_OnPostJoin, I_OnSetAway, I_OnEvent, I_OnRehash };
85 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
88 void OnRehash(User* user)
90 ConfigTag* conf = ServerInstance->Config->ConfValue("ircv3");
91 accountnotify = conf->getBool("accountnotify", conf->getBool("accoutnotify", true));
92 awaynotify = conf->getBool("awaynotify", true);
93 extendedjoin = conf->getBool("extendedjoin", true);
96 void OnEvent(Event& ev)
99 cap_awaynotify.HandleEvent(ev);
101 cap_extendedjoin.HandleEvent(ev);
105 cap_accountnotify.HandleEvent(ev);
107 if (ev.id == "account_login")
109 AccountEvent* ae = static_cast<AccountEvent*>(&ev);
111 // :nick!user@host ACCOUNT account
113 // :nick!user@host ACCOUNT *
114 std::string line = ":" + ae->user->GetFullHost() + " ACCOUNT ";
115 if (ae->account.empty())
118 line += std::string(ae->account);
120 WriteNeighboursWithExt(ae->user, line, cap_accountnotify.ext);
125 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
127 // Remember who is not going to see the JOIN because of other modules
128 if ((awaynotify) && (IS_AWAY(memb->user)))
129 last_excepts = excepts;
135 * Send extended joins to clients who have the extended-join capability.
136 * An extended join looks like this:
138 * :nick!user@host JOIN #chan account :realname
140 * account is the joining user's account if he's logged in, otherwise it's an asterisk (*).
146 const UserMembList* userlist = memb->chan->GetUsers();
147 for (UserMembCIter it = userlist->begin(); it != userlist->end(); ++it)
149 // Send the extended join line if the current member is local, has the extended-join cap and isn't excepted
150 User* member = IS_LOCAL(it->first);
151 if ((member) && (cap_extendedjoin.ext.get(member)) && (excepts.find(member) == excepts.end()))
153 // Construct the lines we're going to send if we haven't constructed them already
156 bool has_account = false;
157 line = ":" + memb->user->GetFullHost() + " JOIN " + memb->chan->name + " ";
158 const AccountExtItem* accountext = GetAccountExtItem();
161 std::string* accountname;
162 accountname = accountext->get(memb->user);
165 line += *accountname;
173 line += " :" + memb->user->fullname;
175 // If the joining user received privileges from another module then we must send them as well,
176 // since silencing the normal join means the MODE will be silenced as well
177 if (!memb->modes.empty())
179 const std::string& modefrom = ServerInstance->Config->CycleHostsFromUser ? memb->user->GetFullHost() : ServerInstance->Config->ServerName;
180 mode = ":" + modefrom + " MODE " + memb->chan->name + " +" + memb->modes;
182 for (unsigned int i = 0; i < memb->modes.length(); i++)
183 mode += " " + memb->user->nick;
187 // Write the JOIN and the MODE, if any
189 if ((!mode.empty()) && (member != memb->user))
192 // Prevent the core from sending the JOIN and MODE to this user
193 excepts.insert(it->first);
198 ModResult OnSetAway(User* user, const std::string &awaymsg)
202 // Going away: n!u@h AWAY :reason
203 // Back from away: n!u@h AWAY
204 std::string line = ":" + user->GetFullHost() + " AWAY";
205 if (!awaymsg.empty())
206 line += " :" + awaymsg;
208 WriteNeighboursWithExt(user, line, cap_awaynotify.ext);
210 return MOD_RES_PASSTHRU;
213 void OnPostJoin(Membership *memb)
215 if ((!awaynotify) || (!IS_AWAY(memb->user)))
218 std::string line = ":" + memb->user->GetFullHost() + " AWAY :" + memb->user->awaymsg;
220 const UserMembList* userlist = memb->chan->GetUsers();
221 for (UserMembCIter it = userlist->begin(); it != userlist->end(); ++it)
223 // Send the away notify line if the current member is local, has the away-notify cap and isn't excepted
224 User* member = IS_LOCAL(it->first);
225 if ((member) && (cap_awaynotify.ext.get(member)) && (last_excepts.find(member) == last_excepts.end()) && (it->second != memb))
231 last_excepts.clear();
236 ServerInstance->Modules->SetPriority(this, I_OnUserJoin, PRIORITY_LAST);
241 return Version("Provides support for extended-join, away-notify and account-notify CAP capabilities", VF_VENDOR);
245 MODULE_INIT(ModuleIRCv3)