2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Comedian Mail - Voicemail System
23 * \author Mark Spencer <markster@digium.com>
25 * \extref Unixodbc - http://www.unixodbc.org
26 * \extref A source distribution of University of Washington's IMAP
27 c-client (http://www.washington.edu/imap/
31 * \note For information about voicemail IMAP storage, read doc/imapstorage.txt
32 * \ingroup applications
33 * \note This module requires res_adsi to load. This needs to be optional
38 * \note This file is now almost impossible to work with, due to all \#ifdefs.
39 * Feels like the database code before realtime. Someone - please come up
40 * with a plan to clean this up.
44 <depend>res_smdi</depend>
48 <category name="MENUSELECT_OPTS_app_voicemail" displayname="Voicemail Build Options" positive_output="yes" remove_on_change="apps/app_voicemail.o apps/app_directory.o">
49 <member name="ODBC_STORAGE" displayname="Storage of Voicemail using ODBC">
54 <conflict>IMAP_STORAGE</conflict>
55 <defaultenabled>no</defaultenabled>
57 <member name="IMAP_STORAGE" displayname="Storage of Voicemail using IMAP4">
58 <depend>imap_tk</depend>
59 <conflict>ODBC_STORAGE</conflict>
61 <defaultenabled>no</defaultenabled>
72 #ifdef USE_SYSTEM_IMAP
73 #include <imap/c-client.h>
74 #include <imap/imap4r1.h>
75 #include <imap/linkage.h>
76 #elif defined (USE_SYSTEM_CCLIENT)
77 #include <c-client/c-client.h>
78 #include <c-client/imap4r1.h>
79 #include <c-client/linkage.h>
87 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
89 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
96 #include "asterisk/logger.h"
97 #include "asterisk/lock.h"
98 #include "asterisk/file.h"
99 #include "asterisk/channel.h"
100 #include "asterisk/pbx.h"
101 #include "asterisk/config.h"
102 #include "asterisk/say.h"
103 #include "asterisk/module.h"
104 #include "asterisk/adsi.h"
105 #include "asterisk/app.h"
106 #include "asterisk/manager.h"
107 #include "asterisk/dsp.h"
108 #include "asterisk/localtime.h"
109 #include "asterisk/cli.h"
110 #include "asterisk/utils.h"
111 #include "asterisk/stringfields.h"
112 #include "asterisk/smdi.h"
113 #include "asterisk/event.h"
114 #include "asterisk/taskprocessor.h"
117 #include "asterisk/res_odbc.h"
121 <application name="VoiceMail" language="en_US">
123 Leave a Voicemail message.
126 <parameter name="mailboxs" argsep="&" required="true">
127 <argument name="mailbox1" argsep="@" required="true">
128 <argument name="mailbox" required="true" />
129 <argument name="context" />
131 <argument name="mailbox2" argsep="@" multiple="true">
132 <argument name="mailbox" required="true" />
133 <argument name="context" />
136 <parameter name="options">
139 <para>Play the <literal>busy</literal> greeting to the calling party.</para>
142 <argument name="c" />
143 <para>Accept digits for a new extension in context <replaceable>c</replaceable>,
144 if played during the greeting. Context defaults to the current context.</para>
147 <argument name="#" required="true" />
148 <para>Use the specified amount of gain when recording the voicemail
149 message. The units are whole-number decibels (dB). Only works on supported
150 technologies, which is DAHDI only.</para>
153 <para>Skip the playback of instructions for leaving a message to the
154 calling party.</para>
157 <para>Play the <literal>unavailable</literal> greeting.</para>
160 <para>Mark message as <literal>URGENT</literal>.</para>
163 <para>Mark message as <literal>PRIORITY</literal>.</para>
169 <para>This application allows the calling party to leave a message for the specified
170 list of mailboxes. When multiple mailboxes are specified, the greeting will be taken from
171 the first mailbox specified. Dialplan execution will stop if the specified mailbox does not
173 <para>The Voicemail application will exit if any of the following DTMF digits are received:</para>
176 <para>Jump to the <literal>o</literal> extension in the current dialplan context.</para>
179 <para>Jump to the <literal>a</literal> extension in the current dialplan context.</para>
182 <para>This application will set the following channel variable upon completion:</para>
184 <variable name="VMSTATUS">
185 <para>This indicates the status of the execution of the VoiceMail application.</para>
186 <value name="SUCCESS" />
187 <value name="USEREXIT" />
188 <value name="FAILED" />
193 <application name="VoiceMailMain" language="en_US">
195 Check Voicemail messages.
198 <parameter name="mailbox" required="true" argsep="@">
199 <argument name="mailbox" />
200 <argument name="context" />
202 <parameter name="options">
205 <para>Consider the <replaceable>mailbox</replaceable> parameter as a prefix to
206 the mailbox that is entered by the caller.</para>
209 <argument name="#" required="true" />
210 <para>Use the specified amount of gain when recording a voicemail message.
211 The units are whole-number decibels (dB).</para>
214 <para>Skip checking the passcode for the mailbox.</para>
217 <argument name="folder" required="true" />
218 <para>Skip folder prompt and go directly to <replaceable>folder</replaceable> specified.
219 Defaults to <literal>INBOX</literal>.</para>
225 <para>This application allows the calling party to check voicemail messages. A specific
226 <replaceable>mailbox</replaceable>, and optional corresponding <replaceable>context</replaceable>,
227 may be specified. If a <replaceable>mailbox</replaceable> is not provided, the calling party will
228 be prompted to enter one. If a <replaceable>context</replaceable> is not specified, the
229 <literal>default</literal> context will be used.</para>
232 <application name="MailboxExists" language="en_US">
234 Check to see if Voicemail mailbox exists.
237 <parameter name="mailbox" required="true" argsep="@">
238 <argument name="mailbox" required="true" />
239 <argument name="context" />
241 <parameter name="options">
242 <para>None options.</para>
246 <para>Check to see if the specified <replaceable>mailbox</replaceable> exists. If no voicemail
247 <replaceable>context</replaceable> is specified, the <literal>default</literal> context
249 <para>This application will set the following channel variable upon completion:</para>
251 <variable name="VMBOXEXISTSSTATUS">
252 <para>This will contain the status of the execution of the MailboxExists application.
253 Possible values include:</para>
254 <value name="SUCCESS" />
255 <value name="FAILED" />
260 <application name="VMAuthenticate" language="en_US">
262 Authenticate with Voicemail passwords.
265 <parameter name="mailbox" required="true" argsep="@">
266 <argument name="mailbox" />
267 <argument name="context" />
269 <parameter name="options">
272 <para>Skip playing the initial prompts.</para>
278 <para>This application behaves the same way as the Authenticate application, but the passwords
279 are taken from <filename>voicemail.conf</filename>. If the <replaceable>mailbox</replaceable> is
280 specified, only that mailbox's password will be considered valid. If the <replaceable>mailbox</replaceable>
281 is not specified, the channel variable <variable>AUTH_MAILBOX</variable> will be set with the authenticated
285 <function name="MAILBOX_EXISTS" language="en_US">
287 Tell if a mailbox is configured.
290 <parameter name="mailbox" required="true" />
291 <parameter name="context" />
294 <para>Returns a boolean of whether the corresponding <replaceable>mailbox</replaceable> exists.
295 If <replaceable>context</replaceable> is not specified, defaults to the <literal>default</literal>
302 static char imapserver[48];
303 static char imapport[8];
304 static char imapflags[128];
305 static char imapfolder[64];
306 static char imapparentfolder[64] = "\0";
307 static char greetingfolder[64];
308 static char authuser[32];
309 static char authpassword[42];
311 static int expungeonhangup = 1;
312 static int imapgreetings = 0;
313 static char delimiter = '\0';
318 /* Forward declarations for IMAP */
319 static int init_mailstream(struct vm_state *vms, int box);
320 static void write_file(char *filename, char *buffer, unsigned long len);
321 static char *get_header_by_tag(char *header, char *tag, char *buf, size_t len);
322 static void vm_imap_delete(int msgnum, struct ast_vm_user *vmu);
323 static char *get_user_by_mailbox(char *mailbox, char *buf, size_t len);
324 static struct vm_state *get_vm_state_by_imapuser(char *user, int interactive);
325 static struct vm_state *get_vm_state_by_mailbox(const char *mailbox, const char *context, int interactive);
326 static struct vm_state *create_vm_state_from_user(struct ast_vm_user *vmu);
327 static void vmstate_insert(struct vm_state *vms);
328 static void vmstate_delete(struct vm_state *vms);
329 static void set_update(MAILSTREAM * stream);
330 static void init_vm_state(struct vm_state *vms);
331 static int save_body(BODY *body, struct vm_state *vms, char *section, char *format, int is_intro);
332 static void get_mailbox_delimiter(MAILSTREAM *stream);
333 static void mm_parsequota (MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pquota);
334 static void imap_mailbox_name(char *spec, size_t len, struct vm_state *vms, int box, int target);
335 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);
336 static void update_messages_by_imapuser(const char *user, unsigned long number);
337 static int vm_delete(char *file);
339 static int imap_remove_file (char *dir, int msgnum);
340 static int imap_retrieve_file (const char *dir, const int msgnum, const char *mailbox, const char *context);
341 static int imap_delete_old_greeting (char *dir, struct vm_state *vms);
342 static void check_quota(struct vm_state *vms, char *mailbox);
343 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box);
345 struct vm_state *vms;
346 AST_LIST_ENTRY(vmstate) list;
349 static AST_LIST_HEAD_STATIC(vmstates, vmstate);
353 #define SMDI_MWI_WAIT_TIMEOUT 1000 /* 1 second */
355 #define COMMAND_TIMEOUT 5000
356 /* Don't modify these here; set your umask at runtime instead */
357 #define VOICEMAIL_DIR_MODE 0777
358 #define VOICEMAIL_FILE_MODE 0666
359 #define CHUNKSIZE 65536
361 #define VOICEMAIL_CONFIG "voicemail.conf"
362 #define ASTERISK_USERNAME "asterisk"
364 /* Define fast-forward, pause, restart, and reverse keys
365 while listening to a voicemail message - these are
366 strings, not characters */
367 #define DEFAULT_LISTEN_CONTROL_FORWARD_KEY "#"
368 #define DEFAULT_LISTEN_CONTROL_REVERSE_KEY "*"
369 #define DEFAULT_LISTEN_CONTROL_PAUSE_KEY "0"
370 #define DEFAULT_LISTEN_CONTROL_RESTART_KEY "2"
371 #define DEFAULT_LISTEN_CONTROL_STOP_KEY "13456789"
372 #define VALID_DTMF "1234567890*#" /* Yes ABCD are valid dtmf but what phones have those? */
374 /* Default mail command to mail voicemail. Change it with the
375 mailcmd= command in voicemail.conf */
376 #define SENDMAIL "/usr/sbin/sendmail -t"
378 #define INTRO "vm-intro"
381 #define MAXMSGLIMIT 9999
383 #define MINPASSWORD 0 /*!< Default minimum mailbox password length */
385 #define BASELINELEN 72
386 #define BASEMAXINLINE 256
389 #define MAX_DATETIME_FORMAT 512
390 #define MAX_NUM_CID_CONTEXTS 10
392 #define VM_REVIEW (1 << 0) /*!< After recording, permit the caller to review the recording before saving */
393 #define VM_OPERATOR (1 << 1) /*!< Allow 0 to be pressed to go to 'o' extension */
394 #define VM_SAYCID (1 << 2) /*!< Repeat the CallerID info during envelope playback */
395 #define VM_SVMAIL (1 << 3) /*!< Allow the user to compose a new VM from within VoicemailMain */
396 #define VM_ENVELOPE (1 << 4) /*!< Play the envelope information (who-from, time received, etc.) */
397 #define VM_SAYDURATION (1 << 5) /*!< Play the length of the message during envelope playback */
398 #define VM_SKIPAFTERCMD (1 << 6) /*!< After deletion, assume caller wants to go to the next message */
399 #define VM_FORCENAME (1 << 7) /*!< Have new users record their name */
400 #define VM_FORCEGREET (1 << 8) /*!< Have new users record their greetings */
401 #define VM_PBXSKIP (1 << 9) /*!< Skip the [PBX] preamble in the Subject line of emails */
402 #define VM_DIRECFORWARD (1 << 10) /*!< Permit caller to use the Directory app for selecting to which mailbox to forward a VM */
403 #define VM_ATTACH (1 << 11) /*!< Attach message to voicemail notifications? */
404 #define VM_DELETE (1 << 12) /*!< Delete message after sending notification */
405 #define VM_ALLOCED (1 << 13) /*!< Structure was malloc'ed, instead of placed in a return (usually static) buffer */
406 #define VM_SEARCH (1 << 14) /*!< Search all contexts for a matching mailbox */
407 #define VM_TEMPGREETWARN (1 << 15) /*!< Remind user tempgreeting is set */
408 #define VM_MOVEHEARD (1 << 16) /*!< Move a "heard" message to Old after listening to it */
409 #define VM_MESSAGEWRAP (1 << 17) /*!< Wrap around from the last message to the first, and vice-versa */
410 #define VM_FWDURGAUTO (1 << 18) /*!< Autoset of Urgent flag on forwarded Urgent messages set globally */
411 #define ERROR_LOCK_PATH -100
424 OPT_SILENT = (1 << 0),
425 OPT_BUSY_GREETING = (1 << 1),
426 OPT_UNAVAIL_GREETING = (1 << 2),
427 OPT_RECORDGAIN = (1 << 3),
428 OPT_PREPEND_MAILBOX = (1 << 4),
429 OPT_AUTOPLAY = (1 << 6),
430 OPT_DTMFEXIT = (1 << 7),
431 OPT_MESSAGE_Urgent = (1 << 8),
432 OPT_MESSAGE_PRIORITY = (1 << 9)
436 OPT_ARG_RECORDGAIN = 0,
437 OPT_ARG_PLAYFOLDER = 1,
438 OPT_ARG_DTMFEXIT = 2,
439 /* This *must* be the last value in this enum! */
440 OPT_ARG_ARRAY_SIZE = 3,
443 AST_APP_OPTIONS(vm_app_options, {
444 AST_APP_OPTION('s', OPT_SILENT),
445 AST_APP_OPTION('b', OPT_BUSY_GREETING),
446 AST_APP_OPTION('u', OPT_UNAVAIL_GREETING),
447 AST_APP_OPTION_ARG('g', OPT_RECORDGAIN, OPT_ARG_RECORDGAIN),
448 AST_APP_OPTION_ARG('d', OPT_DTMFEXIT, OPT_ARG_DTMFEXIT),
449 AST_APP_OPTION('p', OPT_PREPEND_MAILBOX),
450 AST_APP_OPTION_ARG('a', OPT_AUTOPLAY, OPT_ARG_PLAYFOLDER),
451 AST_APP_OPTION('U', OPT_MESSAGE_Urgent),
452 AST_APP_OPTION('P', OPT_MESSAGE_PRIORITY)
455 static int load_config(int reload);
457 /*! \page vmlang Voicemail Language Syntaxes Supported
459 \par Syntaxes supported, not really language codes.
466 \arg \b pt - Portuguese
467 \arg \b pt_BR - Portuguese (Brazil)
469 \arg \b no - Norwegian
471 \arg \b tw - Chinese (Taiwan)
472 \arg \b ua - Ukrainian
474 German requires the following additional soundfile:
475 \arg \b 1F einE (feminine)
477 Spanish requires the following additional soundfile:
478 \arg \b 1M un (masculine)
480 Dutch, Portuguese & Spanish require the following additional soundfiles:
481 \arg \b vm-INBOXs singular of 'new'
482 \arg \b vm-Olds singular of 'old/heard/read'
485 \arg \b vm-INBOX nieuwe (nl)
486 \arg \b vm-Old oude (nl)
489 \arg \b vm-new-a 'new', feminine singular accusative
490 \arg \b vm-new-e 'new', feminine plural accusative
491 \arg \b vm-new-ych 'new', feminine plural genitive
492 \arg \b vm-old-a 'old', feminine singular accusative
493 \arg \b vm-old-e 'old', feminine plural accusative
494 \arg \b vm-old-ych 'old', feminine plural genitive
495 \arg \b digits/1-a 'one', not always same as 'digits/1'
496 \arg \b digits/2-ie 'two', not always same as 'digits/2'
499 \arg \b vm-nytt singular of 'new'
500 \arg \b vm-nya plural of 'new'
501 \arg \b vm-gammalt singular of 'old'
502 \arg \b vm-gamla plural of 'old'
503 \arg \b digits/ett 'one', not always same as 'digits/1'
506 \arg \b vm-ny singular of 'new'
507 \arg \b vm-nye plural of 'new'
508 \arg \b vm-gammel singular of 'old'
509 \arg \b vm-gamle plural of 'old'
517 Ukrainian requires the following additional soundfile:
518 \arg \b vm-nove 'nove'
519 \arg \b vm-stare 'stare'
520 \arg \b digits/ua/1e 'odne'
522 Italian requires the following additional soundfile:
526 \arg \b vm-nuovi new plural
527 \arg \b vm-vecchio old
528 \arg \b vm-vecchi old plural
530 Chinese (Taiwan) requires the following additional soundfile:
531 \arg \b vm-tong A class-word for call (tong1)
532 \arg \b vm-ri A class-word for day (ri4)
533 \arg \b vm-you You (ni3)
534 \arg \b vm-haveno Have no (mei2 you3)
535 \arg \b vm-have Have (you3)
536 \arg \b vm-listen To listen (yao4 ting1)
539 \note Don't use vm-INBOX or vm-Old, because they are the name of the INBOX and Old folders,
540 spelled among others when you have to change folder. For the above reasons, vm-INBOX
541 and vm-Old are spelled plural, to make them sound more as folder name than an adjective.
550 unsigned char iobuf[BASEMAXINLINE];
553 /*! Structure for linked list of users
554 * Use ast_vm_user_destroy() to free one of these structures. */
556 char context[AST_MAX_CONTEXT]; /*!< Voicemail context */
557 char mailbox[AST_MAX_EXTENSION]; /*!< Mailbox id, unique within vm context */
558 char password[80]; /*!< Secret pin code, numbers only */
559 char fullname[80]; /*!< Full name, for directory app */
560 char email[80]; /*!< E-mail address */
561 char pager[80]; /*!< E-mail address to pager (no attachment) */
562 char serveremail[80]; /*!< From: Mail address */
563 char mailcmd[160]; /*!< Configurable mail command */
564 char language[MAX_LANGUAGE]; /*!< Config: Language setting */
565 char zonetag[80]; /*!< Time zone */
568 char uniqueid[80]; /*!< Unique integer identifier */
570 char attachfmt[20]; /*!< Attachment format */
571 unsigned int flags; /*!< VM_ flags */
573 int maxmsg; /*!< Maximum number of msgs per folder for this mailbox */
574 int maxdeletedmsg; /*!< Maximum number of deleted msgs saved for this mailbox */
575 int maxsecs; /*!< Maximum number of seconds per message for this mailbox */
577 char imapuser[80]; /*!< IMAP server login */
578 char imappassword[80]; /*!< IMAP server password if authpassword not defined */
580 double volgain; /*!< Volume gain for voicemails sent via email */
581 AST_LIST_ENTRY(ast_vm_user) list;
584 /*! Voicemail time zones */
586 AST_LIST_ENTRY(vm_zone) list;
589 char msg_format[512];
592 #define VMSTATE_MAX_MSG_ARRAY 256
594 /*! Voicemail mailbox state */
599 char curdir[PATH_MAX];
600 char vmbox[PATH_MAX];
602 char intro[PATH_MAX];
614 int updated; /*!< decremented on each mail check until 1 -allows delay */
615 long msgArray[VMSTATE_MAX_MSG_ARRAY];
616 MAILSTREAM *mailstream;
618 char imapuser[80]; /*!< IMAP server login */
620 char introfn[PATH_MAX]; /*!< Name of prepended file */
621 unsigned int quota_limit;
622 unsigned int quota_usage;
623 struct vm_state *persist_vms;
628 static char odbc_database[80];
629 static char odbc_table[80];
630 #define RETRIEVE(a,b,c,d) retrieve_file(a,b)
631 #define DISPOSE(a,b) remove_file(a,b)
632 #define STORE(a,b,c,d,e,f,g,h,i,j) store_file(a,b,c,d)
633 #define EXISTS(a,b,c,d) (message_exists(a,b))
634 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(a,b,c,d,e,f))
635 #define COPY(a,b,c,d,e,f,g,h) (copy_file(a,b,c,d,e,f))
636 #define DELETE(a,b,c,d) (delete_file(a,b))
639 #define DISPOSE(a,b) (imap_remove_file(a,b))
640 #define STORE(a,b,c,d,e,f,g,h,i,j) (imap_store_file(a,b,c,d,e,f,g,h,i,j))
641 #define RETRIEVE(a,b,c,d) imap_retrieve_file(a,b,c,d)
642 #define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
643 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
644 #define COPY(a,b,c,d,e,f,g,h) (copy_file(g,h));
645 #define DELETE(a,b,c,d) (vm_imap_delete(b,d))
647 #define RETRIEVE(a,b,c,d)
649 #define STORE(a,b,c,d,e,f,g,h,i,j)
650 #define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
651 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
652 #define COPY(a,b,c,d,e,f,g,h) (copy_plain_file(g,h));
653 #define DELETE(a,b,c,d) (vm_delete(c))
657 static char VM_SPOOL_DIR[PATH_MAX];
659 static char ext_pass_cmd[128];
660 static char ext_pass_check_cmd[128];
664 #define PWDCHANGE_INTERNAL (1 << 1)
665 #define PWDCHANGE_EXTERNAL (1 << 2)
666 static int pwdchange = PWDCHANGE_INTERNAL;
669 #define tdesc "Comedian Mail (Voicemail System) with ODBC Storage"
672 # define tdesc "Comedian Mail (Voicemail System) with IMAP Storage"
674 # define tdesc "Comedian Mail (Voicemail System)"
678 static char userscontext[AST_MAX_EXTENSION] = "default";
680 static char *addesc = "Comedian Mail";
682 /* Leave a message */
683 static char *app = "VoiceMail";
685 /* Check mail, control, etc */
686 static char *app2 = "VoiceMailMain";
688 static char *app3 = "MailboxExists";
689 static char *app4 = "VMAuthenticate";
691 static AST_LIST_HEAD_STATIC(users, ast_vm_user);
692 static AST_LIST_HEAD_STATIC(zones, vm_zone);
693 static char zonetag[80];
694 static int maxsilence;
696 static int maxdeletedmsg;
697 static int silencethreshold = 128;
698 static char serveremail[80];
699 static char mailcmd[160]; /* Configurable mail cmd */
700 static char externnotify[160];
701 static struct ast_smdi_interface *smdi_iface = NULL;
702 static char vmfmts[80];
703 static double volgain;
704 static int vmminsecs;
705 static int vmmaxsecs;
708 static int maxlogins;
709 static int minpassword;
711 /*! Poll mailboxes for changes since there is something external to
712 * app_voicemail that may change them. */
713 static unsigned int poll_mailboxes;
715 /*! Polling frequency */
716 static unsigned int poll_freq;
717 /*! By default, poll every 30 seconds */
718 #define DEFAULT_POLL_FREQ 30
720 AST_MUTEX_DEFINE_STATIC(poll_lock);
721 static ast_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
722 static pthread_t poll_thread = AST_PTHREADT_NULL;
723 static unsigned char poll_thread_run;
725 /*! Subscription to ... MWI event subscriptions */
726 static struct ast_event_sub *mwi_sub_sub;
727 /*! Subscription to ... MWI event un-subscriptions */
728 static struct ast_event_sub *mwi_unsub_sub;
731 * \brief An MWI subscription
733 * This is so we can keep track of which mailboxes are subscribed to.
734 * This way, we know which mailboxes to poll when the pollmailboxes
735 * option is being used.
738 AST_RWLIST_ENTRY(mwi_sub) entry;
746 struct mwi_sub_task {
752 static struct ast_taskprocessor *mwi_subscription_tps;
754 static AST_RWLIST_HEAD_STATIC(mwi_subs, mwi_sub);
756 /* custom audio control prompts for voicemail playback */
757 static char listen_control_forward_key[12];
758 static char listen_control_reverse_key[12];
759 static char listen_control_pause_key[12];
760 static char listen_control_restart_key[12];
761 static char listen_control_stop_key[12];
763 /* custom password sounds */
764 static char vm_password[80] = "vm-password";
765 static char vm_newpassword[80] = "vm-newpassword";
766 static char vm_passchanged[80] = "vm-passchanged";
767 static char vm_reenterpassword[80] = "vm-reenterpassword";
768 static char vm_mismatch[80] = "vm-mismatch";
769 static char vm_invalid_password[80] = "vm-invalid-password";
771 static struct ast_flags globalflags = {0};
773 static int saydurationminfo;
775 static char dialcontext[AST_MAX_CONTEXT] = "";
776 static char callcontext[AST_MAX_CONTEXT] = "";
777 static char exitcontext[AST_MAX_CONTEXT] = "";
779 static char cidinternalcontexts[MAX_NUM_CID_CONTEXTS][64];
782 static char *emailbody = NULL;
783 static char *emailsubject = NULL;
784 static char *pagerbody = NULL;
785 static char *pagersubject = NULL;
786 static char fromstring[100];
787 static char pagerfromstring[100];
788 static char charset[32] = "ISO-8859-1";
790 static unsigned char adsifdn[4] = "\x00\x00\x00\x0F";
791 static unsigned char adsisec[4] = "\x9B\xDB\xF7\xAC";
792 static int adsiver = 1;
793 static char emaildateformat[32] = "%A, %B %d, %Y at %r";
795 /* Forward declarations - generic */
796 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box);
797 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);
798 static int dialout(struct ast_channel *chan, struct ast_vm_user *vmu, char *num, char *outgoing_context);
799 static int play_record_review(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime,
800 char *fmt, int outsidecaller, struct ast_vm_user *vmu, int *duration, const char *unlockdir,
801 signed char record_gain, struct vm_state *vms, char *flag);
802 static int vm_tempgreeting(struct ast_channel *chan, struct ast_vm_user *vmu, struct vm_state *vms, char *fmtc, signed char record_gain);
803 static int vm_play_folder_name(struct ast_channel *chan, char *mbox);
804 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);
805 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 *attach2, char *format, int duration, int attach_user_voicemail, struct ast_channel *chan, const char *category, int imap, const char *flag);
806 static void apply_options(struct ast_vm_user *vmu, const char *options);
807 static int add_email_attachment(FILE *p, struct ast_vm_user *vmu, char *format, char *attach, char *greeting_attachment, char *mailbox, char *bound, char *filename, int last, int msgnum);
808 static int is_valid_dtmf(const char *key);
810 #if !(defined(ODBC_STORAGE) || defined(IMAP_STORAGE))
811 static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit);
814 static char *strip_control(const char *input, char *buf, size_t buflen)
817 for (; *input; input++) {
822 if (bufptr == buf + buflen - 1) {
832 * \brief Sets default voicemail system options to a voicemail user.
834 * This applies select global settings to a newly created (dynamic) instance of a voicemail user.
835 * - all the globalflags
836 * - the saydurationminfo
840 * - vmmaxsecs, vmmaxmsg, maxdeletedmsg
843 static void populate_defaults(struct ast_vm_user *vmu)
845 ast_copy_flags(vmu, (&globalflags), AST_FLAGS_ALL);
846 if (saydurationminfo)
847 vmu->saydurationm = saydurationminfo;
848 ast_copy_string(vmu->callback, callcontext, sizeof(vmu->callback));
849 ast_copy_string(vmu->dialout, dialcontext, sizeof(vmu->dialout));
850 ast_copy_string(vmu->exit, exitcontext, sizeof(vmu->exit));
851 ast_copy_string(vmu->zonetag, zonetag, sizeof(vmu->zonetag));
853 vmu->maxsecs = vmmaxsecs;
855 vmu->maxmsg = maxmsg;
857 vmu->maxdeletedmsg = maxdeletedmsg;
858 vmu->volgain = volgain;
862 * \brief Sets a a specific property value.
863 * \param vmu The voicemail user object to work with.
864 * \param var The name of the property to be set.
865 * \param value The value to be set to the property.
867 * The property name must be one of the understood properties. See the source for details.
869 static void apply_option(struct ast_vm_user *vmu, const char *var, const char *value)
872 if (!strcasecmp(var, "attach")) {
873 ast_set2_flag(vmu, ast_true(value), VM_ATTACH);
874 } else if (!strcasecmp(var, "attachfmt")) {
875 ast_copy_string(vmu->attachfmt, value, sizeof(vmu->attachfmt));
876 } else if (!strcasecmp(var, "serveremail")) {
877 ast_copy_string(vmu->serveremail, value, sizeof(vmu->serveremail));
878 } else if (!strcasecmp(var, "language")) {
879 ast_copy_string(vmu->language, value, sizeof(vmu->language));
880 } else if (!strcasecmp(var, "tz")) {
881 ast_copy_string(vmu->zonetag, value, sizeof(vmu->zonetag));
883 } else if (!strcasecmp(var, "imapuser")) {
884 ast_copy_string(vmu->imapuser, value, sizeof(vmu->imapuser));
885 } else if (!strcasecmp(var, "imappassword") || !strcasecmp(var, "imapsecret")) {
886 ast_copy_string(vmu->imappassword, value, sizeof(vmu->imappassword));
888 } else if (!strcasecmp(var, "delete") || !strcasecmp(var, "deletevoicemail")) {
889 ast_set2_flag(vmu, ast_true(value), VM_DELETE);
890 } else if (!strcasecmp(var, "saycid")){
891 ast_set2_flag(vmu, ast_true(value), VM_SAYCID);
892 } else if (!strcasecmp(var,"sendvoicemail")){
893 ast_set2_flag(vmu, ast_true(value), VM_SVMAIL);
894 } else if (!strcasecmp(var, "review")){
895 ast_set2_flag(vmu, ast_true(value), VM_REVIEW);
896 } else if (!strcasecmp(var, "tempgreetwarn")){
897 ast_set2_flag(vmu, ast_true(value), VM_TEMPGREETWARN);
898 } else if (!strcasecmp(var, "messagewrap")){
899 ast_set2_flag(vmu, ast_true(value), VM_MESSAGEWRAP);
900 } else if (!strcasecmp(var, "operator")) {
901 ast_set2_flag(vmu, ast_true(value), VM_OPERATOR);
902 } else if (!strcasecmp(var, "envelope")){
903 ast_set2_flag(vmu, ast_true(value), VM_ENVELOPE);
904 } else if (!strcasecmp(var, "moveheard")){
905 ast_set2_flag(vmu, ast_true(value), VM_MOVEHEARD);
906 } else if (!strcasecmp(var, "sayduration")){
907 ast_set2_flag(vmu, ast_true(value), VM_SAYDURATION);
908 } else if (!strcasecmp(var, "saydurationm")){
909 if (sscanf(value, "%d", &x) == 1) {
910 vmu->saydurationm = x;
912 ast_log(AST_LOG_WARNING, "Invalid min duration for say duration\n");
914 } else if (!strcasecmp(var, "forcename")){
915 ast_set2_flag(vmu, ast_true(value), VM_FORCENAME);
916 } else if (!strcasecmp(var, "forcegreetings")){
917 ast_set2_flag(vmu, ast_true(value), VM_FORCEGREET);
918 } else if (!strcasecmp(var, "callback")) {
919 ast_copy_string(vmu->callback, value, sizeof(vmu->callback));
920 } else if (!strcasecmp(var, "dialout")) {
921 ast_copy_string(vmu->dialout, value, sizeof(vmu->dialout));
922 } else if (!strcasecmp(var, "exitcontext")) {
923 ast_copy_string(vmu->exit, value, sizeof(vmu->exit));
924 } else if (!strcasecmp(var, "maxmessage") || !strcasecmp(var, "maxsecs")) {
925 if (vmu->maxsecs <= 0) {
926 ast_log(AST_LOG_WARNING, "Invalid max message length of %s. Using global value %d\n", value, vmmaxsecs);
927 vmu->maxsecs = vmmaxsecs;
929 vmu->maxsecs = atoi(value);
931 if (!strcasecmp(var, "maxmessage"))
932 ast_log(AST_LOG_WARNING, "Option 'maxmessage' has been deprecated in favor of 'maxsecs'. Please make that change in your voicemail config.\n");
933 } else if (!strcasecmp(var, "maxmsg")) {
934 vmu->maxmsg = atoi(value);
935 if (vmu->maxmsg <= 0) {
936 ast_log(AST_LOG_WARNING, "Invalid number of messages per folder maxmsg=%s. Using default value %d\n", value, MAXMSG);
937 vmu->maxmsg = MAXMSG;
938 } else if (vmu->maxmsg > MAXMSGLIMIT) {
939 ast_log(AST_LOG_WARNING, "Maximum number of messages per folder is %d. Cannot accept value maxmsg=%s\n", MAXMSGLIMIT, value);
940 vmu->maxmsg = MAXMSGLIMIT;
942 } else if (!strcasecmp(var, "backupdeleted")) {
943 if (sscanf(value, "%d", &x) == 1)
944 vmu->maxdeletedmsg = x;
945 else if (ast_true(value))
946 vmu->maxdeletedmsg = MAXMSG;
948 vmu->maxdeletedmsg = 0;
950 if (vmu->maxdeletedmsg < 0) {
951 ast_log(AST_LOG_WARNING, "Invalid number of deleted messages saved per mailbox backupdeleted=%s. Using default value %d\n", value, MAXMSG);
952 vmu->maxdeletedmsg = MAXMSG;
953 } else if (vmu->maxdeletedmsg > MAXMSGLIMIT) {
954 ast_log(AST_LOG_WARNING, "Maximum number of deleted messages saved per mailbox is %d. Cannot accept value backupdeleted=%s\n", MAXMSGLIMIT, value);
955 vmu->maxdeletedmsg = MAXMSGLIMIT;
957 } else if (!strcasecmp(var, "volgain")) {
958 sscanf(value, "%lf", &vmu->volgain);
959 } else if (!strcasecmp(var, "options")) {
960 apply_options(vmu, value);
964 static char *vm_check_password_shell(char *command, char *buf, size_t len)
971 snprintf(buf, len, "FAILURE: Pipe failed: %s", strerror(errno));
974 pid = ast_safe_fork(0);
980 snprintf(buf, len, "FAILURE: Fork failed");
984 if (read(fds[0], buf, len) < 0) {
985 ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
990 AST_DECLARE_APP_ARGS(arg,
993 char *mycmd = ast_strdupa(command);
996 dup2(fds[1], STDOUT_FILENO);
998 ast_close_fds_above_n(STDOUT_FILENO);
1000 AST_NONSTANDARD_APP_ARGS(arg, mycmd, ' ');
1002 execv(arg.v[0], arg.v);
1003 printf("FAILURE: %s", strerror(errno));
1011 * \brief Check that password meets minimum required length
1012 * \param vmu The voicemail user to change the password for.
1013 * \param password The password string to check
1015 * \return zero on ok, 1 on not ok.
1017 static int check_password(struct ast_vm_user *vmu, char *password)
1019 /* check minimum length */
1020 if (strlen(password) < minpassword)
1022 if (!ast_strlen_zero(ext_pass_check_cmd)) {
1023 char cmd[255], buf[255];
1025 ast_log(AST_LOG_DEBUG, "Verify password policies for %s\n", password);
1027 snprintf(cmd, sizeof(cmd), "%s %s %s %s %s", ext_pass_check_cmd, vmu->mailbox, vmu->context, vmu->password, password);
1028 if (vm_check_password_shell(cmd, buf, sizeof(buf))) {
1029 ast_debug(5, "Result: %s\n", buf);
1030 if (!strncasecmp(buf, "VALID", 5)) {
1031 ast_debug(3, "Passed password check: '%s'\n", buf);
1033 } else if (!strncasecmp(buf, "FAILURE", 7)) {
1034 ast_log(AST_LOG_WARNING, "Unable to execute password validation script: '%s'.\n", buf);
1037 ast_log(AST_LOG_NOTICE, "Password doesn't match policies for user %s %s\n", vmu->mailbox, password);
1046 * \brief Performs a change of the voicemail passowrd in the realtime engine.
1047 * \param vmu The voicemail user to change the password for.
1048 * \param password The new value to be set to the password for this user.
1050 * This only works if the voicemail user has a unique id, and if there is a realtime engine configured.
1051 * This is called from the (top level) vm_change_password.
1053 * \return zero on success, -1 on error.
1055 static int change_password_realtime(struct ast_vm_user *vmu, const char *password)
1058 if (!ast_strlen_zero(vmu->uniqueid)) {
1059 if (strlen(password) > 10) {
1060 ast_realtime_require_field("voicemail", "password", RQ_CHAR, strlen(password), SENTINEL);
1062 res = ast_update2_realtime("voicemail", "context", vmu->context, "mailbox", vmu->mailbox, SENTINEL, "password", password, SENTINEL);
1064 ast_copy_string(vmu->password, password, sizeof(vmu->password));
1075 * \brief Destructively Parse options and apply.
1077 static void apply_options(struct ast_vm_user *vmu, const char *options)
1082 stringp = ast_strdupa(options);
1083 while ((s = strsep(&stringp, "|"))) {
1085 if ((var = strsep(&value, "=")) && value) {
1086 apply_option(vmu, var, value);
1092 * \brief Loads the options specific to a voicemail user.
1094 * This is called when a vm_user structure is being set up, such as from load_options.
1096 static void apply_options_full(struct ast_vm_user *retval, struct ast_variable *var)
1098 for (; var; var = var->next) {
1099 if (!strcasecmp(var->name, "vmsecret")) {
1100 ast_copy_string(retval->password, var->value, sizeof(retval->password));
1101 } else if (!strcasecmp(var->name, "secret") || !strcasecmp(var->name, "password")) { /* don't overwrite vmsecret if it exists */
1102 if (ast_strlen_zero(retval->password))
1103 ast_copy_string(retval->password, var->value, sizeof(retval->password));
1104 } else if (!strcasecmp(var->name, "uniqueid")) {
1105 ast_copy_string(retval->uniqueid, var->value, sizeof(retval->uniqueid));
1106 } else if (!strcasecmp(var->name, "pager")) {
1107 ast_copy_string(retval->pager, var->value, sizeof(retval->pager));
1108 } else if (!strcasecmp(var->name, "email")) {
1109 ast_copy_string(retval->email, var->value, sizeof(retval->email));
1110 } else if (!strcasecmp(var->name, "fullname")) {
1111 ast_copy_string(retval->fullname, var->value, sizeof(retval->fullname));
1112 } else if (!strcasecmp(var->name, "context")) {
1113 ast_copy_string(retval->context, var->value, sizeof(retval->context));
1115 } else if (!strcasecmp(var->name, "imapuser")) {
1116 ast_copy_string(retval->imapuser, var->value, sizeof(retval->imapuser));
1117 } else if (!strcasecmp(var->name, "imappassword") || !strcasecmp(var->name, "imapsecret")) {
1118 ast_copy_string(retval->imappassword, var->value, sizeof(retval->imappassword));
1121 apply_option(retval, var->name, var->value);
1126 * \brief Determines if a DTMF key entered is valid.
1127 * \param key The character to be compared. expects a single character. Though is capable of handling a string, this is internally copies using ast_strdupa.
1129 * Tests the character entered against the set of valid DTMF characters.
1130 * \return 1 if the character entered is a valid DTMF digit, 0 if the character is invalid.
1132 static int is_valid_dtmf(const char *key)
1135 char *local_key = ast_strdupa(key);
1137 for (i = 0; i < strlen(key); ++i) {
1138 if (!strchr(VALID_DTMF, *local_key)) {
1139 ast_log(AST_LOG_WARNING, "Invalid DTMF key \"%c\" used in voicemail configuration file\n", *local_key);
1148 * \brief Finds a voicemail user from the realtime engine.
1153 * This is called as a fall through case when the normal find_user() was not able to find a user. That is, the default it so look in the usual voicemail users file first.
1155 * \return The ast_vm_user structure for the user that was found.
1157 static struct ast_vm_user *find_user_realtime(struct ast_vm_user *ivm, const char *context, const char *mailbox)
1159 struct ast_variable *var;
1160 struct ast_vm_user *retval;
1162 if ((retval = (ivm ? ivm : ast_calloc(1, sizeof(*retval))))) {
1164 ast_set_flag(retval, VM_ALLOCED);
1166 memset(retval, 0, sizeof(*retval));
1168 ast_copy_string(retval->mailbox, mailbox, sizeof(retval->mailbox));
1169 populate_defaults(retval);
1170 if (!context && ast_test_flag((&globalflags), VM_SEARCH))
1171 var = ast_load_realtime("voicemail", "mailbox", mailbox, SENTINEL);
1173 var = ast_load_realtime("voicemail", "mailbox", mailbox, "context", context, SENTINEL);
1175 apply_options_full(retval, var);
1176 ast_variables_destroy(var);
1187 * \brief Finds a voicemail user from the users file or the realtime engine.
1192 * \return The ast_vm_user structure for the user that was found.
1194 static struct ast_vm_user *find_user(struct ast_vm_user *ivm, const char *context, const char *mailbox)
1196 /* This function could be made to generate one from a database, too */
1197 struct ast_vm_user *vmu=NULL, *cur;
1198 AST_LIST_LOCK(&users);
1200 if (!context && !ast_test_flag((&globalflags), VM_SEARCH))
1201 context = "default";
1203 AST_LIST_TRAVERSE(&users, cur, list) {
1204 if (ast_test_flag((&globalflags), VM_SEARCH) && !strcasecmp(mailbox, cur->mailbox))
1206 if (context && (!strcasecmp(context, cur->context)) && (!strcasecmp(mailbox, cur->mailbox)))
1210 /* Make a copy, so that on a reload, we have no race */
1211 if ((vmu = (ivm ? ivm : ast_malloc(sizeof(*vmu))))) {
1212 memcpy(vmu, cur, sizeof(*vmu));
1213 ast_set2_flag(vmu, !ivm, VM_ALLOCED);
1214 AST_LIST_NEXT(vmu, list) = NULL;
1217 vmu = find_user_realtime(ivm, context, mailbox);
1218 AST_LIST_UNLOCK(&users);
1223 * \brief Resets a user password to a specified password.
1228 * This does the actual change password work, called by the vm_change_password() function.
1230 * \return zero on success, -1 on error.
1232 static int reset_user_pw(const char *context, const char *mailbox, const char *newpass)
1234 /* This function could be made to generate one from a database, too */
1235 struct ast_vm_user *cur;
1237 AST_LIST_LOCK(&users);
1238 AST_LIST_TRAVERSE(&users, cur, list) {
1239 if ((!context || !strcasecmp(context, cur->context)) &&
1240 (!strcasecmp(mailbox, cur->mailbox)))
1244 ast_copy_string(cur->password, newpass, sizeof(cur->password));
1247 AST_LIST_UNLOCK(&users);
1252 * \brief The handler for the change password option.
1253 * \param vmu The voicemail user to work with.
1254 * \param newpassword The new password (that has been gathered from the appropriate prompting).
1255 * This is called when a new user logs in for the first time and the option to force them to change their password is set.
1256 * It is also called when the user wants to change their password from menu option '5' on the mailbox options menu.
1258 static void vm_change_password(struct ast_vm_user *vmu, const char *newpassword)
1260 struct ast_config *cfg=NULL;
1261 struct ast_variable *var=NULL;
1262 struct ast_category *cat=NULL;
1263 char *category=NULL, *value=NULL, *new=NULL;
1264 const char *tmp=NULL;
1265 struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS };
1266 if (!change_password_realtime(vmu, newpassword))
1269 /* check voicemail.conf */
1270 if ((cfg = ast_config_load(VOICEMAIL_CONFIG, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
1271 while ((category = ast_category_browse(cfg, category))) {
1272 if (!strcasecmp(category, vmu->context)) {
1273 if (!(tmp = ast_variable_retrieve(cfg, category, vmu->mailbox))) {
1274 ast_log(AST_LOG_WARNING, "We could not find the mailbox.\n");
1277 value = strstr(tmp,",");
1279 ast_log(AST_LOG_WARNING, "variable has bad format.\n");
1282 new = alloca((strlen(value)+strlen(newpassword)+1));
1283 sprintf(new,"%s%s", newpassword, value);
1284 if (!(cat = ast_category_get(cfg, category))) {
1285 ast_log(AST_LOG_WARNING, "Failed to get category structure.\n");
1288 ast_variable_update(cat, vmu->mailbox, new, NULL, 0);
1291 /* save the results */
1292 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1293 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1294 ast_config_text_file_save(VOICEMAIL_CONFIG, cfg, "AppVoicemail");
1298 /* check users.conf and update the password stored for the mailbox*/
1299 /* if no vmsecret entry exists create one. */
1300 if ((cfg = ast_config_load("users.conf", config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
1301 ast_debug(4, "we are looking for %s\n", vmu->mailbox);
1302 while ((category = ast_category_browse(cfg, category))) {
1303 ast_debug(4, "users.conf: %s\n", category);
1304 if (!strcasecmp(category, vmu->mailbox)) {
1305 if (!(tmp = ast_variable_retrieve(cfg, category, "vmsecret"))) {
1306 ast_debug(3, "looks like we need to make vmsecret!\n");
1307 var = ast_variable_new("vmsecret", newpassword, "");
1309 new = alloca(strlen(newpassword)+1);
1310 sprintf(new, "%s", newpassword);
1311 if (!(cat = ast_category_get(cfg, category))) {
1312 ast_debug(4, "failed to get category!\n");
1316 ast_variable_update(cat, "vmsecret", new, NULL, 0);
1318 ast_variable_append(cat, var);
1321 /* save the results and clean things up */
1322 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1323 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1324 ast_config_text_file_save("users.conf", cfg, "AppVoicemail");
1328 static void vm_change_password_shell(struct ast_vm_user *vmu, char *newpassword)
1331 snprintf(buf,255,"%s %s %s %s",ext_pass_cmd,vmu->context,vmu->mailbox,newpassword);
1332 if (!ast_safe_system(buf)) {
1333 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1334 /* Reset the password in memory, too */
1335 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1340 * \brief Creates a file system path expression for a folder within the voicemail data folder and the appropriate context.
1341 * \param dest The variable to hold the output generated path expression. This buffer should be of size PATH_MAX.
1342 * \param len The length of the path string that was written out.
1344 * The path is constructed as
1345 * VM_SPOOL_DIRcontext/ext/folder
1347 * \return zero on success, -1 on error.
1349 static int make_dir(char *dest, int len, const char *context, const char *ext, const char *folder)
1351 return snprintf(dest, len, "%s%s/%s/%s", VM_SPOOL_DIR, context, ext, folder);
1355 * \brief Creates a file system path expression for a folder within the voicemail data folder and the appropriate context.
1356 * \param dest The variable to hold the output generated path expression. This buffer should be of size PATH_MAX.
1357 * \param len The length of the path string that was written out.
1359 * The path is constructed as
1360 * VM_SPOOL_DIRcontext/ext/folder
1362 * \return zero on success, -1 on error.
1364 static int make_file(char *dest, const int len, const char *dir, const int num)
1366 return snprintf(dest, len, "%s/msg%04d", dir, num);
1369 /* same as mkstemp, but return a FILE * */
1370 static FILE *vm_mkftemp(char *template)
1373 int pfd = mkstemp(template);
1374 chmod(template, VOICEMAIL_FILE_MODE & ~my_umask);
1376 p = fdopen(pfd, "w+");
1385 /*! \brief basically mkdir -p $dest/$context/$ext/$folder
1386 * \param dest String. base directory.
1387 * \param len Length of dest.
1388 * \param context String. Ignored if is null or empty string.
1389 * \param ext String. Ignored if is null or empty string.
1390 * \param folder String. Ignored if is null or empty string.
1391 * \return -1 on failure, 0 on success.
1393 static int create_dirpath(char *dest, int len, const char *context, const char *ext, const char *folder)
1395 mode_t mode = VOICEMAIL_DIR_MODE;
1398 make_dir(dest, len, context, ext, folder);
1399 if ((res = ast_mkdir(dest, mode))) {
1400 ast_log(AST_LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res));
1406 static const char *mbox(int id)
1408 static const char *msgs[] = {
1426 return (id >= 0 && id < ARRAY_LEN(msgs)) ? msgs[id] : "Unknown";
1429 static void free_user(struct ast_vm_user *vmu)
1431 if (ast_test_flag(vmu, VM_ALLOCED))
1435 /* All IMAP-specific functions should go in this block. This
1436 * keeps them from being spread out all over the code */
1438 static void vm_imap_delete(int msgnum, struct ast_vm_user *vmu)
1441 struct vm_state *vms;
1442 unsigned long messageNum;
1444 /* Greetings aren't stored in IMAP, so we can't delete them there */
1449 if (!(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && !(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
1450 ast_log(LOG_WARNING, "Couldn't find a vm_state for mailbox %s. Unable to set \\DELETED flag for message %d\n", vmu->mailbox, msgnum);
1454 /* find real message number based on msgnum */
1455 /* this may be an index into vms->msgArray based on the msgnum. */
1456 messageNum = vms->msgArray[msgnum];
1457 if (messageNum == 0) {
1458 ast_log(LOG_WARNING, "msgnum %d, mailbox message %lu is zero.\n",msgnum,messageNum);
1461 if (option_debug > 2)
1462 ast_log(LOG_DEBUG, "deleting msgnum %d, which is mailbox message %lu\n",msgnum,messageNum);
1463 /* delete message */
1464 snprintf (arg, sizeof(arg), "%lu",messageNum);
1465 mail_setflag (vms->mailstream,arg,"\\DELETED");
1468 static int imap_retrieve_greeting (const char *dir, const int msgnum, struct ast_vm_user *vmu)
1470 struct vm_state *vms_p;
1471 char *file, *filename;
1476 /* This function is only used for retrieval of IMAP greetings
1477 * regular messages are not retrieved this way, nor are greetings
1478 * if they are stored locally*/
1479 if (msgnum > -1 || !imapgreetings) {
1482 file = strrchr(ast_strdupa(dir), '/');
1486 ast_debug (1, "Failed to procure file name from directory passed.\n");
1491 /* check if someone is accessing this box right now... */
1492 if (!(vms_p = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) ||!(vms_p = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
1493 ast_log(AST_LOG_ERROR, "Voicemail state not found!\n");
1497 /* Greetings will never have a prepended message */
1498 *vms_p->introfn = '\0';
1500 ret = init_mailstream(vms_p, GREETINGS_FOLDER);
1501 if (!vms_p->mailstream) {
1502 ast_log(AST_LOG_ERROR, "IMAP mailstream is NULL\n");
1506 /*XXX Yuck, this could probably be done a lot better */
1507 for (i = 0; i < vms_p->mailstream->nmsgs; i++) {
1508 mail_fetchstructure(vms_p->mailstream, i + 1, &body);
1509 /* We have the body, now we extract the file name of the first attachment. */
1510 if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
1511 attachment = ast_strdupa(body->nested.part->next->body.parameter->value);
1513 ast_log(AST_LOG_ERROR, "There is no file attached to this IMAP message.\n");
1516 filename = strsep(&attachment, ".");
1517 if (!strcmp(filename, file)) {
1518 ast_copy_string(vms_p->fn, dir, sizeof(vms_p->fn));
1519 vms_p->msgArray[vms_p->curmsg] = i + 1;
1520 save_body(body, vms_p, "2", attachment, 0);
1528 static int imap_retrieve_file(const char *dir, const int msgnum, const char *mailbox, const char *context)
1531 char *header_content;
1532 char *attachedfilefmt;
1534 struct vm_state *vms;
1535 char text_file[PATH_MAX];
1536 FILE *text_file_ptr;
1538 struct ast_vm_user *vmu;
1540 if (!(vmu = find_user(NULL, context, mailbox))) {
1541 ast_log(LOG_WARNING, "Couldn't find user with mailbox %s@%s\n", mailbox, context);
1546 if (imapgreetings) {
1547 res = imap_retrieve_greeting(dir, msgnum, vmu);
1555 /* Before anything can happen, we need a vm_state so that we can
1556 * actually access the imap server through the vms->mailstream
1558 if (!(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && !(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
1559 /* This should not happen. If it does, then I guess we'd
1560 * need to create the vm_state, extract which mailbox to
1561 * open, and then set up the msgArray so that the correct
1562 * IMAP message could be accessed. If I have seen correctly
1563 * though, the vms should be obtainable from the vmstates list
1564 * and should have its msgArray properly set up.
1566 ast_log(LOG_ERROR, "Couldn't find a vm_state for mailbox %s!!! Oh no!\n", vmu->mailbox);
1571 make_file(vms->fn, sizeof(vms->fn), dir, msgnum);
1572 snprintf(vms->introfn, sizeof(vms->introfn), "%sintro", vms->fn);
1574 /* Don't try to retrieve a message from IMAP if it already is on the file system */
1575 if (ast_fileexists(vms->fn, NULL, NULL) > 0) {
1580 if (option_debug > 2)
1581 ast_log (LOG_DEBUG,"Before mail_fetchheaders, curmsg is: %d, imap messages is %lu\n", msgnum, vms->msgArray[msgnum]);
1582 if (vms->msgArray[msgnum] == 0) {
1583 ast_log (LOG_WARNING,"Trying to access unknown message\n");
1588 /* This will only work for new messages... */
1589 header_content = mail_fetchheader (vms->mailstream, vms->msgArray[msgnum]);
1590 /* empty string means no valid header */
1591 if (ast_strlen_zero(header_content)) {
1592 ast_log (LOG_ERROR,"Could not fetch header for message number %ld\n",vms->msgArray[msgnum]);
1597 mail_fetchstructure (vms->mailstream,vms->msgArray[msgnum],&body);
1599 /* We have the body, now we extract the file name of the first attachment. */
1600 if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
1601 attachedfilefmt = ast_strdupa(body->nested.part->next->body.parameter->value);
1603 ast_log(LOG_ERROR, "There is no file attached to this IMAP message.\n");
1608 /* Find the format of the attached file */
1610 strsep(&attachedfilefmt, ".");
1611 if (!attachedfilefmt) {
1612 ast_log(LOG_ERROR, "File format could not be obtained from IMAP message attachment\n");
1617 save_body(body, vms, "2", attachedfilefmt, 0);
1618 if (save_body(body, vms, "3", attachedfilefmt, 1)) {
1619 *vms->introfn = '\0';
1622 /* Get info from headers!! */
1623 snprintf(text_file, sizeof(text_file), "%s.%s", vms->fn, "txt");
1625 if (!(text_file_ptr = fopen(text_file, "w"))) {
1626 ast_log(LOG_WARNING, "Unable to open/create file %s: %s\n", text_file, strerror(errno));
1629 fprintf(text_file_ptr, "%s\n", "[message]");
1631 get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Name:", buf, sizeof(buf));
1632 fprintf(text_file_ptr, "callerid=\"%s\" ", S_OR(buf, ""));
1633 get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Num:", buf, sizeof(buf));
1634 fprintf(text_file_ptr, "<%s>\n", S_OR(buf, ""));
1635 get_header_by_tag(header_content, "X-Asterisk-VM-Context:", buf, sizeof(buf));
1636 fprintf(text_file_ptr, "context=%s\n", S_OR(buf, ""));
1637 get_header_by_tag(header_content, "X-Asterisk-VM-Orig-time:", buf, sizeof(buf));
1638 fprintf(text_file_ptr, "origtime=%s\n", S_OR(buf, ""));
1639 get_header_by_tag(header_content, "X-Asterisk-VM-Duration:", buf, sizeof(buf));
1640 fprintf(text_file_ptr, "duration=%s\n", S_OR(buf, ""));
1641 get_header_by_tag(header_content, "X-Asterisk-VM-Category:", buf, sizeof(buf));
1642 fprintf(text_file_ptr, "category=%s\n", S_OR(buf, ""));
1643 get_header_by_tag(header_content, "X-Asterisk-VM-Flag:", buf, sizeof(buf));
1644 fprintf(text_file_ptr, "flag=%s\n", S_OR(buf, ""));
1645 fclose(text_file_ptr);
1652 static int folder_int(const char *folder)
1654 /*assume a NULL folder means INBOX*/
1658 if (!strcasecmp(folder, imapfolder))
1660 if (!strcasecmp(folder, "INBOX"))
1663 else if (!strcasecmp(folder, "Old"))
1665 else if (!strcasecmp(folder, "Work"))
1667 else if (!strcasecmp(folder, "Family"))
1669 else if (!strcasecmp(folder, "Friends"))
1671 else if (!strcasecmp(folder, "Cust1"))
1673 else if (!strcasecmp(folder, "Cust2"))
1675 else if (!strcasecmp(folder, "Cust3"))
1677 else if (!strcasecmp(folder, "Cust4"))
1679 else if (!strcasecmp(folder, "Cust5"))
1681 else /*assume they meant INBOX if folder is not found otherwise*/
1686 * \brief Gets the number of messages that exist in a mailbox folder.
1691 * This method is used when IMAP backend is used.
1692 * \return The number of messages in this mailbox folder (zero or more).
1694 static int messagecount(const char *context, const char *mailbox, const char *folder)
1699 struct ast_vm_user *vmu, vmus;
1700 struct vm_state *vms_p;
1702 int fold = folder_int(folder);
1705 /* If URGENT, then look at INBOX */
1711 if (ast_strlen_zero(mailbox))
1714 /* We have to get the user before we can open the stream! */
1715 vmu = find_user(&vmus, context, mailbox);
1717 ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailbox, context);
1720 /* No IMAP account available */
1721 if (vmu->imapuser[0] == '\0') {
1722 ast_log(AST_LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
1727 /* No IMAP account available */
1728 if (vmu->imapuser[0] == '\0') {
1729 ast_log(AST_LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
1734 /* check if someone is accessing this box right now... */
1735 vms_p = get_vm_state_by_imapuser(vmu->imapuser,1);
1737 vms_p = get_vm_state_by_mailbox(mailbox, context, 1);
1740 ast_debug(3, "Returning before search - user is logged in\n");
1741 if (fold == 0) { /* INBOX */
1742 return vms_p->newmessages;
1744 if (fold == 1) { /* Old messages */
1745 return vms_p->oldmessages;
1747 if (fold == 11) {/*Urgent messages*/
1748 return vms_p->urgentmessages;
1752 /* add one if not there... */
1753 vms_p = get_vm_state_by_imapuser(vmu->imapuser,0);
1755 vms_p = get_vm_state_by_mailbox(mailbox, context, 0);
1759 vms_p = create_vm_state_from_user(vmu);
1761 ret = init_mailstream(vms_p, fold);
1762 if (!vms_p->mailstream) {
1763 ast_log(AST_LOG_ERROR, "Houston we have a problem - IMAP mailstream is NULL\n");
1767 pgm = mail_newsearchpgm ();
1768 hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", (char *)mailbox);
1774 /* In the special case where fold is 1 (old messages) we have to do things a bit
1775 * differently. Old messages are stored in the INBOX but are marked as "seen"
1781 /* look for urgent messages */
1789 vms_p->vmArrayIndex = 0;
1790 mail_search_full (vms_p->mailstream, NULL, pgm, NIL);
1791 if (fold == 0 && urgent == 0)
1792 vms_p->newmessages = vms_p->vmArrayIndex;
1794 vms_p->oldmessages = vms_p->vmArrayIndex;
1795 if (fold == 0 && urgent == 1)
1796 vms_p->urgentmessages = vms_p->vmArrayIndex;
1797 /*Freeing the searchpgm also frees the searchhdr*/
1798 mail_free_searchpgm(&pgm);
1800 return vms_p->vmArrayIndex;
1802 mail_ping(vms_p->mailstream);
1807 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)
1809 char *myserveremail = serveremail;
1811 char introfn[PATH_MAX];
1815 char tmp[80] = "/tmp/astmail-XXXXXX";
1820 int ret; /* for better error checking */
1821 char *imap_flags = NIL;
1823 /* Set urgent flag for IMAP message */
1824 if (!ast_strlen_zero(flag) && !strcmp(flag, "Urgent")) {
1825 ast_debug(3, "Setting message flag \\\\FLAGGED.\n");
1826 imap_flags="\\FLAGGED";
1829 /* Attach only the first format */
1830 fmt = ast_strdupa(fmt);
1832 strsep(&stringp, "|");
1834 if (!ast_strlen_zero(vmu->serveremail))
1835 myserveremail = vmu->serveremail;
1838 make_file(fn, sizeof(fn), dir, msgnum);
1840 ast_copy_string (fn, dir, sizeof(fn));
1842 snprintf(introfn, sizeof(introfn), "%sintro", fn);
1843 if (ast_fileexists(introfn, NULL, NULL) <= 0) {
1847 if (ast_strlen_zero(vmu->email)) {
1848 /* We need the vmu->email to be set when we call make_email_file, but
1849 * if we keep it set, a duplicate e-mail will be created. So at the end
1850 * of this function, we will revert back to an empty string if tempcopy
1853 ast_copy_string(vmu->email, vmu->imapuser, sizeof(vmu->email));
1857 if (!strcmp(fmt, "wav49"))
1859 ast_debug(3, "Storing file '%s', format '%s'\n", fn, fmt);
1861 /* Make a temporary file instead of piping directly to sendmail, in case the mail
1863 if (!(p = vm_mkftemp(tmp))) {
1864 ast_log(AST_LOG_WARNING, "Unable to store '%s' (can't create temporary file)\n", fn);
1866 *(vmu->email) = '\0';
1870 if (msgnum < 0 && imapgreetings) {
1871 if ((ret = init_mailstream(vms, GREETINGS_FOLDER))) {
1872 ast_log(AST_LOG_WARNING, "Unable to open mailstream.\n");
1875 imap_delete_old_greeting(fn, vms);
1878 make_email_file(p, myserveremail, vmu, msgnum, vmu->context, vmu->mailbox, S_OR(chan->cid.cid_num, NULL), S_OR(chan->cid.cid_name, NULL), fn, introfn, fmt, duration, 1, chan, NULL, 1, flag);
1879 /* read mail file to memory */
1882 if (!(buf = ast_malloc(len + 1))) {
1883 ast_log(AST_LOG_ERROR, "Can't allocate %ld bytes to read message\n", len + 1);
1886 *(vmu->email) = '\0';
1889 if (fread(buf, len, 1, p) < len) {
1891 ast_log(LOG_ERROR, "Short read while reading in mail file.\n");
1895 ((char *)buf)[len] = '\0';
1896 INIT(&str, mail_string, buf, len);
1897 ret = init_mailstream(vms, NEW_FOLDER);
1899 imap_mailbox_name(mailbox, sizeof(mailbox), vms, NEW_FOLDER, 1);
1900 if(!mail_append_full(vms->mailstream, mailbox, imap_flags, NIL, &str))
1901 ast_log(LOG_ERROR, "Error while sending the message to %s\n", mailbox);
1906 ast_log(LOG_ERROR, "Could not initialize mailstream for %s\n",mailbox);
1912 ast_debug(3, "%s stored\n", fn);
1915 *(vmu->email) = '\0';
1922 * \brief Gets the number of messages that exist in the inbox folder.
1923 * \param mailbox_context
1924 * \param newmsgs The variable that is updated with the count of new messages within this inbox.
1925 * \param oldmsgs The variable that is updated with the count of old messages within this inbox.
1926 * \param urgentmsgs The variable that is updated with the count of urgent messages within this inbox.
1928 * This method is used when IMAP backend is used.
1929 * Simultaneously determines the count of new,old, and urgent messages. The total messages would then be the sum of these three.
1931 * \return zero on success, -1 on error.
1934 static int inboxcount2(const char *mailbox_context, int *urgentmsgs, int *newmsgs, int *oldmsgs)
1936 char tmp[PATH_MAX] = "";
1948 ast_debug(3,"Mailbox is set to %s\n",mailbox_context);
1949 /* If no mailbox, return immediately */
1950 if (ast_strlen_zero(mailbox_context))
1953 ast_copy_string(tmp, mailbox_context, sizeof(tmp));
1954 context = strchr(tmp, '@');
1955 if (strchr(mailbox_context, ',')) {
1956 int tmpnew, tmpold, tmpurgent;
1957 ast_copy_string(tmp, mailbox_context, sizeof(tmp));
1959 while ((cur = strsep(&mb, ", "))) {
1960 if (!ast_strlen_zero(cur)) {
1961 if (inboxcount2(cur, urgentmsgs ? &tmpurgent : NULL, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
1969 *urgentmsgs += tmpurgent;
1980 context = "default";
1981 mailboxnc = (char *)mailbox_context;
1984 if ((*newmsgs = messagecount(context, mailboxnc, imapfolder)) < 0)
1988 if ((*oldmsgs = messagecount(context, mailboxnc, "Old")) < 0)
1992 if((*urgentmsgs = messagecount(context, mailboxnc, "Urgent")) < 0)
1998 static int inboxcount(const char *mailbox_context, int *newmsgs, int *oldmsgs)
2000 return inboxcount2(mailbox_context, NULL, newmsgs, oldmsgs);
2004 * \brief Determines if the given folder has messages.
2005 * \param mailbox The @ delimited string for user@context. If no context is found, uses 'default' for the context.
2006 * \param folder the folder to look in
2008 * This function is used when the mailbox is stored in an IMAP back end.
2009 * This invokes the messagecount(). Here we are interested in the presence of messages (> 0) only, not the actual count.
2010 * \return 1 if the folder has one or more messages. zero otherwise.
2013 static int has_voicemail(const char *mailbox, const char *folder)
2015 char tmp[256], *tmp2, *box, *context;
2016 ast_copy_string(tmp, mailbox, sizeof(tmp));
2018 if (strchr(tmp2, ',')) {
2019 while ((box = strsep(&tmp2, ","))) {
2020 if (!ast_strlen_zero(box)) {
2021 if (has_voicemail(box, folder))
2026 if ((context= strchr(tmp, '@')))
2029 context = "default";
2030 return messagecount(context, tmp, folder) ? 1 : 0;
2034 * \brief Copies a message from one mailbox to another.
2044 * This works with IMAP storage based mailboxes.
2046 * \return zero on success, -1 on error.
2048 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, char *flag)
2050 struct vm_state *sendvms = NULL, *destvms = NULL;
2051 char messagestring[10]; /*I guess this could be a problem if someone has more than 999999999 messages...*/
2052 if (msgnum >= recip->maxmsg) {
2053 ast_log(LOG_WARNING, "Unable to copy mail, mailbox %s is full\n", recip->mailbox);
2056 if (!(sendvms = get_vm_state_by_imapuser(vmu->imapuser, 0))) {
2057 ast_log(LOG_ERROR, "Couldn't get vm_state for originator's mailbox!!\n");
2060 if (!(destvms = get_vm_state_by_imapuser(recip->imapuser, 0))) {
2061 ast_log(LOG_ERROR, "Couldn't get vm_state for destination mailbox!\n");
2064 snprintf(messagestring, sizeof(messagestring), "%ld", sendvms->msgArray[msgnum]);
2065 if ((mail_copy(sendvms->mailstream, messagestring, (char *) mbox(imbox)) == T))
2067 ast_log(LOG_WARNING, "Unable to copy message from mailbox %s to mailbox %s\n", vmu->mailbox, recip->mailbox);
2071 static void imap_mailbox_name(char *spec, size_t len, struct vm_state *vms, int box, int use_folder)
2073 char tmp[256], *t = tmp;
2074 size_t left = sizeof(tmp);
2076 if (box == OLD_FOLDER) {
2077 ast_copy_string(vms->curbox, mbox(NEW_FOLDER), sizeof(vms->curbox));
2079 ast_copy_string(vms->curbox, mbox(box), sizeof(vms->curbox));
2082 if (box == NEW_FOLDER) {
2083 ast_copy_string(vms->vmbox, "vm-INBOX", sizeof(vms->vmbox));
2085 snprintf(vms->vmbox, sizeof(vms->vmbox), "vm-%s", mbox(box));
2088 /* Build up server information */
2089 ast_build_string(&t, &left, "{%s:%s/imap", imapserver, imapport);
2091 /* Add authentication user if present */
2092 if (!ast_strlen_zero(authuser))
2093 ast_build_string(&t, &left, "/authuser=%s", authuser);
2095 /* Add flags if present */
2096 if (!ast_strlen_zero(imapflags))
2097 ast_build_string(&t, &left, "/%s", imapflags);
2099 /* End with username */
2100 ast_build_string(&t, &left, "/user=%s}", vms->imapuser);
2101 if (box == NEW_FOLDER || box == OLD_FOLDER)
2102 snprintf(spec, len, "%s%s", tmp, use_folder? imapfolder: "INBOX");
2103 else if (box == GREETINGS_FOLDER)
2104 snprintf(spec, len, "%s%s", tmp, greetingfolder);
2105 else { /* Other folders such as Friends, Family, etc... */
2106 if (!ast_strlen_zero(imapparentfolder)) {
2107 /* imapparentfolder would typically be set to INBOX */
2108 snprintf(spec, len, "%s%s%c%s", tmp, imapparentfolder, delimiter, mbox(box));
2110 snprintf(spec, len, "%s%s", tmp, mbox(box));
2115 static int init_mailstream(struct vm_state *vms, int box)
2117 MAILSTREAM *stream = NIL;
2122 ast_log (LOG_ERROR,"vm_state is NULL!\n");
2125 if (option_debug > 2)
2126 ast_log (LOG_DEBUG,"vm_state user is:%s\n",vms->imapuser);
2127 if (vms->mailstream == NIL || !vms->mailstream) {
2129 ast_log (LOG_DEBUG,"mailstream not set.\n");
2131 stream = vms->mailstream;
2133 /* debug = T; user wants protocol telemetry? */
2134 debug = NIL; /* NO protocol telemetry? */
2136 if (delimiter == '\0') { /* did not probe the server yet */
2138 #ifdef USE_SYSTEM_IMAP
2139 #include <imap/linkage.c>
2140 #elif defined(USE_SYSTEM_CCLIENT)
2141 #include <c-client/linkage.c>
2143 #include "linkage.c"
2145 /* Connect to INBOX first to get folders delimiter */
2146 imap_mailbox_name(tmp, sizeof(tmp), vms, 0, 1);
2147 ast_mutex_lock(&vms->lock);
2148 stream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
2149 ast_mutex_unlock(&vms->lock);
2150 if (stream == NIL) {
2151 ast_log (LOG_ERROR, "Can't connect to imap server %s\n", tmp);
2154 get_mailbox_delimiter(stream);
2155 /* update delimiter in imapfolder */
2156 for (cp = imapfolder; *cp; cp++)
2160 /* Now connect to the target folder */
2161 imap_mailbox_name(tmp, sizeof(tmp), vms, box, 1);
2162 if (option_debug > 2)
2163 ast_log (LOG_DEBUG,"Before mail_open, server: %s, box:%d\n", tmp, box);
2164 ast_mutex_lock(&vms->lock);
2165 vms->mailstream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
2166 ast_mutex_unlock(&vms->lock);
2167 if (vms->mailstream == NIL) {
2174 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box)
2178 int ret, urgent = 0;
2180 /* If Urgent, then look at INBOX */
2186 ast_copy_string(vms->imapuser,vmu->imapuser, sizeof(vms->imapuser));
2187 ast_debug(3,"Before init_mailstream, user is %s\n",vmu->imapuser);
2189 if ((ret = init_mailstream(vms, box)) || !vms->mailstream) {
2190 ast_log(AST_LOG_ERROR, "Could not initialize mailstream\n");
2194 create_dirpath(vms->curdir, sizeof(vms->curdir), vmu->context, vms->username, vms->curbox);
2198 ast_debug(3, "Mailbox name set to: %s, about to check quotas\n", mbox(box));
2199 check_quota(vms,(char *)mbox(box));
2202 pgm = mail_newsearchpgm();
2204 /* Check IMAP folder for Asterisk messages only... */
2205 hdr = mail_newsearchheader("X-Asterisk-VM-Extension", vmu->mailbox);
2210 /* if box = NEW_FOLDER, check for new, if box = OLD_FOLDER, check for read */
2211 if (box == NEW_FOLDER && urgent == 1) {
2216 } else if (box == NEW_FOLDER && urgent == 0) {
2221 } else if (box == OLD_FOLDER) {
2226 ast_debug(3,"Before mail_search_full, user is %s\n",vmu->imapuser);
2228 vms->vmArrayIndex = 0;
2229 mail_search_full (vms->mailstream, NULL, pgm, NIL);
2230 vms->lastmsg = vms->vmArrayIndex - 1;
2231 mail_free_searchpgm(&pgm);
2236 static void write_file(char *filename, char *buffer, unsigned long len)
2240 output = fopen (filename, "w");
2241 if (fwrite(buffer, len, 1, output) < len) {
2242 if (ferror(output)) {
2243 ast_log(LOG_ERROR, "Short write while writing e-mail body.\n");
2249 static void update_messages_by_imapuser(const char *user, unsigned long number)
2251 struct vmstate *vlist = NULL;
2253 AST_LIST_LOCK(&vmstates);
2254 AST_LIST_TRAVERSE(&vmstates, vlist, list) {
2256 ast_debug(3, "error: vms is NULL for %s\n", user);
2259 if (!vlist->vms->imapuser) {
2260 ast_debug(3, "error: imapuser is NULL for %s\n", user);
2263 ast_debug(3, "saving mailbox message number %lu as message %d. Interactive set to %d\n", number, vlist->vms->vmArrayIndex, vlist->vms->interactive);
2264 vlist->vms->msgArray[vlist->vms->vmArrayIndex++] = number;
2266 AST_LIST_UNLOCK(&vmstates);
2269 void mm_searched(MAILSTREAM *stream, unsigned long number)
2271 char *mailbox = stream->mailbox, buf[1024] = "", *user;
2273 if (!(user = get_user_by_mailbox(mailbox, buf, sizeof(buf))))
2276 update_messages_by_imapuser(user, number);
2279 static struct ast_vm_user *find_user_realtime_imapuser(const char *imapuser)
2281 struct ast_variable *var;
2282 struct ast_vm_user *vmu;
2284 vmu = ast_calloc(1, sizeof *vmu);
2287 ast_set_flag(vmu, VM_ALLOCED);
2288 populate_defaults(vmu);
2290 var = ast_load_realtime("voicemail", "imapuser", imapuser, NULL);
2292 apply_options_full(vmu, var);
2293 ast_variables_destroy(var);
2301 /* Interfaces to C-client */
2303 void mm_exists(MAILSTREAM * stream, unsigned long number)
2305 /* mail_ping will callback here if new mail! */
2306 ast_debug(4, "Entering EXISTS callback for message %ld\n", number);
2307 if (number == 0) return;
2312 void mm_expunged(MAILSTREAM * stream, unsigned long number)
2314 /* mail_ping will callback here if expunged mail! */
2315 ast_debug(4, "Entering EXPUNGE callback for message %ld\n", number);
2316 if (number == 0) return;
2321 void mm_flags(MAILSTREAM * stream, unsigned long number)
2323 /* mail_ping will callback here if read mail! */
2324 ast_debug(4, "Entering FLAGS callback for message %ld\n", number);
2325 if (number == 0) return;
2330 void mm_notify(MAILSTREAM * stream, char *string, long errflg)
2332 ast_debug(5, "Entering NOTIFY callback, errflag is %ld, string is %s\n", errflg, string);
2333 mm_log (string, errflg);
2337 void mm_list(MAILSTREAM * stream, int delim, char *mailbox, long attributes)
2339 if (delimiter == '\0') {
2343 ast_debug(5, "Delimiter set to %c and mailbox %s\n",delim, mailbox);
2344 if (attributes & LATT_NOINFERIORS)
2345 ast_debug(5, "no inferiors\n");
2346 if (attributes & LATT_NOSELECT)
2347 ast_debug(5, "no select\n");
2348 if (attributes & LATT_MARKED)
2349 ast_debug(5, "marked\n");
2350 if (attributes & LATT_UNMARKED)
2351 ast_debug(5, "unmarked\n");
2355 void mm_lsub(MAILSTREAM * stream, int delim, char *mailbox, long attributes)
2357 ast_debug(5, "Delimiter set to %c and mailbox %s\n",delim, mailbox);
2358 if (attributes & LATT_NOINFERIORS)
2359 ast_debug(5, "no inferiors\n");
2360 if (attributes & LATT_NOSELECT)
2361 ast_debug(5, "no select\n");
2362 if (attributes & LATT_MARKED)
2363 ast_debug(5, "marked\n");
2364 if (attributes & LATT_UNMARKED)
2365 ast_debug(5, "unmarked\n");
2369 void mm_status(MAILSTREAM * stream, char *mailbox, MAILSTATUS * status)
2371 ast_log(AST_LOG_NOTICE, " Mailbox %s", mailbox);
2372 if (status->flags & SA_MESSAGES)
2373 ast_log(AST_LOG_NOTICE, ", %lu messages", status->messages);
2374 if (status->flags & SA_RECENT)
2375 ast_log(AST_LOG_NOTICE, ", %lu recent", status->recent);
2376 if (status->flags & SA_UNSEEN)
2377 ast_log(AST_LOG_NOTICE, ", %lu unseen", status->unseen);
2378 if (status->flags & SA_UIDVALIDITY)
2379 ast_log(AST_LOG_NOTICE, ", %lu UID validity", status->uidvalidity);
2380 if (status->flags & SA_UIDNEXT)
2381 ast_log(AST_LOG_NOTICE, ", %lu next UID", status->uidnext);
2382 ast_log(AST_LOG_NOTICE, "\n");
2386 void mm_log(char *string, long errflg)
2388 switch ((short) errflg) {
2390 ast_debug(1,"IMAP Info: %s\n", string);
2394 ast_log(AST_LOG_WARNING, "IMAP Warning: %s\n", string);
2397 ast_log(AST_LOG_ERROR, "IMAP Error: %s\n", string);
2403 void mm_dlog(char *string)
2405 ast_log(AST_LOG_NOTICE, "%s\n", string);
2409 void mm_login(NETMBX * mb, char *user, char *pwd, long trial)
2411 struct ast_vm_user *vmu;
2413 ast_debug(4, "Entering callback mm_login\n");
2415 ast_copy_string(user, mb->user, MAILTMPLEN);
2417 /* We should only do this when necessary */
2418 if (!ast_strlen_zero(authpassword)) {
2419 ast_copy_string(pwd, authpassword, MAILTMPLEN);
2421 AST_LIST_TRAVERSE(&users, vmu, list) {
2422 if (!strcasecmp(mb->user, vmu->imapuser)) {
2423 ast_copy_string(pwd, vmu->imappassword, MAILTMPLEN);
2428 if ((vmu = find_user_realtime_imapuser(mb->user))) {
2429 ast_copy_string(pwd, vmu->imappassword, MAILTMPLEN);
2437 void mm_critical(MAILSTREAM * stream)
2442 void mm_nocritical(MAILSTREAM * stream)
2447 long mm_diskerror(MAILSTREAM * stream, long errcode, long serious)
2449 kill (getpid (), SIGSTOP);
2454 void mm_fatal(char *string)
2456 ast_log(AST_LOG_ERROR, "IMAP access FATAL error: %s\n", string);
2459 /* C-client callback to handle quota */
2460 static void mm_parsequota(MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pquota)
2462 struct vm_state *vms;
2463 char *mailbox = stream->mailbox, *user;
2464 char buf[1024] = "";
2465 unsigned long usage = 0, limit = 0;
2468 usage = pquota->usage;
2469 limit = pquota->limit;
2470 pquota = pquota->next;
2473 if (!(user = get_user_by_mailbox(mailbox, buf, sizeof(buf))) || !(vms = get_vm_state_by_imapuser(user, 2))) {
2474 ast_log(AST_LOG_ERROR, "No state found.\n");
2478 ast_debug(3, "User %s usage is %lu, limit is %lu\n", user, usage, limit);
2480 vms->quota_usage = usage;
2481 vms->quota_limit = limit;
2484 static char *get_header_by_tag(char *header, char *tag, char *buf, size_t len)
2486 char *start, *eol_pnt;
2489 if (ast_strlen_zero(header) || ast_strlen_zero(tag))
2492 taglen = strlen(tag) + 1;
2496 if (!(start = strstr(header, tag)))
2499 /* Since we can be called multiple times we should clear our buffer */
2500 memset(buf, 0, len);
2502 ast_copy_string(buf, start+taglen, len);
2503 if ((eol_pnt = strchr(buf,'\r')) || (eol_pnt = strchr(buf,'\n')))
2508 static char *get_user_by_mailbox(char *mailbox, char *buf, size_t len)
2510 char *start, *quote, *eol_pnt;
2512 if (ast_strlen_zero(mailbox))
2515 if (!(start = strstr(mailbox, "/user=")))
2518 ast_copy_string(buf, start+6, len);
2520 if (!(quote = strchr(buf, '\"'))) {
2521 if (!(eol_pnt = strchr(buf, '/')))
2522 eol_pnt = strchr(buf,'}');
2526 eol_pnt = strchr(buf+1,'\"');
2532 static struct vm_state *create_vm_state_from_user(struct ast_vm_user *vmu)
2534 struct vm_state *vms_p;
2536 if (option_debug > 4)
2537 ast_log(AST_LOG_DEBUG,"Adding new vmstate for %s\n",vmu->imapuser);
2538 if (!(vms_p = ast_calloc(1, sizeof(*vms_p))))
2540 ast_copy_string(vms_p->imapuser, vmu->imapuser, sizeof(vms_p->imapuser));
2541 ast_copy_string(vms_p->username, vmu->mailbox, sizeof(vms_p->username)); /* save for access from interactive entry point */
2542 ast_copy_string(vms_p->context, vmu->context, sizeof(vms_p->context));
2543 vms_p->mailstream = NIL; /* save for access from interactive entry point */
2544 if (option_debug > 4)
2545 ast_log(AST_LOG_DEBUG,"Copied %s to %s\n",vmu->imapuser,vms_p->imapuser);
2547 /* set mailbox to INBOX! */
2548 ast_copy_string(vms_p->curbox, mbox(0), sizeof(vms_p->curbox));
2549 init_vm_state(vms_p);
2550 vmstate_insert(vms_p);
2554 static struct vm_state *get_vm_state_by_imapuser(char *user, int interactive)
2556 struct vmstate *vlist = NULL;
2558 AST_LIST_LOCK(&vmstates);
2559 AST_LIST_TRAVERSE(&vmstates, vlist, list) {
2561 ast_debug(3, "error: vms is NULL for %s\n", user);
2564 if (!vlist->vms->imapuser) {
2565 ast_debug(3, "error: imapuser is NULL for %s\n", user);
2569 if (!strcmp(vlist->vms->imapuser, user) && (interactive == 2 || vlist->vms->interactive == interactive)) {
2570 AST_LIST_UNLOCK(&vmstates);
2574 AST_LIST_UNLOCK(&vmstates);
2576 ast_debug(3, "%s not found in vmstates\n", user);
2581 static struct vm_state *get_vm_state_by_mailbox(const char *mailbox, const char *context, int interactive)
2584 struct vmstate *vlist = NULL;
2585 const char *local_context = S_OR(context, "default");
2587 AST_LIST_LOCK(&vmstates);
2588 AST_LIST_TRAVERSE(&vmstates, vlist, list) {
2590 ast_debug(3, "error: vms is NULL for %s\n", mailbox);
2593 if (!vlist->vms->username || !vlist->vms->context) {
2594 ast_debug(3, "error: username is NULL for %s\n", mailbox);
2598 ast_debug(3, "comparing mailbox %s@%s (i=%d) to vmstate mailbox %s@%s (i=%d)\n", mailbox, local_context, interactive, vlist->vms->username, vlist->vms->context, vlist->vms->interactive);
2600 if (!strcmp(vlist->vms->username,mailbox) && !strcmp(vlist->vms->context, local_context) && vlist->vms->interactive == interactive) {
2601 ast_debug(3, "Found it!\n");
2602 AST_LIST_UNLOCK(&vmstates);
2606 AST_LIST_UNLOCK(&vmstates);
2608 ast_debug(3, "%s not found in vmstates\n", mailbox);
2613 static void vmstate_insert(struct vm_state *vms)
2616 struct vm_state *altvms;
2618 /* If interactive, it probably already exists, and we should
2619 use the one we already have since it is more up to date.
2620 We can compare the username to find the duplicate */
2621 if (vms->interactive == 1) {
2622 altvms = get_vm_state_by_mailbox(vms->username, vms->context, 0);
2624 ast_debug(3, "Duplicate mailbox %s, copying message info...\n",vms->username);
2625 vms->newmessages = altvms->newmessages;
2626 vms->oldmessages = altvms->oldmessages;
2627 vms->vmArrayIndex = altvms->vmArrayIndex;
2628 vms->lastmsg = altvms->lastmsg;
2629 vms->curmsg = altvms->curmsg;
2630 /* get a pointer to the persistent store */
2631 vms->persist_vms = altvms;
2632 /* Reuse the mailstream? */
2633 vms->mailstream = altvms->mailstream;
2634 /* vms->mailstream = NIL; */
2638 if (!(v = ast_calloc(1, sizeof(*v))))
2643 ast_debug(3, "Inserting vm_state for user:%s, mailbox %s\n",vms->imapuser,vms->username);
2645 AST_LIST_LOCK(&vmstates);
2646 AST_LIST_INSERT_TAIL(&vmstates, v, list);
2647 AST_LIST_UNLOCK(&vmstates);
2650 static void vmstate_delete(struct vm_state *vms)
2652 struct vmstate *vc = NULL;
2653 struct vm_state *altvms = NULL;
2655 /* If interactive, we should copy pertinent info
2656 back to the persistent state (to make update immediate) */
2657 if (vms->interactive == 1 && (altvms = vms->persist_vms)) {
2658 ast_debug(3, "Duplicate mailbox %s, copying message info...\n", vms->username);
2659 altvms->newmessages = vms->newmessages;
2660 altvms->oldmessages = vms->oldmessages;
2661 altvms->updated = 1;
2664 ast_debug(3, "Removing vm_state for user:%s, mailbox %s\n", vms->imapuser, vms->username);
2666 AST_LIST_LOCK(&vmstates);
2667 AST_LIST_TRAVERSE_SAFE_BEGIN(&vmstates, vc, list) {
2668 if (vc->vms == vms) {
2669 AST_LIST_REMOVE_CURRENT(list);
2673 AST_LIST_TRAVERSE_SAFE_END
2674 AST_LIST_UNLOCK(&vmstates);
2677 ast_mutex_destroy(&vc->vms->lock);
2681 ast_log(AST_LOG_ERROR, "No vmstate found for user:%s, mailbox %s\n", vms->imapuser, vms->username);
2684 static void set_update(MAILSTREAM * stream)
2686 struct vm_state *vms;
2687 char *mailbox = stream->mailbox, *user;
2688 char buf[1024] = "";
2690 if (!(user = get_user_by_mailbox(mailbox, buf, sizeof(buf))) || !(vms = get_vm_state_by_imapuser(user, 0))) {
2691 if (user && option_debug > 2)
2692 ast_log(AST_LOG_WARNING, "User %s mailbox not found for update.\n", user);
2696 ast_debug(3, "User %s mailbox set for update.\n", user);
2698 vms->updated = 1; /* Set updated flag since mailbox changed */
2701 static void init_vm_state(struct vm_state *vms)
2704 vms->vmArrayIndex = 0;
2705 for (x = 0; x < VMSTATE_MAX_MSG_ARRAY; x++) {
2706 vms->msgArray[x] = 0;
2708 ast_mutex_init(&vms->lock);
2711 static int save_body(BODY *body, struct vm_state *vms, char *section, char *format, int is_intro)
2715 char *fn = is_intro ? vms->introfn : vms->fn;
2717 unsigned long newlen;
2720 if (!body || body == NIL)
2723 body_content = mail_fetchbody(vms->mailstream, vms->msgArray[vms->curmsg], section, &len);
2724 if (body_content != NIL) {
2725 snprintf(filename, sizeof(filename), "%s.%s", fn, format);
2726 /* ast_debug(1,body_content); */
2727 body_decoded = rfc822_base64((unsigned char *)body_content, len, &newlen);
2728 /* If the body of the file is empty, return an error */
2732 write_file(filename, (char *) body_decoded, newlen);
2734 ast_debug(5, "Body of message is NULL.\n");
2741 * \brief Get delimiter via mm_list callback
2744 * Determines the delimiter character that is used by the underlying IMAP based mail store.
2746 static void get_mailbox_delimiter(MAILSTREAM *stream) {
2748 snprintf(tmp, sizeof(tmp), "{%s}", imapserver);
2749 mail_list(stream, tmp, "*");
2753 * \brief Check Quota for user
2754 * \param vms a pointer to a vm_state struct, will use the mailstream property of this.
2755 * \param mailbox the mailbox to check the quota for.
2757 * Calls imap_getquotaroot, which will populate its results into the vm_state vms input structure.
2759 static void check_quota(struct vm_state *vms, char *mailbox) {
2760 mail_parameters(NULL, SET_QUOTA, (void *) mm_parsequota);
2761 ast_debug(3, "Mailbox name set to: %s, about to check quotas\n", mailbox);
2762 if (vms && vms->mailstream != NULL) {
2763 imap_getquotaroot(vms->mailstream, mailbox);
2765 ast_log(AST_LOG_WARNING, "Mailstream not available for mailbox: %s\n", mailbox);
2769 #endif /* IMAP_STORAGE */
2771 /*! \brief Lock file path
2772 only return failure if ast_lock_path returns 'timeout',
2773 not if the path does not exist or any other reason
2775 static int vm_lock_path(const char *path)
2777 switch (ast_lock_path(path)) {
2778 case AST_LOCK_TIMEOUT:
2787 struct generic_prepare_struct {
2793 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
2795 struct generic_prepare_struct *gps = data;
2799 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
2800 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
2801 ast_log(AST_LOG_WARNING, "SQL Alloc Handle failed!\n");
2804 res = SQLPrepare(stmt, (unsigned char *)gps->sql, SQL_NTS);
2805 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
2806 ast_log(AST_LOG_WARNING, "SQL Prepare failed![%s]\n", gps->sql);
2807 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
2810 for (i = 0; i < gps->argc; i++)
2811 SQLBindParameter(stmt, i + 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(gps->argv[i]), 0, gps->argv[i], 0, NULL);
2817 * \brief Retrieves a file from an ODBC data store.
2818 * \param dir the path to the file to be retreived.
2819 * \param msgnum the message number, such as within a mailbox folder.
2821 * This method is used by the RETRIEVE macro when mailboxes are stored in an ODBC back end.
2822 * The purpose is to get the message from the database store to the local file system, so that the message may be played, or the information file may be read.
2824 * The file is looked up by invoking a SQL on the odbc_table (default 'voicemessages') using the dir and msgnum input parameters.
2825 * The output is the message information file with the name msgnum and the extension .txt
2826 * and the message file with the extension of its format, in the directory with base file name of the msgnum.
2828 * \return 0 on success, -1 on error.
2830 static int retrieve_file(char *dir, int msgnum)
2836 void *fdm = MAP_FAILED;
2837 SQLSMALLINT colcount=0;
2844 SQLSMALLINT datatype;
2845 SQLSMALLINT decimaldigits;
2846 SQLSMALLINT nullable;
2852 char full_fn[PATH_MAX];
2854 char *argv[] = { dir, msgnums };
2855 struct generic_prepare_struct gps = { .sql = sql, .argc = 2, .argv = argv };
2857 struct odbc_obj *obj;
2858 obj = ast_odbc_request_obj(odbc_database, 0);
2860 ast_copy_string(fmt, vmfmts, sizeof(fmt));
2861 c = strchr(fmt, '|');
2864 if (!strcasecmp(fmt, "wav49"))
2866 snprintf(msgnums, sizeof(msgnums),"%d", msgnum);
2868 make_file(fn, sizeof(fn), dir, msgnum);
2870 ast_copy_string(fn, dir, sizeof(fn));
2872 /* Create the information file */
2873 snprintf(full_fn, sizeof(full_fn), "%s.txt", fn);
2875 if (!(f = fopen(full_fn, "w+"))) {
2876 ast_log(AST_LOG_WARNING, "Failed to open/create '%s'\n", full_fn);
2880 snprintf(full_fn, sizeof(full_fn), "%s.%s", fn, fmt);
2881 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE dir=? AND msgnum=?",odbc_table);
2882 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
2884 ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
2885 ast_odbc_release_obj(obj);
2888 res = SQLFetch(stmt);
2889 if (res == SQL_NO_DATA) {
2890 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
2891 ast_odbc_release_obj(obj);
2893 } else if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
2894 ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
2895 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
2896 ast_odbc_release_obj(obj);
2899 fd = open(full_fn, O_RDWR | O_CREAT | O_TRUNC, VOICEMAIL_FILE_MODE);
2901 ast_log(AST_LOG_WARNING, "Failed to write '%s': %s\n", full_fn, strerror(errno));
2902 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
2903 ast_odbc_release_obj(obj);
2906 res = SQLNumResultCols(stmt, &colcount);
2907 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
2908 ast_log(AST_LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
2909 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
2910 ast_odbc_release_obj(obj);
2914 fprintf(f, "[message]\n");
2915 for (x=0;x<colcount;x++) {
2917 collen = sizeof(coltitle);
2918 res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen,
2919 &datatype, &colsize, &decimaldigits, &nullable);
2920 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
2921 ast_log(AST_LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
2922 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
2923 ast_odbc_release_obj(obj);
2926 if (!strcasecmp(coltitle, "recording")) {
2928 res = SQLGetData(stmt, x + 1, SQL_BINARY, rowdata, 0, &colsize2);
2932 lseek(fd, fdlen - 1, SEEK_SET);
2933 if (write(fd, tmp, 1) != 1) {
2938 /* Read out in small chunks */
2939 for (offset = 0; offset < colsize2; offset += CHUNKSIZE) {
2940 if ((fdm = mmap(NULL, CHUNKSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset)) == MAP_FAILED) {
2941 ast_log(AST_LOG_WARNING, "Could not mmap the output file: %s (%d)\n", strerror(errno), errno);
2942 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
2943 ast_odbc_release_obj(obj);
2946 res = SQLGetData(stmt, x + 1, SQL_BINARY, fdm, CHUNKSIZE, NULL);
2947 munmap(fdm, CHUNKSIZE);
2948 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
2949 ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
2951 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
2952 ast_odbc_release_obj(obj);
2957 if (truncate(full_fn, fdlen) < 0) {
2958 ast_log(LOG_WARNING, "Unable to truncate '%s': %s\n", full_fn, strerror(errno));
2962 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
2963 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
2964 ast_log(AST_LOG_WARNING, "SQL Get Data error! coltitle=%s\n[%s]\n\n", coltitle, sql);
2965 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
2966 ast_odbc_release_obj(obj);
2969 if (strcasecmp(coltitle, "msgnum") && strcasecmp(coltitle, "dir") && f)
2970 fprintf(f, "%s=%s\n", coltitle, rowdata);
2973 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
2974 ast_odbc_release_obj(obj);
2976 ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
2986 * \brief Determines the highest message number in use for a given user and mailbox folder.
2988 * \param dir the folder the mailbox folder to look for messages. Used to construct the SQL where clause.
2990 * This method is used when mailboxes are stored in an ODBC back end.
2991 * Typical use to set the msgnum would be to take the value returned from this method and add one to it.
2993 * \return the value of zero or greaterto indicate the last message index in use, -1 to indicate none.
2995 static int last_message_index(struct ast_vm_user *vmu, char *dir)
3002 char *argv[] = { dir };
3003 struct generic_prepare_struct gps = { .sql = sql, .argc = 1, .argv = argv };
3005 struct odbc_obj *obj;
3006 obj = ast_odbc_request_obj(odbc_database, 0);
3008 snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=?",odbc_table);
3009 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
3011 ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
3012 ast_odbc_release_obj(obj);
3015 res = SQLFetch(stmt);
3016 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3017 ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
3018 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3019 ast_odbc_release_obj(obj);
3022 res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
3023 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3024 ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
3025 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3026 ast_odbc_release_obj(obj);
3029 if (sscanf(rowdata, "%d", &x) != 1)
3030 ast_log(AST_LOG_WARNING, "Failed to read message count!\n");
3031 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3032 ast_odbc_release_obj(obj);
3034 ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
3040 * \brief Determines if the specified message exists.
3041 * \param dir the folder the mailbox folder to look for messages.
3042 * \param msgnum the message index to query for.
3044 * This method is used when mailboxes are stored in an ODBC back end.
3046 * \return greater than zero if the message exists, zero when the message does not exist or on error.
3048 static int message_exists(char *dir, int msgnum)
3056 char *argv[] = { dir, msgnums };
3057 struct generic_prepare_struct gps = { .sql = sql, .argc = 2, .argv = argv };
3059 struct odbc_obj *obj;
3060 obj = ast_odbc_request_obj(odbc_database, 0);
3062 snprintf(msgnums, sizeof(msgnums), "%d", msgnum);
3063 snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=? AND msgnum=?",odbc_table);
3064 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
3066 ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
3067 ast_odbc_release_obj(obj);
3070 res = SQLFetch(stmt);
3071 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3072 ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
3073 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3074 ast_odbc_release_obj(obj);
3077 res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
3078 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3079 ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
3080 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3081 ast_odbc_release_obj(obj);
3084 if (sscanf(rowdata, "%d", &x) != 1)
3085 ast_log(AST_LOG_WARNING, "Failed to read message count!\n");
3086 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3087 ast_odbc_release_obj(obj);
3089 ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
3095 * \brief returns the one-based count for messages.
3097 * \param dir the folder the mailbox folder to look for messages. Used to construct the SQL where clause.
3099 * This method is used when mailboxes are stored in an ODBC back end.
3100 * The message index is zero-based, the first message will be index 0. For convenient display it is good to have the
3101 * one-based messages.
3102 * This method just calls last_message_index and returns +1 of its value.
3104 * \return the value greater than zero on success to indicate the one-based count of messages, less than zero on error.
3106 static int count_messages(struct ast_vm_user *vmu, char *dir)
3108 return last_message_index(vmu, dir) + 1;
3112 * \brief Deletes a message from the mailbox folder.
3113 * \param sdir The mailbox folder to work in.
3114 * \param smsg The message index to be deleted.
3116 * This method is used when mailboxes are stored in an ODBC back end.
3117 * The specified message is directly deleted from the database 'voicemessages' table.
3119 * \return the value greater than zero on success to indicate the number of messages, less than zero on error.
3121 static void delete_file(char *sdir, int smsg)
3126 char *argv[] = { sdir, msgnums };
3127 struct generic_prepare_struct gps = { .sql = sql, .argc = 2, .argv = argv };
3129 struct odbc_obj *obj;
3130 obj = ast_odbc_request_obj(odbc_database, 0);
3132 snprintf(msgnums, sizeof(msgnums), "%d",