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