Index: apps/app_voicemail.c
===================================================================
--- apps/app_voicemail.c        (revision 12924)
+++ apps/app_voicemail.c        (working copy)
@@ -1982,7 +1982,6 @@
 #ifdef USE_ODBC_STORAGE
 static int messagecount(const char *mailbox, int *newmsgs, int *oldmsgs)
 {
-       int x = 0;
        int res;
        SQLHSTMT stmt;
        char sql[256];
@@ -1998,8 +1997,28 @@
         if (ast_strlen_zero(mailbox))
                 return 0;

-        ast_copy_string(tmp, mailbox, sizeof(tmp));
-
+       if (strchr(mailbox, ',')) {
+                int tmpnew, tmpold;
+               char *mb, *cur;
+
+               ast_copy_string(tmp, mailbox, sizeof(tmp));
+               mb = tmp;
+               while((cur = strsep(&mb, ", "))) {
+                       if (!ast_strlen_zero(cur)) {
+                               if (messagecount(cur, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
+                                       return -1;
+                               else {
+                                       if (newmsgs)
+                                               *newmsgs += tmpnew;
+                                       if (oldmsgs)
+                                               *oldmsgs += tmpold;
+                               }
+                       }
+               }
+               return 0;
+       }
+
+        ast_copy_string(tmp, mailbox, sizeof(tmp));
        context = strchr(tmp, '@');
         if (context) {
                 *context = '\0';
@@ -2010,77 +2029,82 @@
        odbc_obj *obj;
        obj = fetch_odbc_obj(odbc_database, 0);
        if (obj) {
-               res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
-               if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-                       ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
-                       goto yuck;
-               }
-               snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir LIKE '%%%s/%s/%s'", odbc_table, context, tmp, "INBOX");
-               res = SQLPrepare(stmt, sql, SQL_NTS);
-               if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-                       ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
+               if (newmsgs) {
+                       res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
+                       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+                               ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
+                               goto yuck;
+                       }
+                       snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir LIKE '%%%s/%s/%s'", odbc_table, context, tmp, "INBOX");
+                       res = SQLPrepare(stmt, sql, SQL_NTS);
+                       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+                               ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
+                               SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+                               goto yuck;
+                       }
+                       res = odbc_smart_execute(obj, stmt);
+                       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+                               ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
+                               SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+                               goto yuck;
+                       }
+                       res = SQLFetch(stmt);
+                       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+                               ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
+                               SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+                               goto yuck;
+                       }
+                       res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
+                       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+                               ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
+                               SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+                               goto yuck;
+                       }
+                       *newmsgs = atoi(rowdata);
                        SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-                       goto yuck;
-               }
-               res = odbc_smart_execute(obj, stmt);
-               if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-                       ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
+               }
+               if (oldmsgs) {
+                       res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
+                       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+                               ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
+                               goto yuck;
+                       }
+                       snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir like '%%%s/%s/%s'", odbc_table, context, tmp, "Old");
+                       res = SQLPrepare(stmt, sql, SQL_NTS);
+                       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+                               ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
+                               SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+                               goto yuck;
+                       }
+                       res = odbc_smart_execute(obj, stmt);
+                       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+                               ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
+                               SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+                               goto yuck;
+                       }
+                       res = SQLFetch(stmt);
+                       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+                               ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
+                               SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+                               goto yuck;
+                       }
+                       res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
+                       if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+                               ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
+                               SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+                               goto yuck;
+                       }
                        SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-                       goto yuck;
-               }
-               res = SQLFetch(stmt);
-               if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-                       ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
-                       SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-                       goto yuck;
-               }
-               res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
-               if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-                       ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
-                       SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-                       goto yuck;
-               }
-               *newmsgs = atoi(rowdata);
-               SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+                       *oldmsgs = atoi(rowdata);
+               }

-               res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
-               if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-                       ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
-                       goto yuck;
-               }
-               snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir like '%%%s/%s/%s'", odbc_table, context, tmp, "Old");
-               res = SQLPrepare(stmt, sql, SQL_NTS);
-               if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-                       ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
-                       SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-                       goto yuck;
-               }
-               res = odbc_smart_execute(obj, stmt);
-               if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-                       ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
-                       SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-                       goto yuck;
-               }
-               res = SQLFetch(stmt);
-               if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-                       ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
-                       SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-                       goto yuck;
-               }
-               res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
-               if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-                       ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
-                       SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-                       goto yuck;
-               }
-               SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-               *oldmsgs = atoi(rowdata);
-               x = 1;
+         return 0;
+
        } else
                ast_log(LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);

 yuck:
-       return x;
+       return -1;
 }

 static int has_voicemail(const char *mailbox, const char *folder)
