forked from olivecoder/cppagi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcppagi.cpp
417 lines (363 loc) · 8.41 KB
/
cppagi.cpp
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
/*
cppagi.cpp
Robert Oliveira, 2005.02.03
*/
#include <string>
#include <cctype>
#include <algorithm>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include "cppagi.h"
agi::agi()
{
char line[256];
int max=200;
AGI_LOG("agi constructor", AGI_INFO);
setlinebuf(stdout);
setlinebuf(stderr);
while ( cin && max-- ) {
cin.getline(line,sizeof(line)-1);
if ( strlen(line)==0 ) break;
environment.push_back(line);
}
hangup_detected=false;
clear();
}
agi::~agi()
{
AGI_LOG("agi destructor", AGI_INFO);
}
char char_toupper(char c)
{
return std::toupper(c);
}
string agi::upper(const string &s)
{
string result=s;
transform(result.begin(), result.end(), result.begin(), char_toupper);
return result;
}
void agi::log(const string& message, int verbosity, const string &file,
const string &function, int line )
{
string where, msg;
#ifdef DEBUG
char strline[16];
snprintf(strline,sizeof(strline),"%d",line);
where = file + ", ";
where += function + "(";
where += strline;
where += ")\t";
msg = where + message;
#else
msg = message;
#endif
verbose(agi::ltime() + " " + msg, verbosity);
}
agi_error agi::error( const string& msg, const string& file,
const string& function, int line)
{
agi_error newError(*this, msg, file, function, line);
return newError;
}
string agi::getEnv(const string &varname)
{
vector<string>::iterator i;
string uppervarname;
size_t p;
uppervarname = upper(varname);
for( i=environment.begin(); i!=environment.end(); i++ ) {
p = i->find(':',0);
if ( p!=string::npos && p!=0 ) {
if ( upper(i->substr(0,p))==uppervarname ) {
p++; p++;
if ( p < i->size() ) {
return i->substr( p, i->size() - p );
}
}
}
}
AGI_LOG("variable \""+varname+"\" not found!",AGI_WARNING);
return "";
}
string agi::ltime(time_t t)
{
tm *ptm;
char strtime[64];
if (t==0) t=time(NULL);
ptm=localtime(&t);
snprintf(strtime,sizeof(strtime),"%02d.%02d.%02d %02d:%02d:%02d",
ptm->tm_year-100,ptm->tm_mon,ptm->tm_mday,
ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
return string(strtime);
}
void agi::clear(void) {
code=AGI_CODEOK;
result=0;
dtmf_buffer="";
}
int agi::exec(const string &command)
{
execCommand = command;
cout << command << endl;
return read();
}
int agi::read(void) {
char line[256];
char *next;
code = result = 0;
cin.getline(line,sizeof(line)-1);
response = line;
code = strtol(line,&next,10);
if ( next!=NULL ) {
while (*next && *next!='=') next++;
if ( *next=='=' ) next++;
result = strtol(next,&next,10);
}
// throw only on first hangup indication
if ( result == -1 && ! hangup_detected ) {
hangup_detected = true;
throw agi_hangup("Hangup on "+execCommand);
}
return result;
}
int agi::getCode(void)
{
return code;
}
int agi::getResult(void)
{
return result;
}
bool agi::isConnected(void)
{
getChannelStatus();
return (! hangup_detected);
}
string agi::getData(void) // todo: split data
{
size_t begin, end;
begin = response.find('(');
end = response.rfind(')');
if ( begin!=string::npos && end!=string::npos ) {
return response.substr(begin+1,end-begin-1);
} else {
return "";
}
}
bool agi::fail(void) {
return ( (code!=AGI_CODEOK) || (result==-1) );
}
void agi::verbose(const string &message, int verbosity)
{
request.str("");
request << "VERBOSE \"" << message << "\" " << verbosity;
exec(request.str());
}
void agi::answer(void)
{
exec("ANSWER");
}
void agi::play(const string &file, const string valid_digits )
{
int dtmf;
request.str("");
request << "STREAM FILE \"" << file << "\" ";
if ( valid_digits.size()==0 ) {
request << " \"\" ";
} else {
request << " \"" << valid_digits << "\" ";
}
exec(request.str());
dtmf = getResult();
if ( valid_digits.find((char)dtmf,0) != string::npos ) {
dtmf_buffer += (char)dtmf;
}
}
void agi::record(const string &file, const string &format, int timeout,
int silence)
{
request.str("");
request << "RECORD FILE " << file << " " << format << " 1# ";
request << timeout << " beep";
if ( silence ) {
request << " s=" << silence;
}
exec(request.str());
}
string agi::getDtmf(int length, int timeout, char terminator)
{
string digits = "";
char ch=-1;
while ( length!=0 && ch!=0 && ch!=terminator && !fail() ) {
AGI_LOG("WAIT FOR DIGIT (received: \""+digits+"\")",AGI_INFO);
if ( dtmf_buffer.size() > 0 ) {
ch = dtmf_buffer[0];
dtmf_buffer.erase(0,1);
} else {
request.str("");
request << "WAIT FOR DIGIT " << timeout;
exec(request.str());
ch = (char) result; // 0=timeout -1=fail
}
if ( ch!=0 && ch!=terminator && !fail() ) {
digits += ch;
length--;
}
}
return digits;
}
string agi::input(const string &prompt, int length, int timeout,
char terminator)
{
play(prompt);
return getDtmf(length, timeout, terminator);
}
char agi::menu( const string &prompt, const string &options,
const string &error, const string &retry_message, int retry)
{
char dtmf='\0';
bool success=false;
if ( dtmf_buffer.size() > 0 ) {
dtmf = getDtmf(1)[0];
success = ( options.find(dtmf,0) != string::npos );
}
if ( ! retry > 0 ) {
AGI_LOG("menu assertion (retry > 0) failed", AGI_WARNING);
}
while ( ! success && retry-- && ! fail() ) {
play(prompt);
dtmf = getDtmf(1)[0];
success = ( options.find(dtmf,0) != string::npos );
if ( ! success ) {
if ( error.size()!=0 ) {
play(error);
}
if ( retry && retry_message.size()!=0 ) {
play(retry_message);
}
}
}
if ( success ) {
return dtmf;
} else {
return 0;
}
}
void agi::jump(const string &context, const string &extension, int priority)
{
exec("SET CONTEXT " + context);
if ( ! fail() ) {
exec("SET EXTENSION " + extension);
if ( ! fail() ) {
request.str("");
request << "SET PRIORITY " << priority;
exec(request.str());
}
}
}
void agi::sayDigits(const string &digits)
{
exec("SAY DIGITS " + digits + " #");
}
void agi::sayNumber(const string &number)
{
exec("SAY NUMBER " + number + " #");
}
void agi::saySetsOfTen(const string &digits)
{
string number;
for (size_t i=0; i<digits.size(); i++, i++) {
number = digits.substr(i,2);
if ( number[0]=='0' && number.size()==2 ) {
sayDigits(number.substr(0,1));
sayDigits(number.substr(1,1));
} else if ( number[0]=='1' && number.size()==2 ) {
sayNumber(number);
} else if ( number[1]=='0' && number.size()==2 ) {
sayNumber(number);
} else if ( number.size()==2 ) {
sayNumber(number.substr(0,1)+"0");
play("digits/and");
sayDigits(number.substr(1,1));
} else {
sayNumber(number);
}
}
}
void agi::sayTime(string timestr)
{
timestr = upper(timestr);
if ( timestr=="NOW" ) {
time_t now;
now = time(NULL);
timestr=ctime(&now);
}
exec("SAY TIME " + timestr );
}
bool agi::hangup(int cause)
{
char cause_str[16];
snprintf(cause_str,sizeof(cause_str),"%i",cause);
setVar("PRI_CAUSE",cause_str);
return (exec("HANGUP")==1);
}
int agi::getChannelStatus(const string &channel)
{
string qryChannel;
if ( channel.size()==0 ) {
qryChannel = getEnv("agi_channel");
} else {
qryChannel = channel;
}
exec("CHANNEL STATUS "+qryChannel);
return result;
}
void agi::setCallerId(const string &cid)
{
exec("SET CALLERID "+cid);
}
void agi::setLanguage(const string &language)
{
exec("EXEC SetLanguage "+language);
}
string agi::getLanguage(void)
{
return getEnv("agi_language");
}
string agi::dbGet(const string family, const string key)
{
exec("DATABASE GET \"" + family + "\" \"" +key+ "\"");
return getData();
}
void agi::dbPut(const string family, const string key, const string value)
{
exec("DATABASE PUT \"" + family + "\" \"" +key+ "\" \""+value+"\"" );
}
void agi::dbDel(const string family, const string key)
{
exec("DATABASE DEL \"" + family + "\" \"" +key+ "\"");
}
string agi::getVar(const string &var)
{
exec("GET VARIABLE \""+var+"\"");
return getData();
}
void agi::setVar(const string &var, const string &value)
{
exec("SET VARIABLE \""+var+"\" \""+value+"\"");
}
string agi::sprintf(const std::string &fmt, ...)
{
std::string result;
char buffer[8192];
va_list ap;
va_start(ap, fmt);
memset(buffer,0,sizeof(buffer));
vsnprintf(buffer,sizeof(buffer),fmt.c_str(),ap);
va_end(ap);
result = buffer;
return result;
}