Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (C) 1999 - 2006, Digium, Inc.
* Mark Spencer <markster@digium.com>
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
*
* \brief Comedian Mail - Voicemail System
*
* \author Mark Spencer <markster@digium.com>
*
* \extref Unixodbc - http://www.unixodbc.org
* \extref A source distribution of University of Washington's IMAP
c-client (http://www.washington.edu/imap/
*
* \note For information about voicemail IMAP storage, read doc/imapstorage.txt
* \note This module requires res_adsi to load. This needs to be optional
* during compilation.
*
*
*
* \note This file is now almost impossible to work with, due to all #ifdefs.
* Feels like the database code before realtime. Someone - please come up
* with a plan to clean this up.
Russell Bryant
committed
/*** MAKEOPTS
<category name="MENUSELECT_OPTS_app_voicemail" displayname="Voicemail Build Options" positive_output="yes" remove_on_change="apps/app_voicemail.o">
Russell Bryant
committed
<member name="ODBC_STORAGE" displayname="Storage of Voicemail using ODBC">
<depend>unixodbc</depend>
<conflict>IMAP_STORAGE</conflict>
Russell Bryant
committed
<defaultenabled>no</defaultenabled>
</member>
Kevin P. Fleming
committed
<member name="IMAP_STORAGE" displayname="Storage of Voicemail using IMAP4">
<depend>imap_tk</depend>
<conflict>ODBC_STORAGE</conflict>
Kevin P. Fleming
committed
<defaultenabled>no</defaultenabled>
</member>
Russell Bryant
committed
</category>
***/
Kevin P. Fleming
committed
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <time.h>
#include <dirent.h>
#ifdef IMAP_STORAGE
#include <ctype.h>
#include <signal.h>
#include <pwd.h>
Russell Bryant
committed
#ifdef USE_SYSTEM_IMAP
#include <imap/c-client.h>
#include <imap/imap4r1.h>
#include <imap/linkage.h>
#else
#include "c-client.h"
#include "imap4r1.h"
#include "linkage.h"
#endif
Russell Bryant
committed
#endif
Kevin P. Fleming
committed
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/options.h"
#include "asterisk/config.h"
#include "asterisk/say.h"
#include "asterisk/module.h"
#include "asterisk/adsi.h"
#include "asterisk/app.h"
#include "asterisk/manager.h"
#include "asterisk/dsp.h"
#include "asterisk/localtime.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/stringfields.h"
Matthew Fredrickson
committed
#include "asterisk/smdi.h"
Russell Bryant
committed
#ifdef ODBC_STORAGE
Kevin P. Fleming
committed
#include "asterisk/res_odbc.h"
#ifdef IMAP_STORAGE
static char imapserver[48];
static char imapport[8];
static char imapflags[128];
Matt O'Gorman
committed
static char imapfolder[64];
Mark Michelson
committed
static char greetingfolder[64];
static char authuser[32];
static char authpassword[42];
Matt O'Gorman
committed
static int expungeonhangup = 1;
Mark Michelson
committed
static int imapgreetings = 0;
Matt O'Gorman
committed
AST_MUTEX_DEFINE_STATIC(delimiter_lock);
static char delimiter = '\0';
struct vm_state;
Matt O'Gorman
committed
struct ast_vm_user;
/* Forward declarations for IMAP */
static int init_mailstream(struct vm_state *vms, int box);
Russell Bryant
committed
static void write_file(char *filename, char *buffer, unsigned long len);
static void display_body(BODY *body, char *pfx, long i);
static char *get_header_by_tag(char *header, char *tag, char *buf, size_t len);
static void vm_imap_delete(int msgnum, struct vm_state *vms);
static char *get_user_by_mailbox(char *mailbox, char *buf, size_t len);
static struct vm_state *get_vm_state_by_imapuser(char *user, int interactive);
static struct vm_state *get_vm_state_by_mailbox(const char *mailbox, int interactive);
static void vmstate_insert(struct vm_state *vms);
static void vmstate_delete(struct vm_state *vms);
static void set_update(MAILSTREAM * stream);
static void init_vm_state(struct vm_state *vms);
static void check_msgArray(struct vm_state *vms);
static void copy_msgArray(struct vm_state *dst, struct vm_state *src);
static int save_body(BODY *body, struct vm_state *vms, char *section, char *format);
static int make_gsm_file(char *dest, char *imapuser, char *dir, int num);
static void get_mailbox_delimiter(MAILSTREAM *stream);
static void mm_parsequota (MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pquota);
Matt O'Gorman
committed
static void imap_mailbox_name(char *spec, struct vm_state *vms, int box, int target);
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);
static void update_messages_by_imapuser(const char *user, unsigned long number);
Mark Michelson
committed
static int imap_remove_file (char *dir, int msgnum);
static int imap_retrieve_file (char *dir, int msgnum, char *mailbox, char *context);
static int imap_delete_old_greeting (char *dir, struct vm_state *vms);
AST_LIST_ENTRY(vmstate) list;
static AST_LIST_HEAD_STATIC(vmstates, vmstate);
#endif
#define SMDI_MWI_WAIT_TIMEOUT 1000 /* 1 second */
/* Don't modify these here; set your umask at runtime instead */
#define VOICEMAIL_DIR_MODE 0777
#define VOICEMAIL_FILE_MODE 0666
#define VOICEMAIL_CONFIG "voicemail.conf"
#define ASTERISK_USERNAME "asterisk"
/* Default mail command to mail voicemail. Change it with the
mailcmd= command in voicemail.conf */
#define SENDMAIL "/usr/sbin/sendmail -t"
#define INTRO "vm-intro"
#define MAXMSG 100
#define MAXMSGLIMIT 9999
#define BASEMAXINLINE 256
#define BASELINELEN 72
#define BASEMAXINLINE 256
#define eol "\r\n"
#define MAX_DATETIME_FORMAT 512
Olle Johansson
committed
#define MAX_NUM_CID_CONTEXTS 10
#define VM_REVIEW (1 << 0)
#define VM_OPERATOR (1 << 1)
#define VM_SAYCID (1 << 2)
#define VM_SVMAIL (1 << 3)
#define VM_ENVELOPE (1 << 4)
#define VM_SAYDURATION (1 << 5)
#define VM_SKIPAFTERCMD (1 << 6)
#define VM_FORCENAME (1 << 7) /*!< Have new users record their name */
#define VM_FORCEGREET (1 << 8) /*!< Have new users record their greetings */
#define VM_PBXSKIP (1 << 9)
#define VM_DIRECFORWARD (1 << 10) /*!< directory_forward */
#define VM_ATTACH (1 << 11)
#define VM_DELETE (1 << 12)
#define VM_ALLOCED (1 << 13)
#define VM_SEARCH (1 << 14)
#define VM_TEMPGREETWARN (1 << 15) /*!< Remind user tempgreeting is set */
#define ERROR_LOCK_PATH -100
Mark Michelson
committed
enum {
NEW_FOLDER,
OLD_FOLDER,
WORK_FOLDER,
FAMILY_FOLDER,
FRIENDS_FOLDER,
GREETINGS_FOLDER
} vm_box;
OPT_SILENT = (1 << 0),
OPT_BUSY_GREETING = (1 << 1),
OPT_UNAVAIL_GREETING = (1 << 2),
OPT_RECORDGAIN = (1 << 3),
OPT_PREPEND_MAILBOX = (1 << 4),
OPT_AUTOPLAY = (1 << 6),
} vm_option_flags;
enum {
OPT_ARG_RECORDGAIN = 0,
OPT_ARG_PLAYFOLDER = 1,
/* This *must* be the last value in this enum! */
OPT_ARG_ARRAY_SIZE = 2,
} vm_option_args;
AST_APP_OPTIONS(vm_app_options, {
AST_APP_OPTION('s', OPT_SILENT),
AST_APP_OPTION('b', OPT_BUSY_GREETING),
AST_APP_OPTION('u', OPT_UNAVAIL_GREETING),
AST_APP_OPTION_ARG('g', OPT_RECORDGAIN, OPT_ARG_RECORDGAIN),
AST_APP_OPTION('p', OPT_PREPEND_MAILBOX),
AST_APP_OPTION_ARG('a', OPT_AUTOPLAY, OPT_ARG_PLAYFOLDER),
Kevin P. Fleming
committed
});
static int load_config(void);
/*! \page vmlang Voicemail Language Syntaxes Supported
\par Syntaxes supported, not really language codes.
\arg \b en - English
\arg \b de - German
\arg \b es - Spanish
\arg \b fr - French
\arg \b it - Italian
\arg \b nl - Dutch
\arg \b pt - Portuguese
\arg \b pt_BR - Portuguese (Brazil)
\arg \b gr - Greek
\arg \b no - Norwegian
\arg \b se - Swedish
\arg \b tw - Chinese (Taiwan)
German requires the following additional soundfile:
Spanish requires the following additional soundfile:
Dutch, Portuguese & Spanish require the following additional soundfiles:
\arg \b vm-INBOXs singular of 'new'
\arg \b vm-Olds singular of 'old/heard/read'
NB these are plural:
Russell Bryant
committed
Polish uses:
\arg \b vm-new-a 'new', feminine singular accusative
\arg \b vm-new-e 'new', feminine plural accusative
\arg \b vm-new-ych 'new', feminine plural genitive
\arg \b vm-old-a 'old', feminine singular accusative
\arg \b vm-old-e 'old', feminine plural accusative
\arg \b vm-old-ych 'old', feminine plural genitive
\arg \b digits/1-a 'one', not always same as 'digits/1'
\arg \b digits/2-ie 'two', not always same as 'digits/2'
Swedish uses:
\arg \b vm-nytt singular of 'new'
\arg \b vm-nya plural of 'new'
\arg \b vm-gammalt singular of 'old'
\arg \b vm-gamla plural of 'old'
\arg \b digits/ett 'one', not always same as 'digits/1'
Norwegian uses:
\arg \b vm-ny singular of 'new'
\arg \b vm-nye plural of 'new'
\arg \b vm-gammel singular of 'old'
\arg \b vm-gamle plural of 'old'
Italian requires the following additional soundfile:
For vm_intro_it:
\arg \b vm-nuovo new
\arg \b vm-nuovi new plural
\arg \b vm-vecchio old
\arg \b vm-vecchi old plural
Chinese (Taiwan) requires the following additional soundfile:
\arg \b vm-tong A class-word for call (tong1)
\arg \b vm-ri A class-word for day (ri4)
\arg \b vm-you You (ni3)
\arg \b vm-haveno Have no (mei2 you3)
\arg \b vm-have Have (you3)
\arg \b vm-listen To listen (yao4 ting1)
\note Don't use vm-INBOX or vm-Old, because they are the name of the INBOX and Old folders,
spelled among others when you have to change folder. For the above reasons, vm-INBOX
and vm-Old are spelled plural, to make them sound more as folder name than an adjective.
struct baseio {
int iocp;
int iolen;
int linelength;
int ateof;
unsigned char iobuf[BASEMAXINLINE];
};
/*! Structure for linked list of users
* Use ast_vm_user_destroy() to free one of these structures. */
struct ast_vm_user {
char context[AST_MAX_CONTEXT]; /*!< Voicemail context */
char mailbox[AST_MAX_EXTENSION]; /*!< Mailbox id, unique within vm context */
char password[80]; /*!< Secret pin code, numbers only */
char fullname[80]; /*!< Full name, for directory app */
char email[80]; /*!< E-mail address */
char pager[80]; /*!< E-mail address to pager (no attachment) */
char serveremail[80]; /*!< From: Mail address */
char mailcmd[160]; /*!< Configurable mail command */
char language[MAX_LANGUAGE]; /*!< Config: Language setting */
char zonetag[80]; /*!< Time zone */
char callback[80];
char dialout[80];
char attachfmt[20]; /*!< Attachment format */
unsigned int flags; /*!< VM_ flags */
int saydurationm;
int maxmsg; /*!< Maximum number of msgs per folder for this mailbox */
int maxsecs; /*!< Maximum number of seconds per message for this mailbox */
char imapuser[80]; /*!< IMAP server login */
char imappassword[80]; /*!< IMAP server password if authpassword not defined */
double volgain; /*!< Volume gain for voicemails sent via email */
AST_LIST_ENTRY(ast_vm_user) list;
};
struct vm_zone {
Russell Bryant
committed
AST_LIST_ENTRY(vm_zone) list;
char name[80];
char timezone[80];
char msg_format[512];
};
Martin Pycko
committed
struct vm_state {
Olle Johansson
committed
char curbox[80];
char username[80];
char curdir[PATH_MAX];
char vmbox[PATH_MAX];
char fn[PATH_MAX];
char fn2[PATH_MAX];
int *deleted;
int *heard;
Martin Pycko
committed
int curmsg;
int lastmsg;
int newmessages;
int oldmessages;
int starting;
int repeats;
int updated; /*!< decremented on each mail check until 1 -allows delay */
long msgArray[256];
MAILSTREAM *mailstream;
int vmArrayIndex;
char imapuser[80]; /*!< IMAP server login */
int interactive;
unsigned int quota_limit;
unsigned int quota_usage;
struct vm_state *persist_vms;
#endif
Martin Pycko
committed
};
Russell Bryant
committed
#ifdef ODBC_STORAGE
static char odbc_database[80];
static char odbc_table[80];
Mark Michelson
committed
#define RETRIEVE(a,b,c,d) retrieve_file(a,b)
#define DISPOSE(a,b) remove_file(a,b)
Matt O'Gorman
committed
#define STORE(a,b,c,d,e,f,g,h,i) store_file(a,b,c,d)
#define EXISTS(a,b,c,d) (message_exists(a,b))
#define RENAME(a,b,c,d,e,f,g,h) (rename_file(a,b,c,d,e,f))
#define COPY(a,b,c,d,e,f,g,h) (copy_file(a,b,c,d,e,f))
#define DELETE(a,b,c) (delete_file(a,b))
#else
Mark Michelson
committed
#define RETRIEVE(a,b,c,d) (imap_retrieve_file(a,b,c,d ))
#define DISPOSE(a,b) (imap_remove_file(a,b))
Matt O'Gorman
committed
#define STORE(a,b,c,d,e,f,g,h,i) (imap_store_file(a,b,c,d,e,f,g,h,i))
#define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
#define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
#define COPY(a,b,c,d,e,f,g,h) (copy_file(g,h));
#define IMAP_DELETE(a,b,c,d) (vm_imap_delete(b,d))
#define DELETE(a,b,c) (vm_delete(c))
#else
Mark Michelson
committed
#define RETRIEVE(a,b,c,d)
Matt O'Gorman
committed
#define STORE(a,b,c,d,e,f,g,h,i)
#define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
#define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
#define COPY(a,b,c,d,e,f,g,h) (copy_file(g,h));
#define DELETE(a,b,c) (vm_delete(c))
#endif
Kevin P. Fleming
committed
static char VM_SPOOL_DIR[PATH_MAX];
Olle Johansson
committed
static char ext_pass_cmd[128];
Jason Parker
committed
#define PWDCHANGE_INTERNAL (1 << 1)
#define PWDCHANGE_EXTERNAL (1 << 2)
static int pwdchange = PWDCHANGE_INTERNAL;
Kevin P. Fleming
committed
#ifdef ODBC_STORAGE
#define tdesc "Comedian Mail (Voicemail System) with ODBC Storage"
Russell Bryant
committed
# ifdef IMAP_STORAGE
# define tdesc "Comedian Mail (Voicemail System) with IMAP Storage"
# else
# define tdesc "Comedian Mail (Voicemail System)"
# endif
static char userscontext[AST_MAX_EXTENSION] = "default";
static char *synopsis_vm = "Leave a Voicemail message";
" VoiceMail(mailbox[@context][&mailbox[@context]][...][|options]): This\n"
"application allows the calling party to leave a message for the specified\n"
"list of mailboxes. When multiple mailboxes are specified, the greeting will\n"
"be taken from the first mailbox specified. Dialplan execution will stop if the\n"
"specified mailbox does not exist.\n"
" The Voicemail application will exit if any of the following DTMF digits are\n"
"received:\n"
" 0 - Jump to the 'o' extension in the current dialplan context.\n"
" * - Jump to the 'a' extension in the current dialplan context.\n"
" This application will set the following channel variable upon completion:\n"
" VMSTATUS - This indicates the status of the execution of the VoiceMail\n"
" application. The possible values are:\n"
" SUCCESS | USEREXIT | FAILED\n\n"
" Options:\n"
" b - Play the 'busy' greeting to the calling party.\n"
" g(#) - Use the specified amount of gain when recording the voicemail\n"
" message. The units are whole-number decibels (dB).\n"
" s - Skip the playback of instructions for leaving a message to the\n"
" calling party.\n"
Russell Bryant
committed
" u - Play the 'unavailable greeting.\n";
static char *synopsis_vmain = "Check Voicemail messages";
" VoiceMailMain([mailbox][@context][|options]): This application allows the\n"
"calling party to check voicemail messages. A specific mailbox, and optional\n"
"corresponding context, may be specified. If a mailbox is not provided, the\n"
"calling party will be prompted to enter one. If a context is not specified,\n"
"the 'default' context will be used.\n\n"
" Options:\n"
" p - Consider the mailbox parameter as a prefix to the mailbox that\n"
" is entered by the caller.\n"
" g(#) - Use the specified amount of gain when recording a voicemail\n"
" message. The units are whole-number decibels (dB).\n"
" s - Skip checking the passcode for the mailbox.\n"
" a(#) - Skip folder prompt and go directly to folder specified.\n"
" Defaults to INBOX\n";
static char *synopsis_vm_box_exists =
"Check to see if Voicemail mailbox exists";
static char *descrip_vm_box_exists =
" MailboxExists(mailbox[@context][|options]): Check to see if the specified\n"
"mailbox exists. If no voicemail context is specified, the 'default' context\n"
"will be used.\n"
" This application will set the following channel variable upon completion:\n"
" VMBOXEXISTSSTATUS - This will contain the status of the execution of the\n"
" MailboxExists application. Possible values include:\n"
" SUCCESS | FAILED\n\n"
Russell Bryant
committed
" Options: (none)\n";
static char *synopsis_vmauthenticate = "Authenticate with Voicemail passwords";
static char *descrip_vmauthenticate =
" VMAuthenticate([mailbox][@context][|options]): This application behaves the\n"
"same way as the Authenticate application, but the passwords are taken from\n"
"voicemail.conf.\n"
" If the mailbox is specified, only that mailbox's password will be considered\n"
"valid. If the mailbox is not specified, the channel variable AUTH_MAILBOX will\n"
"be set with the authenticated mailbox.\n\n"
" Options:\n"
" s - Skip playing the initial prompts.\n";
/* Leave a message */
static char *app = "VoiceMail";
/* Check mail, control, etc */
static char *app2 = "VoiceMailMain";
static char *app3 = "MailboxExists";
static char *app4 = "VMAuthenticate";
static AST_LIST_HEAD_STATIC(users, ast_vm_user);
Russell Bryant
committed
static AST_LIST_HEAD_STATIC(zones, vm_zone);
static int maxsilence;
static int maxmsg;
static int silencethreshold = 128;
static char serveremail[80];
Olle Johansson
committed
static char mailcmd[160]; /* Configurable mail cmd */
static char externnotify[160];
Matthew Fredrickson
committed
static struct ast_smdi_interface *smdi_iface = NULL;
static char vmfmts[80];
Tilghman Lesher
committed
static double volgain;
static int vmminsecs;
static int vmmaxsecs;
static int maxgreet;
static int skipms;
static int maxlogins;
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*! Poll mailboxes for changes since there is something external to
* app_voicemail that may change them. */
static unsigned int poll_mailboxes;
/*! Polling frequency */
static unsigned int poll_freq;
/*! By default, poll every 30 seconds */
#define DEFAULT_POLL_FREQ 30
AST_MUTEX_DEFINE_STATIC(poll_lock);
static ast_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
static pthread_t poll_thread = AST_PTHREADT_NULL;
static unsigned char poll_thread_run;
/*! Subscription to ... MWI event subscriptions */
static struct ast_event_sub *mwi_sub_sub;
/*! Subscription to ... MWI event un-subscriptions */
static struct ast_event_sub *mwi_unsub_sub;
/*!
* \brief An MWI subscription
*
* This is so we can keep track of which mailboxes are subscribed to.
* This way, we know which mailboxes to poll when the pollmailboxes
* option is being used.
*/
struct mwi_sub {
AST_RWLIST_ENTRY(mwi_sub) entry;
int old_new;
int old_old;
uint32_t uniqueid;
char mailbox[1];
};
static AST_RWLIST_HEAD_STATIC(mwi_subs, mwi_sub);
static char vm_password[80] = "vm-password";
static char vm_newpassword[80] = "vm-newpassword";
static char vm_passchanged[80] = "vm-passchanged";
static char vm_reenterpassword[80] = "vm-reenterpassword";
static char vm_mismatch[80] = "vm-mismatch";
static struct ast_flags globalflags = {0};
static int saydurationminfo;
static char dialcontext[AST_MAX_CONTEXT];
static char callcontext[AST_MAX_CONTEXT];
static char exitcontext[AST_MAX_CONTEXT];
static char cidinternalcontexts[MAX_NUM_CID_CONTEXTS][64];
static char *emailbody = NULL;
static char *emailsubject = NULL;
static char *pagerbody = NULL;
static char *pagersubject = NULL;
static char fromstring[100];
static char pagerfromstring[100];
static char emailtitle[100];
static char charset[32] = "ISO-8859-1";
static unsigned char adsifdn[4] = "\x00\x00\x00\x0F";
static unsigned char adsisec[4] = "\x9B\xDB\xF7\xAC";
Kevin P. Fleming
committed
static char emaildateformat[32] = "%A, %B %d, %Y at %r";
/* Forward declarations - generic */
static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu,int box);
static int advanced_options(struct ast_channel *chan, struct ast_vm_user *vmu, struct vm_state *vms, int msg, int option, signed char record_gain);
static int dialout(struct ast_channel *chan, struct ast_vm_user *vmu, char *num, char *outgoing_context);
static int play_record_review(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime,
char *fmt, int outsidecaller, struct ast_vm_user *vmu, int *duration, const char *unlockdir,
signed char record_gain, struct vm_state *vms);
static int vm_tempgreeting(struct ast_channel *chan, struct ast_vm_user *vmu, struct vm_state *vms, char *fmtc, signed char record_gain);
static int vm_play_folder_name(struct ast_channel *chan, char *mbox);
static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu, int msgnum, long duration, char *fmt, char *cidnum, char *cidname);
static void make_email_file(FILE *p, char *srcemail, struct ast_vm_user *vmu, int msgnum, char *context, char *mailbox, char *cidnum, char *cidname, char *attach, char *format, int duration, int attach_user_voicemail, struct ast_channel *chan, const char *category, int imap);
static void apply_options(struct ast_vm_user *vmu, const char *options);
#if !(defined(ODBC_STORAGE) || defined(IMAP_STORAGE))
static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit);
#endif
static void populate_defaults(struct ast_vm_user *vmu)
ast_copy_flags(vmu, (&globalflags), AST_FLAGS_ALL);
Kevin P. Fleming
committed
if (saydurationminfo)
vmu->saydurationm = saydurationminfo;
ast_copy_string(vmu->callback, callcontext, sizeof(vmu->callback));
ast_copy_string(vmu->dialout, dialcontext, sizeof(vmu->dialout));
ast_copy_string(vmu->exit, exitcontext, sizeof(vmu->exit));
if (vmmaxsecs)
vmu->maxsecs = vmmaxsecs;
Kevin P. Fleming
committed
if (maxmsg)
vmu->maxmsg = maxmsg;
Tilghman Lesher
committed
vmu->volgain = volgain;
static void apply_option(struct ast_vm_user *vmu, const char *var, const char *value)
ast_set2_flag(vmu, ast_true(value), VM_ATTACH);
} else if (!strcasecmp(var, "attachfmt")) {
ast_copy_string(vmu->attachfmt, value, sizeof(vmu->attachfmt));
ast_copy_string(vmu->serveremail, value, sizeof(vmu->serveremail));
ast_copy_string(vmu->language, value, sizeof(vmu->language));
ast_copy_string(vmu->zonetag, value, sizeof(vmu->zonetag));
#ifdef IMAP_STORAGE
} else if (!strcasecmp(var, "imapuser")) {
ast_copy_string(vmu->imapuser, value, sizeof(vmu->imapuser));
Matt O'Gorman
committed
} else if (!strcasecmp(var, "imappassword")) {
ast_copy_string(vmu->imappassword, value, sizeof(vmu->imappassword));
} else if (!strcasecmp(var, "delete") || !strcasecmp(var, "deletevoicemail")) {
ast_set2_flag(vmu, ast_true(value), VM_DELETE);
ast_set2_flag(vmu, ast_true(value), VM_SAYCID);
ast_set2_flag(vmu, ast_true(value), VM_SVMAIL);
ast_set2_flag(vmu, ast_true(value), VM_REVIEW);
} else if (!strcasecmp(var, "tempgreetwarn")){
ast_set2_flag(vmu, ast_true(value), VM_TEMPGREETWARN);
ast_set2_flag(vmu, ast_true(value), VM_OPERATOR);
ast_set2_flag(vmu, ast_true(value), VM_ENVELOPE);
} else if (!strcasecmp(var, "sayduration")){
ast_set2_flag(vmu, ast_true(value), VM_SAYDURATION);
} else if (!strcasecmp(var, "saydurationm")){
if (sscanf(value, "%d", &x) == 1) {
vmu->saydurationm = x;
} else {
ast_log(LOG_WARNING, "Invalid min duration for say duration\n");
}
ast_set2_flag(vmu, ast_true(value), VM_FORCENAME);
ast_set2_flag(vmu, ast_true(value), VM_FORCEGREET);
ast_copy_string(vmu->callback, value, sizeof(vmu->callback));
ast_copy_string(vmu->dialout, value, sizeof(vmu->dialout));
ast_copy_string(vmu->exit, value, sizeof(vmu->exit));
} else if (!strcasecmp(var, "maxmessage")) {
if (vmu->maxsecs <= 0) {
ast_log(LOG_WARNING, "Invalid max message length of %s. Using global value %i\n", value, vmmaxsecs);
vmu->maxsecs = vmmaxsecs;
} else {
vmu->maxsecs = atoi(value);
}
} else if (!strcasecmp(var, "maxmsg")) {
vmu->maxmsg = atoi(value);
ast_log(LOG_WARNING, "Invalid number of messages per folder maxmsg=%s. Using default value %i\n", value, MAXMSG);
vmu->maxmsg = MAXMSG;
} else if (vmu->maxmsg > MAXMSGLIMIT) {
ast_log(LOG_WARNING, "Maximum number of messages per folder is %i. Cannot accept value maxmsg=%s\n", MAXMSGLIMIT, value);
vmu->maxmsg = MAXMSGLIMIT;
}
Tilghman Lesher
committed
} else if (!strcasecmp(var, "volgain")) {
sscanf(value, "%lf", &vmu->volgain);
} else if (!strcasecmp(var, "options")) {
apply_options(vmu, value);
static int change_password_realtime(struct ast_vm_user *vmu, const char *password)
int res;
if (!ast_strlen_zero(vmu->uniqueid)) {
res = ast_update_realtime("voicemail", "uniqueid", vmu->uniqueid, "password", password, NULL);
Russell Bryant
committed
if (res > 0) {
ast_copy_string(vmu->password, password, sizeof(vmu->password));
Russell Bryant
committed
res = 0;
} else if (!res) {
res = -1;
}
}
static void apply_options(struct ast_vm_user *vmu, const char *options)
{ /* Destructively Parse options and apply */
char *stringp;
char *s;
char *var, *value;
stringp = ast_strdupa(options);
while ((s = strsep(&stringp, "|"))) {
value = s;
if ((var = strsep(&value, "=")) && value) {
apply_option(vmu, var, value);
}
}
static void apply_options_full(struct ast_vm_user *retval, struct ast_variable *var)
{
struct ast_variable *tmp;
tmp = var;
while (tmp) {
if (!strcasecmp(tmp->name, "vmsecret")) {
ast_copy_string(retval->password, tmp->value, sizeof(retval->password));
} else if (!strcasecmp(tmp->name, "secret") || !strcasecmp(tmp->name, "password")) { /* don't overwrite vmsecret if it exists */
if (ast_strlen_zero(retval->password))
ast_copy_string(retval->password, tmp->value, sizeof(retval->password));
} else if (!strcasecmp(tmp->name, "uniqueid")) {
ast_copy_string(retval->uniqueid, tmp->value, sizeof(retval->uniqueid));
} else if (!strcasecmp(tmp->name, "pager")) {
ast_copy_string(retval->pager, tmp->value, sizeof(retval->pager));
} else if (!strcasecmp(tmp->name, "email")) {
ast_copy_string(retval->email, tmp->value, sizeof(retval->email));
} else if (!strcasecmp(tmp->name, "fullname")) {
ast_copy_string(retval->fullname, tmp->value, sizeof(retval->fullname));
} else if (!strcasecmp(tmp->name, "context")) {
ast_copy_string(retval->context, tmp->value, sizeof(retval->context));
#ifdef IMAP_STORAGE
} else if (!strcasecmp(tmp->name, "imapuser")) {
ast_copy_string(retval->imapuser, tmp->value, sizeof(retval->imapuser));
} else if (!strcasecmp(tmp->name, "imappassword")) {
ast_copy_string(retval->imappassword, tmp->value, sizeof(retval->imappassword));
#endif
} else
apply_option(retval, tmp->name, tmp->value);
tmp = tmp->next;
}
}
static struct ast_vm_user *find_user_realtime(struct ast_vm_user *ivm, const char *context, const char *mailbox)
struct ast_variable *var;
struct ast_vm_user *retval;
BJ Weschke
committed
if ((retval = (ivm ? ivm : ast_calloc(1, sizeof(*retval))))) {
if (mailbox)
ast_copy_string(retval->mailbox, mailbox, sizeof(retval->mailbox));
populate_defaults(retval);
var = ast_load_realtime("voicemail", "mailbox", mailbox, NULL);
else
var = ast_load_realtime("voicemail", "mailbox", mailbox, "context", context, NULL);
apply_options_full(retval, var);
Tilghman Lesher
committed
ast_free(retval);
}
static struct ast_vm_user *find_user(struct ast_vm_user *ivm, const char *context, const char *mailbox)
{
/* This function could be made to generate one from a database, too */
struct ast_vm_user *vmu=NULL, *cur;
AST_LIST_LOCK(&users);
if (!context && !ast_test_flag((&globalflags), VM_SEARCH))
AST_LIST_TRAVERSE(&users, cur, list) {
if (ast_test_flag((&globalflags), VM_SEARCH) && !strcasecmp(mailbox, cur->mailbox))
break;
if (context && (!strcasecmp(context, cur->context)) && (!strcasecmp(mailbox, cur->mailbox)))
}
if (cur) {
BJ Weschke
committed
/* Make a copy, so that on a reload, we have no race */
if ((vmu = (ivm ? ivm : ast_malloc(sizeof(*vmu))))) {
ast_set2_flag(vmu, !ivm, VM_ALLOCED);
AST_LIST_NEXT(vmu, list) = NULL;
} else
vmu = find_user_realtime(ivm, context, mailbox);
AST_LIST_UNLOCK(&users);
return vmu;
}
static int reset_user_pw(const char *context, const char *mailbox, const char *newpass)
{
/* This function could be made to generate one from a database, too */
struct ast_vm_user *cur;
int res = -1;
AST_LIST_LOCK(&users);
AST_LIST_TRAVERSE(&users, cur, list) {
if ((!context || !strcasecmp(context, cur->context)) &&
(!strcasecmp(mailbox, cur->mailbox)))
break;
}
if (cur) {
ast_copy_string(cur->password, newpass, sizeof(cur->password));
res = 0;
}
AST_LIST_UNLOCK(&users);
return res;
static void vm_change_password(struct ast_vm_user *vmu, const char *newpassword)
Matt O'Gorman
committed
struct ast_config *cfg=NULL;
struct ast_variable *var=NULL;
struct ast_category *cat=NULL;
char *category=NULL, *value=NULL, *new=NULL;
const char *tmp=NULL;
if (!change_password_realtime(vmu, newpassword))
return;
Matt O'Gorman
committed
/* check voicemail.conf */
if ((cfg = ast_config_load_with_comments(VOICEMAIL_CONFIG))) {
while ((category = ast_category_browse(cfg, category))) {
if (!strcasecmp(category, vmu->context)) {
if (!(tmp = ast_variable_retrieve(cfg, category, vmu->mailbox))) {
Matt O'Gorman
committed
ast_log(LOG_WARNING, "We could not find the mailbox.\n");
break;
}
value = strstr(tmp,",");
if (!value) {
ast_log(LOG_WARNING, "variable has bad format.\n");
break;
}
new = alloca((strlen(value)+strlen(newpassword)+1));
Matt O'Gorman
committed
sprintf(new,"%s%s", newpassword, value);
if (!(cat = ast_category_get(cfg, category))) {
ast_log(LOG_WARNING, "Failed to get category structure.\n");
break;
}
ast_variable_update(cat, vmu->mailbox, new, NULL, 0);
Matt O'Gorman
committed
/* save the results */
reset_user_pw(vmu->context, vmu->mailbox, newpassword);
ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
config_text_file_save(VOICEMAIL_CONFIG, cfg, "AppVoicemail");
}
category = NULL;
var = NULL;
/* check users.conf and update the password stored for the mailbox*/
/* if no vmsecret entry exists create one. */
Matt O'Gorman
committed
if ((cfg = ast_config_load_with_comments("users.conf"))) {
ast_debug(4, "we are looking for %s\n", vmu->mailbox);
Matt O'Gorman
committed
while ((category = ast_category_browse(cfg, category))) {
ast_debug(4, "users.conf: %s\n", category);
Matt O'Gorman
committed
if (!strcasecmp(category, vmu->mailbox)) {
if (!(tmp = ast_variable_retrieve(cfg, category, "vmsecret"))) {
ast_debug(3, "looks like we need to make vmsecret!\n");
var = ast_variable_new("vmsecret", newpassword);
Matt O'Gorman
committed
}
new = alloca(strlen(newpassword)+1);
Matt O'Gorman
committed
sprintf(new, "%s", newpassword);
if (!(cat = ast_category_get(cfg, category))) {
ast_debug(4, "failed to get category!\n");
Matt O'Gorman
committed
}
if (!var)
ast_variable_update(cat, "vmsecret", new, NULL, 0);
Matt O'Gorman
committed
else
ast_variable_append(cat, var);
Matt O'Gorman
committed
/* save the results and clean things up */
reset_user_pw(vmu->context, vmu->mailbox, newpassword);
ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
config_text_file_save("users.conf", cfg, "AppVoicemail");
}
Mark Spencer
committed
static void vm_change_password_shell(struct ast_vm_user *vmu, char *newpassword)
{
Olle Johansson
committed
char buf[255];
snprintf(buf,255,"%s %s %s %s",ext_pass_cmd,vmu->context,vmu->mailbox,newpassword);
if (!ast_safe_system(buf))
ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
Mark Spencer
committed
}
static int make_dir(char *dest, int len, const char *context, const char *ext, const char *folder)
return snprintf(dest, len, "%s%s/%s/%s", VM_SPOOL_DIR, context, ext, folder);
}
#ifdef IMAP_STORAGE
static int make_gsm_file(char *dest, char *imapuser, char *dir, int num)
{
Tilghman Lesher
committed
int res;
if ((res = ast_mkdir(dir, 01777))) {
ast_log(LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dir, strerror(res));
return sprintf(dest, "%s/msg%04d", dir, num);
}
/* return sprintf(dest, "%s/s/msg%04d", dir, imapuser, num); */
Matt O'Gorman
committed
return sprintf(dest, "%s/msg%04d", dir, num);
}
static void vm_imap_delete(int msgnum, struct vm_state *vms)
{
unsigned long messageNum = 0;
char arg[10];
/* find real message number based on msgnum */
/* this may be an index into vms->msgArray based on the msgnum. */
messageNum = vms->msgArray[msgnum];
if (messageNum == 0) {
ast_log(LOG_WARNING, "msgnum %d, mailbox message %lu is zero.\n",msgnum,messageNum);
return;
}
ast_debug(3, "deleting msgnum %d, which is mailbox message %lu\n",msgnum,messageNum);
sprintf (arg,"%lu",messageNum);
mail_setflag (vms->mailstream,arg,"\\DELETED");
static int make_file(char *dest, int len, char *dir, int num)
{
return snprintf(dest, len, "%s/msg%04d", dir, num);
/*! \brief basically mkdir -p $dest/$context/$ext/$folder
* \param context String. Ignored if is null or empty string.
* \param ext String. Ignored if is null or empty string.
* \param folder String. Ignored if is null or empty string.