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