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 WriteNeighboursWithExt : public User::ForEachNeighborHandler
25 const LocalIntExt& ext;
26 const std::string& msg;
28 void Execute(LocalUser* user) CXX11_OVERRIDE
35 WriteNeighboursWithExt(User* user, const std::string& message, const LocalIntExt& extension)
39 user->ForEachNeighbor(*this, false);
43 class ModuleIRCv3 : public Module, public AccountEventListener
45 GenericCap cap_accountnotify;
46 GenericCap cap_awaynotify;
47 GenericCap cap_extendedjoin;
56 : AccountEventListener(this)
57 , cap_accountnotify(this, "account-notify"),
58 cap_awaynotify(this, "away-notify"),
59 cap_extendedjoin(this, "extended-join")
63 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
65 ConfigTag* conf = ServerInstance->Config->ConfValue("ircv3");
66 accountnotify = conf->getBool("accountnotify", true);
67 awaynotify = conf->getBool("awaynotify", true);
68 extendedjoin = conf->getBool("extendedjoin", true);
71 void OnEvent(Event& ev) CXX11_OVERRIDE
74 cap_awaynotify.HandleEvent(ev);
76 cap_extendedjoin.HandleEvent(ev);
79 cap_accountnotify.HandleEvent(ev);
82 void OnAccountChange(User* user, const std::string& newaccount) CXX11_OVERRIDE
84 // :nick!user@host ACCOUNT account
86 // :nick!user@host ACCOUNT *
87 std::string line = ":" + user->GetFullHost() + " ACCOUNT ";
88 if (newaccount.empty())
93 WriteNeighboursWithExt(user, line, cap_accountnotify.ext);
96 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
98 // Remember who is not going to see the JOIN because of other modules
99 if ((awaynotify) && (memb->user->IsAway()))
100 last_excepts = excepts;
106 * Send extended joins to clients who have the extended-join capability.
107 * An extended join looks like this:
109 * :nick!user@host JOIN #chan account :realname
111 * account is the joining user's account if he's logged in, otherwise it's an asterisk (*).
117 const Channel::MemberMap& userlist = memb->chan->GetUsers();
118 for (Channel::MemberMap::const_iterator it = userlist.begin(); it != userlist.end(); ++it)
120 // Send the extended join line if the current member is local, has the extended-join cap and isn't excepted
121 User* member = IS_LOCAL(it->first);
122 if ((member) && (cap_extendedjoin.ext.get(member)) && (excepts.find(member) == excepts.end()))
124 // Construct the lines we're going to send if we haven't constructed them already
127 bool has_account = false;
128 line = ":" + memb->user->GetFullHost() + " JOIN " + memb->chan->name + " ";
129 const AccountExtItem* accountext = GetAccountExtItem();
132 std::string* accountname;
133 accountname = accountext->get(memb->user);
136 line += *accountname;
144 line += " :" + memb->user->fullname;
146 // If the joining user received privileges from another module then we must send them as well,
147 // since silencing the normal join means the MODE will be silenced as well
148 if (!memb->modes.empty())
150 const std::string& modefrom = ServerInstance->Config->CycleHostsFromUser ? memb->user->GetFullHost() : ServerInstance->Config->ServerName;
151 mode = ":" + modefrom + " MODE " + memb->chan->name + " +" + memb->modes;
153 for (unsigned int i = 0; i < memb->modes.length(); i++)
154 mode += " " + memb->user->nick;
158 // Write the JOIN and the MODE, if any
160 if ((!mode.empty()) && (member != memb->user))
163 // Prevent the core from sending the JOIN and MODE to this user
164 excepts.insert(it->first);
169 ModResult OnSetAway(User* user, const std::string &awaymsg) CXX11_OVERRIDE
173 // Going away: n!u@h AWAY :reason
174 // Back from away: n!u@h AWAY
175 std::string line = ":" + user->GetFullHost() + " AWAY";
176 if (!awaymsg.empty())
177 line += " :" + awaymsg;
179 WriteNeighboursWithExt(user, line, cap_awaynotify.ext);
181 return MOD_RES_PASSTHRU;
184 void OnPostJoin(Membership *memb) CXX11_OVERRIDE
186 if ((!awaynotify) || (!memb->user->IsAway()))
189 std::string line = ":" + memb->user->GetFullHost() + " AWAY :" + memb->user->awaymsg;
191 const Channel::MemberMap& userlist = memb->chan->GetUsers();
192 for (Channel::MemberMap::const_iterator it = userlist.begin(); it != userlist.end(); ++it)
194 // Send the away notify line if the current member is local, has the away-notify cap and isn't excepted
195 User* member = IS_LOCAL(it->first);
196 if ((member) && (cap_awaynotify.ext.get(member)) && (last_excepts.find(member) == last_excepts.end()))
202 last_excepts.clear();
207 ServerInstance->Modules->SetPriority(this, I_OnUserJoin, PRIORITY_LAST);
210 Version GetVersion() CXX11_OVERRIDE
212 return Version("Provides support for extended-join, away-notify and account-notify CAP capabilities", VF_VENDOR);
216 MODULE_INIT(ModuleIRCv3)