-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall.h
446 lines (384 loc) · 12.4 KB
/
call.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* GnuDialer - Complete, free predictive dialer
*
* Complete, free predictive dialer for contact centers.
*
* Copyright (C) 2006, GnuDialer Project
*
* Heath Schultz <[email protected]>
* Richard Lyman <[email protected]>
*
* This program is free software, distributed under the terms of
* the GNU General Public License.
*/
#include <queue>
#include <iostream>
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>
#include <vector>
#include <curl/curl.h>
#include <string>
#include <nlohmann/json.hpp>
#include "DBConnection.h"
#include "ParsedCall.h"
#include "exceptions.h"
#include "Socket.h"
#include "itos.h"
#include "agent.h"
#ifndef CALL
#define CALL
const bool doColor = true;
const std::string neon = "\033[1;32m"; // set foreground color to light green
const std::string norm = "\033[0m"; // reset to system
using json = nlohmann::json;
class Asterisk;
class Call;
std::string workingCampaign;
bool countCallsForCampaign(const Call &);
class Call
{
public:
Call(const std::string &number,
const std::string &campaign,
u_long leadid,
const std::string &callerid,
bool usecloser,
const std::string &dspmode,
const std::string &trunk,
const std::string &dialprefix,
const std::string &transfer,
const unsigned short int &timeout,
bool setCalled,
bool setAnswered,
u_long serverId)
{
itsNumber = number;
itsCampaign = campaign;
itsLeadId = leadid;
itsCallerId = callerid;
itsUseCloser = usecloser;
itsDSPMode = dspmode;
itsTrunk = trunk;
itsDialPrefix = dialprefix;
itsTransfer = transfer;
itsTimeout = timeout;
called = setCalled;
answered = setAnswered;
itsServerId = serverId;
timeval tv;
gettimeofday(&tv, NULL);
itsTime = tv.tv_sec % 1000000;
// ParsedCall parsedCall = this->saveCallToDB();
// this->SetId(parsedCall.id);
}
const std::string &GetId() const { return itsId; }
const std::string &GetNumber() const { return itsNumber; }
const std::string &GetCampaign() const { return itsCampaign; }
u_long GetLeadId() const { return itsLeadId; }
const std::string &GetCallerId() const { return itsCallerId; }
const bool &GetUseCloser() const { return itsUseCloser; }
const std::string &GetDSPMode() const { return itsDSPMode; }
const std::string &GetTrunk() const { return itsTrunk; }
const std::string &GetDialPrefix() const { return itsDialPrefix; }
const std::string &GetTransfer() const { return itsTransfer; }
const unsigned long int &GetTime() const { return itsTime; }
const unsigned short int &GetTimeout() const { return itsTimeout; }
const u_long GetServerId() const { return itsServerId; }
const bool &HasBeenCalled() const { return called; }
const bool &HasBeenAnswered() const { return answered; }
void SetId(std::string &newId) { itsId = newId; }
void SetAnswered(bool v = true) { answered = v; }
void SetCalled(bool v) { called = v; }
// TODO move to ARI interface
void DoCall()
{
std::string response;
called = true;
signal(SIGCLD, SIG_IGN);
int pid = fork();
if (pid == 0)
{
usleep(rand() % 2000000);
CURL *curl;
CURLcode res;
curl = curl_easy_init();
std::string mainHost = getMainHost();
std::string itsUseCloserStr = itsUseCloser ? "true" : "false";
std::string ariUser = getAriUser();
std::string ariProto = getAriProto();
std::string ariPass = getAriPass();
std::string stasisApp = "gnudialer_stasis_app";
// std::cout << "ARI creds: " << TheAsterisk.GetAriHost() << ":" + TheAsterisk.GetAriPort() + " - " + TheAsterisk.GetAriUser() + ":" + TheAsterisk.GetAriPass() << std::endl;
if (curl)
{
std::string url = ariProto + "://" + mainHost + ":8088/ari/channels?api_key=" + ariUser + ":" + ariPass + "&app=" + stasisApp;
std::string dialPrefix = (itsDialPrefix == "none") ? "" : itsDialPrefix;
std::string finalNumber = dialPrefix + itsNumber;
std::cout << "TRUNK: " + itsTrunk << std::endl;
size_t pos = itsTrunk.find('!');
while (pos != std::string::npos)
{
itsTrunk.replace(pos, 1, ":");
pos = itsTrunk.find('!', pos + 1);
}
pos = itsTrunk.find("_EXTEN_");
if (pos != std::string::npos)
{
// Replace _EXTEN_ with the actual number
itsTrunk.replace(pos, 7, finalNumber); // 7 is the length of "_EXTEN_"
}
else
{
throw std::runtime_error("Placeholder _EXTEN_ not found in the trunk string. Trunk example: SIP/faketrunk/[email protected]!5062");
}
this->SetCalled(true);
this->updateCallInDB();
/*
std::string postFields = "endpoint=" + itsTrunk +
"&extension=" + dialPrefix + itsNumber +
"&context=" + (itsTransfer == "TRANSFER" ? "gdtransfer" : "gdstasis") +
"&priority=1" +
"&callerId=" + itsCampaign + "-" + std::to_string(itsLeadId) + "-" + itsUseCloserStr +
"&timeout=" + itos(itsTimeout) +
"&variables[LEADID]=" + std::to_string(itsLeadId) +
"&variables[CAMPAIGN]=" + itsCampaign +
"&variables[DSPMODE]=" + itsDSPMode +
"&variables[ISTRANSFER]=" + itsTransfer;
*/
json variables = {
{"LEADID", std::to_string(itsLeadId)},
{"CAMPAIGN", itsCampaign},
{"DSPMODE", itsDSPMode},
{"METHOD", "auto"},
{"ISTRANSFER", itsTransfer}};
json jsonPayload = {
{"endpoint", itsTrunk},
{"extension", dialPrefix + itsNumber},
{"context", (itsTransfer == "TRANSFER" ? "gdtransfer" : "gdstasis")},
{"priority", 1},
{"callerId", "~" + itsCampaign + "-" + std::to_string(itsLeadId) + "~"},
{"timeout", itsTimeout},
{"variables", variables}};
std::string jsonPostFields = jsonPayload.dump();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonPostFields.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, jsonPostFields.length());
// Add JSON header
struct curl_slist *headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set up response handling
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](void *ptr, size_t size, size_t nmemb, std::string *data)
{
data->append((char *)ptr, size * nmemb);
return size * nmemb; });
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
std::string errorMessage = "curl_easy_perform() failed: " + std::string(curl_easy_strerror(res));
std::cerr << errorMessage << std::endl;
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
throw std::runtime_error(errorMessage);
}
else
{
std::cout << mainHost << ": " + itsCampaign + " - " + itsNumber + " - " + std::to_string(itsLeadId) + " - " + itsUseCloserStr << std::endl;
}
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
if (doColor)
{
std::cout << mainHost << neon << ": " + itsCampaign + " - " + itsNumber + " - " + std::to_string(itsLeadId) + " - " + itsUseCloserStr << norm << std::endl;
}
else
{
std::cout << mainHost << ": " + itsCampaign + " - " + itsNumber + " - " + std::to_string(itsLeadId) + " - " + itsUseCloserStr << std::endl;
}
usleep(10000000);
exit(0);
}
if (pid == -1)
{
throw xForkError();
}
}
~Call() {}
private:
std::string itsId, itsNumber, itsCampaign, itsCallerId, itsDSPMode, itsTrunk, itsDialPrefix, itsTransfer;
unsigned long int itsTime;
unsigned short int itsTimeout;
u_long itsServerId, itsLeadId;
bool called, answered, itsUseCloser;
ParsedCall saveCallToDB()
{
DBConnection dbConn;
ParsedCall parsedCall = this->convertToParsedCall(); // Convert Call to ParsedCall
return dbConn.insertCall(parsedCall);
}
void updateCallInDB()
{
DBConnection dbConn;
ParsedCall parsedCall = this->convertToParsedCall(); // Convert Call to ParsedCall
dbConn.updateCall(parsedCall);
}
ParsedCall convertToParsedCall()
{
ParsedCall parsedCall;
parsedCall.id = this->GetId();
parsedCall.phone = this->GetNumber();
parsedCall.campaign = this->GetCampaign();
parsedCall.leadid = this->GetLeadId();
parsedCall.callerid = this->GetCallerId();
parsedCall.usecloser = this->GetUseCloser();
parsedCall.dspmode = this->GetDSPMode();
parsedCall.trunk = this->GetTrunk();
parsedCall.dialprefix = this->GetDialPrefix();
parsedCall.transfer = this->GetTransfer();
parsedCall.timeout = this->GetTimeout();
parsedCall.server_id = this->GetServerId(); // assuming GetServerId() returns api_id
parsedCall.called = this->HasBeenCalled();
parsedCall.answered = this->HasBeenAnswered();
return parsedCall;
}
};
class CallCache
{
public:
CallCache()
{
loadCallsFromDB();
}
~CallCache() {}
void push_back(const Call &TheCall)
{
itsCalls.push_back(TheCall);
}
void AddCall(const std::string &phone,
const std::string &campaign,
u_long leadid,
const std::string &callerid,
bool usecloser,
const std::string &dspmode,
const std::string &trunk,
const std::string &dialprefix,
const std::string &transfer,
const unsigned short int &itsTimeout)
{
Call TheCall(phone, campaign, leadid, callerid, usecloser, dspmode, trunk, dialprefix, transfer, itsTimeout, false, false, std::stoul(getServerId()));
ParsedCall parsedCall = saveCallToDB(TheCall);
TheCall.SetId(parsedCall.id);
itsCalls.push_back(TheCall);
}
void SetAnswered(const std::string &campaign, u_long leadid)
{
for (unsigned int i = 0; i < itsCalls.size(); i++)
{
if (itsCalls.at(i).GetCampaign() == campaign && itsCalls.at(i).GetLeadId() == leadid)
{
itsCalls.at(i).SetAnswered();
}
}
}
unsigned int LinesDialing_OBSOLETE(const std::string &campaign)
{
workingCampaign = campaign;
timeval tv;
gettimeofday(&tv, NULL);
for (unsigned long int cur = tv.tv_sec % 1000000; itsCalls.size() && cur - itsCalls.front().GetTime() >
itsCalls.front().GetTimeout() / 1000;)
{
itsCalls.pop_front();
}
return std::count_if(itsCalls.begin(), itsCalls.end(), countCallsForCampaign);
}
unsigned int LinesDialing(const std::string &campaign)
{
DBConnection dbConn;
u_long serverId = std::stoul(getServerId());
return dbConn.getLinesDialing(campaign, serverId);
}
void CallAll()
{
std::cout << "CalAll SIZE: " << itsCalls.size() << std::endl;
for (unsigned int i = 0; i < itsCalls.size(); i++)
{
if (!itsCalls.at(i).HasBeenCalled())
{
try
{
itsCalls.at(i).SetCalled(true);
itsCalls.at(i).DoCall();
}
catch (const xConnectionError &e)
{
std::cerr << "Exception: Unable to connect to " << e.GetHost() << "! Disabling host." << std::endl;
}
}
}
}
private:
std::deque<Call> itsCalls;
ParsedCall convertToParsedCall(const Call &call)
{
ParsedCall parsedCall;
parsedCall.id = call.GetId();
parsedCall.phone = call.GetNumber();
parsedCall.campaign = call.GetCampaign();
parsedCall.leadid = call.GetLeadId();
parsedCall.callerid = call.GetCallerId();
parsedCall.usecloser = call.GetUseCloser();
parsedCall.dspmode = call.GetDSPMode();
parsedCall.trunk = call.GetTrunk();
parsedCall.dialprefix = call.GetDialPrefix();
parsedCall.transfer = call.GetTransfer();
parsedCall.timeout = call.GetTimeout();
parsedCall.server_id = call.GetServerId(); // assuming GetServerId() returns api_id
parsedCall.called = call.HasBeenCalled();
parsedCall.answered = call.HasBeenAnswered();
return parsedCall;
}
void loadCallsFromDB()
{
u_long serverId = std::stoul(getServerId());
DBConnection dbConn;
itsCalls.clear();
auto calls = dbConn.fetchAllCalls(serverId);
for (const auto &dbCall : calls)
{
Call call(dbCall.phone, dbCall.campaign, dbCall.leadid, dbCall.callerid,
dbCall.usecloser, dbCall.dspmode, dbCall.trunk, dbCall.dialprefix,
dbCall.transfer, dbCall.timeout, dbCall.called, dbCall.answered, dbCall.server_id);
itsCalls.push_back(call);
}
}
ParsedCall saveCallToDB(const Call &call)
{
DBConnection dbConn;
ParsedCall parsedCall = convertToParsedCall(call); // Convert Call to ParsedCall
return dbConn.insertCall(parsedCall);
}
void updateCallInDB(const Call &call)
{
DBConnection dbConn;
ParsedCall parsedCall = convertToParsedCall(call); // Convert Call to ParsedCall
dbConn.updateCall(parsedCall);
}
};
bool countCallsForCampaign(const Call &TheCall)
{
if (TheCall.GetCampaign() == workingCampaign && TheCall.HasBeenAnswered() == false)
{
return true;
}
else
{
return false;
}
}
#endif