Index: apps/app_voicemail.c =================================================================== --- apps/app_voicemail.c (revision 307395) +++ apps/app_voicemail.c (working copy) @@ -3419,7 +3419,8 @@ * This method is used when mailboxes are stored in an ODBC back end. * Typical use to set the msgnum would be to take the value returned from this method and add one to it. * - * \return the value of zero or greaterto indicate the last message index in use, -1 to indicate none. + * \return the value of zero or greater to indicate the last message index in use, -1 to indicate none. + */ static int last_message_index(struct ast_vm_user *vmu, char *dir) { @@ -3434,7 +3435,8 @@ struct odbc_obj *obj; obj = ast_odbc_request_obj(odbc_database, 0); if (obj) { - snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=?", odbc_table); + snprintf(sql, sizeof(sql), "SELECT msgnum FROM %s WHERE dir=? order by msgnum desc limit 1", odbc_table); + stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps); if (!stmt) { ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql); @@ -3443,7 +3445,12 @@ } res = SQLFetch(stmt); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { - ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql); + if (res == SQL_NO_DATA) { + ast_log(AST_LOG_DEBUG, "Directory '%s' has no messages and therefore no index was retrieved.\n", dir); + } else { + ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql); + } + SQLFreeHandle (SQL_HANDLE_STMT, stmt); ast_odbc_release_obj(obj); goto yuck; @@ -3456,12 +3463,13 @@ goto yuck; } if (sscanf(rowdata, "%30d", &x) != 1) - ast_log(AST_LOG_WARNING, "Failed to read message count!\n"); + ast_log(AST_LOG_WARNING, "Failed to read message index!\n"); SQLFreeHandle (SQL_HANDLE_STMT, stmt); ast_odbc_release_obj(obj); + return x; } else ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database); -yuck: +yuck: return x - 1; } @@ -3516,25 +3524,63 @@ ast_odbc_release_obj(obj); } else ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database); -yuck: +yuck: return x; } /*! - * \brief returns the one-based count for messages. + * \brief returns the number of messages found. * \param vmu * \param dir the folder the mailbox folder to look for messages. Used to construct the SQL where clause. * * This method is used when mailboxes are stored in an ODBC back end. - * The message index is zero-based, the first message will be index 0. For convenient display it is good to have the - * one-based messages. - * This method just calls last_message_index and returns +1 of its value. * - * \return the value greater than zero on success to indicate the one-based count of messages, less than zero on error. + * \return The count of messages being zero or more, less than zero on error. */ static int count_messages(struct ast_vm_user *vmu, char *dir) { - return last_message_index(vmu, dir) + 1; + int x = 0; + int res; + SQLHSTMT stmt; + char sql[PATH_MAX]; + char rowdata[20]; + char *argv[] = { dir }; + struct generic_prepare_struct gps = { .sql = sql, .argc = 1, .argv = argv }; + + struct odbc_obj *obj; + obj = ast_odbc_request_obj(odbc_database, 0); + if (obj) { + snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=?", odbc_table); + stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps); + if (!stmt) { + ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql); + ast_odbc_release_obj(obj); + goto yuck; + } + res = SQLFetch(stmt); + if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql); + SQLFreeHandle (SQL_HANDLE_STMT, stmt); + ast_odbc_release_obj(obj); + goto yuck; + } + res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL); + if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { + ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql); + SQLFreeHandle (SQL_HANDLE_STMT, stmt); + ast_odbc_release_obj(obj); + goto yuck; + } + if (sscanf(rowdata, "%30d", &x) != 1) + ast_log(AST_LOG_WARNING, "Failed to read message count!\n"); + SQLFreeHandle (SQL_HANDLE_STMT, stmt); + ast_odbc_release_obj(obj); + return x; + } else + ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database); +yuck: + return x - 1; + } /*! @@ -3923,6 +3969,8 @@ DIR *msgdir; struct dirent *msgdirent; int msgdirint; + char extension[4]; + int stopcount = 0; /* Reading the entire directory into a file map scales better than * doing a stat repeatedly on a predicted sequence. I suspect this @@ -3933,14 +3981,20 @@ } while ((msgdirent = readdir(msgdir))) { - if (sscanf(msgdirent->d_name, "msg%30d", &msgdirint) == 1 && msgdirint < MAXMSGLIMIT) + if (sscanf(msgdirent->d_name, "msg%30d.%3s", &msgdirint, extension) == 2 && !strcmp(extension, "txt") && msgdirint < MAXMSGLIMIT) { map[msgdirint] = 1; + stopcount++; + ast_debug(4, "%s map[%d] = %d, count = %d\n", dir, msgdirint, map[msgdirint], stopcount); + } } closedir(msgdir); for (x = 0; x < vmu->maxmsg; x++) { - if (map[x] == 0) + if (map[x] == 1) { + stopcount--; + } else if (map[x] == 0 && !stopcount) { break; + } } return x - 1; @@ -6013,6 +6067,36 @@ return res; } +#if !defined(IMAP_STORAGE) +static int resequence_mailbox(struct ast_vm_user *vmu, char *dir, int stopcount) +{ + /* we know the actual number of messages, so stop process when number is hit */ + + int x, dest; + char sfn[PATH_MAX]; + char dfn[PATH_MAX]; + + if (vm_lock_path(dir)) + return ERROR_LOCK_PATH; + + for (x = 0, dest = 0; dest != stopcount && x < vmu->maxmsg + 10; x++) { + make_file(sfn, sizeof(sfn), dir, x); + if (EXISTS(dir, x, sfn, NULL)) { + + if (x != dest) { + make_file(dfn, sizeof(dfn), dir, dest); + RENAME(dir, x, vmu->mailbox, vmu->context, dir, dest, sfn, dfn); + } + + dest++; + } + } + ast_unlock_path(dir); + + return dest; +} +#endif + static int say_and_wait(struct ast_channel *chan, int num, const char *language) { int d; @@ -7724,7 +7808,8 @@ if (last_msg < -1) { return last_msg; } else if (vms->lastmsg != last_msg) { - ast_log(LOG_NOTICE, "Mailbox: %s, expected %d but found %d message(s) in box with max threshold of %d.\n", vms->curdir, last_msg + 1, vms->lastmsg + 1, vmu->maxmsg); + ast_log(LOG_NOTICE, "Resequencing Mailbox: %s, expected %d but found %d message(s) in box with max threshold of %d.\n", vms->curdir, last_msg + 1, vms->lastmsg + 1, vmu->maxmsg); + resequence_mailbox(vmu, vms->curdir, count_msg); } return 0; @@ -7734,7 +7819,9 @@ static int close_mailbox(struct vm_state *vms, struct ast_vm_user *vmu) { int x = 0; + #ifndef IMAP_STORAGE + int last_msg_idx; int res = 0, nummsg; char fn2[PATH_MAX]; #endif @@ -7750,8 +7837,14 @@ return ERROR_LOCK_PATH; } + /* update count as message may have arrived while we've got mailbox open */ + last_msg_idx = last_message_index(vmu, vms->curdir); + if (last_msg_idx != vms->lastmsg) { + ast_log(AST_LOG_NOTICE, "%d messages received after mailbox opened.\n", last_msg_idx - vms->lastmsg); + } + /* must check up to last detected message, just in case it is erroneously greater than maxmsg */ - for (x = 0; x < vms->lastmsg + 1; x++) { + for (x = 0; x < last_msg_idx + 1; x++) { if (!vms->deleted[x] && ((strcasecmp(vms->curbox, "INBOX") && strcasecmp(vms->curbox, "Urgent")) || !vms->heard[x] || (vms->heard[x] && !ast_test_flag(vmu, VM_MOVEHEARD)))) { /* Save this message. It's not in INBOX or hasn't been heard */ make_file(vms->fn, sizeof(vms->fn), vms->curdir, x);