update asterisk to 1.2.16, add chan-oss subpackage (closes: #1492)
[openwrt/svn-archive/archive.git] / net / asterisk / patches / 007-app_mysql.patch
1 diff -Nru asterisk-1.2.14.org/apps/app_sql_mysql.c asterisk-1.2.14/apps/app_sql_mysql.c
2 --- asterisk-1.2.14.org/apps/app_sql_mysql.c 1970-01-01 01:00:00.000000000 +0100
3 +++ asterisk-1.2.14/apps/app_sql_mysql.c 2006-12-27 09:00:04.000000000 +0100
4 @@ -0,0 +1,445 @@
5 +/*
6 + * Asterisk -- A telephony toolkit for Linux.
7 + *
8 + * Connect to MySQL
9 + *
10 + * Copyright (C) 2004, Constantine Filin and Christos Ricudis
11 + *
12 + * Christos Ricudis <ricudis@itc.auth.gr>
13 + * Constantine Filin <cf@intermedia.net>
14 + *
15 + * This program is free software, distributed under the terms of
16 + * the GNU General Public License
17 + */
18 +
19 +#include <stdlib.h>
20 +#include <unistd.h>
21 +#include <string.h>
22 +#include <stdlib.h>
23 +#include <sys/types.h>
24 +#include <stdio.h>
25 +#include <unistd.h>
26 +
27 +#include <mysql.h>
28 +
29 +#include <asterisk/file.h>
30 +#include <asterisk/logger.h>
31 +#include <asterisk/channel.h>
32 +#include <asterisk/pbx.h>
33 +#include <asterisk/module.h>
34 +#include <asterisk/linkedlists.h>
35 +#include <asterisk/chanvars.h>
36 +#include <asterisk/lock.h>
37 +
38 +#define EXTRA_LOG 0
39 +
40 +static char *tdesc = "Simple Mysql Interface";
41 +
42 +static char *app = "MYSQL";
43 +
44 +static char *synopsis = "Do several mySQLy things";
45 +
46 +static char *descrip =
47 +"MYSQL(): Do several mySQLy things\n"
48 +"Syntax:\n"
49 +" MYSQL(Connect connid dhhost dbuser dbpass dbname)\n"
50 +" Connects to a database. Arguments contain standard MySQL parameters\n"
51 +" passed to function mysql_real_connect. Connection identifer returned\n"
52 +" in ${var}\n"
53 +" MYSQL(Query resultid ${connid} query-string)\n"
54 +" Executes standard MySQL query contained in query-string using established\n"
55 +" connection identified by ${connection_identifier}. Result of query is\n"
56 +" is stored in ${var}.\n"
57 +" MYSQL(Fetch fetchid ${resultid} var1 var2 ... varN)\n"
58 +" Fetches a single row from a result set contained in ${result_identifier}.\n"
59 +" Assigns returned fields to ${var1} ... ${varn}. ${fetchid} is set TRUE\n"
60 +" if additional rows exist in result set.\n"
61 +" MYSQL(Clear ${resultid})\n"
62 +" Frees memory and datastructures associated with result set.\n"
63 +" MYSQL(Disconnect ${connid})\n"
64 +" Disconnects from named connection to MySQL.\n"
65 +" On exit, always returns 0. Sets MYSQL_STATUS to 0 on success and -1 on error.\n";
66 +
67 +/*
68 +EXAMPLES OF USE :
69 +
70 +exten => s,2,MYSQL(Connect connid localhost asterisk mypass credit)
71 +exten => s,3,MYSQL(Query resultid ${connid} SELECT username,credit FROM credit WHERE callerid=${CALLERIDNUM})
72 +exten => s,4,MYSQL(Fetch fetchid ${resultid} datavar1 datavar2)
73 +exten => s,5,GotoIf(${fetchid}?6:8)
74 +exten => s,6,Festival("User ${datavar1} currently has credit balance of ${datavar2} dollars.")
75 +exten => s,7,Goto(s,4)
76 +exten => s,8,MYSQL(Clear ${resultid})
77 +exten => s,9,MYSQL(Disconnect ${connid})
78 +*/
79 +
80 +STANDARD_LOCAL_USER;
81 +LOCAL_USER_DECL;
82 +
83 +AST_MUTEX_DEFINE_STATIC(_mysql_mutex);
84 +
85 +#define AST_MYSQL_ID_DUMMY 0
86 +#define AST_MYSQL_ID_CONNID 1
87 +#define AST_MYSQL_ID_RESID 2
88 +#define AST_MYSQL_ID_FETCHID 3
89 +
90 +struct ast_MYSQL_id {
91 + int identifier_type; /* 0=dummy, 1=connid, 2=resultid */
92 + int identifier;
93 + void *data;
94 + AST_LIST_ENTRY(ast_MYSQL_id) entries;
95 +} *ast_MYSQL_id;
96 +
97 +AST_LIST_HEAD(MYSQLidshead,ast_MYSQL_id) _mysql_ids_head;
98 +
99 +/* helpful procs */
100 +static void *find_identifier(int identifier,int identifier_type) {
101 + struct MYSQLidshead *headp;
102 + struct ast_MYSQL_id *i;
103 + void *res=NULL;
104 + int found=0;
105 +
106 + headp=&_mysql_ids_head;
107 +
108 + if (AST_LIST_LOCK(headp)) {
109 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
110 + } else {
111 + AST_LIST_TRAVERSE(headp,i,entries) {
112 + if ((i->identifier==identifier) && (i->identifier_type==identifier_type)) {
113 + found=1;
114 + res=i->data;
115 + break;
116 + }
117 + }
118 + if (!found) {
119 + ast_log(LOG_WARNING,"Identifier %d, identifier_type %d not found in identifier list\n",identifier,identifier_type);
120 + }
121 + AST_LIST_UNLOCK(headp);
122 + }
123 +
124 + return res;
125 +}
126 +
127 +static int add_identifier(int identifier_type,void *data) {
128 + struct ast_MYSQL_id *i,*j;
129 + struct MYSQLidshead *headp;
130 + int maxidentifier=0;
131 +
132 + headp=&_mysql_ids_head;
133 + i=NULL;
134 + j=NULL;
135 +
136 + if (AST_LIST_LOCK(headp)) {
137 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
138 + return(-1);
139 + } else {
140 + i=malloc(sizeof(struct ast_MYSQL_id));
141 + AST_LIST_TRAVERSE(headp,j,entries) {
142 + if (j->identifier>maxidentifier) {
143 + maxidentifier=j->identifier;
144 + }
145 + }
146 + i->identifier=maxidentifier+1;
147 + i->identifier_type=identifier_type;
148 + i->data=data;
149 + AST_LIST_INSERT_HEAD(headp,i,entries);
150 + AST_LIST_UNLOCK(headp);
151 + }
152 + return i->identifier;
153 +}
154 +
155 +static int del_identifier(int identifier,int identifier_type) {
156 + struct ast_MYSQL_id *i;
157 + struct MYSQLidshead *headp;
158 + int found=0;
159 +
160 + headp=&_mysql_ids_head;
161 +
162 + if (AST_LIST_LOCK(headp)) {
163 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
164 + } else {
165 + AST_LIST_TRAVERSE(headp,i,entries) {
166 + if ((i->identifier==identifier) &&
167 + (i->identifier_type==identifier_type)) {
168 + AST_LIST_REMOVE(headp,i,entries);
169 + free(i);
170 + found=1;
171 + break;
172 + }
173 + }
174 + AST_LIST_UNLOCK(headp);
175 + }
176 +
177 + if (found==0) {
178 + ast_log(LOG_WARNING,"Could not find identifier %d, identifier_type %d in list to delete\n",identifier,identifier_type);
179 + return(-1);
180 + } else {
181 + return(0);
182 + }
183 +}
184 +
185 +static int set_asterisk_int(struct ast_channel *chan, char *varname, int id) {
186 + if( id>=0 ) {
187 + char s[100] = "";
188 + snprintf(s, sizeof(s)-1, "%d", id);
189 +#if EXTRA_LOG
190 + ast_log(LOG_WARNING,"MYSQL: setting var '%s' to value '%s'\n",varname,s);
191 +#endif
192 + pbx_builtin_setvar_helper(chan,varname,s);
193 + }
194 + return id;
195 +}
196 +
197 +static int add_identifier_and_set_asterisk_int(struct ast_channel *chan, char *varname, int identifier_type, void *data) {
198 + return set_asterisk_int(chan,varname,add_identifier(identifier_type,data));
199 +}
200 +
201 +static int safe_scan_int( char** data, char* delim, int def ) {
202 + char* end;
203 + int res = def;
204 + char* s = strsep(data,delim);
205 + if( s ) {
206 + res = strtol(s,&end,10);
207 + if (*end) res = def; /* not an integer */
208 + }
209 + return res;
210 +}
211 +
212 +/* MYSQL operations */
213 +static int aMYSQL_connect(struct ast_channel *chan, char *data) {
214 +
215 + MYSQL *mysql;
216 +
217 + char *connid_var;
218 + char *dbhost;
219 + char *dbuser;
220 + char *dbpass;
221 + char *dbname;
222 +
223 + strsep(&data," "); // eat the first token, we already know it :P
224 +
225 + connid_var=strsep(&data," ");
226 + dbhost=strsep(&data," ");
227 + dbuser=strsep(&data," ");
228 + dbpass=strsep(&data," ");
229 + dbname=strsep(&data,"\n");
230 +
231 + if( connid_var && dbhost && dbuser && dbpass && dbname ) {
232 + mysql = mysql_init(NULL);
233 + if (mysql) {
234 + if (mysql_real_connect(mysql,dbhost,dbuser,dbpass,dbname,0,NULL,0)) {
235 + add_identifier_and_set_asterisk_int(chan,connid_var,AST_MYSQL_ID_CONNID,mysql);
236 + return 0;
237 + }
238 + else {
239 + ast_log(LOG_WARNING,"mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed\n",dbhost,dbuser,dbname);
240 + }
241 + }
242 + else {
243 + ast_log(LOG_WARNING,"myslq_init returned NULL\n");
244 + }
245 + }
246 + else {
247 + ast_log(LOG_WARNING,"MYSQL(connect is missing some arguments\n");
248 + }
249 +
250 + return -1;
251 +}
252 +
253 +static int aMYSQL_query(struct ast_channel *chan, char *data) {
254 +
255 + MYSQL *mysql;
256 + MYSQL_RES *mysqlres;
257 +
258 + char *resultid_var;
259 + int connid;
260 + char *querystring;
261 +
262 + strsep(&data," "); // eat the first token, we already know it :P
263 +
264 + resultid_var = strsep(&data," ");
265 + connid = safe_scan_int(&data," ",-1);
266 + querystring = strsep(&data,"\n");
267 +
268 + if (resultid_var && (connid>=0) && querystring) {
269 + if ((mysql=find_identifier(connid,AST_MYSQL_ID_CONNID))!=NULL) {
270 + mysql_query(mysql,querystring);
271 + if ((mysqlres=mysql_use_result(mysql))!=NULL) {
272 + add_identifier_and_set_asterisk_int(chan,resultid_var,AST_MYSQL_ID_RESID,mysqlres);
273 + return 0;
274 + }
275 + else if( mysql_field_count(mysql)==0 ) {
276 + return 0; // See http://dev.mysql.com/doc/mysql/en/mysql_field_count.html
277 + }
278 + else {
279 + ast_log(LOG_WARNING,"aMYSQL_query: mysql_store_result() failed on query %s\n",querystring);
280 + }
281 + }
282 + else {
283 + ast_log(LOG_WARNING,"aMYSQL_query: Invalid connection identifier %d passed in aMYSQL_query\n",connid);
284 + }
285 + }
286 + else {
287 + ast_log(LOG_WARNING,"aMYSQL_query: missing some arguments\n");
288 + }
289 +
290 + return -1;
291 +}
292 +
293 +
294 +static int aMYSQL_fetch(struct ast_channel *chan, char *data) {
295 +
296 + MYSQL_RES *mysqlres;
297 + MYSQL_ROW mysqlrow;
298 +
299 + char *fetchid_var,*s5,*s6;
300 + int resultid,numFields,j;
301 +
302 + strsep(&data," "); // eat the first token, we already know it :P
303 +
304 + fetchid_var = strsep(&data," ");
305 + resultid = safe_scan_int(&data," ",-1);
306 +
307 + if (fetchid_var && (resultid>=0) ) {
308 + if ((mysqlres=find_identifier(resultid,AST_MYSQL_ID_RESID))!=NULL) {
309 + /* Grab the next row */
310 + if ((mysqlrow=mysql_fetch_row(mysqlres))!=NULL) {
311 + numFields=mysql_num_fields(mysqlres);
312 + for (j=0;j<numFields;j++) {
313 + s5=strsep(&data," ");
314 + if (s5==NULL) {
315 + ast_log(LOG_WARNING,"ast_MYSQL_fetch: More fields (%d) than variables (%d)\n",numFields,j);
316 + break;
317 + }
318 + s6=mysqlrow[j];
319 + pbx_builtin_setvar_helper(chan,s5, s6 ? s6 : "NULL");
320 + }
321 +#ifdef EXTRA_LOG
322 + ast_log(LOG_WARNING,"ast_MYSQL_fetch: numFields=%d\n",numFields);
323 +#endif
324 + set_asterisk_int(chan,fetchid_var,1); // try more rows
325 + } else {
326 +#if EXTRA_LOG
327 + ast_log(LOG_WARNING,"ast_MYSQL_fetch : EOF\n");
328 +#endif
329 + set_asterisk_int(chan,fetchid_var,0); // no more rows
330 + }
331 + return 0;
332 + }
333 + else {
334 + ast_log(LOG_WARNING,"aMYSQL_fetch: Invalid result identifier %d passed\n",resultid);
335 + }
336 + }
337 + else {
338 + ast_log(LOG_WARNING,"aMYSQL_fetch: missing some arguments\n");
339 + }
340 +
341 + return -1;
342 +}
343 +
344 +static int aMYSQL_clear(struct ast_channel *chan, char *data) {
345 +
346 + MYSQL_RES *mysqlres;
347 +
348 + int id;
349 + strsep(&data," "); // eat the first token, we already know it :P
350 + id = safe_scan_int(&data," \n",-1);
351 + if ((mysqlres=find_identifier(id,AST_MYSQL_ID_RESID))==NULL) {
352 + ast_log(LOG_WARNING,"Invalid result identifier %d passed in aMYSQL_clear\n",id);
353 + } else {
354 + mysql_free_result(mysqlres);
355 + del_identifier(id,AST_MYSQL_ID_RESID);
356 + }
357 +
358 + return 0;
359 +}
360 +
361 +static int aMYSQL_disconnect(struct ast_channel *chan, char *data) {
362 +
363 + MYSQL *mysql;
364 + int id;
365 + strsep(&data," "); // eat the first token, we already know it :P
366 +
367 + id = safe_scan_int(&data," \n",-1);
368 + if ((mysql=find_identifier(id,AST_MYSQL_ID_CONNID))==NULL) {
369 + ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aMYSQL_disconnect\n",id);
370 + } else {
371 + mysql_close(mysql);
372 + del_identifier(id,AST_MYSQL_ID_CONNID);
373 + }
374 +
375 + return 0;
376 +}
377 +
378 +static int MYSQL_exec(struct ast_channel *chan, void *data)
379 +{
380 + struct localuser *u;
381 + int result;
382 + char sresult[10];
383 +
384 +#if EXTRA_LOG
385 + fprintf(stderr,"MYSQL_exec: data=%s\n",(char*)data);
386 +#endif
387 +
388 + if (!data) {
389 + ast_log(LOG_WARNING, "APP_MYSQL requires an argument (see manual)\n");
390 + return -1;
391 + }
392 +
393 + LOCAL_USER_ADD(u);
394 + result=0;
395 +
396 + ast_mutex_lock(&_mysql_mutex);
397 +
398 + if (strncasecmp("connect",data,strlen("connect"))==0) {
399 + result=aMYSQL_connect(chan,ast_strdupa(data));
400 + } else if (strncasecmp("query",data,strlen("query"))==0) {
401 + result=aMYSQL_query(chan,ast_strdupa(data));
402 + } else if (strncasecmp("fetch",data,strlen("fetch"))==0) {
403 + result=aMYSQL_fetch(chan,ast_strdupa(data));
404 + } else if (strncasecmp("clear",data,strlen("clear"))==0) {
405 + result=aMYSQL_clear(chan,ast_strdupa(data));
406 + } else if (strncasecmp("disconnect",data,strlen("disconnect"))==0) {
407 + result=aMYSQL_disconnect(chan,ast_strdupa(data));
408 + } else {
409 + ast_log(LOG_WARNING, "Unknown argument to MYSQL application : %s\n",(char *)data);
410 + result=-1;
411 + }
412 +
413 + ast_mutex_unlock(&_mysql_mutex);
414 +
415 + LOCAL_USER_REMOVE(u);
416 + snprintf(sresult, sizeof(sresult), "%d", result);
417 + pbx_builtin_setvar_helper(chan, "MYSQL_STATUS", sresult);
418 + return 0;
419 +}
420 +
421 +int unload_module(void)
422 +{
423 + STANDARD_HANGUP_LOCALUSERS;
424 + return ast_unregister_application(app);
425 +}
426 +
427 +int load_module(void)
428 +{
429 + struct MYSQLidshead *headp = &_mysql_ids_head;
430 + AST_LIST_HEAD_INIT(headp);
431 + return ast_register_application(app, MYSQL_exec, synopsis, descrip);
432 +}
433 +
434 +char *description(void)
435 +{
436 + return tdesc;
437 +}
438 +
439 +int usecount(void)
440 +{
441 + int res;
442 + STANDARD_USECOUNT(res);
443 + return res;
444 +}
445 +
446 +char *key()
447 +{
448 + return ASTERISK_GPL_KEY;
449 +}