]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ircv3.cpp
Merge pull request #1270 from SaberUK/master+sasl
[user/henk/code/inspircd.git] / src / modules / m_ircv3.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2012 Attila Molnar <attilamolnar@hush.com>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19 #include "inspircd.h"
20 #include "modules/account.h"
21 #include "modules/cap.h"
22 #include "modules/ircv3.h"
23
24 class ModuleIRCv3 : public Module, public AccountEventListener
25 {
26         Cap::Capability cap_accountnotify;
27         Cap::Capability cap_awaynotify;
28         Cap::Capability cap_extendedjoin;
29
30         CUList last_excepts;
31
32  public:
33         ModuleIRCv3()
34                 : AccountEventListener(this)
35                 , cap_accountnotify(this, "account-notify"),
36                                         cap_awaynotify(this, "away-notify"),
37                                         cap_extendedjoin(this, "extended-join")
38         {
39         }
40
41         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
42         {
43                 ConfigTag* conf = ServerInstance->Config->ConfValue("ircv3");
44                 cap_accountnotify.SetActive(conf->getBool("accountnotify", true));
45                 cap_awaynotify.SetActive(conf->getBool("awaynotify", true));
46                 cap_extendedjoin.SetActive(conf->getBool("extendedjoin", true));
47         }
48
49         void OnAccountChange(User* user, const std::string& newaccount) CXX11_OVERRIDE
50         {
51                 // :nick!user@host ACCOUNT account
52                 // or
53                 // :nick!user@host ACCOUNT *
54                 std::string line = ":" + user->GetFullHost() + " ACCOUNT ";
55                 if (newaccount.empty())
56                         line += "*";
57                 else
58                         line += newaccount;
59
60                 IRCv3::WriteNeighborsWithCap(user, line, cap_accountnotify);
61         }
62
63         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
64         {
65                 // Remember who is not going to see the JOIN because of other modules
66                 if ((cap_awaynotify.IsActive()) && (memb->user->IsAway()))
67                         last_excepts = excepts;
68
69                 if (!cap_extendedjoin.IsActive())
70                         return;
71
72                 /*
73                  * Send extended joins to clients who have the extended-join capability.
74                  * An extended join looks like this:
75                  *
76                  * :nick!user@host JOIN #chan account :realname
77                  *
78                  * account is the joining user's account if he's logged in, otherwise it's an asterisk (*).
79                  */
80
81                 std::string line;
82                 std::string mode;
83
84                 const Channel::MemberMap& userlist = memb->chan->GetUsers();
85                 for (Channel::MemberMap::const_iterator it = userlist.begin(); it != userlist.end(); ++it)
86                 {
87                         // Send the extended join line if the current member is local, has the extended-join cap and isn't excepted
88                         User* member = IS_LOCAL(it->first);
89                         if ((member) && (cap_extendedjoin.get(member)) && (excepts.find(member) == excepts.end()))
90                         {
91                                 // Construct the lines we're going to send if we haven't constructed them already
92                                 if (line.empty())
93                                 {
94                                         bool has_account = false;
95                                         line = ":" + memb->user->GetFullHost() + " JOIN " + memb->chan->name + " ";
96                                         const AccountExtItem* accountext = GetAccountExtItem();
97                                         if (accountext)
98                                         {
99                                                 std::string* accountname;
100                                                 accountname = accountext->get(memb->user);
101                                                 if (accountname)
102                                                 {
103                                                         line += *accountname;
104                                                         has_account = true;
105                                                 }
106                                         }
107
108                                         if (!has_account)
109                                                 line += "*";
110
111                                         line += " :" + memb->user->fullname;
112
113                                         // If the joining user received privileges from another module then we must send them as well,
114                                         // since silencing the normal join means the MODE will be silenced as well
115                                         if (!memb->modes.empty())
116                                         {
117                                                 const std::string& modefrom = ServerInstance->Config->CycleHostsFromUser ? memb->user->GetFullHost() : ServerInstance->Config->ServerName;
118                                                 mode = ":" + modefrom + " MODE " + memb->chan->name + " +" + memb->modes;
119
120                                                 for (unsigned int i = 0; i < memb->modes.length(); i++)
121                                                         mode += " " + memb->user->nick;
122                                         }
123                                 }
124
125                                 // Write the JOIN and the MODE, if any
126                                 member->Write(line);
127                                 if ((!mode.empty()) && (member != memb->user))
128                                         member->Write(mode);
129
130                                 // Prevent the core from sending the JOIN and MODE to this user
131                                 excepts.insert(it->first);
132                         }
133                 }
134         }
135
136         ModResult OnSetAway(User* user, const std::string &awaymsg) CXX11_OVERRIDE
137         {
138                 if (cap_awaynotify.IsActive())
139                 {
140                         // Going away: n!u@h AWAY :reason
141                         // Back from away: n!u@h AWAY
142                         std::string line = ":" + user->GetFullHost() + " AWAY";
143                         if (!awaymsg.empty())
144                                 line += " :" + awaymsg;
145
146                         IRCv3::WriteNeighborsWithCap(user, line, cap_awaynotify);
147                 }
148                 return MOD_RES_PASSTHRU;
149         }
150
151         void OnPostJoin(Membership *memb) CXX11_OVERRIDE
152         {
153                 if ((!cap_awaynotify.IsActive()) || (!memb->user->IsAway()))
154                         return;
155
156                 std::string line = ":" + memb->user->GetFullHost() + " AWAY :" + memb->user->awaymsg;
157
158                 const Channel::MemberMap& userlist = memb->chan->GetUsers();
159                 for (Channel::MemberMap::const_iterator it = userlist.begin(); it != userlist.end(); ++it)
160                 {
161                         // Send the away notify line if the current member is local, has the away-notify cap and isn't excepted
162                         User* member = IS_LOCAL(it->first);
163                         if ((member) && (cap_awaynotify.get(member)) && (last_excepts.find(member) == last_excepts.end()) && (it->second != memb))
164                         {
165                                 member->Write(line);
166                         }
167                 }
168
169                 last_excepts.clear();
170         }
171
172         void Prioritize() CXX11_OVERRIDE
173         {
174                 ServerInstance->Modules->SetPriority(this, I_OnUserJoin, PRIORITY_LAST);
175         }
176
177         Version GetVersion() CXX11_OVERRIDE
178         {
179                 return Version("Provides support for extended-join, away-notify and account-notify CAP capabilities", VF_VENDOR);
180         }
181 };
182
183 MODULE_INIT(ModuleIRCv3)