forked from asterisk/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbx_config.c
2183 lines (1907 loc) · 61.6 KB
/
pbx_config.c
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2006, Digium, Inc.
*
* Mark Spencer <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Populate and remember extensions from static config file
*
*
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
/*** DOCUMENTATION
<manager name="DialplanExtensionAdd" language="en_US">
<synopsis>
Add an extension to the dialplan
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Context" required="true">
<para>Context where the extension will be created. The context will
be created if it does not already exist.</para>
</parameter>
<parameter name="Extension" required="true">
<para>Name of the extension that will be created (may include callerid match by separating
with '/')</para>
</parameter>
<parameter name="Priority" required="true">
<para>Priority being added to this extension. Must be either <literal>hint</literal> or a
numerical value.</para>
</parameter>
<parameter name="Application" required="true">
<para>The application to use for this extension at the requested priority</para>
</parameter>
<parameter name="ApplicationData" required="false">
<para>Arguments to the application.</para>
</parameter>
<parameter name="Replace" required="false">
<para>If set to 'yes', '1', 'true' or any of the other values we evaluate as true, then
if an extension already exists at the requested context, extension, and priority it will
be overwritten. Otherwise, the existing extension will remain and the action will fail.
</para>
</parameter>
</syntax>
</manager>
<manager name="DialplanExtensionRemove" language="en_US">
<synopsis>
Remove an extension from the dialplan
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Context" required="true">
<para>Context of the extension being removed</para>
</parameter>
<parameter name="Extension" required="true">
<para>Name of the extension being removed (may include callerid match by separating with '/')</para>
</parameter>
<parameter name="Priority" required="false">
<para>If provided, only remove this priority from the extension instead of all
priorities in the extension.</para>
</parameter>
</syntax>
</manager>
***/
#include "asterisk.h"
#include <ctype.h>
#include "asterisk/paths.h" /* ast_config_AST_CONFIG_DIR */
#include "asterisk/pbx.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/cli.h"
#include "asterisk/channel.h" /* AST_MAX_EXTENSION */
#include "asterisk/callerid.h"
static const char config[] = "extensions.conf";
static const char registrar[] = "pbx_config";
static char userscontext[AST_MAX_EXTENSION] = "default";
static int static_config = 0;
static int write_protect_config = 1;
static int autofallthrough_config = 1;
static int clearglobalvars_config = 0;
static int extenpatternmatchnew_config = 0;
static char *overrideswitch_config = NULL;
static struct stasis_subscription *fully_booted_subscription;
AST_MUTEX_DEFINE_STATIC(save_dialplan_lock);
AST_MUTEX_DEFINE_STATIC(reload_lock);
static struct ast_context *local_contexts = NULL;
static struct ast_hashtab *local_table = NULL;
/*
* Prototypes for our completion functions
*/
static char *complete_dialplan_remove_include(struct ast_cli_args *);
static char *complete_dialplan_add_include(struct ast_cli_args *);
static char *complete_dialplan_remove_ignorepat(struct ast_cli_args *);
static char *complete_dialplan_add_ignorepat(struct ast_cli_args *);
static char *complete_dialplan_remove_extension(struct ast_cli_args *);
static char *complete_dialplan_add_extension(struct ast_cli_args *);
static char *complete_dialplan_remove_context(struct ast_cli_args *);
/*
* Implementation of functions provided by this module
*/
/*!
* * REMOVE context command stuff
*/
static char *handle_cli_dialplan_remove_context(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
switch (cmd) {
case CLI_INIT:
e->command = "dialplan remove context";
e->usage =
"Usage: dialplan remove context <context>\n"
" Removes all extensions from a specified context.\n";
return NULL;
case CLI_GENERATE:
return complete_dialplan_remove_context(a);
}
if (a->argc != 4) {
return CLI_SHOWUSAGE;
}
if (ast_context_destroy_by_name(a->argv[3], NULL)) {
ast_cli(a->fd, "There is no such context as '%s'\n", a->argv[3]);
return CLI_SUCCESS;
} else {
ast_cli(a->fd, "Removed context '%s'\n", a->argv[3]);
return CLI_SUCCESS;
}
}
/*!
* REMOVE INCLUDE command stuff
*/
static char *handle_cli_dialplan_remove_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
switch (cmd) {
case CLI_INIT:
e->command = "dialplan remove include";
e->usage =
"Usage: dialplan remove include <context> from <context>\n"
" Remove an included context from another context.\n";
return NULL;
case CLI_GENERATE:
return complete_dialplan_remove_include(a);
}
if (a->argc != 6 || strcmp(a->argv[4], "from"))
return CLI_SHOWUSAGE;
if (!ast_context_remove_include(a->argv[5], a->argv[3], registrar)) {
ast_cli(a->fd, "We are not including '%s' into '%s' now\n",
a->argv[3], a->argv[5]);
return CLI_SUCCESS;
}
ast_cli(a->fd, "Failed to remove '%s' include from '%s' context\n",
a->argv[3], a->argv[5]);
return CLI_FAILURE;
}
/*! \brief return true if 'name' is included by context c */
static int lookup_ci(struct ast_context *c, const char *name)
{
int idx;
int ret = 0;
if (ast_rdlock_context(c)) {
/* error, skip */
return 0;
}
for (idx = 0; idx < ast_context_includes_count(c); idx++) {
const struct ast_include *i = ast_context_includes_get(c, idx);
if (!strcmp(name, ast_get_include_name(i))) {
ret = -1;
break;
}
}
ast_unlock_context(c);
return ret;
}
/*! \brief return true if 'name' is in the ignorepats for context c */
static int lookup_c_ip(struct ast_context *c, const char *name)
{
int idx;
int ret = 0;
if (ast_rdlock_context(c)) {
/* error, skip */
return 0;
}
for (idx = 0; idx < ast_context_ignorepats_count(c); idx++) {
const struct ast_ignorepat *ip = ast_context_ignorepats_get(c, idx);
if (!strcmp(name, ast_get_ignorepat_name(ip))) {
ret = -1;
break;
}
}
ast_unlock_context(c);
return ret;
}
/*! \brief moves to the n-th word in the string, or empty string if none */
static const char *skip_words(const char *p, int n)
{
int in_blank = 0;
for (;n && *p; p++) {
if (isblank(*p) /* XXX order is important */ && !in_blank) {
n--; /* one word is gone */
in_blank = 1;
} else if (/* !is_blank(*p), we know already, && */ in_blank) {
in_blank = 0;
}
}
return p;
}
/*! \brief match the first 'len' chars of word. len==0 always succeeds */
static int partial_match(const char *s, const char *word, int len)
{
return (len == 0 || !strncmp(s, word, len));
}
/*! \brief split extension\@context in two parts, return -1 on error.
* The return string is malloc'ed and pointed by *ext
*/
static int split_ec(const char *src, char **ext, char ** const ctx, char ** const cid)
{
char *i, *c, *e = ast_strdup(src); /* now src is not used anymore */
if (e == NULL)
return -1; /* malloc error */
/* now, parse values from 'exten@context' */
*ext = e;
c = strchr(e, '@');
if (c == NULL) /* no context part */
*ctx = ""; /* it is not overwritten, anyways */
else { /* found context, check for duplicity ... */
*c++ = '\0';
*ctx = c;
if (strchr(c, '@')) { /* two @, not allowed */
ast_free(e);
return -1;
}
}
if (cid && (i = strchr(e, '/'))) {
*i++ = '\0';
*cid = i;
} else if (cid) {
/* Signal none detected */
*cid = NULL;
}
return 0;
}
/* _X_ is the string we need to complete */
static char *complete_dialplan_remove_include(struct ast_cli_args *a)
{
int which = 0;
char *res = NULL;
int len = strlen(a->word); /* how many bytes to match */
struct ast_context *c = NULL;
if (a->pos == 3) { /* "dialplan remove include _X_" */
if (ast_wrlock_contexts()) {
ast_log(LOG_ERROR, "Failed to lock context list\n");
return NULL;
}
/* walk contexts and their includes, return the n-th match */
while (!res && (c = ast_walk_contexts(c))) {
int idx;
if (ast_rdlock_context(c)) /* error ? skip this one */
continue;
for (idx = 0; idx < ast_context_includes_count(c); idx++) {
const struct ast_include *i = ast_context_includes_get(c, idx);
const char *i_name = ast_get_include_name(i);
struct ast_context *nc = NULL;
int already_served = 0;
if (!partial_match(i_name, a->word, len))
continue; /* not matched */
/* check if this include is already served or not */
/* go through all contexts again till we reach actual
* context or already_served = 1
*/
while ( (nc = ast_walk_contexts(nc)) && nc != c && !already_served)
already_served = lookup_ci(nc, i_name);
if (!already_served && ++which > a->n) {
res = ast_strdup(i_name);
break;
}
}
ast_unlock_context(c);
}
ast_unlock_contexts();
return res;
} else if (a->pos == 4) { /* "dialplan remove include CTX _X_" */
/*
* complete as 'from', but only if previous context is really
* included somewhere
*/
char *context, *dupline;
const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'include' */
if (a->n > 0)
return NULL;
context = dupline = ast_strdup(s);
if (!dupline) {
ast_log(LOG_ERROR, "Out of free memory\n");
return NULL;
}
strsep(&dupline, " ");
if (ast_rdlock_contexts()) {
ast_log(LOG_ERROR, "Failed to lock contexts list\n");
ast_free(context);
return NULL;
}
/* go through all contexts and check if is included ... */
while (!res && (c = ast_walk_contexts(c)))
if (lookup_ci(c, context)) /* context is really included, complete "from" command */
res = ast_strdup("from");
ast_unlock_contexts();
if (!res)
ast_log(LOG_WARNING, "%s not included anywhere\n", context);
ast_free(context);
return res;
} else if (a->pos == 5) { /* "dialplan remove include CTX from _X_" */
/*
* Context from which we removing include ...
*/
char *context, *dupline, *from;
const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'include' */
context = dupline = ast_strdup(s);
if (!dupline) {
ast_log(LOG_ERROR, "Out of free memory\n");
return NULL;
}
strsep(&dupline, " "); /* skip context */
/* fourth word must be 'from' */
from = strsep(&dupline, " ");
if (!from || strcmp(from, "from")) {
ast_free(context);
return NULL;
}
if (ast_rdlock_contexts()) {
ast_log(LOG_ERROR, "Failed to lock context list\n");
ast_free(context);
return NULL;
}
/* walk through all contexts ... */
c = NULL;
while ( !res && (c = ast_walk_contexts(c))) {
const char *c_name = ast_get_context_name(c);
if (!partial_match(c_name, a->word, len)) /* not a good target */
continue;
/* walk through all includes and check if it is our context */
if (lookup_ci(c, context) && ++which > a->n)
res = ast_strdup(c_name);
}
ast_unlock_contexts();
ast_free(context);
return res;
}
return NULL;
}
/*!
* REMOVE EXTENSION command stuff
*/
static char *handle_cli_dialplan_remove_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
int removing_priority = 0;
char *exten, *context, *cid;
char *ret = CLI_FAILURE;
switch (cmd) {
case CLI_INIT:
e->command = "dialplan remove extension";
e->usage =
"Usage: dialplan remove extension exten[/cid]@context [priority]\n"
" Remove an extension from a given context. If a priority\n"
" is given, only that specific priority from the given extension\n"
" will be removed.\n";
return NULL;
case CLI_GENERATE:
return complete_dialplan_remove_extension(a);
}
if (a->argc != 5 && a->argc != 4)
return CLI_SHOWUSAGE;
/*
* Priority input checking ...
*/
if (a->argc == 5) {
const char *c = a->argv[4];
/* check for digits in whole parameter for right priority ...
* why? because atoi (strtol) returns 0 if any characters in
* string and whole extension will be removed, it's not good
*/
if (!strcmp("hint", c))
removing_priority = PRIORITY_HINT;
else {
while (*c && isdigit(*c))
c++;
if (*c) { /* non-digit in string */
ast_cli(a->fd, "Invalid priority '%s'\n", a->argv[4]);
return CLI_FAILURE;
}
removing_priority = atoi(a->argv[4]);
}
if (removing_priority == 0) {
ast_cli(a->fd, "If you want to remove whole extension, please " \
"omit priority argument\n");
return CLI_FAILURE;
}
}
/* XXX original overwrote argv[3] */
/*
* Format exten@context checking ...
*/
if (split_ec(a->argv[3], &exten, &context, &cid))
return CLI_FAILURE; /* XXX malloc failure */
if ((!strlen(exten)) || (!(strlen(context)))) {
ast_cli(a->fd, "Missing extension or context name in third argument '%s'\n",
a->argv[3]);
ast_free(exten);
return CLI_FAILURE;
}
if (!ast_context_remove_extension_callerid(context, exten, removing_priority,
/* Do NOT substitute S_OR; it is NOT the same thing */
cid ? cid : (removing_priority ? "" : NULL), cid ? 1 : 0, registrar)) {
if (!removing_priority)
ast_cli(a->fd, "Whole extension %s@%s removed\n",
exten, context);
else
ast_cli(a->fd, "Extension %s@%s with priority %d removed\n",
exten, context, removing_priority);
ret = CLI_SUCCESS;
} else {
if (cid) {
ast_cli(a->fd, "Failed to remove extension %s/%s@%s\n", exten, cid, context);
} else {
ast_cli(a->fd, "Failed to remove extension %s@%s\n", exten, context);
}
ret = CLI_FAILURE;
}
ast_free(exten);
return ret;
}
static int manager_dialplan_extension_remove(struct mansession *s, const struct message *m)
{
const char *context = astman_get_header(m, "Context");
const char *extension = astman_get_header(m, "Extension");
const char *priority = astman_get_header(m, "Priority");
int ipriority;
char *exten;
char *cidmatch = NULL;
if (ast_strlen_zero(context) || ast_strlen_zero(extension)) {
astman_send_error(s, m, "Context and Extension must be provided "
"for DialplanExtensionRemove");
return 0;
}
exten = ast_strdupa(extension);
if (strchr(exten, '/')) {
cidmatch = exten;
strsep(&cidmatch, "/");
}
if (ast_strlen_zero(priority)) {
ipriority = 0;
} else if (!strcmp("hint", priority)) {
ipriority = PRIORITY_HINT;
} else if ((sscanf(priority, "%30d", &ipriority) != 1) || ipriority <= 0) {
astman_send_error(s, m, "The priority specified was invalid.");
return 0;
}
if (!ast_context_remove_extension_callerid(context, exten, ipriority,
/* Do not substitute S_OR; it is not the same thing */
!ast_strlen_zero(cidmatch) ? cidmatch : (ipriority ? "" : NULL),
!ast_strlen_zero(cidmatch) ? 1 : 0, registrar)) {
if (ipriority) {
astman_send_ack(s, m, "Removed the requested priority from the extension");
} else {
astman_send_ack(s, m, "Removed the requested extension");
}
} else {
astman_send_error(s, m, "Failed to remove requested extension");
}
return 0;
}
static char *complete_dialplan_remove_extension(struct ast_cli_args *a)
{
char *ret = NULL;
int which = 0;
if (a->pos == 3) { /* 'dialplan remove extension _X_' (exten@context ... */
struct ast_context *c = NULL;
char *context = NULL, *exten = NULL, *cid = NULL;
int le = 0; /* length of extension */
int lc = 0; /* length of context */
int lcid = 0; /* length of cid */
lc = split_ec(a->word, &exten, &context, &cid);
if (lc) { /* error */
return NULL;
}
le = strlen(exten);
lc = strlen(context);
lcid = cid ? strlen(cid) : -1;
if (ast_rdlock_contexts()) {
ast_log(LOG_ERROR, "Failed to lock context list\n");
goto error2;
}
/* find our context ... */
while ( (c = ast_walk_contexts(c)) ) { /* match our context if any */
struct ast_exten *e = NULL;
/* XXX locking ? */
if (!partial_match(ast_get_context_name(c), context, lc))
continue; /* context not matched */
while ( (e = ast_walk_context_extensions(c, e)) ) { /* try to complete extensions ... */
if ( !strchr(a->word, '/') ||
(!strchr(a->word, '@') && partial_match(ast_get_extension_cidmatch(e), cid, lcid)) ||
(strchr(a->word, '@') && !strcmp(ast_get_extension_cidmatch(e), cid))) {
if ( ((strchr(a->word, '/') || strchr(a->word, '@')) && !strcmp(ast_get_extension_name(e), exten)) ||
(!strchr(a->word, '/') && !strchr(a->word, '@') && partial_match(ast_get_extension_name(e), exten, le))) { /* n-th match */
if (++which > a->n) {
/* If there is an extension then return exten@context. */
if (ast_get_extension_matchcid(e) && (!strchr(a->word, '@') || strchr(a->word, '/'))) {
if (ast_asprintf(&ret, "%s/%s@%s", ast_get_extension_name(e), ast_get_extension_cidmatch(e), ast_get_context_name(c)) < 0) {
ret = NULL;
}
break;
} else if (!ast_get_extension_matchcid(e) && !strchr(a->word, '/')) {
if (ast_asprintf(&ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c)) < 0) {
ret = NULL;
}
break;
}
}
}
}
}
if (e) /* got a match */
break;
}
ast_unlock_contexts();
error2:
ast_free(exten);
} else if (a->pos == 4) { /* 'dialplan remove extension EXT _X_' (priority) */
char *exten = NULL, *context, *cid, *p;
struct ast_context *c;
int le, lc, len;
const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'extension' */
int i = split_ec(s, &exten, &context, &cid); /* parse ext@context */
if (i) /* error */
goto error3;
if ( (p = strchr(exten, ' ')) ) /* remove space after extension */
*p = '\0';
if ( (p = strchr(context, ' ')) ) /* remove space after context */
*p = '\0';
le = strlen(exten);
lc = strlen(context);
len = strlen(a->word);
if (le == 0 || lc == 0)
goto error3;
if (ast_rdlock_contexts()) {
ast_log(LOG_ERROR, "Failed to lock context list\n");
goto error3;
}
/* walk contexts */
c = NULL;
while ( (c = ast_walk_contexts(c)) ) {
/* XXX locking on c ? */
struct ast_exten *e;
if (strcmp(ast_get_context_name(c), context) != 0)
continue;
/* got it, we must match here */
e = NULL;
while ( (e = ast_walk_context_extensions(c, e)) ) {
struct ast_exten *priority;
char buffer[10];
if (cid && strcmp(ast_get_extension_cidmatch(e), cid) != 0) {
continue;
}
if (strcmp(ast_get_extension_name(e), exten) != 0)
continue;
/* XXX lock e ? */
priority = NULL;
while ( !ret && (priority = ast_walk_extension_priorities(e, priority)) ) {
snprintf(buffer, sizeof(buffer), "%d", ast_get_extension_priority(priority));
if (partial_match(buffer, a->word, len) && ++which > a->n) /* n-th match */
ret = ast_strdup(buffer);
}
break;
}
break;
}
ast_unlock_contexts();
error3:
ast_free(exten);
}
return ret;
}
/*!
* Include context ...
*/
static char *handle_cli_dialplan_add_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
const char *into_context;
switch (cmd) {
case CLI_INIT:
e->command = "dialplan add include";
e->usage =
"Usage: dialplan add include <context> into <context>\n"
" Include a context in another context.\n";
return NULL;
case CLI_GENERATE:
return complete_dialplan_add_include(a);
}
if (a->argc != 6) /* dialplan add include CTX in CTX */
return CLI_SHOWUSAGE;
/* fifth arg must be 'into' ... */
if (strcmp(a->argv[4], "into"))
return CLI_SHOWUSAGE;
into_context = a->argv[5];
if (!ast_context_find(into_context)) {
ast_cli(a->fd, "Context '%s' did not exist prior to add include - the context will be created.\n", into_context);
}
if (!ast_context_find_or_create(NULL, NULL, into_context, registrar)) {
ast_cli(a->fd, "ast_context_find_or_create() failed\n");
ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",a->argv[3], a->argv[5]);
return CLI_FAILURE;
}
if (ast_context_add_include(a->argv[5], a->argv[3], registrar)) {
switch (errno) {
case ENOMEM:
ast_cli(a->fd, "Out of memory for context addition\n");
break;
case EBUSY:
ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
break;
case EEXIST:
ast_cli(a->fd, "Context '%s' already included in '%s' context\n",
a->argv[3], a->argv[5]);
break;
case ENOENT:
case EINVAL:
ast_cli(a->fd, "There is no existence of context '%s'\n",
errno == ENOENT ? a->argv[5] : a->argv[3]);
break;
default:
ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",
a->argv[3], a->argv[5]);
break;
}
return CLI_FAILURE;
}
/* show some info ... */
ast_cli(a->fd, "Context '%s' included in '%s' context\n",
a->argv[3], a->argv[5]);
return CLI_SUCCESS;
}
static char *complete_dialplan_add_include(struct ast_cli_args *a)
{
struct ast_context *c;
int which = 0;
char *ret = NULL;
int len = strlen(a->word);
if (a->pos == 3) { /* 'dialplan add include _X_' (context) ... */
if (ast_rdlock_contexts()) {
ast_log(LOG_ERROR, "Failed to lock context list\n");
return NULL;
}
for (c = NULL; !ret && (c = ast_walk_contexts(c)); )
if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
ret = ast_strdup(ast_get_context_name(c));
ast_unlock_contexts();
return ret;
} else if (a->pos == 4) { /* dialplan add include CTX _X_ */
/* always complete as 'into' */
return (a->n == 0) ? ast_strdup("into") : NULL;
} else if (a->pos == 5) { /* 'dialplan add include CTX into _X_' (dst context) */
char *context, *dupline, *into;
const char *s = skip_words(a->line, 3); /* should not fail */
context = dupline = ast_strdup(s);
if (!dupline) {
ast_log(LOG_ERROR, "Out of free memory\n");
return NULL;
}
strsep(&dupline, " "); /* skip context */
into = strsep(&dupline, " ");
/* error if missing context or fifth word is not 'into' */
if (!strlen(context) || strcmp(into, "into")) {
ast_log(LOG_ERROR, "bad context %s or missing into %s\n",
context, into);
goto error3;
}
if (ast_rdlock_contexts()) {
ast_log(LOG_ERROR, "Failed to lock context list\n");
goto error3;
}
for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
if (!strcmp(context, ast_get_context_name(c)))
continue; /* skip ourselves */
if (partial_match(ast_get_context_name(c), a->word, len) &&
!lookup_ci(c, context) /* not included yet */ &&
++which > a->n) {
ret = ast_strdup(ast_get_context_name(c));
}
}
ast_unlock_contexts();
error3:
ast_free(context);
return ret;
}
return NULL;
}
/*!
* \brief 'save dialplan' CLI command implementation functions ...
*/
static char *handle_cli_dialplan_save(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
char filename[256], overrideswitch[256] = "";
struct ast_context *c;
struct ast_config *cfg;
struct ast_variable *v;
int incomplete = 0; /* incomplete config write? */
FILE *output;
struct ast_flags config_flags = { 0 };
const char *base, *slash;
switch (cmd) {
case CLI_INIT:
e->command = "dialplan save";
e->usage =
"Usage: dialplan save [/path/to/extension/file]\n"
" Save dialplan created by pbx_config module.\n"
"\n"
"Example: dialplan save (/etc/asterisk/extensions.conf)\n"
" dialplan save /home/markster (/home/markster/extensions.conf)\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (! (static_config && !write_protect_config)) {
ast_cli(a->fd,
"I can't save dialplan now, see '%s' example file.\n",
config);
return CLI_FAILURE;
}
if (a->argc != 2 && a->argc != 3)
return CLI_SHOWUSAGE;
if (ast_mutex_lock(&save_dialplan_lock)) {
ast_cli(a->fd,
"Failed to lock dialplan saving (another process saving?)\n");
return CLI_FAILURE;
}
/* XXX the code here is quite loose, a pathname with .conf in it
* is assumed to be a complete pathname
*/
if (a->argc == 3) { /* have config path. Look for *.conf */
base = a->argv[2];
if (!strstr(a->argv[2], ".conf")) { /*no, this is assumed to be a pathname */
/* if filename ends with '/', do not add one */
slash = (*(a->argv[2] + strlen(a->argv[2]) -1) == '/') ? "/" : "";
} else { /* yes, complete file name */
slash = "";
}
} else {
/* no config file, default one */
base = ast_config_AST_CONFIG_DIR;
slash = "/";
}
snprintf(filename, sizeof(filename), "%s%s%s", base, slash, config);
cfg = ast_config_load("extensions.conf", config_flags);
if (!cfg) {
ast_cli(a->fd, "Failed to load extensions.conf\n");
ast_mutex_unlock(&save_dialplan_lock);
return CLI_FAILURE;
}
/* try to lock contexts list */
if (ast_rdlock_contexts()) {
ast_cli(a->fd, "Failed to lock contexts list\n");
ast_mutex_unlock(&save_dialplan_lock);
ast_config_destroy(cfg);
return CLI_FAILURE;
}
/* create new file ... */
if (!(output = fopen(filename, "wt"))) {
ast_cli(a->fd, "Failed to create file '%s'\n",
filename);
ast_unlock_contexts();
ast_mutex_unlock(&save_dialplan_lock);
ast_config_destroy(cfg);
return CLI_FAILURE;
}
/* fireout general info */
if (overrideswitch_config) {
snprintf(overrideswitch, sizeof(overrideswitch), "overrideswitch=%s\n", overrideswitch_config);
}
fprintf(output, "[general]\nstatic=%s\nwriteprotect=%s\nautofallthrough=%s\nclearglobalvars=%s\n%sextenpatternmatchnew=%s\n\n",
static_config ? "yes" : "no",
write_protect_config ? "yes" : "no",
autofallthrough_config ? "yes" : "no",
clearglobalvars_config ? "yes" : "no",
overrideswitch_config ? overrideswitch : "",
extenpatternmatchnew_config ? "yes" : "no");
if ((v = ast_variable_browse(cfg, "globals"))) {
fprintf(output, "[globals]\n");
while(v) {
int escaped_len = 2 * strlen(v->value) + 1;
char escaped[escaped_len];
ast_escape_semicolons(v->value, escaped, escaped_len);
fprintf(output, "%s => %s\n", v->name, escaped);
v = v->next;
}
fprintf(output, "\n");
}
ast_config_destroy(cfg);
#define PUT_CTX_HDR do { \
if (!context_header_written) { \
fprintf(output, "[%s]\n", ast_get_context_name(c)); \
context_header_written = 1; \
} \
} while (0)
/* walk all contexts */
for (c = NULL; (c = ast_walk_contexts(c)); ) {
int context_header_written = 0;
struct ast_exten *ext, *last_written_e = NULL;
int idx;
/* try to lock context and fireout all info */
if (ast_rdlock_context(c)) { /* lock failure */
incomplete = 1;
continue;
}
/* registered by this module? */
/* XXX do we need this ? */
if (!strcmp(ast_get_context_registrar(c), registrar)) {
fprintf(output, "[%s]\n", ast_get_context_name(c));
context_header_written = 1;
}
/* walk extensions ... */
for (ext = NULL; (ext = ast_walk_context_extensions(c, ext)); ) {
struct ast_exten *p = NULL;
/* fireout priorities */
while ( (p = ast_walk_extension_priorities(ext, p)) ) {
if (strcmp(ast_get_extension_registrar(p), registrar) != 0) /* not this source */
continue;
/* make empty line between different extensions */
if (last_written_e != NULL &&
strcmp(ast_get_extension_name(last_written_e),
ast_get_extension_name(p)))
fprintf(output, "\n");
last_written_e = p;
PUT_CTX_HDR;
if (ast_get_extension_priority(p) == PRIORITY_HINT) { /* easy */
fprintf(output, "exten => %s,hint,%s\n",
ast_get_extension_name(p),
ast_get_extension_app(p));
} else {
const char *sep, *cid;
const char *el = ast_get_extension_label(p);
char label[128] = "";
char *appdata = ast_get_extension_app_data(p);
int escaped_len = (!ast_strlen_zero(appdata)) ? 2 * strlen(appdata) + 1 : 1;
char escaped[escaped_len];
if (ast_get_extension_matchcid(p)) {
sep = "/";
cid = ast_get_extension_cidmatch(p);
} else {
sep = cid = "";
}
if (el && (snprintf(label, sizeof(label), "(%s)", el) != (strlen(el) + 2))) {
incomplete = 1; /* error encountered or label > 125 chars */
}
if (!ast_strlen_zero(appdata)) {
ast_escape_semicolons(appdata, escaped, escaped_len);
} else {
escaped[0] = '\0';
}
fprintf(output, "exten => %s%s%s,%d%s,%s(%s)\n",
ast_get_extension_name(p), (ast_strlen_zero(sep) ? "" : sep), (ast_strlen_zero(cid) ? "" : cid),
ast_get_extension_priority(p), label,
ast_get_extension_app(p), escaped);
}
}
}