1 /*
2 * Copyright (C) 2004-2020 ZNC, see the NOTICE file for details.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <znc/Query.h>
18 #include <znc/User.h>
19 #include <znc/IRCNetwork.h>
20 #include <znc/Message.h>
21
22 using std::vector;
23
CQuery(const CString & sName,CIRCNetwork * pNetwork)24 CQuery::CQuery(const CString& sName, CIRCNetwork* pNetwork)
25 : m_sName(sName), m_pNetwork(pNetwork), m_Buffer() {
26 SetBufferCount(m_pNetwork->GetUser()->GetQueryBufferSize(), true);
27 }
28
~CQuery()29 CQuery::~CQuery() {}
30
SendBuffer(CClient * pClient)31 void CQuery::SendBuffer(CClient* pClient) { SendBuffer(pClient, m_Buffer); }
32
SendBuffer(CClient * pClient,const CBuffer & Buffer)33 void CQuery::SendBuffer(CClient* pClient, const CBuffer& Buffer) {
34 if (m_pNetwork && m_pNetwork->IsUserAttached()) {
35 // Based on CChan::SendBuffer()
36 if (!Buffer.IsEmpty()) {
37 const vector<CClient*>& vClients = m_pNetwork->GetClients();
38 for (CClient* pEachClient : vClients) {
39 CClient* pUseClient = (pClient ? pClient : pEachClient);
40
41 MCString msParams;
42 msParams["target"] = pUseClient->GetNick();
43
44 bool bWasPlaybackActive = pUseClient->IsPlaybackActive();
45 pUseClient->SetPlaybackActive(true);
46
47 NETWORKMODULECALL(OnPrivBufferStarting(*this, *pUseClient),
48 m_pNetwork->GetUser(), m_pNetwork, nullptr,
49 NOTHING);
50
51 bool bBatch = pUseClient->HasBatch();
52 CString sBatchName = m_sName.MD5();
53
54 if (bBatch) {
55 m_pNetwork->PutUser(":znc.in BATCH +" + sBatchName +
56 " znc.in/playback " + m_sName,
57 pUseClient);
58 }
59
60 size_t uSize = Buffer.Size();
61 for (size_t uIdx = 0; uIdx < uSize; uIdx++) {
62 const CBufLine& BufLine = Buffer.GetBufLine(uIdx);
63 CMessage Message = BufLine.ToMessage(*pUseClient, msParams);
64 if (!pUseClient->HasEchoMessage() &&
65 !pUseClient->HasSelfMessage()) {
66 if (Message.GetNick().NickEquals(
67 pUseClient->GetNick())) {
68 continue;
69 }
70 }
71 Message.SetNetwork(m_pNetwork);
72 Message.SetClient(pUseClient);
73 if (bBatch) {
74 Message.SetTag("batch", sBatchName);
75 }
76 bool bContinue = false;
77 NETWORKMODULECALL(OnPrivBufferPlayMessage(Message),
78 m_pNetwork->GetUser(), m_pNetwork,
79 nullptr, &bContinue);
80 if (bContinue) continue;
81 m_pNetwork->PutUser(Message, pUseClient);
82 }
83
84 if (bBatch) {
85 m_pNetwork->PutUser(":znc.in BATCH -" + sBatchName,
86 pUseClient);
87 }
88
89 NETWORKMODULECALL(OnPrivBufferEnding(*this, *pUseClient),
90 m_pNetwork->GetUser(), m_pNetwork, nullptr,
91 NOTHING);
92
93 pUseClient->SetPlaybackActive(bWasPlaybackActive);
94
95 if (pClient) break;
96 }
97 }
98 }
99 }
100