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 ClientProtocol::Event endevent;
50 BatchInfo(ClientProtocol::EventProvider& protoevprov, IRCv3::Batch::Batch& b)
52 , startevent(protoevprov, startmsg)
54 , endevent(protoevprov, endmsg)
59 class IRCv3::Batch::ManagerImpl : public Manager
61 typedef std::vector<Batch*> BatchList;
64 ClientProtocol::EventProvider protoevprov;
65 LocalIntExt batchbits;
66 BatchList active_batches;
69 bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
74 Batch& batch = *static_cast<Batch*>(tagdata.provdata);
75 // Check if this is the first message the user is getting that is part of the batch
76 const intptr_t bits = batchbits.get(user);
77 if (!(bits & batch.GetBit()))
79 // Send the start batch command ("BATCH +reftag TYPE"), remember the user so we can send them a
80 // "BATCH -reftag" message later when the batch ends and set the flag we just checked so this is
81 // only done once per user per batch.
82 batchbits.set(user, (bits | batch.GetBit()));
83 batch.batchinfo->users.push_back(user);
84 user->Send(batch.batchinfo->startevent);
90 unsigned int NextFreeId() const
92 if (active_batches.empty())
94 return active_batches.back()->GetId()+1;
98 ManagerImpl(Module* mod)
101 , protoevprov(mod, "BATCH")
102 , batchbits("batchbits", ExtensionItem::EXT_USER, mod)
109 // Set batchbits to 0 for all users in case we were reloaded and the previous, now meaningless,
110 // batchbits are set on users
111 const UserManager::LocalList& users = ServerInstance->Users.GetLocalUsers();
112 for (UserManager::LocalList::const_iterator i = users.begin(); i != users.end(); ++i)
114 LocalUser* const user = *i;
115 batchbits.set(user, 0);
122 while (!active_batches.empty())
123 ManagerImpl::End(*active_batches.back());
126 void RemoveFromAll(LocalUser* user)
128 const intptr_t bits = batchbits.get(user);
130 // User is quitting, remove them from all lists
131 for (BatchList::iterator i = active_batches.begin(); i != active_batches.end(); ++i)
134 // Check the bit first to avoid list scan in case they're not on the list
135 if ((bits & batch.GetBit()) != 0)
136 stdalgo::vector::swaperase(batch.batchinfo->users, user);
140 void Start(Batch& batch) CXX11_OVERRIDE
145 if (batch.IsRunning())
146 return; // Already started, don't start again
148 const size_t id = NextFreeId();
149 if (id >= MAX_BATCHES)
153 // Set the manager field which Batch::IsRunning() checks and is also used by AddToBatch()
154 // to set the message tag
155 batch.manager = this;
156 batch.batchinfo = new IRCv3::Batch::BatchInfo(protoevprov, batch);
157 batch.batchstartmsg = &batch.batchinfo->startmsg;
158 batch.batchendmsg = &batch.batchinfo->endmsg;
159 active_batches.push_back(&batch);
162 void End(Batch& batch) CXX11_OVERRIDE
164 if (!batch.IsRunning())
167 // Mark batch as stopped
168 batch.manager = NULL;
170 BatchInfo& batchinfo = *batch.batchinfo;
171 // Send end batch message to all users who got the batch start message and unset bit so it can be reused
172 for (std::vector<LocalUser*>::const_iterator i = batchinfo.users.begin(); i != batchinfo.users.end(); ++i)
174 LocalUser* const user = *i;
175 user->Send(batchinfo.endevent);
176 batchbits.set(user, batchbits.get(user) & ~batch.GetBit());
179 // erase() not swaperase because the reftag generation logic depends on the order of the elements
180 stdalgo::erase(active_batches, &batch);
181 delete batch.batchinfo;
182 batch.batchinfo = NULL;
186 class ModuleIRCv3Batch : public Module
188 IRCv3::Batch::ManagerImpl manager;
196 void init() CXX11_OVERRIDE
201 void OnUnloadModule(Module* mod) CXX11_OVERRIDE
207 void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
209 // Remove the user from all internal lists
210 manager.RemoveFromAll(user);
213 Version GetVersion() CXX11_OVERRIDE
215 return Version("Provides the batch IRCv3 extension", VF_VENDOR);
219 MODULE_INIT(ModuleIRCv3Batch)