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/>.
20 #include "modules/account.h"
21 #include "modules/cap.h"
23 class ModuleIRCv3 : public Module
25 GenericCap cap_accountnotify;
26 GenericCap cap_awaynotify;
27 GenericCap cap_extendedjoin;
34 void WriteNeighboursWithExt(User* user, const std::string& line, const LocalIntExt& ext)
36 IncludeChanList chans(user->chans.begin(), user->chans.end());
38 std::map<User*, bool> exceptions;
39 FOREACH_MOD(OnBuildNeighborList, (user, chans, exceptions));
41 // Send it to all local users who were explicitly marked as neighbours by modules and have the required ext
42 for (std::map<User*, bool>::const_iterator i = exceptions.begin(); i != exceptions.end(); ++i)
44 LocalUser* u = IS_LOCAL(i->first);
45 if ((u) && (i->second) && (ext.get(u)))
49 // Now consider sending it to all other users who has at least a common channel with the user
50 std::set<User*> already_sent;
51 for (IncludeChanList::const_iterator i = chans.begin(); i != chans.end(); ++i)
53 const UserMembList* userlist = (*i)->chan->GetUsers();
54 for (UserMembList::const_iterator m = userlist->begin(); m != userlist->end(); ++m)
57 * Send the line if the channel member in question meets all of the following criteria:
59 * - not the user who is doing the action (i.e. whose channels we're iterating)
60 * - has the given extension
61 * - not on the except list built by modules
62 * - we haven't sent the line to the member yet
65 LocalUser* member = IS_LOCAL(m->first);
66 if ((member) && (member != user) && (ext.get(member)) && (exceptions.find(member) == exceptions.end()) && (already_sent.insert(member).second))
73 ModuleIRCv3() : cap_accountnotify(this, "account-notify"),
74 cap_awaynotify(this, "away-notify"),
75 cap_extendedjoin(this, "extended-join")
79 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
81 ConfigTag* conf = ServerInstance->Config->ConfValue("ircv3");
82 accountnotify = conf->getBool("accountnotify", true);
83 awaynotify = conf->getBool("awaynotify", true);
84 extendedjoin = conf->getBool("extendedjoin", true);
87 void OnEvent(Event& ev) CXX11_OVERRIDE
90 cap_awaynotify.HandleEvent(ev);
92 cap_extendedjoin.HandleEvent(ev);
96 cap_accountnotify.HandleEvent(ev);
98 if (ev.id == "account_login")
100 AccountEvent* ae = static_cast<AccountEvent*>(&ev);
102 // :nick!user@host ACCOUNT account
104 // :nick!user@host ACCOUNT *
105 std::string line = ":" + ae->user->GetFullHost() + " ACCOUNT ";
106 if (ae->account.empty())
109 line += std::string(ae->account);
111 WriteNeighboursWithExt(ae->user, line, cap_accountnotify.ext);
116 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
118 // Remember who is not going to see the JOIN because of other modules
119 if ((awaynotify) && (memb->user->IsAway()))
120 last_excepts = excepts;
126 * Send extended joins to clients who have the extended-join capability.
127 * An extended join looks like this:
129 * :nick!user@host JOIN #chan account :realname
131 * account is the joining user's account if he's logged in, otherwise it's an asterisk (*).
137 const UserMembList* userlist = memb->chan->GetUsers();
138 for (UserMembCIter it = userlist->begin(); it != userlist->end(); ++it)
140 // Send the extended join line if the current member is local, has the extended-join cap and isn't excepted
141 User* member = IS_LOCAL(it->first);
142 if ((member) && (cap_extendedjoin.ext.get(member)) && (excepts.find(member) == excepts.end()))
144 // Construct the lines we're going to send if we haven't constructed them already
147 bool has_account = false;
148 line = ":" + memb->user->GetFullHost() + " JOIN " + memb->chan->name + " ";
149 const AccountExtItem* accountext = GetAccountExtItem();
152 std::string* accountname;
153 accountname = accountext->get(memb->user);
156 line += *accountname;
164 line += " :" + memb->user->fullname;
166 // If the joining user received privileges from another module then we must send them as well,
167 // since silencing the normal join means the MODE will be silenced as well
168 if (!memb->modes.empty())
170 const std::string& modefrom = ServerInstance->Config->CycleHostsFromUser ? memb->user->GetFullHost() : ServerInstance->Config->ServerName;
171 mode = ":" + modefrom + " MODE " + memb->chan->name + " +" + memb->modes;
173 for (unsigned int i = 0; i < memb->modes.length(); i++)
174 mode += " " + memb->user->nick;
178 // Write the JOIN and the MODE, if any
180 if ((!mode.empty()) && (member != memb->user))
183 // Prevent the core from sending the JOIN and MODE to this user
184 excepts.insert(it->first);
189 ModResult OnSetAway(User* user, const std::string &awaymsg) CXX11_OVERRIDE
193 // Going away: n!u@h AWAY :reason
194 // Back from away: n!u@h AWAY
195 std::string line = ":" + user->GetFullHost() + " AWAY";
196 if (!awaymsg.empty())
197 line += " :" + awaymsg;
199 WriteNeighboursWithExt(user, line, cap_awaynotify.ext);
201 return MOD_RES_PASSTHRU;
204 void OnPostJoin(Membership *memb) CXX11_OVERRIDE
206 if ((!awaynotify) || (!memb->user->IsAway()))
209 std::string line = ":" + memb->user->GetFullHost() + " AWAY :" + memb->user->awaymsg;
211 const UserMembList* userlist = memb->chan->GetUsers();
212 for (UserMembCIter it = userlist->begin(); it != userlist->end(); ++it)
214 // Send the away notify line if the current member is local, has the away-notify cap and isn't excepted
215 User* member = IS_LOCAL(it->first);
216 if ((member) && (cap_awaynotify.ext.get(member)) && (last_excepts.find(member) == last_excepts.end()))
222 last_excepts.clear();
227 ServerInstance->Modules->SetPriority(this, I_OnUserJoin, PRIORITY_LAST);
230 Version GetVersion() CXX11_OVERRIDE
232 return Version("Provides support for extended-join, away-notify and account-notify CAP capabilities", VF_VENDOR);
236 MODULE_INIT(ModuleIRCv3)