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