2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
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.
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
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/>.
21 #include "modules/cap.h"
22 #include "modules/ircv3_batch.h"
24 class BatchMessage : public ClientProtocol::Message
27 BatchMessage(const IRCv3::Batch::Batch& batch, bool start)
28 : ClientProtocol::Message("BATCH", ServerInstance->Config->ServerName)
30 char c = (start ? '+' : '-');
31 PushParam(std::string(1, c) + batch.GetRefTagStr());
32 if ((start) && (!batch.GetType().empty()))
33 PushParamRef(batch.GetType());
37 /** Extra structure allocated only for running batches, containing objects only relevant for
38 * that specific run of the batch.
40 struct IRCv3::Batch::BatchInfo
42 /** List of users that have received the batch start message
44 std::vector<LocalUser*> users;
45 BatchMessage startmsg;
46 ClientProtocol::Event startevent;
48 BatchInfo(ClientProtocol::EventProvider& protoevprov, IRCv3::Batch::Batch& b)
50 , startevent(protoevprov, startmsg)
55 class IRCv3::Batch::ManagerImpl : public Manager
57 typedef std::vector<Batch*> BatchList;
60 ClientProtocol::EventProvider protoevprov;
61 LocalIntExt batchbits;
62 BatchList active_batches;
65 bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
70 Batch& batch = *static_cast<Batch*>(tagdata.provdata);
71 // Check if this is the first message the user is getting that is part of the batch
72 const intptr_t bits = batchbits.get(user);
73 if (!(bits & batch.GetBit()))
75 // Send the start batch command ("BATCH +reftag TYPE"), remember the user so we can send them a
76 // "BATCH -reftag" message later when the batch ends and set the flag we just checked so this is
77 // only done once per user per batch.
78 batchbits.set(user, (bits | batch.GetBit()));
79 batch.batchinfo->users.push_back(user);
80 user->Send(batch.batchinfo->startevent);
86 unsigned int NextFreeId() const
88 if (active_batches.empty())
90 return active_batches.back()->GetId()+1;
94 ManagerImpl(Module* mod)
97 , protoevprov(mod, "BATCH")
98 , batchbits("batchbits", ExtensionItem::EXT_USER, mod)
105 // Set batchbits to 0 for all users in case we were reloaded and the previous, now meaningless,
106 // batchbits are set on users
107 const UserManager::LocalList& users = ServerInstance->Users.GetLocalUsers();
108 for (UserManager::LocalList::const_iterator i = users.begin(); i != users.end(); ++i)
110 LocalUser* const user = *i;
111 batchbits.set(user, 0);
118 while (!active_batches.empty())
119 ManagerImpl::End(*active_batches.back());
122 void RemoveFromAll(LocalUser* user)
124 const intptr_t bits = batchbits.get(user);
126 // User is quitting, remove them from all lists
127 for (BatchList::iterator i = active_batches.begin(); i != active_batches.end(); ++i)
130 // Check the bit first to avoid list scan in case they're not on the list
131 if ((bits & batch.GetBit()) != 0)
132 stdalgo::vector::swaperase(batch.batchinfo->users, user);
136 void Start(Batch& batch) CXX11_OVERRIDE
141 if (batch.IsRunning())
142 return; // Already started, don't start again
144 const size_t id = NextFreeId();
145 if (id >= MAX_BATCHES)
149 // Set the manager field which Batch::IsRunning() checks and is also used by AddToBatch()
150 // to set the message tag
151 batch.manager = this;
152 batch.batchinfo = new IRCv3::Batch::BatchInfo(protoevprov, batch);
153 batch.batchstartmsg = &batch.batchinfo->startmsg;
154 active_batches.push_back(&batch);
157 void End(Batch& batch) CXX11_OVERRIDE
159 if (!batch.IsRunning())
162 // Mark batch as stopped
163 batch.manager = NULL;
165 BatchInfo& batchinfo = *batch.batchinfo;
166 // Send end batch message to all users who got the batch start message and unset bit so it can be reused
167 BatchMessage endbatchmsg(batch, false);
168 ClientProtocol::Event endbatchevent(protoevprov, endbatchmsg);
169 for (std::vector<LocalUser*>::const_iterator i = batchinfo.users.begin(); i != batchinfo.users.end(); ++i)
171 LocalUser* const user = *i;
172 user->Send(endbatchevent);
173 batchbits.set(user, batchbits.get(user) & ~batch.GetBit());
176 // erase() not swaperase because the reftag generation logic depends on the order of the elements
177 stdalgo::erase(active_batches, &batch);
178 delete batch.batchinfo;
179 batch.batchinfo = NULL;
183 class ModuleIRCv3Batch : public Module
185 IRCv3::Batch::ManagerImpl manager;
193 void init() CXX11_OVERRIDE
198 void OnUnloadModule(Module* mod) CXX11_OVERRIDE
204 void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
206 // Remove the user from all internal lists
207 manager.RemoveFromAll(user);
210 Version GetVersion() CXX11_OVERRIDE
212 return Version("Provides the batch IRCv3 extension", VF_VENDOR);
216 MODULE_INIT(ModuleIRCv3Batch)