/* * Copyright (C) 2002-2003, Bill Heckel * * Written by Bill Heckel though many of the * bits used are recycled... * * This program is free software, distributed under the terms of * the GNU General Public License */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DATE_FORMAT "%Y-%m-%d %T" static char *tdesc = "loginACD"; static char *app = "loginACD"; static char *synopsis = "loginACD"; static char *descrip = " loginACD(): logs a user in or out of the ACD system\n" "usually returns 0\n"; STANDARD_LOCAL_USER; LOCAL_USER_DECL; int acdlogin_exec(struct ast_channel *chan, void *data); int acdlogin_exec(struct ast_channel *chan, void *data) { struct localuser *u; MYSQL myConn ; MYSQL_RES * res ; MYSQL_ROW row ; int password=0; char status[2]="N"; char agent[6]=""; char pass[6]=""; if (ast_streamfile(chan, "login-agent", chan->language)) { ast_log(LOG_WARNING, "Unable to stream login-agent file\n"); return 0; } if (ast_readstring(chan, agent, sizeof(agent) - 1, 2000, 10000, "#") < 0) { ast_log(LOG_WARNING, "Couldn't read extension\n"); } if (data) { if (strchr(data,'Y')) { status[0]='Y'; } // do we have a 'Y' if (strchr(data,'P')) { password=1; } // do we need a password } if (password) { if (ast_streamfile(chan, "vm-password", chan->language)) { ast_log(LOG_WARNING, "Unable to stream vm-password file\n"); return 0; } if (ast_readstring(chan, pass, sizeof(pass) - 1, 2000, 10000, "#") < 0) { ast_log(LOG_WARNING, "Couldn't read password\n"); } } ast_verbose(" ACDlogin entered with %s %s %s\n",agent,(char *)data,pass); LOCAL_USER_ADD(u); mysql_init(&myConn); if (!mysql_real_connect(&myConn,0,"root","","acd", 0,0, 0)) { ast_log(LOG_ERROR,"Failed to connect to MySQL\n"); return 0; // we can't do anything without mysql } char sqlquery[128]; sprintf(sqlquery,"update agent set active='%s', chactive=now() where agentid='%s'",status,agent); if (mysql_query(&myConn,sqlquery)) { ast_log(LOG_ERROR,"Failed to run MySQL query.\n"); } // now we check it sprintf(sqlquery,"select active from agent where agentid='%s'",agent); if (mysql_query(&myConn,sqlquery)) { ast_log(LOG_ERROR,"Failed to run MySQL query.\n"); } else { if (ast_streamfile(chan, "login-agent", chan->language)) { ast_log(LOG_WARNING, "Unable to stream login-agent file\n"); } else ast_waitstream(chan,""); ast_say_digit_str(chan,agent,"",chan->language); res = mysql_store_result( &myConn ); if ((row = mysql_fetch_row( res ))) { ast_verbose(" ACD_Login: Set agent %s to %s\n",agent,status); if (row[0][0]=='Y') { if (ast_streamfile(chan, "login-yes", chan->language)) { ast_log(LOG_WARNING, "Unable to stream login-yes file\n"); } else ast_waitstream(chan,""); } else { if (ast_streamfile(chan, "login-no", chan->language)) { ast_log(LOG_WARNING, "Unable to stream login-no file\n"); } else ast_waitstream(chan,""); } } else { ast_streamfile(chan, "login-invalid", chan->language); ast_waitstream(chan,""); } mysql_free_result(res); } mysql_close(&myConn); LOCAL_USER_REMOVE(u); return 0; } int unload_module(void) { STANDARD_HANGUP_LOCALUSERS; return ast_unregister_application(app); } char *description(void) { return tdesc; } int usecount(void) { int res; STANDARD_USECOUNT(res); return res; } char *key() { return ASTERISK_GPL_KEY; } int load_module(void) { int res; MYSQL *mysql; mysql = mysql_init(NULL); mysql = mysql_real_connect(mysql,"localhost","root","","acd", 0, NULL, 0); if (mysql == NULL) { ast_log(LOG_ERROR, "Failed to connect to mysql database.\n"); return -1; } else { ast_log(LOG_DEBUG,"Successfully connected to MySQL database.\n"); } mysql_close(mysql); res = ast_register_application(app, acdlogin_exec, synopsis, descrip); if (res) { ast_log(LOG_ERROR, "Unable to register ACD login app\n"); } return res; }