Index: apps/app_meetme.c =================================================================== RCS file: /usr/cvsroot/asterisk/apps/app_meetme.c,v retrieving revision 1.61 diff -u -r1.61 app_meetme.c --- apps/app_meetme.c 2 Oct 2004 00:58:31 -0000 1.61 +++ apps/app_meetme.c 5 Oct 2004 21:12:09 -0000 @@ -5,7 +5,8 @@ * * Copyright (C) 1999-2004, Digium, Inc. * - * Mark Spencer + * Mark Spencer - Original Author + * Matthew Boehm - MySQL Patch * * This program is free software, distributed under the terms of * the GNU General Public License @@ -38,6 +39,21 @@ #include #endif /* __linux__ */ +#ifdef USEMYSQLCONF + #include + AST_MUTEX_DEFINE_STATIC( mysql_lock ); + #define CONFERENCE_TABLE "conference_rooms" + char dbuser[80]; + char dbpass[80]; + char dbhost[80]; + char dbname[80]; + char dbsock[100]; + int dbport = 0; + int connected = 0; + time_t connect_time = 0; + MYSQL mysql; +#endif + static char *tdesc = "MeetMe conference bridge"; static char *app = "MeetMe"; @@ -155,6 +171,71 @@ #define CONFFLAG_EXIT_CONTEXT (1 << 12) /* If set, the MeetMe will exit to the specified context */ #define CONFFLAG_MARKEDUSER (1 << 13) /* If set, the user will be marked */ +#ifdef USEMYSQLCONF +static void mysql_reconnect() +{ + if ((!connected) && (dbhost || dbsock) && dbuser && dbpass && dbname) { + mysql_init(&mysql); + if (mysql_real_connect(&mysql, dbhost, dbuser, dbpass, dbname, dbport, dbsock, 0)) { + ast_log(LOG_DEBUG, "MeetMe MySQL: Successfully connected to MySQL database.\n"); + connected = 1; + connect_time = time(NULL); + } else { + ast_log(LOG_ERROR, "MeetMe MySQL: Failed to connect database server %s on %s. Check debug for more info.\n", dbname, dbhost); + ast_log(LOG_DEBUG, "MeetMe MySQL: Cannot Connect: %s\n", mysql_error(&mysql)); + } + } else { + int error; + error = mysql_ping(&mysql); + + if (error != 0) { + connected = 0; + ast_log(LOG_ERROR, "MeetMe MySQL: Attempted to reconnect. Failed. Check debug for more info.\n"); + ast_log(LOG_DEBUG, "MeetMe MySQL: Server Error: %s\n", mysql_error(&mysql)); + } + ast_log(LOG_DEBUG, "MeetMe MySQL: Everything is fine.\n"); + connected = 1; + } +} + +static int show_meetme_mysql_status(int fd) +{ + mysql_reconnect(); + if (connected) { + char status[256], status2[100] = ""; + int ctime = time(NULL) - connect_time; + if (dbport) + snprintf(status, 255, "Connected to %s@%s, port %d", dbname, dbhost, dbport); + else if (dbsock) + snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock); + else + snprintf(status, 255, "Connected to %s@%s", dbname, dbhost); + + if (dbuser && *dbuser) + snprintf(status2, 99, " with username %s", dbuser); + if (ctime > 31536000) { + ast_cli(fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 31536000, (ctime % 31536000) / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60); + } else if (ctime > 86400) { + ast_cli(fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60); + } else if (ctime > 3600) { + ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60); + } else if (ctime > 60) { + ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60); + } else { + ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime); + } + /* if (records == totalrecords) + ast_cli(fd, " Wrote %d records since last restart.\n", totalrecords); + else + ast_cli(fd, " Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records); */ + + return RESULT_SUCCESS; + } else { + ast_cli(fd, "Not currently connected to a MySQL server.\n"); + return RESULT_FAILURE; + } +} +#endif static int careful_write(int fd, unsigned char *data, int len) { @@ -300,13 +381,16 @@ } if (argc == 1) { /* 'MeetMe': List all the conferences */ - now = time(NULL); + now = time(NULL); cnf = confs; if (!cnf) { - ast_cli(fd, "No active MeetMe conferences.\n"); - return RESULT_SUCCESS; - } - ast_cli(fd, header_format, "Conf Num", "Parties", "Marked", "Activity", "Creation"); + ast_cli(fd, "No active MeetMe conferences.\n"); +#ifdef USEMYSQLCONF + show_meetme_mysql_status(fd); +#endif + return RESULT_SUCCESS; + } + ast_cli(fd, header_format, "Conf Num", "Parties", "Marked", "Activity", "Creation"); while(cnf) { if (cnf->markedusers == 0) strncpy(cmdline, "N/A ", sizeof(cmdline) - 1); @@ -322,10 +406,12 @@ cnf = cnf->next; } ast_cli(fd, "* Total number of MeetMe users: %d\n", total); + show_meetme_mysql_status(fd); return RESULT_SUCCESS; } if (argc < 3) return RESULT_SHOWUSAGE; + strncpy(cmdline, argv[2], sizeof(cmdline) - 1); /* Argv 2: conference number */ if (strstr(argv[1], "lock")) { if (strcmp(argv[1], "lock") == 0) { @@ -1083,6 +1169,40 @@ cnf = build_conf(confno, "", make, dynamic); } } else { +#ifdef USEMYSQLCONF + MYSQL_RES *result; + MYSQL_ROW row; + char sql_query[150]; + + sprintf(sql_query,"SELECT conference_room, conference_pin FROM %s WHERE conference_room = %i LIMIT 1", CONFERENCE_TABLE, atoi(confno)); + ast_log(LOG_DEBUG,"MeetMe MySQL: SQL Cmd: %s\n", sql_query); + + mysql_reconnect(); + + ast_mutex_lock(&mysql_lock); + if(mysql_real_query(&mysql, sql_query, strlen(sql_query))) { + ast_log(LOG_WARNING, "MeetMe MySQL failed to query database. Check debug for more info.\n"); + ast_log(LOG_DEBUG, "MeetMe MySQL Error: %s\n", mysql_error(&mysql)); + ast_mutex_unlock(&mysql_lock); + return NULL; + } + + result = mysql_store_result(&mysql); + row = mysql_fetch_row(result); + ast_mutex_unlock(&mysql_lock); + + /* at this point, our row should contain the conference room and the conference pin. + if a pin is not warranted, it should be stored as NULL in the table. + now we can build a conference and return it. */ + + if(row[1]==NULL) { + cnf = build_conf(row[0], "", make, dynamic); + } else { + cnf = build_conf(row[0], row[1], make, dynamic); + } + + mysql_free_result(result); +#else /* Check the config */ cfg = ast_load("meetme.conf"); if (!cfg) { @@ -1113,6 +1233,7 @@ ast_log(LOG_DEBUG, "%s isn't a valid conference\n", confno); } ast_destroy(cfg); +#endif } } else if (dynamic_pin) { /* Correct for the user selecting 'D' instead of 'd' to have @@ -1517,6 +1638,10 @@ int unload_module(void) { +#ifdef USEMYSQLCONF + mysql_close(&mysql); +#endif + STANDARD_HANGUP_LOCALUSERS; ast_cli_unregister(&cli_show_confs); ast_cli_unregister(&cli_conf); @@ -1527,6 +1652,62 @@ int load_module(void) { +#ifdef USEMYSQLCONF + struct ast_config *cfg; + char *s; + + /* We will continue to use meetme.conf to store only database connectivity information */ + cfg = ast_load("meetme.conf"); + + if(cfg) { + if (!(s=ast_variable_retrieve(cfg, "general", "dbuser"))) { + strncpy(dbuser, "asterisk", sizeof(dbuser) - 1); + } else { + strncpy(dbuser, s, sizeof(dbuser) - 1); + } + + if (!(s=ast_variable_retrieve(cfg, "general", "dbpass"))) { + strncpy(dbpass, "asterisk", sizeof(dbpass) - 1); + } else { + strncpy(dbpass, s, sizeof(dbpass) - 1); + } + + if (!(s=ast_variable_retrieve(cfg, "general", "dbhost"))) { + strncpy(dbhost, "localhost", sizeof(dbhost) - 1); + } else { + strncpy(dbhost, s, sizeof(dbhost) - 1); + } + + if (!(s=ast_variable_retrieve(cfg, "general", "dbname"))) { + strncpy(dbname, "asterisk", sizeof(dbname) - 1); + } else { + strncpy(dbname, s, sizeof(dbname) - 1); + } + + if (!(s=ast_variable_retrieve(cfg, "general", "dbport"))) { + dbport = 3306; + } else { + dbport = atoi(s); + } + + if (!(s=ast_variable_retrieve(cfg, "general", "dbsock"))) { + dbsock[0] = '\0'; + } else { + strncpy(dbsock, s, sizeof(dbsock) - 1); + } + } + ast_destroy(cfg); + + ast_log(LOG_DEBUG, "MeetMe MySQL host: %s\n", dbhost); + ast_log(LOG_DEBUG, "MeetMe MySQL user: %s\n", dbuser); + ast_log(LOG_DEBUG, "MeetMe MySQL pass: %s\n", dbpass); + ast_log(LOG_DEBUG, "MeetMe MySQL db: %s\n", dbname); + ast_log(LOG_DEBUG, "MeetMe MySQL sock: %s\n", dbsock); + ast_log(LOG_DEBUG, "MeetMe MySQL port: %i\n", dbport); + + mysql_reconnect(); + +#endif ast_cli_register(&cli_show_confs); ast_cli_register(&cli_conf); ast_register_application(app3, admin_exec, synopsis3, descrip3);