Review Board 1.7.16


Voicemail check cleanup

Review Request #525 - Created Feb. 23, 2010 and submitted

Tilghman Lesher
/trunk
15654, 16448
Reviewers
asterisk-dev
Asterisk
A few cleanups to fix a few bugs.

- Urgent voicemails were not attached, because the attachment code looked in the wrong folder.
- Urgent voicemails were sometimes counted twice when displaying the count of new messages.
- Backends were inconsistent as to which voicemails each API counted.
Unit test written, tested, and included.

Diff revision 2

This is not the most recent revision of the diff. The latest diff is revision 3. See what's changed.

1 2 3
1 2 3

  1. /trunk/apps/app_voicemail.c: Loading...
/trunk/apps/app_voicemail.c
Revision 248394 New Change
[20] 112 lines
[+20] [+] ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
113
#include "asterisk/stringfields.h"
113
#include "asterisk/stringfields.h"
114
#include "asterisk/smdi.h"
114
#include "asterisk/smdi.h"
115
#include "asterisk/astobj2.h"
115
#include "asterisk/astobj2.h"
116
#include "asterisk/event.h"
116
#include "asterisk/event.h"
117
#include "asterisk/taskprocessor.h"
117
#include "asterisk/taskprocessor.h"

    
   
118
#include "asterisk/test.h"
118

    
   
119

   
119
#ifdef ODBC_STORAGE
120
#ifdef ODBC_STORAGE
120
#include "asterisk/res_odbc.h"
121
#include "asterisk/res_odbc.h"
121
#endif
122
#endif
122

    
   
123

   
[+20] [20] 1768 lines
[+20] [+] static int folder_int(const char *folder)
1891
		return 9;
1892
		return 9;
1892
	else /*assume they meant INBOX if folder is not found otherwise*/
1893
	else /*assume they meant INBOX if folder is not found otherwise*/
1893
		return 0;
1894
		return 0;
1894
}
1895
}
1895

    
   
1896

   
1896
/*!
1897
static int __messagecount(const char *context, const char *mailbox, const char *folder)
1897
 * \brief Gets the number of messages that exist in a mailbox folder.

   
1898
 * \param context

   
1899
 * \param mailbox

   
1900
 * \param folder

   
1901
 * 

   
1902
 * This method is used when IMAP backend is used.

   
1903
 * \return The number of messages in this mailbox folder (zero or more).

   
1904
 */

   
1905
static int messagecount(const char *context, const char *mailbox, const char *folder)

   
1906
{
1898
{
1907
	SEARCHPGM *pgm;
1899
	SEARCHPGM *pgm;
1908
	SEARCHHEADER *hdr;
1900
	SEARCHHEADER *hdr;
1909

    
   
1901

   
1910
	struct ast_vm_user *vmu, vmus;
1902
	struct ast_vm_user *vmu, vmus;
[+20] [20] 107 lines
[+20] static int folder_int(const char *folder) static int __messagecount(const char *context, const char *mailbox, const char *folder)
2018
		ast_mutex_unlock(&vms_p->lock);
2010
		ast_mutex_unlock(&vms_p->lock);
2019
	}
2011
	}
2020
	return 0;
2012
	return 0;
2021
}
2013
}
2022

    
   
2014

   

    
   
2015
/*!
Moved from 1897

    
   
2016
 * \brief Gets the number of messages that exist in a mailbox folder.
Moved from 1898

    
   
2017
 * \param context
Moved from 1899

    
   
2018
 * \param mailbox
Moved from 1900

    
   
2019
 * \param folder
Moved from 1901

    
   
2020
 * 
Moved from 1902

    
   
2021
 * This method is used when IMAP backend is used.
Moved from 1903

    
   
2022
 * \return The number of messages in this mailbox folder (zero or more).
Moved from 1904

    
   
2023
 */
Moved from 1905

    
   
2024
static int messagecount(const char *context, const char *mailbox, const char *folder)

    
   
2025
{

    
   
2026
	if (!strcmp(folder, "INBOX")) {

    
   
2027
		return __messagecount(context, mailbox, "INBOX") + __messagecount(context, mailbox, "Urgent");

    
   
2028
	} else {

    
   
2029
		return __messagecount(context, mailbox, folder);

    
   
2030
	}

    
   
2031
}

    
   
2032

   
2023
static int imap_store_file(char *dir, char *mailboxuser, char *mailboxcontext, int msgnum, struct ast_channel *chan, struct ast_vm_user *vmu, char *fmt, int duration, struct vm_state *vms, const char *flag)
2033
static int imap_store_file(char *dir, char *mailboxuser, char *mailboxcontext, int msgnum, struct ast_channel *chan, struct ast_vm_user *vmu, char *fmt, int duration, struct vm_state *vms, const char *flag)
2024
{
2034
{
2025
	char *myserveremail = serveremail;
2035
	char *myserveremail = serveremail;
2026
	char fn[PATH_MAX];
2036
	char fn[PATH_MAX];
2027
	char introfn[PATH_MAX];
2037
	char introfn[PATH_MAX];
[+20] [20] 180 lines
[+20] [+] static int inboxcount2(const char *mailbox_context, int *urgentmsgs, int *newmsgs, int *oldmsgs)
2208
		struct ast_vm_user *vmu = find_user(NULL, context, mailboxnc);
2218
		struct ast_vm_user *vmu = find_user(NULL, context, mailboxnc);
2209
		if (!vmu) {
2219
		if (!vmu) {
2210
			ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailboxnc, context);
2220
			ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailboxnc, context);
2211
			return -1;
2221
			return -1;
2212
		}
2222
		}
2213
		if ((*newmsgs = messagecount(context, mailboxnc, vmu->imapfolder)) < 0)
2223
		if ((*newmsgs = __messagecount(context, mailboxnc, vmu->imapfolder)) < 0) {
2214
			return -1;
2224
			return -1;
2215
	}
2225
		}

    
   
2226
	}
2216
	if (oldmsgs) {
2227
	if (oldmsgs) {
2217
		if ((*oldmsgs = messagecount(context, mailboxnc, "Old")) < 0)
2228
		if ((*oldmsgs = __messagecount(context, mailboxnc, "Old")) < 0) {
2218
			return -1;
2229
			return -1;
2219
	}
2230
		}

    
   
2231
	}
2220
	if (urgentmsgs) {
2232
	if (urgentmsgs) {
2221
		if((*urgentmsgs = messagecount(context, mailboxnc, "Urgent")) < 0)
2233
		if ((*urgentmsgs = __messagecount(context, mailboxnc, "Urgent")) < 0) {
2222
			return -1;
2234
			return -1;
2223
	}
2235
		}
2224
	return 0;

   
2225
}
2236
	}
2226

    
   
2237
	return 0;
2227
static int inboxcount(const char *mailbox_context, int *newmsgs, int *oldmsgs)

   
2228
{

   
2229
	return inboxcount2(mailbox_context, NULL, newmsgs, oldmsgs);

   
2230
}
2238
}
2231

    
   
2239

   
2232
/** 
2240
/** 
2233
 * \brief Determines if the given folder has messages.
2241
 * \brief Determines if the given folder has messages.
2234
 * \param mailbox The @ delimited string for user@context. If no context is found, uses 'default' for the context.
2242
 * \param mailbox The @ delimited string for user@context. If no context is found, uses 'default' for the context.
[+20] [20] 7 lines
[+20] static int inboxcount(const char *mailbox_context, int *newmsgs, int *oldmsgs) static int inboxcount2(const char *mailbox_context, int *urgentmsgs, int *newmsgs, int *oldmsgs)
2242
static int has_voicemail(const char *mailbox, const char *folder)
2250
static int has_voicemail(const char *mailbox, const char *folder)
2243
{
2251
{
2244
	char tmp[256], *tmp2, *box, *context;
2252
	char tmp[256], *tmp2, *box, *context;
2245
	ast_copy_string(tmp, mailbox, sizeof(tmp));
2253
	ast_copy_string(tmp, mailbox, sizeof(tmp));
2246
	tmp2 = tmp;
2254
	tmp2 = tmp;
2247
	if (strchr(tmp2, ',')) {
2255
	if (strchr(tmp2, ',') || strchr(tmp2, '&')) {
2248
		while ((box = strsep(&tmp2, ","))) {
2256
		while ((box = strsep(&tmp2, ",&"))) {
2249
			if (!ast_strlen_zero(box)) {
2257
			if (!ast_strlen_zero(box)) {
2250
				if (has_voicemail(box, folder))
2258
				if (has_voicemail(box, folder)) {
2251
					return 1;
2259
					return 1;
2252
			}
2260
				}
2253
		}
2261
			}
2254
	}
2262
		}
2255
	if ((context = strchr(tmp, '@')))
2263
	}

    
   
2264
	if ((context = strchr(tmp, '@'))) {
2256
		*context++ = '\0';
2265
		*context++ = '\0';
2257
	else
2266
	} else {
2258
		context = "default";
2267
		context = "default";
2259
	return messagecount(context, tmp, folder) ? 1 : 0;

   
2260
}
2268
	}

    
   
2269
	return __messagecount(context, tmp, folder) ? 1 : 0;

    
   
2270
}
2261

    
   
2271

   
2262
/*!
2272
/*!
2263
 * \brief Copies a message from one mailbox to another.
2273
 * \brief Copies a message from one mailbox to another.
2264
 * \param chan
2274
 * \param chan
2265
 * \param vmu
2275
 * \param vmu
[+20] [20] 2649 lines
[+20] [+] static int inboxcount2(const char *mailbox, int *urgentmsgs, int *newmsgs, int *oldmsgs)
4915
	}
4925
	}
4916

    
   
4926

   
4917
	return x;
4927
	return x;
4918
}
4928
}
4919

    
   
4929

   
4920
static int inboxcount(const char *mailbox, int *newmsgs, int *oldmsgs)

   
4921
{

   
4922
	return inboxcount2(mailbox, NULL, newmsgs, oldmsgs);

   
4923
}

   
4924

    
   

   
4925
/*!
4930
/*!
4926
 * \brief Gets the number of messages that exist in a mailbox folder.
4931
 * \brief Gets the number of messages that exist in a mailbox folder.
4927
 * \param context
4932
 * \param context
4928
 * \param mailbox
4933
 * \param mailbox
4929
 * \param folder
4934
 * \param folder
[+20] [20] 16 lines
[+20] [+] static int messagecount(const char *context, const char *mailbox, const char *folder)
4946
	if (ast_strlen_zero(mailbox))
4951
	if (ast_strlen_zero(mailbox))
4947
		return 0;
4952
		return 0;
4948

    
   
4953

   
4949
	obj = ast_odbc_request_obj(odbc_database, 0);
4954
	obj = ast_odbc_request_obj(odbc_database, 0);
4950
	if (obj) {
4955
	if (obj) {

    
   
4956
		if (!strcmp(folder, "INBOX")) {

    
   
4957
			snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir = '%s%s/%s/INBOX' OR dir = '%s%s/%s/Urgent", odbc_table, VM_SPOOL_DIR, context, mailbox, VM_SPOOL_DIR, context, mailbox);

    
   
4958
		} else {
4951
		snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir = '%s%s/%s/%s'", odbc_table, VM_SPOOL_DIR, context, mailbox, folder);
4959
			snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir = '%s%s/%s/%s'", odbc_table, VM_SPOOL_DIR, context, mailbox, folder);

    
   
4960
		}
4952
		stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
4961
		stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
4953
		if (!stmt) {
4962
		if (!stmt) {
4954
			ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
4963
			ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
4955
			goto yuck;
4964
			goto yuck;
4956
		}
4965
		}
[+20] [20] 30 lines
[+20] [+] yuck:
4987
 */
4996
 */
4988
static int has_voicemail(const char *mailbox, const char *folder)
4997
static int has_voicemail(const char *mailbox, const char *folder)
4989
{
4998
{
4990
	char tmp[256], *tmp2 = tmp, *box, *context;
4999
	char tmp[256], *tmp2 = tmp, *box, *context;
4991
	ast_copy_string(tmp, mailbox, sizeof(tmp));
5000
	ast_copy_string(tmp, mailbox, sizeof(tmp));
4992
	while ((context = box = strsep(&tmp2, ","))) {
5001
	while ((context = box = strsep(&tmp2, ",&"))) {
4993
		strsep(&context, "@");
5002
		strsep(&context, "@");
4994
		if (ast_strlen_zero(context))
5003
		if (ast_strlen_zero(context))
4995
			context = "default";
5004
			context = "default";
4996
		if (messagecount(context, box, folder))
5005
		if (messagecount(context, box, folder))
4997
			return 1;
5006
			return 1;
[+20] [20] 68 lines
[+20] [+] static int copy_message(struct ast_channel *chan, struct ast_vm_user *vmu, int imbox, int msgnum, long duration, struct ast_vm_user *recip, char *fmt, char *dir, const char *flag)
5066
#endif
5075
#endif
5067
#if !(defined(IMAP_STORAGE) || defined(ODBC_STORAGE))
5076
#if !(defined(IMAP_STORAGE) || defined(ODBC_STORAGE))
5068

    
   
5077

   
5069
static int messagecount(const char *context, const char *mailbox, const char *folder)
5078
static int messagecount(const char *context, const char *mailbox, const char *folder)
5070
{
5079
{
5071
	return __has_voicemail(context, mailbox, folder, 0);
5080
	return __has_voicemail(context, mailbox, folder, 0) + (strcmp(folder, "INBOX") ? 0 : __has_voicemail(context, mailbox, "Urgent", 0));
5072
}
5081
}
5073

    
   
5082

   
5074
static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit)
5083
static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit)
5075
{
5084
{
5076
	DIR *dir;
5085
	DIR *dir;
[+20] [20] 19 lines
[+20] static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit)
5096
		if (!strncasecmp(de->d_name, "msg", 3)) {
5105
		if (!strncasecmp(de->d_name, "msg", 3)) {
5097
			if (shortcircuit) {
5106
			if (shortcircuit) {
5098
				ret = 1;
5107
				ret = 1;
5099
				break;
5108
				break;
5100
			} else if (!strncasecmp(de->d_name + 8, "txt", 3)) {
5109
			} else if (!strncasecmp(de->d_name + 8, "txt", 3)) {
5101
				if (shortcircuit) return 1;

   
5102
				ret++;
5110
				ret++;
5103
			}
5111
			}
5104
		}
5112
		}
5105
	}
5113
	}
5106

    
   
5114

   
5107
	closedir(dir);
5115
	closedir(dir);
5108

    
   
5116

   
5109
	/* If we are checking INBOX, we should check Urgent as well */

   
5110
	if (strcmp(folder, "INBOX") == 0) {

   
5111
		return (ret + __has_voicemail(context, mailbox, "Urgent", shortcircuit));

   
5112
	} else {

   
5113
		return ret;
5117
	return ret;
5114
	}
5118
}
5115
}

   
5116

    
   
5119

   
5117
/** 
5120
/** 
5118
 * \brief Determines if the given folder has messages.
5121
 * \brief Determines if the given folder has messages.
5119
 * \param mailbox The @ delimited string for user@context. If no context is found, uses 'default' for the context.
5122
 * \param mailbox The @ delimited string for user@context. If no context is found, uses 'default' for the context.
5120
 * \param folder the folder to look in
5123
 * \param folder the folder to look in
5121
 *
5124
 *
5122
 * This function is used when the mailbox is stored in a filesystem back end.
5125
 * This function is used when the mailbox is stored in a filesystem back end.
5123
 * This invokes the messagecount(). Here we are interested in the presence of messages (> 0) only, not the actual count.
5126
 * This invokes the __has_voicemail(). Here we are interested in the presence of messages (> 0) only, not the actual count.
5124
 * \return 1 if the folder has one or more messages. zero otherwise.
5127
 * \return 1 if the folder has one or more messages. zero otherwise.
5125
 */
5128
 */
5126
static int has_voicemail(const char *mailbox, const char *folder)
5129
static int has_voicemail(const char *mailbox, const char *folder)
5127
{
5130
{
5128
	char tmp[256], *tmp2 = tmp, *box, *context;
5131
	char tmp[256], *tmp2 = tmp, *box, *context;
5129
	ast_copy_string(tmp, mailbox, sizeof(tmp));
5132
	ast_copy_string(tmp, mailbox, sizeof(tmp));
5130
	while ((box = strsep(&tmp2, ","))) {
5133
	while ((box = strsep(&tmp2, ",&"))) {
5131
		if ((context = strchr(box, '@')))
5134
		if ((context = strchr(box, '@')))
5132
			*context++ = '\0';
5135
			*context++ = '\0';
5133
		else
5136
		else
5134
			context = "default";
5137
			context = "default";
5135
		if (__has_voicemail(context, box, folder, 1))
5138
		if (__has_voicemail(context, box, folder, 1))
5136
			return 1;
5139
			return 1;

    
   
5140
		/* If we are checking INBOX, we should check Urgent as well */

    
   
5141
		if (!strcmp(folder, "INBOX") && __has_voicemail(context, box, "Urgent", 1)) {

    
   
5142
			return 1;

    
   
5143
		}
5137
	}
5144
	}
5138
	return 0;
5145
	return 0;
5139
}
5146
}
5140

    
   
5147

   
5141

    
   
5148

   
[+20] [20] 51 lines
[+20] [+] static int inboxcount2(const char *mailbox, int *urgentmsgs, int *newmsgs, int *oldmsgs)
5193
		*urgentmsgs = __has_voicemail(context, tmp, "Urgent", 0);
5200
		*urgentmsgs = __has_voicemail(context, tmp, "Urgent", 0);
5194

    
   
5201

   
5195
	return 0;
5202
	return 0;
5196
}
5203
}
5197

    
   
5204

   

    
   
5205
#endif

    
   
5206

   

    
   
5207
/* Exactly the same function for file-based, ODBC-based, and IMAP-based, so why create 3 different copies? */
5198
static int inboxcount(const char *mailbox, int *newmsgs, int *oldmsgs)
5208
static int inboxcount(const char *mailbox, int *newmsgs, int *oldmsgs)
5199
{
5209
{
5200
	return inboxcount2(mailbox, NULL, newmsgs, oldmsgs);
5210
	int urgentmsgs = 0;

    
   
5211
	int res = inboxcount2(mailbox, &urgentmsgs, newmsgs, oldmsgs);

    
   
5212
	if (newmsgs) {

    
   
5213
		*newmsgs += urgentmsgs;

    
   
5214
	}

    
   
5215
	return res;
5201
}
5216
}
5202

    
   
5217

   
5203
#endif

   
5204

    
   

   
5205
static void run_externnotify(char *context, char *extension, const char *flag)
5218
static void run_externnotify(char *context, char *extension, const char *flag)
5206
{
5219
{
5207
	char arguments[255];
5220
	char arguments[255];
5208
	char ext_context[256] = "";
5221
	char ext_context[256] = "";
5209
	int newvoicemails = 0, oldvoicemails = 0, urgentvoicemails = 0;
5222
	int newvoicemails = 0, oldvoicemails = 0, urgentvoicemails = 0;
[+20] [20] 519 lines
[+20] [+] static int leave_voicemail(struct ast_channel *chan, char *ext, struct leave_vm_options *options)
5729
						char sfn[PATH_MAX];
5742
						char sfn[PATH_MAX];
5730
						char dfn[PATH_MAX];
5743
						char dfn[PATH_MAX];
5731
						int x;
5744
						int x;
5732
						/* It's easier just to try to make it than to check for its existence */
5745
						/* It's easier just to try to make it than to check for its existence */
5733
						create_dirpath(urgdir, sizeof(urgdir), vmu->context, ext, "Urgent");
5746
						create_dirpath(urgdir, sizeof(urgdir), vmu->context, ext, "Urgent");
5734
						ast_debug(5, "Created an Urgent message, moving file from %s to %s.\n", sfn, dfn);

   
5735
						x = last_message_index(vmu, urgdir) + 1;
5747
						x = last_message_index(vmu, urgdir) + 1;
5736
						make_file(sfn, sizeof(sfn), dir, msgnum);
5748
						make_file(sfn, sizeof(sfn), dir, msgnum);
5737
						make_file(dfn, sizeof(dfn), urgdir, x);
5749
						make_file(dfn, sizeof(dfn), urgdir, x);

    
   
5750
						ast_debug(5, "Created an Urgent message, moving file from %s to %s.\n", sfn, dfn);
5738
						RENAME(dir, msgnum, vmu->mailbox, vmu->context, urgdir, x, sfn, dfn);
5751
						RENAME(dir, msgnum, vmu->mailbox, vmu->context, urgdir, x, sfn, dfn);

    
   
5752
						/* Notification must happen for this new message in Urgent folder, not INBOX */

    
   
5753
						ast_copy_string(fn, dfn, sizeof(fn));

    
   
5754
						msgnum = x;
5739
					}
5755
					}
5740
#endif
5756
#endif
5741
					/* Notification needs to happen after the copy, though. */
5757
					/* Notification needs to happen after the copy, though. */
5742
					if (ast_fileexists(fn, NULL, NULL)) {
5758
					if (ast_fileexists(fn, NULL, NULL)) {
5743
#ifdef IMAP_STORAGE
5759
#ifdef IMAP_STORAGE
[+20] [20] 881 lines
[+20] [+] static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu, struct vm_state *vms, int msgnum, long duration, char *fmt, char *cidnum, char *cidname, const char *flag)
6625
	if ((category = pbx_builtin_getvar_helper(chan, "VM_CATEGORY"))) {
6641
	if ((category = pbx_builtin_getvar_helper(chan, "VM_CATEGORY"))) {
6626
		category = ast_strdupa(category);
6642
		category = ast_strdupa(category);
6627
	}
6643
	}
6628
	ast_channel_unlock(chan);
6644
	ast_channel_unlock(chan);
6629

    
   
6645

   
6630
	make_dir(todir, sizeof(todir), vmu->context, vmu->mailbox, "INBOX");
6646
	make_dir(todir, sizeof(todir), vmu->context, vmu->mailbox, !ast_strlen_zero(flag) && !strcmp(flag, "Urgent") ? "Urgent" : "INBOX");
6631
	make_file(fn, sizeof(fn), todir, msgnum);
6647
	make_file(fn, sizeof(fn), todir, msgnum);
6632
	snprintf(ext_context, sizeof(ext_context), "%s@%s", vmu->mailbox, vmu->context);
6648
	snprintf(ext_context, sizeof(ext_context), "%s@%s", vmu->mailbox, vmu->context);
6633

    
   
6649

   
6634
	if (!ast_strlen_zero(vmu->attachfmt)) {
6650
	if (!ast_strlen_zero(vmu->attachfmt)) {
6635
		if (strstr(fmt, vmu->attachfmt))
6651
		if (strstr(fmt, vmu->attachfmt))
[+20] [20] 4934 lines
[+20] [+] static int write_password_to_file(const char *secretfn, const char *password) {
11570
		return -1;
11586
		return -1;
11571
	}
11587
	}
11572
	return 0;
11588
	return 0;
11573
}
11589
}
11574

    
   
11590

   

    
   
11591
AST_TEST_DEFINE(test_voicemail_msgcount)

    
   
11592
{

    
   
11593
	int i, j, res = AST_TEST_PASS;

    
   
11594
	struct ast_vm_user *vmu;

    
   
11595
	struct vm_state vms;

    
   
11596
	struct {

    
   
11597
		char dir[256];

    
   
11598
		char file[256];

    
   
11599
		char txtfile[256];

    
   
11600
	} tmp[3];

    
   
11601
	char syscmd[256];

    
   
11602
	const char origweasels[] = "tt-weasels";

    
   
11603
	const char testcontext[] = "test";

    
   
11604
	const char testmailbox[] = "00000000";

    
   
11605
	const char testspec[] = "00000000@test";

    
   
11606
	FILE *txt;

    
   
11607
	int new, old, urgent;

    
   
11608
	const char *folders[3] = { "Old", "Urgent", "INBOX" };

    
   
11609
	const int folder2mbox[3] = { 1, 11, 0 };

    
   
11610
	const int expected_results[3][12] = {

    
   
11611
		/* hasvm-old, hasvm-urgent, hasvm-new, ic-old, ic-urgent, ic-new, ic2-old, ic2-urgent, ic2-new, mc-old, mc-urgent, mc-new */

    
   
11612
		{          1,            0,         0,      1,         0,      0,       1,          0,       0,      1,         0,      0 },

    
   
11613
		{          1,            1,         1,      1,         0,      1,       1,          1,       0,      1,         1,      1 },

    
   
11614
		{          1,            1,         1,      1,         0,      2,       1,          1,       1,      1,         1,      2 },

    
   
11615
	};

    
   
11616

   

    
   
11617
	switch (cmd) {

    
   
11618
	case TEST_INIT:

    
   
11619
		info->name = "test_voicemail_msgcount";

    
   
11620
		info->category = "apps/app_voicemail/";

    
   
11621
		info->summary = "Test Voicemail status checks";

    
   
11622
		info->description =

    
   
11623
			"Verify that message counts are correct when retrieved through the public API";

    
   
11624
		return AST_TEST_NOT_RUN;

    
   
11625
	case TEST_EXECUTE:

    
   
11626
		break;

    
   
11627
	}

    
   
11628

   

    
   
11629
	/* Make sure the original path was completely empty */

    
   
11630
	create_dirpath(tmp[0].dir, sizeof(tmp[0].dir), testcontext, testmailbox, "");

    
   
11631
	snprintf(tmp[0].file, sizeof(tmp[0].file), "rm -rf %s", tmp[0].dir);

    
   
11632
	i = system(tmp[0].file);

    
   
11633

   

    
   
11634
	if (!(vmu = find_user(NULL, testcontext, testmailbox)) &&

    
   
11635
		!(vmu = find_or_create(testcontext, testmailbox))) {

    
   
11636
		ast_test_status_update(test, "Cannot create vmu structure\n");

    
   
11637
		return AST_TEST_NOT_RUN;

    
   
11638
	}

    
   
11639

   

    
   
11640
	populate_defaults(vmu);

    
   
11641
	memset(&vms, 0, sizeof(vms));

    
   
11642

   

    
   
11643
	/* Create temporary voicemail */

    
   
11644
	for (i = 0; i < 3; i++) {

    
   
11645
		create_dirpath(tmp[i].dir, sizeof(tmp[i].dir), testcontext, testmailbox, folders[i]);

    
   
11646
		make_file(tmp[i].file, sizeof(tmp[i].file), tmp[i].dir, 0);

    
   
11647
		snprintf(tmp[i].txtfile, sizeof(tmp[i].txtfile), "%s.txt", tmp[i].file);

    
   
11648

   

    
   
11649
		if (ast_fileexists(origweasels, "gsm", "en") > 0) {

    
   
11650
			snprintf(syscmd, sizeof(syscmd), "cp %s/sounds/en/%s.gsm %s/%s/%s/%s/msg0000.gsm", ast_config_AST_DATA_DIR, origweasels,

    
   
11651
				VM_SPOOL_DIR, testcontext, testmailbox, folders[i]);

    
   
11652
			j = system(syscmd);

    
   
11653
		}

    
   
11654

   

    
   
11655
		if ((txt = fopen(tmp[i].txtfile, "w+"))) {

    
   
11656
			fprintf(txt, "; just a stub\n[message]\nflag=%s\n", strcmp(folders[i], "Urgent") ? "" : "Urgent");

    
   
11657
			fclose(txt);

    
   
11658
		} else {

    
   
11659
			ast_test_status_update(test, "Unable to write message file '%s'\n", tmp[i].txtfile);

    
   
11660
			res = AST_TEST_FAIL;

    
   
11661
			break;

    
   
11662
		}

    
   
11663
		open_mailbox(&vms, vmu, folder2mbox[i]);

    
   
11664
		STORE(tmp[i].dir, testmailbox, testcontext, 0, NULL, vmu, "gsm", 600, &vms, strcmp(folders[i], "Urgent") ? "" : "Urgent");

    
   
11665

   

    
   
11666
		/* hasvm-old, hasvm-urgent, hasvm-new, ic-old, ic-urgent, ic-new, ic2-old, ic2-urgent, ic2-new, mc-old, mc-urgent, mc-new */

    
   
11667
		for (j = 0; j < 3; j++) {

    
   
11668
			if (ast_app_has_voicemail(testspec, folders[j]) != expected_results[i][0 + j]) {

    
   
11669
				ast_test_status_update(test, "has_voicemail(%s, %s) returned %d and we expected %d\n",

    
   
11670
					testspec, folders[j], ast_app_has_voicemail(testspec, folders[j]), expected_results[i][0 + j]);

    
   
11671
				res = AST_TEST_FAIL;

    
   
11672
			}

    
   
11673
		}

    
   
11674

   

    
   
11675
		new = old = urgent = 0;

    
   
11676
		if (ast_app_inboxcount(testspec, &new, &old)) {

    
   
11677
			ast_test_status_update(test, "inboxcount returned failure\n");

    
   
11678
			res = AST_TEST_FAIL;

    
   
11679
		} else if (old != expected_results[i][3 + 0] || new != expected_results[i][3 + 2]) {

    
   
11680
			ast_test_status_update(test, "inboxcount(%s) returned old=%d (expected %d) and new=%d (expected %d)\n",

    
   
11681
				testspec, old, expected_results[i][3 + 0], new, expected_results[i][3 + 2]);

    
   
11682
			res = AST_TEST_FAIL;

    
   
11683
		}

    
   
11684

   

    
   
11685
		new = old = urgent = 0;

    
   
11686
		if (ast_app_inboxcount2(testspec, &urgent, &new, &old)) {

    
   
11687
			ast_test_status_update(test, "inboxcount2 returned failure\n");

    
   
11688
			res = AST_TEST_FAIL;

    
   
11689
		} else if (old != expected_results[i][6 + 0] ||

    
   
11690
				urgent != expected_results[i][6 + 1] ||

    
   
11691
				   new != expected_results[i][6 + 2]    ) {

    
   
11692
			ast_test_status_update(test, "inboxcount2(%s) returned old=%d (expected %d), urgent=%d (expected %d), and new=%d (expected %d)\n",

    
   
11693
				testspec, old, expected_results[i][5 + 0], urgent, expected_results[i][5 + 1], new, expected_results[i][5 + 2]);

    
   
11694
			res = AST_TEST_FAIL;

    
   
11695
		}

    
   
11696

   

    
   
11697
		new = old = urgent = 0;

    
   
11698
		for (j = 0; j < 3; j++) {

    
   
11699
			if (ast_app_messagecount(testcontext, testmailbox, folders[j]) != expected_results[i][9 + j]) {

    
   
11700
				ast_test_status_update(test, "messagecount(%s, %s) returned %d and we expected %d\n",

    
   
11701
					testspec, folders[j], ast_app_messagecount(testcontext, testmailbox, folders[j]), expected_results[i][9 + j]);

    
   
11702
				res = AST_TEST_FAIL;

    
   
11703
			}

    
   
11704
		}

    
   
11705
	}

    
   
11706

   

    
   
11707
	if (vms.deleted) {

    
   
11708
		ast_free(vms.deleted);

    
   
11709
	}

    
   
11710
	if (vms.heard) {

    
   
11711
		ast_free(vms.heard);

    
   
11712
	}

    
   
11713

   

    
   
11714
	return res;

    
   
11715
}

    
   
11716

   
11575
static int reload(void)
11717
static int reload(void)
11576
{
11718
{
11577
	return load_config(1);
11719
	return load_config(1);
11578
}
11720
}
11579

    
   
11721

   
[+20] [20] 18 lines
[+20] [+] static int unload_module(void)
11598
	ast_unload_realtime("voicemail");
11740
	ast_unload_realtime("voicemail");
11599
	ast_unload_realtime("voicemail_data");
11741
	ast_unload_realtime("voicemail_data");
11600

    
   
11742

   
11601
	free_vm_users();
11743
	free_vm_users();
11602
	free_vm_zones();
11744
	free_vm_zones();

    
   
11745
	AST_TEST_UNREGISTER(test_voicemail_msgcount);
11603
	return res;
11746
	return res;
11604
}
11747
}
11605

    
   
11748

   
11606
static int load_module(void)
11749
static int load_module(void)
11607
{
11750
{
[+20] [20] 28 lines
[+20] static int load_module(void)
11636

    
   
11779

   
11637
	ast_install_vm_functions(has_voicemail, inboxcount, inboxcount2, messagecount, sayname);
11780
	ast_install_vm_functions(has_voicemail, inboxcount, inboxcount2, messagecount, sayname);
11638
	ast_realtime_require_field("voicemail", "uniqueid", RQ_UINTEGER3, 11, "password", RQ_CHAR, 10, SENTINEL);
11781
	ast_realtime_require_field("voicemail", "uniqueid", RQ_UINTEGER3, 11, "password", RQ_CHAR, 10, SENTINEL);
11639
	ast_realtime_require_field("voicemail_data", "filename", RQ_CHAR, 30, "duration", RQ_UINTEGER3, 5, SENTINEL);
11782
	ast_realtime_require_field("voicemail_data", "filename", RQ_CHAR, 30, "duration", RQ_UINTEGER3, 5, SENTINEL);
11640

    
   
11783

   

    
   
11784
	AST_TEST_REGISTER(test_voicemail_msgcount);

    
   
11785

   
11641
	return res;
11786
	return res;
11642
}
11787
}
11643

    
   
11788

   
11644
static int dialout(struct ast_channel *chan, struct ast_vm_user *vmu, char *num, char *outgoing_context) 
11789
static int dialout(struct ast_channel *chan, struct ast_vm_user *vmu, char *num, char *outgoing_context) 
11645
{
11790
{
[+20] [20] 495 lines
  1. /trunk/apps/app_voicemail.c: Loading...

https://reviewboard.asterisk.org/ runs on a server provided by Digium, Inc. and uses bandwidth donated to the open source Asterisk community by API Digital Communications in Huntsville, AL USA.
Please report problems with this site to asteriskteam@digium.com.