aboutsummaryrefslogtreecommitdiffstats
path: root/pbx/pbx_realtime.c
blob: 453f336198516ca9cb225739a72daab7225ceefc (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Realtime PBX Module
 *
 * Copyright (C) 2004, Digium Inc.
 *
 * Written by Mark Spencer <markster@digium.com>
 *
 * This program is Free Software distributed under the terms of
 * of the GNU General Public License.
 */

#include <asterisk/file.h>
#include <asterisk/logger.h>
#include <asterisk/channel.h>
#include <asterisk/config.h>
#include <asterisk/options.h>
#include <asterisk/pbx.h>
#include <asterisk/module.h>
#include <asterisk/frame.h>
#include <asterisk/file.h>
#include <asterisk/cli.h>
#include <asterisk/lock.h>
#include <asterisk/md5.h>
#include <asterisk/linkedlists.h>
#include <asterisk/chanvars.h>
#include <asterisk/sched.h>
#include <asterisk/io.h>
#include <asterisk/utils.h>
#include <asterisk/crypto.h>
#include <asterisk/astdb.h>

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define MODE_MATCH 0
#define MODE_MATCHMORE 1
#define MODE_CANMATCH 2

static char *tdesc = "Realtime Switch";

/* Realtime switch looks up extensions in the supplied realtime table.

	[context@][realtimetable][/options]

	If the realtimetable is omitted it is assumed to be "extensions".  If no context is 
	specified the context is assumed to be whatever is the container.

	The realtime table should have entries for context,exten,priority,app,args
	
	The realtime table currently does not support patterns or callerid fields.

*/


#define REALTIME_COMMON(mode) \
	char *buf; \
	char *opts; \
	const char *cxt; \
	char *table; \
	int res=-1; \
	struct ast_variable *var=NULL; \
	buf = ast_strdupa(data); \
	if (buf) { \
		opts = strchr(buf, '/'); \
		if (opts) { \
			*opts='\0'; \
			opts++; \
		} else \
			opts=""; \
		table = strchr(buf, '@'); \
		if (table) { \
			*table = '\0'; \
			table++;\
			cxt = buf; \
		} else cxt = NULL; \
		if (!cxt || ast_strlen_zero(cxt)) \
			cxt = context;\
		if (!table || ast_strlen_zero(table)) \
			table = "extensions"; \
		var = realtime_switch_common(table, cxt, exten, priority, mode); \
	} else \
		res = -1; 

static struct ast_variable *realtime_switch_common(const char *table, const char *context, const char *exten, int priority, int mode)
{
	struct ast_variable *var;
	char pri[20];
	snprintf(pri, sizeof(pri), "%d", priority);
	printf("%s/%s/%s/%s exists\n", table, context, exten, pri);
	var = ast_load_realtime(table, "context", context, "exten", exten, "priority", pri, NULL);
	return var;
}

static int realtime_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
{
	REALTIME_COMMON(MODE_MATCH);
	if (var) ast_destroy_realtime(var);
	if (var)
		res = 1;
	return res > 0 ? res : 0;
}

static int realtime_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
{
	REALTIME_COMMON(MODE_CANMATCH);
	if (var) ast_destroy_realtime(var);
	if (var)
		res = 1;
	return res > 0 ? res : 0;
}

static int realtime_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, int newstack, const char *data)
{
	char app[256];
	char *appdata="";
	struct ast_app *a;
	struct ast_variable *v;
	REALTIME_COMMON(MODE_MATCH);
	if (var) {
		v = var;
		while(v) {
			if (!strcasecmp(v->name, "app"))
				strncpy(app, v->value, sizeof(app) -1 );
			else if (!strcasecmp(v->name, "appdata"))
				appdata = ast_strdupa(v->value);
			v = v->next;
		}
		ast_destroy_realtime(var);
		if (!ast_strlen_zero(app)) {
			a = pbx_findapp(app);
			if (a) {
				res = pbx_exec(chan, a, appdata, newstack);
			} else
				ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context);
		}
	}
	return res;
}

static int realtime_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
{
	REALTIME_COMMON(MODE_MATCHMORE);
	if (var) ast_destroy_realtime(var);
	return res > 0 ? res : 0;
}

static struct ast_switch realtime_switch =
{
        name:                   "Realtime",
        description:    		"Realtime Dialplan Switch",
        exists:                 realtime_exists,
        canmatch:               realtime_canmatch,
        exec:                   realtime_exec,
        matchmore:              realtime_matchmore,
};

char *description(void)
{
	return tdesc;
}

int usecount(void)
{
	return 1;
}

char *key()
{
	return ASTERISK_GPL_KEY;
}

int unload_module(void)
{
	ast_unregister_switch(&realtime_switch);
	return 0;
}

int load_module(void)
{
	ast_register_switch(&realtime_switch);
	return 0;
}