53f2fac8ed7349b6aacdcdf752377deb45138cda
[asterisk/asterisk.git] / apps / app_realtime.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * RealTime App
5  * 
6  * Copyright (C) 1999-2004, Digium, Inc.
7  *
8  * Anthony Minessale <anthmct@yahoo.com>
9  * Mark Spencer <markster@digium.com>
10  *
11  * This program is free software, distributed under the terms of
12  * the GNU General Public License
13  */
14
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/options.h>
19 #include <asterisk/pbx.h>
20 #include <asterisk/config.h>
21 #include <asterisk/module.h>
22 #include <asterisk/lock.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #define next_one(var) var = var->next
28 #define crop_data(str) { *(str) = '\0' ; (str)++; }
29
30 static char *tdesc = "Realtime Data Lookup/Rewrite";
31 static char *app = "RealTime";
32 static char *uapp = "RealTimeUpdate";
33 static char *synopsis = "Realtime Data Lookup";
34 static char *usynopsis = "Realtime Data Rewrite";
35 static char *USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])";
36 static char *UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)";
37 static char *desc = "Use the RealTime config handler system to read data into channel variables.\n"
38 "RealTime(<family>|<colmatch>|<value>[|<prefix>])\n\n"
39 "All unique column names will be set as channel variables with optional prefix to the name.\n"
40 "e.g. prefix of 'var_' would make the column 'name' become the variable ${var_name}\n\n";
41 static char *udesc = "Use the RealTime config handler system to update a value\n"
42 "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)\n\n"
43 "The column <newcol> in 'family' matching column <colmatch>=<value> will be updated to <newval>\n";
44
45 STANDARD_LOCAL_USER;
46 LOCAL_USER_DECL;
47
48 static int realtime_update_exec(struct ast_channel *chan, void *data) {
49         char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
50         struct localuser *u;
51         int res = 0;
52         if (!data) {
53         ast_log(LOG_ERROR,"Invalid input %s\n",UUSAGE);
54         return -1;
55     }
56         LOCAL_USER_ADD(u);
57         if ((family = ast_strdupa(data))) {
58                 if ((colmatch = strchr(family,'|'))) {
59                         crop_data(colmatch);
60                         if ((value = strchr(colmatch,'|'))) {
61                                 crop_data(value);
62                                 if ((newcol = strchr(value,'|'))) {
63                                         crop_data(newcol);
64                                         if ((newval = strchr(newcol,'|'))) 
65                                                 crop_data(newval);
66                                 }
67                         }
68                 }
69         }
70         if (! (family && value && colmatch && newcol && newval) ) {
71                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
72                 res = -1;
73         } else {
74                 ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
75         }
76
77         LOCAL_USER_REMOVE(u);
78         return res;
79
80 }
81
82
83 static int realtime_exec(struct ast_channel *chan, void *data)
84 {
85         int res=0;
86         struct localuser *u;
87         struct ast_variable *var, *itt;
88         char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
89         size_t len;
90
91         if (!data) {
92                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
93                 return -1;
94         }
95         LOCAL_USER_ADD(u);
96         if ((family = ast_strdupa(data))) {
97                 if ((colmatch = strchr(family,'|'))) {
98                         crop_data(colmatch);
99                         if ((value = strchr(colmatch,'|'))) {
100                                 crop_data(value);
101                                 if ((prefix = strchr(value,'|')))
102                                         crop_data(prefix);
103                         }
104                 }
105         }
106         if (! (family && value && colmatch) ) {
107                 ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
108                 res = -1;
109         } else {
110                 if (option_verbose > 3)
111                         ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
112                 if ((var = ast_load_realtime(family, colmatch, value, NULL))) {
113                         for (itt = var; itt; itt = itt->next) {
114                                 if(prefix) {
115                                         len = strlen(prefix) + strlen(itt->name) + 2;
116                                         vname = alloca(len);
117                                         snprintf(vname,len,"%s%s",prefix,itt->name);
118                                         
119                                 } else 
120                                         vname = itt->name;
121
122                                 pbx_builtin_setvar_helper(chan, vname, itt->value);
123                         }
124                         ast_destroy_realtime(var);
125                 } else if (option_verbose > 3)
126                         ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
127         }
128         
129         LOCAL_USER_REMOVE(u);
130         return res;
131 }
132
133 int unload_module(void)
134 {
135         STANDARD_HANGUP_LOCALUSERS;
136         ast_unregister_application(uapp);
137         return ast_unregister_application(app);
138 }
139
140 int load_module(void)
141 {
142         ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
143         return ast_register_application(app, realtime_exec, synopsis, desc);
144 }
145
146 char *description(void)
147 {
148         return tdesc;
149 }
150
151 int usecount(void)
152 {
153         int res;
154         STANDARD_USECOUNT(res);
155         return res;
156 }
157
158 char *key()
159 {
160         return ASTERISK_GPL_KEY;
161 }
162