aboutsummaryrefslogtreecommitdiffstats
path: root/apps/app_realtime.c
blob: 0523756a43667e1de0a2883db9241336c1b73de5 (plain)
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
/*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * RealTime App
 * 
 * Copyright (C) 1999-2004, Digium, Inc.
 *
 * Anthony Minessale <anthmct@yahoo.com>
 * Mark Spencer <markster@digium.com>
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License
 */

#include <asterisk/file.h>
#include <asterisk/logger.h>
#include <asterisk/channel.h>
#include <asterisk/options.h>
#include <asterisk/pbx.h>
#include <asterisk/config.h>
#include <asterisk/module.h>
#include <asterisk/lock.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define next_one(var) var = var->next
#define crop_data(str) { *(str) = '\0' ; (str)++; }

static char *tdesc = "Realtime Data Lookup/Rewrite";
static char *app = "RealTime";
static char *uapp = "RealTimeUpdate";
static char *synopsis = "Realtime Data Lookup";
static char *usynopsis = "Realtime Data Rewrite";
static char *USAGE = "RealTime(<family>|<colmatch>|<value>[|<prefix>])";
static char *UUSAGE = "RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)";
static char *desc = "Use the RealTime config handler system to read data into channel variables.\n"
"RealTime(<family>|<colmatch>|<value>[|<prefix>])\n\n"
"All unique column names will be set as channel variables with optional prefix to the name.\n"
"e.g. prefix of 'var_' would make the column 'name' become the variable ${var_name}\n\n";
static char *udesc = "Use the RealTime config handler system to update a value\n"
"RealTimeUpdate(<family>|<colmatch>|<value>|<newcol>|<newval>)\n\n"
"The column <newcol> in 'family' matching column <colmatch>=<value> will be updated to <newval>\n";

STANDARD_LOCAL_USER;
LOCAL_USER_DECL;

static int realtime_update_exec(struct ast_channel *chan, void *data) {
	char *family=NULL, *colmatch=NULL, *value=NULL, *newcol=NULL, *newval=NULL;
	struct localuser *u;
	int res = 0;
	if (!data) {
        ast_log(LOG_ERROR,"Invalid input %s\n",UUSAGE);
        return -1;
    }
	LOCAL_USER_ADD(u);
	if ((family = ast_strdupa(data))) {
		if ((colmatch = strchr(family,'|'))) {
			crop_data(colmatch);
			if ((value = strchr(colmatch,'|'))) {
				crop_data(value);
				if ((newcol = strchr(value,'|'))) {
					crop_data(newcol);
					if ((newval = strchr(newcol,'|'))) 
						crop_data(newval);
				}
			}
		}
	}
	if (! (family && value && colmatch && newcol && newval) ) {
		ast_log(LOG_ERROR,"Invalid input: usage %s\n",UUSAGE);
		res = -1;
	} else {
		ast_update_realtime(family,colmatch,value,newcol,newval,NULL);
	}

	LOCAL_USER_REMOVE(u);
	return res;

}


static int realtime_exec(struct ast_channel *chan, void *data)
{
	int res=0;
	struct localuser *u;
	struct ast_variable *var, *itt;
	char *family=NULL, *colmatch=NULL, *value=NULL, *prefix=NULL, *vname=NULL;
	size_t len;

	if (!data) {
		ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
		return -1;
	}
	LOCAL_USER_ADD(u);
	if ((family = ast_strdupa(data))) {
		if ((colmatch = strchr(family,'|'))) {
			crop_data(colmatch);
			if ((value = strchr(colmatch,'|'))) {
				crop_data(value);
				if ((prefix = strchr(value,'|')))
					crop_data(prefix);
			}
		}
	}
	if (! (family && value && colmatch) ) {
		ast_log(LOG_ERROR,"Invalid input: usage %s\n",USAGE);
		res = -1;
	} else {
		if (option_verbose > 3)
			ast_verbose(VERBOSE_PREFIX_4"Realtime Lookup: family:'%s' colmatch:'%s' value:'%s'\n",family,colmatch,value);
		if ((var = ast_load_realtime(family, colmatch, value))) {
			for (itt = var; itt; itt = itt->next) {
				if(prefix) {
					len = strlen(prefix) + strlen(itt->name) + 2;
					vname = alloca(len);
					snprintf(vname,len,"%s%s",prefix,itt->name);
					
				} else 
					vname = itt->name;

				pbx_builtin_setvar_helper(chan, vname, itt->value);
			}
			ast_destroy_realtime(var);
		} else if (option_verbose > 3)
			ast_verbose(VERBOSE_PREFIX_4"No Realtime Matches Found.\n");
	}
	
	LOCAL_USER_REMOVE(u);
	return res;
}

int unload_module(void)
{
	STANDARD_HANGUP_LOCALUSERS;
	ast_unregister_application(uapp);
	return ast_unregister_application(app);
}

int load_module(void)
{
	ast_register_application(uapp, realtime_update_exec, usynopsis, udesc);
	return ast_register_application(app, realtime_exec, synopsis, desc);
}

char *description(void)
{
	return tdesc;
}

int usecount(void)
{
	int res;
	STANDARD_USECOUNT(res);
	return res;
}

char *key()
{
	return ASTERISK_GPL_KEY;
}