-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.c
More file actions
624 lines (567 loc) · 22.3 KB
/
action.c
File metadata and controls
624 lines (567 loc) · 22.3 KB
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*******************************************************************************
* astapi - library for using Asterisk Manager API.
* Copyright (C) 2010 Baligh GUESMI
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc.,
******************************************************************************/
/*******************************************************************************
* @file action.c
* @brief
* @author Baligh.GUESMI
* @date 20100524
******************************************************************************/
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include "astman.h"
#include "astevent.h"
#include "astlog.h"
#include "action.h"
/*******************************************************************************
*
******************************************************************************/
//*** MSGBUF macros ***
struct msgbuf {
struct message *msg;
int count;
int len;
};
/*******************************************************************************
*
******************************************************************************/
#define MSGBUF_INIT(L) \
struct msgbuf _buf; \
_buf.msg = (struct message *)calloc(L, sizeof(struct message)); \
_buf.count = 0; \
_buf.len = L; \
if (!_buf.msg) return ASTMAN_FAILURE;
/*******************************************************************************
*
******************************************************************************/
#define MSGBUF_ADD(M) \
memcpy(_buf.msg+_buf.count, M, sizeof(struct message)); \
_buf.count++; \
if (_buf.count >= _buf.len) { \
_buf.msg = (struct message *)realloc(_buf.msg, _buf.len * 2 * sizeof(struct message)); \
memset(_buf.msg+_buf.len, 0, _buf.len * sizeof(struct message)); \
_buf.len *= 2; \
}
/*******************************************************************************
*
******************************************************************************/
#define MSGBUF_MSG _buf.msg;
/*******************************************************************************
*
******************************************************************************/
#define MSGBUF_NB _buf.count;
/*******************************************************************************
* @fn astman_originate(struct mansession *s, struct message *m,
* char *channel,
* char *exten, char *context, int priority,
* char *application, char *data,
* int timeout,
* char *callerid,
* char *variable,
* char *account,
* int async,
* char *actionid)
* @brief ASTERISK AMI ACTION Originate
* @param Channel: Channel name to call(required)
* @param Exten: Extension to use (requires 'Context' and 'Priority')
* @param Context: Context to use (requires 'Exten' and 'Priority')
* @param Priority: Priority to use (requires 'Exten' and 'Context')
* @param Application: Application to use
* @param Data: Data to use (requires 'Application')
* @param Timeout: How long to wait for call to be answered (in ms)
* @param CallerID: Caller ID to be set on the outgoing channel
* @param Variable: Channel variable to set, multiple Variable: headers are allowed
* @param Account: Account code
* @param Async: Set to 'true' for fast origination
*
* Generates an outgoing call to a Extension/Context/Priority or Application/Data
******************************************************************************/
int astman_originate(struct mansession *s, struct message *m,
char *channel,
char *exten, char *context, int priority,
char *application, char *data,
int timeout,
char *callerid,
char *variable,
char *account,
int async,
char *actionid) {
int res;
char params[MAX_LEN*12] = "";
char strbuf[12];
if (astman_strlen_zero(channel))
return ASTMAN_FAILURE;
if (!astman_strlen_zero(exten) && !astman_strlen_zero(context) &&
priority > 0 ) {
astman_add_param(params, sizeof(params), "Exten", exten);
astman_add_param(params, sizeof(params), "Context", context);
snprintf(strbuf, sizeof(strbuf), "%d", priority);
astman_add_param(params, sizeof(params), "Priority", strbuf);
} else if (astman_add_param(params, sizeof(params), "Application", application) > 0) {
astman_add_param(params, sizeof(params), "Data", data);
} else {
return ASTMAN_FAILURE;
}
if (timeout > 0) {
snprintf(strbuf, sizeof(strbuf), "%d", timeout);
astman_add_param(params, sizeof(params), "Timeout", strbuf);
}
astman_add_param(params, sizeof(params), "Channel", channel);
astman_add_param(params, sizeof(params), "CallerId", callerid);
astman_add_param(params, sizeof(params), "Variable", variable);
astman_add_param(params, sizeof(params), "Account", account);
astman_add_param(params, sizeof(params), "Async", (async)?"true":"false");
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action_params(s, "Originate", params);
res = astman_wait_for_response(s, m, 0);
if ( res > 0 && response_is(m, "Success")) {
return res;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @fn astman_ping(struct mansession *s, struct message *m, char *actionid)
* @brief Ping
* A 'Ping' action will ellicit a 'Pong' response. Used to keep
* the manager connection open.
* @param m:
* @param actionid:
* @param s:
******************************************************************************/
int astman_ping(struct mansession *s, struct message *m, char *actionid) {
int res;
char params[MAX_LEN] = "";
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action_params(s, "Ping", params);
res = astman_wait_for_response(s, m, 0);
if ( res > 0 && response_is(m, "Pong")) {
return res;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief Command
* Execute CLI Command
******************************************************************************/
int astman_command(struct mansession *s, struct message *m,
char *command,
char *actionid) {
int res;
char params[MAX_LEN] = "";
if (astman_strlen_zero(command))
return ASTMAN_FAILURE;
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action(s, "Command", "Command: %s\r\n%s", command, params);
res = astman_wait_for_response(s, m, 0);
if ( res > 0 && response_is(m, "Follows")) {
return res;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @fn
* @brief QueueStatus
* Queue, queue member and queued calls status
******************************************************************************/
int astman_queuestatus(struct mansession *s, struct message **m,
char *actionid) {
int res;
char params[MAX_LEN] = "";
char event[80];
struct message msg;
MSGBUF_INIT(1);
astman_add_event_handler_system(s, astman_queues_callback);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action_params(s, "QueueStatus", params);
res = astman_wait_for_response(s, &msg, 0);
if ( res > 0 && response_is(&msg, "Success")) {
for(;;) {
res = astman_wait_for_response(s, &msg, 0);
if ( res > 0 ) {
strncpy(event, astman_get_header(&msg, "Event"), sizeof(event));
if (!strcasecmp(event, "QueueParams") ||
!strcasecmp(event, "QueueMember") ||
!strcasecmp(event, "QueueEnter")) {
MSGBUF_ADD(&msg);
} else if (!strcasecmp(event, "QueueStatusComplete")) {
*m = MSGBUF_MSG;
astman_add_event_handler_system(s, NULL);
return ASTMAN_SUCCESS;
}
} else {
goto out;
}
} /* infinity loop */
}
out:
astman_add_event_handler_system(s, NULL);
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @fn astman_absolutetimeout(struct mansession *s, struct message *m,
* char *channel,
* int timeout,
* char *actionid)
* @brief AbsoluteTimeout
* Hangup a channel after a certain time.
* @param IN Channel: Channel name to hangup
* @param IN Timeout: Maximum duration of the call (sec)
******************************************************************************/
int astman_absolutetimeout(struct mansession *s, struct message *m,
char *channel,
int timeout,
char *actionid) {
int res;
char params[MAX_LEN] = "";
char strtimeout[12] = "";
if (astman_strlen_zero(channel))
return ASTMAN_FAILURE;
if (timeout>0) {
snprintf(strtimeout, sizeof(strtimeout), "%d", timeout);
} else {
return ASTMAN_FAILURE;
}
astman_add_param(params, sizeof(params), "Channel", channel);
astman_add_param(params, sizeof(params), "Timeout", strtimeout);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action_params(s, "AbsoluteTimeout", params);
res = astman_wait_for_response(s, m, 0);
if ( res > 0 && response_is(m, "Success")) {
return res;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief Status
* Lists channel status
******************************************************************************/
int astman_status(struct mansession *s, struct message **m,
char *actionid) {
int res;
char params[MAX_LEN] = "";
char event[80];
struct message msg;
MSGBUF_INIT(1);
astman_add_event_handler_system(s, astman_status_callback);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action_params(s, "Status", params);
res = astman_wait_for_response(s, &msg, 0);
if ( res > 0 && response_is(&msg, "Success")) {
for(;;) {
res = astman_wait_for_response(s, &msg, 0);
if ( res > 0 ) {
strncpy(event, astman_get_header(&msg, "Event"), sizeof(event));
if (!strcasecmp(event, "Status")) {
MSGBUF_ADD(&msg);
} else if (!strcasecmp(event, "StatusComplete")) {
*m = MSGBUF_MSG;
astman_add_event_handler_system(s, NULL);
return ASTMAN_SUCCESS;
}
} else {
goto out;
}
} /* infinity loop */
}
out:
astman_add_event_handler_system(s, NULL);
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief Events
* Enable/Disable sending of events to this manager
* @param EventMask:
* system
* call
* log
* verbose
* command
* agent
* user
* all if all events should be sent.
* none if no events should be sent.
******************************************************************************/
int astman_events(struct mansession *s, struct message *m,
char *eventmask,
char *actionid) {
int res;
char params[MAX_LEN] = "";
if (astman_strlen_zero(eventmask))
return ASTMAN_FAILURE;
astman_add_param(params, sizeof(params), "EventMask", eventmask);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action_params(s, "Events", params);
res = astman_wait_for_response(s, m, 0);
if ( res > 0 )
return res;
else
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief GetVar
* Get the value of a global or local channel variable.
*
* @param Channel: Channel to read variable from
* @param Variable: Variable name
******************************************************************************/
int astman_getvar(struct mansession *s, struct message *m,
char *channel, char *variable,
char *actionid) {
int res;
char params[MAX_LEN] = "";
if (astman_strlen_zero(variable))
return ASTMAN_FAILURE;
astman_add_param(params, sizeof(params), "Channel", channel);
astman_add_param(params, sizeof(params), "Variable", variable);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action_params(s, "GetVar", params);
res = astman_wait_for_response(s, m, 0);
if ( res > 0 && response_is(m, "Success")) {
return res;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief SetVar
* Set a global or local channel variable.
*
* @param Channel: Channel to set variable for
* @param Variable: Variable name
* @param Value: Value
******************************************************************************/
int astman_setvar(struct mansession *s, struct message *m,
char *channel, char *variable, char *value,
char *actionid) {
int res;
char params[MAX_LEN] = "";
if (astman_strlen_zero(variable))
return ASTMAN_FAILURE;
astman_add_param(params, sizeof(params), "Channel", channel);
astman_add_param(params, sizeof(params), "Variable", variable);
astman_add_param(params, sizeof(params), "Value", value);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action_params(s, "SetVar", params);
res = astman_wait_for_response(s, m, 0);
if ( res > 0 && response_is(m, "Success")) {
return res;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief ListCommands
* List available manager commands
******************************************************************************/
int astman_list_commands(struct mansession *s, struct message *m,
char *actionid)
{
int res = 0;
char params[MAX_LEN] = "";
astman_add_param(params, sizeof(params), "Parameters", "ActionId");
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action_params(s, "ListCommands", params);
res = astman_wait_for_response(s, m, 0);
if ( res > 0 && response_is(m, "Success")) {
return res;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief Action: ListCategories
* Synopsis: List categories in configuration file
* Privilege: config,all
* Description: A 'ListCategories' action will dump the categories in
* a given file.
* @param filename: Configuration filename (e.g. foo.conf)
******************************************************************************/
int astman_list_categories(struct mansession *s, struct message *m,
char *filename, char *actionid)
{
char params[MAX_LEN] = "";
int res = 0;
astman_add_param(params, sizeof(params), "Filename", filename);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action(s, "ListCategories", params);
res = astman_wait_for_response(s, m, 0);
if ( res > 0 && response_is(m, "Success")) {
return res;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief Action: GetConfig
* Synopsis: Retrieve configuration
* Privilege: system,config,all
* Description: A 'GetConfig' action will dump the contents
* of a configuration file by category and contents or
* optionally by specified category only.
* @warning Variables: (Names marked with * are required)
* @param filename: Configuration filename (e.g. foo.conf)
* @param category: Category in configuration file
******************************************************************************/
int astman_get_config(struct mansession *s, struct message *m,
char *filename, char * category, char *actionid)
{
int rv = 0;
char params[MAX_LEN] = "";
astman_add_param(params, sizeof(params), "Filename", filename);
astman_add_param(params, sizeof(params), "Category", category);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action_params(s, "GetConfig", params);
rv = astman_wait_for_response(s, m, 3);
if ( rv > 0 && response_is(m, "Success")) {
return ASTMAN_SUCCESS;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief Action: SIPpeers
* Synopsis: List SIP peers (text format)
* Privilege: system,reporting,all
* Description: Lists SIP peers in text format with details on current status.
* Peerlist will follow as separate events, followed by a final event called
* PeerlistComplete.
*
* @param ActionID: <id> Action ID for this transaction. Will be returned.
******************************************************************************/
int astman_sip_peers(struct mansession *s, struct message **m,
char *actionid)
{
int res = 0;
struct message msg;
char params[MAX_LEN] = "";
MSGBUF_INIT(1);
astman_add_event_handler_system(s, &astman_sippeers_callback);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action(s, "SIPpeers", params);
res = astman_wait_for_response(s, &msg, 0);
if ( res > 0 && response_is(&msg, "Success")) {
if(strlen(astman_get_header(&msg, "Eventlist"))) {
while(astman_wait_for_response(s, &msg, 0)==ASTMAN_SUCCESS) {
if(strncasecmp(astman_get_header(&msg, ASTMAN_HEADER_EVENT),
ASTMAN_EVENT_PEER_LIST_COMPLETE,
strlen(ASTMAN_EVENT_PEER_LIST_COMPLETE)))
{
MSGBUF_ADD(&msg);
} else {
*m = MSGBUF_MSG;
astman_add_event_handler_system(s, NULL);
return MSGBUF_NB;
}
}
}
}
astman_add_event_handler_system(s, NULL);
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief Action: SIPshowpeer
* Synopsis: Show SIP peer (text format)
* Privilege: system,reporting,all
* Description: Show one SIP peer with details on current status.
*
* Peer: <name> The peer name you want to check.
* ActionID: <id> Optional action ID for this AMI transaction.
*
******************************************************************************/
int astman_sip_show_peer(struct mansession *s, struct message *m,
char *peer, char *actionid)
{
int res = 0;
char params[MAX_LEN] = "";
if(astman_strlen_zero(peer)) {
return ASTMAN_FAILURE;
}
astman_add_param(params, sizeof(params), "Peer", peer);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action(s, "SIPShowpeer", params);
res = astman_wait_for_response(s, m, 0);
if ( res > 0 && response_is(m, "Success")) {
return ASTMAN_SUCCESS;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief Action: SIPqualifypeer
* Synopsis: Show SIP peer (text format)
* Privilege: system,reporting,all
* Description: Show one SIP peer with details on current status.
*
* Peer: <name> The peer name you want to check.
* ActionID: <id> Optional action ID for this AMI transaction.
*
******************************************************************************/
int astman_sip_qualify_peer(struct mansession *s, struct message *m,
char *peer, char *actionid)
{
int res = 0;
char params[MAX_LEN] = "";
if(astman_strlen_zero(peer)) {
return ASTMAN_FAILURE;
}
astman_add_param(params, sizeof(params), "Peer", peer);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action(s, "SIPqualifypeer", params);
res = astman_wait_for_response(s, m, 1);
if ( res > 0 && response_is(m, "Success")) {
return ASTMAN_SUCCESS;
}
return ASTMAN_FAILURE;
}
/*******************************************************************************
* @brief Action: SIPshowregistry
Synopsis: Show SIP registrations (text format)
Privilege: system,reporting,all
Description: Lists all registration requests and status
Registrations will follow as separate events. followed by a final event called
RegistrationsComplete.
Variables:
ActionID: <id> Action ID for this transaction. Will be returned.
* @param ActionID: <id> Action ID for this transaction. Will be returned.
******************************************************************************/
int astman_sip_show_registry(struct mansession *s, struct message **m,
char *actionid)
{
int res = 0;
struct message msg;
char params[MAX_LEN] = "";
MSGBUF_INIT(1);
astman_add_event_handler_system(s, &astman_sipshowregistry_callback);
astman_add_param(params, sizeof(params), "ActionId", actionid);
astman_manager_action(s, "SIPshowregistry", params);
res = astman_wait_for_response(s, &msg, 0);
if ( res > 0 && response_is(&msg, "Success")) {
if(strlen(astman_get_header(&msg, "Eventlist"))) {
while(astman_wait_for_response(s, &msg, 0)==ASTMAN_SUCCESS) {
if(strncasecmp(astman_get_header(&msg, ASTMAN_HEADER_EVENT),
ASTMAN_EVENT_REGISTRATIONS_COMPLETE,
strlen(ASTMAN_EVENT_REGISTRATIONS_COMPLETE)))
{
MSGBUF_ADD(&msg);
} else {
*m = MSGBUF_MSG;
astman_add_event_handler_system(s, NULL);
return MSGBUF_NB;
}
}
}
}
astman_add_event_handler_system(s, NULL);
return ASTMAN_FAILURE;
}