1 /*
2 * InspIRCd -- Internet Relay Chat Daemon
3 *
4 * Copyright (C) 2018-2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2018 Attila Molnar <attilamolnar@hush.com>
6 *
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #include "inspircd.h"
22
Serializer(Module * mod,const char * Name)23 ClientProtocol::Serializer::Serializer(Module* mod, const char* Name)
24 : DataProvider(mod, std::string("serializer/") + Name)
25 , evprov(mod)
26 {
27 }
28
HandleTag(LocalUser * user,const std::string & tagname,std::string & tagvalue,TagMap & tags) const29 bool ClientProtocol::Serializer::HandleTag(LocalUser* user, const std::string& tagname, std::string& tagvalue, TagMap& tags) const
30 {
31 // Catch and block empty tags
32 if (tagname.empty())
33 return false;
34
35 const ::Events::ModuleEventProvider::SubscriberList& list = evprov.GetSubscribers();
36 for (::Events::ModuleEventProvider::SubscriberList::const_iterator i = list.begin(); i != list.end(); ++i)
37 {
38 MessageTagProvider* const tagprov = static_cast<MessageTagProvider*>(*i);
39 const ModResult res = tagprov->OnProcessTag(user, tagname, tagvalue);
40 if (res == MOD_RES_ALLOW)
41 return tags.insert(std::make_pair(tagname, MessageTagData(tagprov, tagvalue))).second;
42 else if (res == MOD_RES_DENY)
43 break;
44 }
45
46 // No module handles the tag but that's not an error
47 return true;
48 }
49
MakeTagWhitelist(LocalUser * user,const TagMap & tagmap) const50 ClientProtocol::TagSelection ClientProtocol::Serializer::MakeTagWhitelist(LocalUser* user, const TagMap& tagmap) const
51 {
52 TagSelection tagwl;
53 for (TagMap::const_iterator i = tagmap.begin(); i != tagmap.end(); ++i)
54 {
55 const MessageTagData& tagdata = i->second;
56 if (tagdata.tagprov->ShouldSendTag(user, tagdata))
57 tagwl.Select(tagmap, i);
58 }
59 return tagwl;
60 }
61
SerializeForUser(LocalUser * user,Message & msg)62 const ClientProtocol::SerializedMessage& ClientProtocol::Serializer::SerializeForUser(LocalUser* user, Message& msg)
63 {
64 if (!msg.msginit_done)
65 {
66 msg.msginit_done = true;
67 FOREACH_MOD_CUSTOM(evprov, MessageTagProvider, OnPopulateTags, (msg));
68 }
69 return msg.GetSerialized(Message::SerializedInfo(this, MakeTagWhitelist(user, msg.GetTags())));
70 }
71
GetSerialized(const SerializedInfo & serializeinfo) const72 const ClientProtocol::SerializedMessage& ClientProtocol::Message::GetSerialized(const SerializedInfo& serializeinfo) const
73 {
74 // First check if the serialized line they're asking for is in the cache
75 for (SerializedList::const_iterator i = serlist.begin(); i != serlist.end(); ++i)
76 {
77 const SerializedInfo& curr = i->first;
78 if (curr == serializeinfo)
79 return i->second;
80 }
81
82 // Not cached, generate it and put it in the cache for later use
83 serlist.push_back(std::make_pair(serializeinfo, serializeinfo.serializer->Serialize(*this, serializeinfo.tagwl)));
84 return serlist.back().second;
85 }
86
GetMessagesForUser(LocalUser * user,MessageList & messagelist)87 void ClientProtocol::Event::GetMessagesForUser(LocalUser* user, MessageList& messagelist)
88 {
89 if (!eventinit_done)
90 {
91 eventinit_done = true;
92 FOREACH_MOD_CUSTOM(*event, EventHook, OnEventInit, (*this));
93 }
94
95 // Most of the time there's only a single message but in rare cases there are more
96 if (initialmsg)
97 messagelist.assign(1, initialmsg);
98 else
99 messagelist = *initialmsglist;
100
101 // Let modules modify the message list
102 ModResult res;
103 FIRST_MOD_RESULT_CUSTOM(*event, EventHook, OnPreEventSend, res, (user, *this, messagelist));
104 if (res == MOD_RES_DENY)
105 messagelist.clear();
106 }
107