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