1 diff -ruN asterisk-1.2.0-old/apps/app_sql_mysql.c asterisk-1.2.0-new/apps/app_sql_mysql.c
2 --- asterisk-1.2.0-old/apps/app_sql_mysql.c 1970-01-01 01:00:00.000000000 +0100
3 +++ asterisk-1.2.0-new/apps/app_sql_mysql.c 2005-06-07 18:36:28.000000000 +0200
6 + * Asterisk -- A telephony toolkit for Linux.
10 + * Copyright (C) 2004, Constantine Filin and Christos Ricudis
12 + * Christos Ricudis <ricudis@itc.auth.gr>
13 + * Constantine Filin <cf@intermedia.net>
15 + * This program is free software, distributed under the terms of
16 + * the GNU General Public License
23 +#include <sys/types.h>
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>
40 +static char *tdesc = "Simple Mysql Interface";
42 +static char *app = "MYSQL";
44 +static char *synopsis = "Do several mySQLy things";
46 +static char *descrip =
47 +"MYSQL(): Do several mySQLy things\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"
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";
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})
83 +AST_MUTEX_DEFINE_STATIC(_mysql_mutex);
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
90 +struct ast_MYSQL_id {
91 + int identifier_type; /* 0=dummy, 1=connid, 2=resultid */
94 + AST_LIST_ENTRY(ast_MYSQL_id) entries;
97 +AST_LIST_HEAD(MYSQLidshead,ast_MYSQL_id) _mysql_ids_head;
100 +static void *find_identifier(int identifier,int identifier_type) {
101 + struct MYSQLidshead *headp;
102 + struct ast_MYSQL_id *i;
106 + headp=&_mysql_ids_head;
108 + if (AST_LIST_LOCK(headp)) {
109 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
111 + AST_LIST_TRAVERSE(headp,i,entries) {
112 + if ((i->identifier==identifier) && (i->identifier_type==identifier_type)) {
119 + ast_log(LOG_WARNING,"Identifier %d, identifier_type %d not found in identifier list\n",identifier,identifier_type);
121 + AST_LIST_UNLOCK(headp);
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;
132 + headp=&_mysql_ids_head;
136 + if (AST_LIST_LOCK(headp)) {
137 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
140 + i=malloc(sizeof(struct ast_MYSQL_id));
141 + AST_LIST_TRAVERSE(headp,j,entries) {
142 + if (j->identifier>maxidentifier) {
143 + maxidentifier=j->identifier;
146 + i->identifier=maxidentifier+1;
147 + i->identifier_type=identifier_type;
149 + AST_LIST_INSERT_HEAD(headp,i,entries);
150 + AST_LIST_UNLOCK(headp);
152 + return i->identifier;
155 +static int del_identifier(int identifier,int identifier_type) {
156 + struct ast_MYSQL_id *i;
157 + struct MYSQLidshead *headp;
160 + headp=&_mysql_ids_head;
162 + if (AST_LIST_LOCK(headp)) {
163 + ast_log(LOG_WARNING,"Unable to lock identifiers list\n");
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);
174 + AST_LIST_UNLOCK(headp);
178 + ast_log(LOG_WARNING,"Could not find identifier %d, identifier_type %d in list to delete\n",identifier,identifier_type);
185 +static int set_asterisk_int(struct ast_channel *chan, char *varname, int id) {
188 + snprintf(s, sizeof(s)-1, "%d", id);
190 + ast_log(LOG_WARNING,"MYSQL: setting var '%s' to value '%s'\n",varname,s);
192 + pbx_builtin_setvar_helper(chan,varname,s);
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));
201 +static int safe_scan_int( char** data, char* delim, int def ) {
204 + char* s = strsep(data,delim);
206 + res = strtol(s,&end,10);
207 + if (*end) res = def; /* not an integer */
212 +/* MYSQL operations */
213 +static int aMYSQL_connect(struct ast_channel *chan, char *data) {
223 + strsep(&data," "); // eat the first token, we already know it :P
225 + connid_var=strsep(&data," ");
226 + dbhost=strsep(&data," ");
227 + dbuser=strsep(&data," ");
228 + dbpass=strsep(&data," ");
229 + dbname=strsep(&data,"\n");
231 + if( connid_var && dbhost && dbuser && dbpass && dbname ) {
232 + mysql = mysql_init(NULL);
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);
239 + ast_log(LOG_WARNING,"mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed\n",dbhost,dbuser,dbname);
243 + ast_log(LOG_WARNING,"myslq_init returned NULL\n");
247 + ast_log(LOG_WARNING,"MYSQL(connect is missing some arguments\n");
253 +static int aMYSQL_query(struct ast_channel *chan, char *data) {
256 + MYSQL_RES *mysqlres;
258 + char *resultid_var;
262 + strsep(&data," "); // eat the first token, we already know it :P
264 + resultid_var = strsep(&data," ");
265 + connid = safe_scan_int(&data," ",-1);
266 + querystring = strsep(&data,"\n");
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);
275 + else if( mysql_field_count(mysql)==0 ) {
276 + return 0; // See http://dev.mysql.com/doc/mysql/en/mysql_field_count.html
279 + ast_log(LOG_WARNING,"aMYSQL_query: mysql_store_result() failed on query %s\n",querystring);
283 + ast_log(LOG_WARNING,"aMYSQL_query: Invalid connection identifier %d passed in aMYSQL_query\n",connid);
287 + ast_log(LOG_WARNING,"aMYSQL_query: missing some arguments\n");
294 +static int aMYSQL_fetch(struct ast_channel *chan, char *data) {
296 + MYSQL_RES *mysqlres;
297 + MYSQL_ROW mysqlrow;
299 + char *fetchid_var,*s5,*s6;
300 + int resultid,numFields,j;
302 + strsep(&data," "); // eat the first token, we already know it :P
304 + fetchid_var = strsep(&data," ");
305 + resultid = safe_scan_int(&data," ",-1);
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," ");
315 + ast_log(LOG_WARNING,"ast_MYSQL_fetch: More fields (%d) than variables (%d)\n",numFields,j);
319 + pbx_builtin_setvar_helper(chan,s5, s6 ? s6 : "NULL");
322 + ast_log(LOG_WARNING,"ast_MYSQL_fetch: numFields=%d\n",numFields);
324 + set_asterisk_int(chan,fetchid_var,1); // try more rows
327 + ast_log(LOG_WARNING,"ast_MYSQL_fetch : EOF\n");
329 + set_asterisk_int(chan,fetchid_var,0); // no more rows
334 + ast_log(LOG_WARNING,"aMYSQL_fetch: Invalid result identifier %d passed\n",resultid);
338 + ast_log(LOG_WARNING,"aMYSQL_fetch: missing some arguments\n");
344 +static int aMYSQL_clear(struct ast_channel *chan, char *data) {
346 + MYSQL_RES *mysqlres;
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);
354 + mysql_free_result(mysqlres);
355 + del_identifier(id,AST_MYSQL_ID_RESID);
361 +static int aMYSQL_disconnect(struct ast_channel *chan, char *data) {
365 + strsep(&data," "); // eat the first token, we already know it :P
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);
371 + mysql_close(mysql);
372 + del_identifier(id,AST_MYSQL_ID_CONNID);
378 +static int MYSQL_exec(struct ast_channel *chan, void *data)
380 + struct localuser *u;
385 + fprintf(stderr,"MYSQL_exec: data=%s\n",(char*)data);
389 + ast_log(LOG_WARNING, "APP_MYSQL requires an argument (see manual)\n");
396 + ast_mutex_lock(&_mysql_mutex);
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));
409 + ast_log(LOG_WARNING, "Unknown argument to MYSQL application : %s\n",(char *)data);
413 + ast_mutex_unlock(&_mysql_mutex);
415 + LOCAL_USER_REMOVE(u);
416 + snprintf(sresult, sizeof(sresult), "%d", result);
417 + pbx_builtin_setvar_helper(chan, "MYSQL_STATUS", sresult);
421 +int unload_module(void)
423 + STANDARD_HANGUP_LOCALUSERS;
424 + return ast_unregister_application(app);
427 +int load_module(void)
429 + struct MYSQLidshead *headp = &_mysql_ids_head;
430 + AST_LIST_HEAD_INIT(headp);
431 + return ast_register_application(app, MYSQL_exec, synopsis, descrip);
434 +char *description(void)
442 + STANDARD_USECOUNT(res);
448 + return ASTERISK_GPL_KEY;