Version 0.1.8 from FTP
[asterisk/asterisk.git] / pbx / pbx_config.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Populate and remember extensions from static config file
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <asterisk/pbx.h>
15 #include <asterisk/config.h>
16 #include <asterisk/module.h>
17 #include <asterisk/logger.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 /* For where to put dynamic tables */
22 #include "../asterisk.h"
23
24 static char *dtext = "Text Extension Configuration";
25 static char *config = "extensions.conf";
26 static char *registrar = "pbx_config";
27
28 static int static_config = 0;
29
30 int unload_module(void)
31 {
32         ast_context_destroy(NULL, registrar);
33         return 0;
34 }
35
36 static int pbx_load_module(void)
37 {
38         struct ast_config *cfg;
39         struct ast_variable *v;
40         char *cxt, *ext, *pri, *appl, *data, *tc;
41         struct ast_context *con;
42
43         cfg = ast_load(config);
44         if (cfg) {
45                 /* Use existing config to populate the PBX table */
46                 static_config = ast_true(ast_variable_retrieve(cfg, "general", "static"));
47                 cxt = ast_category_browse(cfg, NULL);
48                 while(cxt) {
49                         /* All categories but "general" are considered contexts */
50                         if (!strcasecmp(cxt, "general")) {
51                                 cxt = ast_category_browse(cfg, cxt);
52                                 continue;
53                         }
54                         if ((con=ast_context_create(cxt, registrar))) {
55                                 v = ast_variable_browse(cfg, cxt);
56                                 while(v) {
57                                         if (!strcasecmp(v->name, "exten")) {
58                                                 tc = strdup(v->value);
59                                                 ext = strtok(tc, ",");
60                                                 if (!ext)
61                                                         ext="";
62                                                 pri = strtok(NULL, ",");
63                                                 if (!pri)
64                                                         pri="";
65                                                 appl = strtok(NULL, ",");
66                                                 if (!appl)
67                                                         appl="";
68                                                 data = strtok(NULL, ",");
69                                                 if (!data)
70                                                         data="";
71                                                 if (ast_add_extension2(con, 0, ext, atoi(pri), appl, strdup(data), free, registrar)) {
72                                                         ast_log(LOG_WARNING, "Unable to register extension at line %d\n", v->lineno);
73                                                 }
74                                                 free(tc);
75                                         } else if(!strcasecmp(v->name, "include")) {
76                                                 if (ast_context_add_include2(con, v->value, registrar))
77                                                         ast_log(LOG_WARNING, "Unable to include context '%s' in context '%s'\n", v->value, cxt);
78                                         }
79                                         v = v->next;
80                                 }
81                         }
82                         cxt = ast_category_browse(cfg, cxt);
83                 }
84                 ast_destroy(cfg);
85         }
86         return 0;
87 }
88
89 int load_module(void)
90 {
91         return pbx_load_module();
92 }
93
94 int reload(void)
95 {
96         ast_context_destroy(NULL, registrar);
97         load_module();
98         return 0;
99 }
100
101 int usecount(void)
102 {
103         return 0;
104 }
105
106 char *description(void)
107 {
108         return dtext;
109 }
110
111 char *key(void)
112 {
113         return ASTERISK_GPL_KEY;
114 }