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