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 * \author Mark Spencer <markster@digium.com>
22 * \brief Comedian Mail - Voicemail System
24 * \extref unixODBC (http://www.unixodbc.org/)
25 * \extref A source distribution of University of Washington's IMAP c-client
26 * (http://www.washington.edu/imap/)
30 * \note For information about voicemail IMAP storage, https://wiki.asterisk.org/wiki/display/AST/IMAP+Voicemail+Storage
31 * \ingroup applications
32 * \note This module requires res_adsi to load. This needs to be optional
35 * \note This file is now almost impossible to work with, due to all \#ifdefs.
36 * Feels like the database code before realtime. Someone - please come up
37 * with a plan to clean this up.
41 <use type="module">res_adsi</use>
42 <use type="module">res_smdi</use>
43 <support_level>core</support_level>
47 <category name="MENUSELECT_OPTS_app_voicemail" displayname="Voicemail Build Options" positive_output="yes" touch_on_change="apps/app_voicemail.c apps/app_directory.c">
48 <member name="FILE_STORAGE" displayname="Storage of Voicemail using filesystem">
49 <conflict>ODBC_STORAGE</conflict>
50 <conflict>IMAP_STORAGE</conflict>
51 <defaultenabled>yes</defaultenabled>
52 <support_level>core</support_level>
54 <member name="ODBC_STORAGE" displayname="Storage of Voicemail using ODBC">
55 <depend>generic_odbc</depend>
57 <conflict>IMAP_STORAGE</conflict>
58 <conflict>FILE_STORAGE</conflict>
59 <defaultenabled>no</defaultenabled>
60 <support_level>core</support_level>
62 <member name="IMAP_STORAGE" displayname="Storage of Voicemail using IMAP4">
63 <depend>imap_tk</depend>
64 <conflict>ODBC_STORAGE</conflict>
65 <conflict>FILE_STORAGE</conflict>
66 <use type="external">openssl</use>
67 <defaultenabled>no</defaultenabled>
68 <support_level>core</support_level>
79 #ifdef USE_SYSTEM_IMAP
80 #include <imap/c-client.h>
81 #include <imap/imap4r1.h>
82 #include <imap/linkage.h>
83 #elif defined (USE_SYSTEM_CCLIENT)
84 #include <c-client/c-client.h>
85 #include <c-client/imap4r1.h>
86 #include <c-client/linkage.h>
94 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
96 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
102 #if defined(__FreeBSD__) || defined(__OpenBSD__)
103 #include <sys/wait.h>
106 #include "asterisk/logger.h"
107 #include "asterisk/lock.h"
108 #include "asterisk/file.h"
109 #include "asterisk/channel.h"
110 #include "asterisk/pbx.h"
111 #include "asterisk/config.h"
112 #include "asterisk/say.h"
113 #include "asterisk/module.h"
114 #include "asterisk/adsi.h"
115 #include "asterisk/app.h"
116 #include "asterisk/manager.h"
117 #include "asterisk/dsp.h"
118 #include "asterisk/localtime.h"
119 #include "asterisk/cli.h"
120 #include "asterisk/utils.h"
121 #include "asterisk/stringfields.h"
122 #include "asterisk/smdi.h"
123 #include "asterisk/astobj2.h"
124 #include "asterisk/event.h"
125 #include "asterisk/taskprocessor.h"
126 #include "asterisk/test.h"
129 #include "asterisk/res_odbc.h"
133 #include "asterisk/threadstorage.h"
137 <application name="VoiceMail" language="en_US">
139 Leave a Voicemail message.
142 <parameter name="mailboxs" argsep="&" required="true">
143 <argument name="mailbox1" argsep="@" required="true">
144 <argument name="mailbox" required="true" />
145 <argument name="context" />
147 <argument name="mailbox2" argsep="@" multiple="true">
148 <argument name="mailbox" required="true" />
149 <argument name="context" />
152 <parameter name="options">
155 <para>Play the <literal>busy</literal> greeting to the calling party.</para>
158 <argument name="c" />
159 <para>Accept digits for a new extension in context <replaceable>c</replaceable>,
160 if played during the greeting. Context defaults to the current context.</para>
163 <argument name="#" required="true" />
164 <para>Use the specified amount of gain when recording the voicemail
165 message. The units are whole-number decibels (dB). Only works on supported
166 technologies, which is DAHDI only.</para>
169 <para>Skip the playback of instructions for leaving a message to the
170 calling party.</para>
173 <para>Play the <literal>unavailable</literal> greeting.</para>
176 <para>Mark message as <literal>URGENT</literal>.</para>
179 <para>Mark message as <literal>PRIORITY</literal>.</para>
185 <para>This application allows the calling party to leave a message for the specified
186 list of mailboxes. When multiple mailboxes are specified, the greeting will be taken from
187 the first mailbox specified. Dialplan execution will stop if the specified mailbox does not
189 <para>The Voicemail application will exit if any of the following DTMF digits are received:</para>
192 <para>Jump to the <literal>o</literal> extension in the current dialplan context.</para>
195 <para>Jump to the <literal>a</literal> extension in the current dialplan context.</para>
198 <para>This application will set the following channel variable upon completion:</para>
200 <variable name="VMSTATUS">
201 <para>This indicates the status of the execution of the VoiceMail application.</para>
202 <value name="SUCCESS" />
203 <value name="USEREXIT" />
204 <value name="FAILED" />
209 <ref type="application">VoiceMailMain</ref>
212 <application name="VoiceMailMain" language="en_US">
214 Check Voicemail messages.
217 <parameter name="mailbox" required="true" argsep="@">
218 <argument name="mailbox" />
219 <argument name="context" />
221 <parameter name="options">
224 <para>Consider the <replaceable>mailbox</replaceable> parameter as a prefix to
225 the mailbox that is entered by the caller.</para>
228 <argument name="#" required="true" />
229 <para>Use the specified amount of gain when recording a voicemail message.
230 The units are whole-number decibels (dB).</para>
233 <para>Skip checking the passcode for the mailbox.</para>
236 <argument name="folder" required="true" />
237 <para>Skip folder prompt and go directly to <replaceable>folder</replaceable> specified.
238 Defaults to <literal>INBOX</literal> (or <literal>0</literal>).</para>
240 <enum name="0"><para>INBOX</para></enum>
241 <enum name="1"><para>Old</para></enum>
242 <enum name="2"><para>Work</para></enum>
243 <enum name="3"><para>Family</para></enum>
244 <enum name="4"><para>Friends</para></enum>
245 <enum name="5"><para>Cust1</para></enum>
246 <enum name="6"><para>Cust2</para></enum>
247 <enum name="7"><para>Cust3</para></enum>
248 <enum name="8"><para>Cust4</para></enum>
249 <enum name="9"><para>Cust5</para></enum>
256 <para>This application allows the calling party to check voicemail messages. A specific
257 <replaceable>mailbox</replaceable>, and optional corresponding <replaceable>context</replaceable>,
258 may be specified. If a <replaceable>mailbox</replaceable> is not provided, the calling party will
259 be prompted to enter one. If a <replaceable>context</replaceable> is not specified, the
260 <literal>default</literal> context will be used.</para>
261 <para>The VoiceMailMain application will exit if the following DTMF digit is entered as Mailbox
262 or Password, and the extension exists:</para>
265 <para>Jump to the <literal>a</literal> extension in the current dialplan context.</para>
270 <ref type="application">VoiceMail</ref>
273 <application name="MailboxExists" language="en_US">
275 Check to see if Voicemail mailbox exists.
278 <parameter name="mailbox" required="true" argsep="@">
279 <argument name="mailbox" required="true" />
280 <argument name="context" />
282 <parameter name="options">
283 <para>None options.</para>
287 <para>Check to see if the specified <replaceable>mailbox</replaceable> exists. If no voicemail
288 <replaceable>context</replaceable> is specified, the <literal>default</literal> context
290 <para>This application will set the following channel variable upon completion:</para>
292 <variable name="VMBOXEXISTSSTATUS">
293 <para>This will contain the status of the execution of the MailboxExists application.
294 Possible values include:</para>
295 <value name="SUCCESS" />
296 <value name="FAILED" />
301 <application name="VMAuthenticate" language="en_US">
303 Authenticate with Voicemail passwords.
306 <parameter name="mailbox" required="true" argsep="@">
307 <argument name="mailbox" />
308 <argument name="context" />
310 <parameter name="options">
313 <para>Skip playing the initial prompts.</para>
319 <para>This application behaves the same way as the Authenticate application, but the passwords
320 are taken from <filename>voicemail.conf</filename>. If the <replaceable>mailbox</replaceable> is
321 specified, only that mailbox's password will be considered valid. If the <replaceable>mailbox</replaceable>
322 is not specified, the channel variable <variable>AUTH_MAILBOX</variable> will be set with the authenticated
324 <para>The VMAuthenticate application will exit if the following DTMF digit is entered as Mailbox
325 or Password, and the extension exists:</para>
328 <para>Jump to the <literal>a</literal> extension in the current dialplan context.</para>
333 <application name="VMSayName" language="en_US">
335 Play the name of a voicemail user
338 <parameter name="mailbox" required="true" argsep="@">
339 <argument name="mailbox" />
340 <argument name="context" />
344 <para>This application will say the recorded name of the voicemail user specified as the
345 argument to this application. If no context is provided, <literal>default</literal> is assumed.</para>
348 <function name="MAILBOX_EXISTS" language="en_US">
350 Tell if a mailbox is configured.
353 <parameter name="mailbox" required="true" />
354 <parameter name="context" />
357 <para>Returns a boolean of whether the corresponding <replaceable>mailbox</replaceable> exists.
358 If <replaceable>context</replaceable> is not specified, defaults to the <literal>default</literal>
362 <manager name="VoicemailUsersList" language="en_US">
364 List All Voicemail User Information.
367 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
375 static char imapserver[48];
376 static char imapport[8];
377 static char imapflags[128];
378 static char imapfolder[64];
379 static char imapparentfolder[64] = "\0";
380 static char greetingfolder[64];
381 static char authuser[32];
382 static char authpassword[42];
383 static int imapversion = 1;
385 static int expungeonhangup = 1;
386 static int imapgreetings = 0;
387 static char delimiter = '\0';
392 AST_THREADSTORAGE(ts_vmstate);
394 /* Forward declarations for IMAP */
395 static int init_mailstream(struct vm_state *vms, int box);
396 static void write_file(char *filename, char *buffer, unsigned long len);
397 static char *get_header_by_tag(char *header, char *tag, char *buf, size_t len);
398 static void vm_imap_delete(char *file, int msgnum, struct ast_vm_user *vmu);
399 static char *get_user_by_mailbox(char *mailbox, char *buf, size_t len);
400 static struct vm_state *get_vm_state_by_imapuser(const char *user, int interactive);
401 static struct vm_state *get_vm_state_by_mailbox(const char *mailbox, const char *context, int interactive);
402 static struct vm_state *create_vm_state_from_user(struct ast_vm_user *vmu);
403 static void vmstate_insert(struct vm_state *vms);
404 static void vmstate_delete(struct vm_state *vms);
405 static void set_update(MAILSTREAM * stream);
406 static void init_vm_state(struct vm_state *vms);
407 static int save_body(BODY *body, struct vm_state *vms, char *section, char *format, int is_intro);
408 static void get_mailbox_delimiter(MAILSTREAM *stream);
409 static void mm_parsequota (MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pquota);
410 static void imap_mailbox_name(char *spec, size_t len, struct vm_state *vms, int box, int target);
411 static int imap_store_file(const char *dir, const char *mailboxuser, const char *mailboxcontext, int msgnum, struct ast_channel *chan, struct ast_vm_user *vmu, char *fmt, int duration, struct vm_state *vms, const char *flag);
412 static void update_messages_by_imapuser(const char *user, unsigned long number);
413 static int vm_delete(char *file);
415 static int imap_remove_file (char *dir, int msgnum);
416 static int imap_retrieve_file (const char *dir, const int msgnum, const char *mailbox, const char *context);
417 static int imap_delete_old_greeting (char *dir, struct vm_state *vms);
418 static void check_quota(struct vm_state *vms, char *mailbox);
419 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box);
421 struct vm_state *vms;
422 AST_LIST_ENTRY(vmstate) list;
425 static AST_LIST_HEAD_STATIC(vmstates, vmstate);
429 #define SMDI_MWI_WAIT_TIMEOUT 1000 /* 1 second */
431 #define COMMAND_TIMEOUT 5000
432 /* Don't modify these here; set your umask at runtime instead */
433 #define VOICEMAIL_DIR_MODE 0777
434 #define VOICEMAIL_FILE_MODE 0666
435 #define CHUNKSIZE 65536
437 #define VOICEMAIL_CONFIG "voicemail.conf"
438 #define ASTERISK_USERNAME "asterisk"
440 /* Define fast-forward, pause, restart, and reverse keys
441 * while listening to a voicemail message - these are
442 * strings, not characters */
443 #define DEFAULT_LISTEN_CONTROL_FORWARD_KEY "#"
444 #define DEFAULT_LISTEN_CONTROL_REVERSE_KEY "*"
445 #define DEFAULT_LISTEN_CONTROL_PAUSE_KEY "0"
446 #define DEFAULT_LISTEN_CONTROL_RESTART_KEY "2"
447 #define DEFAULT_LISTEN_CONTROL_STOP_KEY "13456789"
448 #define VALID_DTMF "1234567890*#" /* Yes ABCD are valid dtmf but what phones have those? */
450 /* Default mail command to mail voicemail. Change it with the
451 * mailcmd= command in voicemail.conf */
452 #define SENDMAIL "/usr/sbin/sendmail -t"
454 #define INTRO "vm-intro"
457 #define MAXMSGLIMIT 9999
459 #define MINPASSWORD 0 /*!< Default minimum mailbox password length */
461 #define BASELINELEN 72
462 #define BASEMAXINLINE 256
469 #define MAX_DATETIME_FORMAT 512
470 #define MAX_NUM_CID_CONTEXTS 10
472 #define VM_REVIEW (1 << 0) /*!< After recording, permit the caller to review the recording before saving */
473 #define VM_OPERATOR (1 << 1) /*!< Allow 0 to be pressed to go to 'o' extension */
474 #define VM_SAYCID (1 << 2) /*!< Repeat the CallerID info during envelope playback */
475 #define VM_SVMAIL (1 << 3) /*!< Allow the user to compose a new VM from within VoicemailMain */
476 #define VM_ENVELOPE (1 << 4) /*!< Play the envelope information (who-from, time received, etc.) */
477 #define VM_SAYDURATION (1 << 5) /*!< Play the length of the message during envelope playback */
478 #define VM_SKIPAFTERCMD (1 << 6) /*!< After deletion, assume caller wants to go to the next message */
479 #define VM_FORCENAME (1 << 7) /*!< Have new users record their name */
480 #define VM_FORCEGREET (1 << 8) /*!< Have new users record their greetings */
481 #define VM_PBXSKIP (1 << 9) /*!< Skip the [PBX] preamble in the Subject line of emails */
482 #define VM_DIRECFORWARD (1 << 10) /*!< Permit caller to use the Directory app for selecting to which mailbox to forward a VM */
483 #define VM_ATTACH (1 << 11) /*!< Attach message to voicemail notifications? */
484 #define VM_DELETE (1 << 12) /*!< Delete message after sending notification */
485 #define VM_ALLOCED (1 << 13) /*!< Structure was malloc'ed, instead of placed in a return (usually static) buffer */
486 #define VM_SEARCH (1 << 14) /*!< Search all contexts for a matching mailbox */
487 #define VM_TEMPGREETWARN (1 << 15) /*!< Remind user tempgreeting is set */
488 #define VM_MOVEHEARD (1 << 16) /*!< Move a "heard" message to Old after listening to it */
489 #define VM_MESSAGEWRAP (1 << 17) /*!< Wrap around from the last message to the first, and vice-versa */
490 #define VM_FWDURGAUTO (1 << 18) /*!< Autoset of Urgent flag on forwarded Urgent messages set globally */
491 #define ERROR_LOCK_PATH -100
492 #define OPERATOR_EXIT 300
504 enum vm_option_flags {
505 OPT_SILENT = (1 << 0),
506 OPT_BUSY_GREETING = (1 << 1),
507 OPT_UNAVAIL_GREETING = (1 << 2),
508 OPT_RECORDGAIN = (1 << 3),
509 OPT_PREPEND_MAILBOX = (1 << 4),
510 OPT_AUTOPLAY = (1 << 6),
511 OPT_DTMFEXIT = (1 << 7),
512 OPT_MESSAGE_Urgent = (1 << 8),
513 OPT_MESSAGE_PRIORITY = (1 << 9)
516 enum vm_option_args {
517 OPT_ARG_RECORDGAIN = 0,
518 OPT_ARG_PLAYFOLDER = 1,
519 OPT_ARG_DTMFEXIT = 2,
520 /* This *must* be the last value in this enum! */
521 OPT_ARG_ARRAY_SIZE = 3,
524 enum vm_passwordlocation {
525 OPT_PWLOC_VOICEMAILCONF = 0,
526 OPT_PWLOC_SPOOLDIR = 1,
527 OPT_PWLOC_USERSCONF = 2,
530 AST_APP_OPTIONS(vm_app_options, {
531 AST_APP_OPTION('s', OPT_SILENT),
532 AST_APP_OPTION('b', OPT_BUSY_GREETING),
533 AST_APP_OPTION('u', OPT_UNAVAIL_GREETING),
534 AST_APP_OPTION_ARG('g', OPT_RECORDGAIN, OPT_ARG_RECORDGAIN),
535 AST_APP_OPTION_ARG('d', OPT_DTMFEXIT, OPT_ARG_DTMFEXIT),
536 AST_APP_OPTION('p', OPT_PREPEND_MAILBOX),
537 AST_APP_OPTION_ARG('a', OPT_AUTOPLAY, OPT_ARG_PLAYFOLDER),
538 AST_APP_OPTION('U', OPT_MESSAGE_Urgent),
539 AST_APP_OPTION('P', OPT_MESSAGE_PRIORITY)
542 static int load_config(int reload);
544 /*! \page vmlang Voicemail Language Syntaxes Supported
546 \par Syntaxes supported, not really language codes.
553 \arg \b pt - Portuguese
554 \arg \b pt_BR - Portuguese (Brazil)
556 \arg \b no - Norwegian
558 \arg \b tw - Chinese (Taiwan)
559 \arg \b ua - Ukrainian
561 German requires the following additional soundfile:
562 \arg \b 1F einE (feminine)
564 Spanish requires the following additional soundfile:
565 \arg \b 1M un (masculine)
567 Dutch, Portuguese & Spanish require the following additional soundfiles:
568 \arg \b vm-INBOXs singular of 'new'
569 \arg \b vm-Olds singular of 'old/heard/read'
572 \arg \b vm-INBOX nieuwe (nl)
573 \arg \b vm-Old oude (nl)
576 \arg \b vm-new-a 'new', feminine singular accusative
577 \arg \b vm-new-e 'new', feminine plural accusative
578 \arg \b vm-new-ych 'new', feminine plural genitive
579 \arg \b vm-old-a 'old', feminine singular accusative
580 \arg \b vm-old-e 'old', feminine plural accusative
581 \arg \b vm-old-ych 'old', feminine plural genitive
582 \arg \b digits/1-a 'one', not always same as 'digits/1'
583 \arg \b digits/2-ie 'two', not always same as 'digits/2'
586 \arg \b vm-nytt singular of 'new'
587 \arg \b vm-nya plural of 'new'
588 \arg \b vm-gammalt singular of 'old'
589 \arg \b vm-gamla plural of 'old'
590 \arg \b digits/ett 'one', not always same as 'digits/1'
593 \arg \b vm-ny singular of 'new'
594 \arg \b vm-nye plural of 'new'
595 \arg \b vm-gammel singular of 'old'
596 \arg \b vm-gamle plural of 'old'
604 Italian requires the following additional soundfile:
608 \arg \b vm-nuovi new plural
609 \arg \b vm-vecchio old
610 \arg \b vm-vecchi old plural
612 Chinese (Taiwan) requires the following additional soundfile:
613 \arg \b vm-tong A class-word for call (tong1)
614 \arg \b vm-ri A class-word for day (ri4)
615 \arg \b vm-you You (ni3)
616 \arg \b vm-haveno Have no (mei2 you3)
617 \arg \b vm-have Have (you3)
618 \arg \b vm-listen To listen (yao4 ting1)
621 \note Don't use vm-INBOX or vm-Old, because they are the name of the INBOX and Old folders,
622 spelled among others when you have to change folder. For the above reasons, vm-INBOX
623 and vm-Old are spelled plural, to make them sound more as folder name than an adjective.
632 unsigned char iobuf[BASEMAXINLINE];
635 /*! Structure for linked list of users
636 * Use ast_vm_user_destroy() to free one of these structures. */
638 char context[AST_MAX_CONTEXT]; /*!< Voicemail context */
639 char mailbox[AST_MAX_EXTENSION]; /*!< Mailbox id, unique within vm context */
640 char password[80]; /*!< Secret pin code, numbers only */
641 char fullname[80]; /*!< Full name, for directory app */
642 char email[80]; /*!< E-mail address */
643 char *emailsubject; /*!< E-mail subject */
644 char *emailbody; /*!< E-mail body */
645 char pager[80]; /*!< E-mail address to pager (no attachment) */
646 char serveremail[80]; /*!< From: Mail address */
647 char mailcmd[160]; /*!< Configurable mail command */
648 char language[MAX_LANGUAGE]; /*!< Config: Language setting */
649 char zonetag[80]; /*!< Time zone */
650 char locale[20]; /*!< The locale (for presentation of date/time) */
653 char uniqueid[80]; /*!< Unique integer identifier */
655 char attachfmt[20]; /*!< Attachment format */
656 unsigned int flags; /*!< VM_ flags */
658 int minsecs; /*!< Minimum number of seconds per message for this mailbox */
659 int maxmsg; /*!< Maximum number of msgs per folder for this mailbox */
660 int maxdeletedmsg; /*!< Maximum number of deleted msgs saved for this mailbox */
661 int maxsecs; /*!< Maximum number of seconds per message for this mailbox */
662 int passwordlocation; /*!< Storage location of the password */
664 char imapuser[80]; /*!< IMAP server login */
665 char imappassword[80]; /*!< IMAP server password if authpassword not defined */
666 char imapfolder[64]; /*!< IMAP voicemail folder */
667 char imapvmshareid[80]; /*!< Shared mailbox ID to use rather than the dialed one */
668 int imapversion; /*!< If configuration changes, use the new values */
670 double volgain; /*!< Volume gain for voicemails sent via email */
671 AST_LIST_ENTRY(ast_vm_user) list;
674 /*! Voicemail time zones */
676 AST_LIST_ENTRY(vm_zone) list;
679 char msg_format[512];
682 #define VMSTATE_MAX_MSG_ARRAY 256
684 /*! Voicemail mailbox state */
689 char curdir[PATH_MAX];
690 char vmbox[PATH_MAX];
692 char intro[PATH_MAX];
695 int dh_arraysize; /* used for deleted / heard allocation */
705 int updated; /*!< decremented on each mail check until 1 -allows delay */
706 long msgArray[VMSTATE_MAX_MSG_ARRAY];
707 MAILSTREAM *mailstream;
709 char imapuser[80]; /*!< IMAP server login */
710 char imapfolder[64]; /*!< IMAP voicemail folder */
713 char introfn[PATH_MAX]; /*!< Name of prepended file */
714 unsigned int quota_limit;
715 unsigned int quota_usage;
716 struct vm_state *persist_vms;
721 static char odbc_database[80];
722 static char odbc_table[80];
723 #define RETRIEVE(a,b,c,d) retrieve_file(a,b)
724 #define DISPOSE(a,b) remove_file(a,b)
725 #define STORE(a,b,c,d,e,f,g,h,i,j) store_file(a,b,c,d)
726 #define EXISTS(a,b,c,d) (message_exists(a,b))
727 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(a,b,c,d,e,f))
728 #define COPY(a,b,c,d,e,f,g,h) (copy_file(a,b,c,d,e,f))
729 #define DELETE(a,b,c,d) (delete_file(a,b))
732 #define DISPOSE(a,b) (imap_remove_file(a,b))
733 #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))
734 #define RETRIEVE(a,b,c,d) imap_retrieve_file(a,b,c,d)
735 #define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
736 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
737 #define COPY(a,b,c,d,e,f,g,h) (copy_file(g,h));
738 #define DELETE(a,b,c,d) (vm_imap_delete(a,b,d))
740 #define RETRIEVE(a,b,c,d)
742 #define STORE(a,b,c,d,e,f,g,h,i,j)
743 #define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
744 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
745 #define COPY(a,b,c,d,e,f,g,h) (copy_plain_file(g,h));
746 #define DELETE(a,b,c,d) (vm_delete(c))
750 static char VM_SPOOL_DIR[PATH_MAX];
752 static char ext_pass_cmd[128];
753 static char ext_pass_check_cmd[128];
757 #define PWDCHANGE_INTERNAL (1 << 1)
758 #define PWDCHANGE_EXTERNAL (1 << 2)
759 static int pwdchange = PWDCHANGE_INTERNAL;
762 #define tdesc "Comedian Mail (Voicemail System) with ODBC Storage"
765 # define tdesc "Comedian Mail (Voicemail System) with IMAP Storage"
767 # define tdesc "Comedian Mail (Voicemail System)"
771 static char userscontext[AST_MAX_EXTENSION] = "default";
773 static char *addesc = "Comedian Mail";
775 /* Leave a message */
776 static char *app = "VoiceMail";
778 /* Check mail, control, etc */
779 static char *app2 = "VoiceMailMain";
781 static char *app3 = "MailboxExists";
782 static char *app4 = "VMAuthenticate";
784 static char *sayname_app = "VMSayName";
786 static AST_LIST_HEAD_STATIC(users, ast_vm_user);
787 static AST_LIST_HEAD_STATIC(zones, vm_zone);
788 static char zonetag[80];
789 static char locale[20];
790 static int maxsilence;
792 static int maxdeletedmsg;
793 static int silencethreshold = 128;
794 static char serveremail[80];
795 static char mailcmd[160]; /* Configurable mail cmd */
796 static char externnotify[160];
797 static struct ast_smdi_interface *smdi_iface = NULL;
798 static char vmfmts[80];
799 static double volgain;
800 static int vmminsecs;
801 static int vmmaxsecs;
804 static int maxlogins;
805 static int minpassword;
806 static int passwordlocation;
808 /*! Poll mailboxes for changes since there is something external to
809 * app_voicemail that may change them. */
810 static unsigned int poll_mailboxes;
812 /*! Polling frequency */
813 static unsigned int poll_freq;
814 /*! By default, poll every 30 seconds */
815 #define DEFAULT_POLL_FREQ 30
817 AST_MUTEX_DEFINE_STATIC(poll_lock);
818 static ast_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
819 static pthread_t poll_thread = AST_PTHREADT_NULL;
820 static unsigned char poll_thread_run;
822 /*! Subscription to ... MWI event subscriptions */
823 static struct ast_event_sub *mwi_sub_sub;
824 /*! Subscription to ... MWI event un-subscriptions */
825 static struct ast_event_sub *mwi_unsub_sub;
828 * \brief An MWI subscription
830 * This is so we can keep track of which mailboxes are subscribed to.
831 * This way, we know which mailboxes to poll when the pollmailboxes
832 * option is being used.
835 AST_RWLIST_ENTRY(mwi_sub) entry;
843 struct mwi_sub_task {
849 static struct ast_taskprocessor *mwi_subscription_tps;
851 static AST_RWLIST_HEAD_STATIC(mwi_subs, mwi_sub);
853 /* custom audio control prompts for voicemail playback */
854 static char listen_control_forward_key[12];
855 static char listen_control_reverse_key[12];
856 static char listen_control_pause_key[12];
857 static char listen_control_restart_key[12];
858 static char listen_control_stop_key[12];
860 /* custom password sounds */
861 static char vm_password[80] = "vm-password";
862 static char vm_newpassword[80] = "vm-newpassword";
863 static char vm_passchanged[80] = "vm-passchanged";
864 static char vm_reenterpassword[80] = "vm-reenterpassword";
865 static char vm_mismatch[80] = "vm-mismatch";
866 static char vm_invalid_password[80] = "vm-invalid-password";
867 static char vm_pls_try_again[80] = "vm-pls-try-again";
870 * XXX If we have the time, motivation, etc. to fix up this prompt, one of the following would be appropriate:
871 * 1. create a sound along the lines of "Please try again. When done, press the pound key" which could be spliced
872 * from existing sound clips. This would require some programming changes in the area of vm_forward options and also
873 * app.c's __ast_play_and_record function
874 * 2. create a sound prompt saying "Please try again. When done recording, press any key to stop and send the prepended
875 * message." At the time of this comment, I think this would require new voice work to be commissioned.
876 * 3. Something way different like providing instructions before a time out or a post-recording menu. This would require
877 * more effort than either of the other two.
879 static char vm_prepend_timeout[80] = "vm-then-pound";
881 static struct ast_flags globalflags = {0};
883 static int saydurationminfo;
885 static char dialcontext[AST_MAX_CONTEXT] = "";
886 static char callcontext[AST_MAX_CONTEXT] = "";
887 static char exitcontext[AST_MAX_CONTEXT] = "";
889 static char cidinternalcontexts[MAX_NUM_CID_CONTEXTS][64];
892 static char *emailbody = NULL;
893 static char *emailsubject = NULL;
894 static char *pagerbody = NULL;
895 static char *pagersubject = NULL;
896 static char fromstring[100];
897 static char pagerfromstring[100];
898 static char charset[32] = "ISO-8859-1";
900 static unsigned char adsifdn[4] = "\x00\x00\x00\x0F";
901 static unsigned char adsisec[4] = "\x9B\xDB\xF7\xAC";
902 static int adsiver = 1;
903 static char emaildateformat[32] = "%A, %B %d, %Y at %r";
904 static char pagerdateformat[32] = "%A, %B %d, %Y at %r";
906 /* Forward declarations - generic */
907 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box);
908 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);
909 static int dialout(struct ast_channel *chan, struct ast_vm_user *vmu, char *num, char *outgoing_context);
910 static int play_record_review(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime,
911 char *fmt, int outsidecaller, struct ast_vm_user *vmu, int *duration, int *sound_duration, const char *unlockdir,
912 signed char record_gain, struct vm_state *vms, char *flag);
913 static int vm_tempgreeting(struct ast_channel *chan, struct ast_vm_user *vmu, struct vm_state *vms, char *fmtc, signed char record_gain);
914 static int vm_play_folder_name(struct ast_channel *chan, char *mbox);
915 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);
916 static void make_email_file(FILE *p, char *srcemail, struct ast_vm_user *vmu, int msgnum, char *context, char *mailbox, const char *fromfolder, 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);
917 static void apply_options(struct ast_vm_user *vmu, const char *options);
918 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);
919 static int is_valid_dtmf(const char *key);
920 static void read_password_from_file(const char *secretfn, char *password, int passwordlen);
921 static int write_password_to_file(const char *secretfn, const char *password);
922 static const char *substitute_escapes(const char *value);
924 struct ao2_container *inprocess_container;
932 static int inprocess_hash_fn(const void *obj, const int flags)
934 const struct inprocess *i = obj;
935 return atoi(i->mailbox);
938 static int inprocess_cmp_fn(void *obj, void *arg, int flags)
940 struct inprocess *i = obj, *j = arg;
941 if (strcmp(i->mailbox, j->mailbox)) {
944 return !strcmp(i->context, j->context) ? CMP_MATCH : 0;
947 static int inprocess_count(const char *context, const char *mailbox, int delta)
949 struct inprocess *i, *arg = alloca(sizeof(*arg) + strlen(context) + strlen(mailbox) + 2);
950 arg->context = arg->mailbox + strlen(mailbox) + 1;
951 strcpy(arg->mailbox, mailbox); /* SAFE */
952 strcpy(arg->context, context); /* SAFE */
953 ao2_lock(inprocess_container);
954 if ((i = ao2_find(inprocess_container, arg, 0))) {
955 int ret = ast_atomic_fetchadd_int(&i->count, delta);
956 ao2_unlock(inprocess_container);
961 ast_log(LOG_WARNING, "BUG: ref count decrement on non-existing object???\n");
963 if (!(i = ao2_alloc(sizeof(*i) + strlen(context) + strlen(mailbox) + 2, NULL))) {
964 ao2_unlock(inprocess_container);
967 i->context = i->mailbox + strlen(mailbox) + 1;
968 strcpy(i->mailbox, mailbox); /* SAFE */
969 strcpy(i->context, context); /* SAFE */
971 ao2_link(inprocess_container, i);
972 ao2_unlock(inprocess_container);
977 #if !(defined(ODBC_STORAGE) || defined(IMAP_STORAGE))
978 static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit);
982 * \brief Strips control and non 7-bit clean characters from input string.
984 * \note To map control and none 7-bit characters to a 7-bit clean characters
985 * please use ast_str_encode_mine().
987 static char *strip_control_and_high(const char *input, char *buf, size_t buflen)
990 for (; *input; input++) {
995 if (bufptr == buf + buflen - 1) {
1005 * \brief Sets default voicemail system options to a voicemail user.
1007 * This applies select global settings to a newly created (dynamic) instance of a voicemail user.
1008 * - all the globalflags
1009 * - the saydurationminfo
1013 * - vmmaxsecs, vmmaxmsg, maxdeletedmsg
1015 * - emailsubject, emailbody set to NULL
1017 static void populate_defaults(struct ast_vm_user *vmu)
1019 ast_copy_flags(vmu, (&globalflags), AST_FLAGS_ALL);
1020 vmu->passwordlocation = passwordlocation;
1021 if (saydurationminfo) {
1022 vmu->saydurationm = saydurationminfo;
1024 ast_copy_string(vmu->callback, callcontext, sizeof(vmu->callback));
1025 ast_copy_string(vmu->dialout, dialcontext, sizeof(vmu->dialout));
1026 ast_copy_string(vmu->exit, exitcontext, sizeof(vmu->exit));
1027 ast_copy_string(vmu->zonetag, zonetag, sizeof(vmu->zonetag));
1028 ast_copy_string(vmu->locale, locale, sizeof(vmu->locale));
1030 vmu->minsecs = vmminsecs;
1033 vmu->maxsecs = vmmaxsecs;
1036 vmu->maxmsg = maxmsg;
1038 if (maxdeletedmsg) {
1039 vmu->maxdeletedmsg = maxdeletedmsg;
1041 vmu->volgain = volgain;
1042 ast_free(vmu->emailsubject);
1043 vmu->emailsubject = NULL;
1044 ast_free(vmu->emailbody);
1045 vmu->emailbody = NULL;
1047 ast_copy_string(vmu->imapfolder, imapfolder, sizeof(vmu->imapfolder));
1052 * \brief Sets a a specific property value.
1053 * \param vmu The voicemail user object to work with.
1054 * \param var The name of the property to be set.
1055 * \param value The value to be set to the property.
1057 * The property name must be one of the understood properties. See the source for details.
1059 static void apply_option(struct ast_vm_user *vmu, const char *var, const char *value)
1062 if (!strcasecmp(var, "attach")) {
1063 ast_set2_flag(vmu, ast_true(value), VM_ATTACH);
1064 } else if (!strcasecmp(var, "attachfmt")) {
1065 ast_copy_string(vmu->attachfmt, value, sizeof(vmu->attachfmt));
1066 } else if (!strcasecmp(var, "serveremail")) {
1067 ast_copy_string(vmu->serveremail, value, sizeof(vmu->serveremail));
1068 } else if (!strcasecmp(var, "emailbody")) {
1069 ast_free(vmu->emailbody);
1070 vmu->emailbody = ast_strdup(substitute_escapes(value));
1071 } else if (!strcasecmp(var, "emailsubject")) {
1072 ast_free(vmu->emailsubject);
1073 vmu->emailsubject = ast_strdup(substitute_escapes(value));
1074 } else if (!strcasecmp(var, "language")) {
1075 ast_copy_string(vmu->language, value, sizeof(vmu->language));
1076 } else if (!strcasecmp(var, "tz")) {
1077 ast_copy_string(vmu->zonetag, value, sizeof(vmu->zonetag));
1078 } else if (!strcasecmp(var, "locale")) {
1079 ast_copy_string(vmu->locale, value, sizeof(vmu->locale));
1081 } else if (!strcasecmp(var, "imapuser")) {
1082 ast_copy_string(vmu->imapuser, value, sizeof(vmu->imapuser));
1083 vmu->imapversion = imapversion;
1084 } else if (!strcasecmp(var, "imappassword") || !strcasecmp(var, "imapsecret")) {
1085 ast_copy_string(vmu->imappassword, value, sizeof(vmu->imappassword));
1086 vmu->imapversion = imapversion;
1087 } else if (!strcasecmp(var, "imapfolder")) {
1088 ast_copy_string(vmu->imapfolder, value, sizeof(vmu->imapfolder));
1089 } else if (!strcasecmp(var, "imapvmshareid")) {
1090 ast_copy_string(vmu->imapvmshareid, value, sizeof(vmu->imapvmshareid));
1091 vmu->imapversion = imapversion;
1093 } else if (!strcasecmp(var, "delete") || !strcasecmp(var, "deletevoicemail")) {
1094 ast_set2_flag(vmu, ast_true(value), VM_DELETE);
1095 } else if (!strcasecmp(var, "saycid")){
1096 ast_set2_flag(vmu, ast_true(value), VM_SAYCID);
1097 } else if (!strcasecmp(var, "sendvoicemail")){
1098 ast_set2_flag(vmu, ast_true(value), VM_SVMAIL);
1099 } else if (!strcasecmp(var, "review")){
1100 ast_set2_flag(vmu, ast_true(value), VM_REVIEW);
1101 } else if (!strcasecmp(var, "tempgreetwarn")){
1102 ast_set2_flag(vmu, ast_true(value), VM_TEMPGREETWARN);
1103 } else if (!strcasecmp(var, "messagewrap")){
1104 ast_set2_flag(vmu, ast_true(value), VM_MESSAGEWRAP);
1105 } else if (!strcasecmp(var, "operator")) {
1106 ast_set2_flag(vmu, ast_true(value), VM_OPERATOR);
1107 } else if (!strcasecmp(var, "envelope")){
1108 ast_set2_flag(vmu, ast_true(value), VM_ENVELOPE);
1109 } else if (!strcasecmp(var, "moveheard")){
1110 ast_set2_flag(vmu, ast_true(value), VM_MOVEHEARD);
1111 } else if (!strcasecmp(var, "sayduration")){
1112 ast_set2_flag(vmu, ast_true(value), VM_SAYDURATION);
1113 } else if (!strcasecmp(var, "saydurationm")){
1114 if (sscanf(value, "%30d", &x) == 1) {
1115 vmu->saydurationm = x;
1117 ast_log(AST_LOG_WARNING, "Invalid min duration for say duration\n");
1119 } else if (!strcasecmp(var, "forcename")){
1120 ast_set2_flag(vmu, ast_true(value), VM_FORCENAME);
1121 } else if (!strcasecmp(var, "forcegreetings")){
1122 ast_set2_flag(vmu, ast_true(value), VM_FORCEGREET);
1123 } else if (!strcasecmp(var, "callback")) {
1124 ast_copy_string(vmu->callback, value, sizeof(vmu->callback));
1125 } else if (!strcasecmp(var, "dialout")) {
1126 ast_copy_string(vmu->dialout, value, sizeof(vmu->dialout));
1127 } else if (!strcasecmp(var, "exitcontext")) {
1128 ast_copy_string(vmu->exit, value, sizeof(vmu->exit));
1129 } else if (!strcasecmp(var, "minsecs")) {
1130 if (sscanf(value, "%30d", &x) == 1 && x >= 0) {
1133 ast_log(LOG_WARNING, "Invalid min message length of %s. Using global value %d\n", value, vmminsecs);
1134 vmu->minsecs = vmminsecs;
1136 } else if (!strcasecmp(var, "maxmessage") || !strcasecmp(var, "maxsecs")) {
1137 vmu->maxsecs = atoi(value);
1138 if (vmu->maxsecs <= 0) {
1139 ast_log(AST_LOG_WARNING, "Invalid max message length of %s. Using global value %d\n", value, vmmaxsecs);
1140 vmu->maxsecs = vmmaxsecs;
1142 vmu->maxsecs = atoi(value);
1144 if (!strcasecmp(var, "maxmessage"))
1145 ast_log(AST_LOG_WARNING, "Option 'maxmessage' has been deprecated in favor of 'maxsecs'. Please make that change in your voicemail config.\n");
1146 } else if (!strcasecmp(var, "maxmsg")) {
1147 vmu->maxmsg = atoi(value);
1148 /* Accept maxmsg=0 (Greetings only voicemail) */
1149 if (vmu->maxmsg < 0) {
1150 ast_log(AST_LOG_WARNING, "Invalid number of messages per folder maxmsg=%s. Using default value %d\n", value, MAXMSG);
1151 vmu->maxmsg = MAXMSG;
1152 } else if (vmu->maxmsg > MAXMSGLIMIT) {
1153 ast_log(AST_LOG_WARNING, "Maximum number of messages per folder is %d. Cannot accept value maxmsg=%s\n", MAXMSGLIMIT, value);
1154 vmu->maxmsg = MAXMSGLIMIT;
1156 } else if (!strcasecmp(var, "nextaftercmd")) {
1157 ast_set2_flag(vmu, ast_true(value), VM_SKIPAFTERCMD);
1158 } else if (!strcasecmp(var, "backupdeleted")) {
1159 if (sscanf(value, "%30d", &x) == 1)
1160 vmu->maxdeletedmsg = x;
1161 else if (ast_true(value))
1162 vmu->maxdeletedmsg = MAXMSG;
1164 vmu->maxdeletedmsg = 0;
1166 if (vmu->maxdeletedmsg < 0) {
1167 ast_log(AST_LOG_WARNING, "Invalid number of deleted messages saved per mailbox backupdeleted=%s. Using default value %d\n", value, MAXMSG);
1168 vmu->maxdeletedmsg = MAXMSG;
1169 } else if (vmu->maxdeletedmsg > MAXMSGLIMIT) {
1170 ast_log(AST_LOG_WARNING, "Maximum number of deleted messages saved per mailbox is %d. Cannot accept value backupdeleted=%s\n", MAXMSGLIMIT, value);
1171 vmu->maxdeletedmsg = MAXMSGLIMIT;
1173 } else if (!strcasecmp(var, "volgain")) {
1174 sscanf(value, "%30lf", &vmu->volgain);
1175 } else if (!strcasecmp(var, "passwordlocation")) {
1176 if (!strcasecmp(value, "spooldir")) {
1177 vmu->passwordlocation = OPT_PWLOC_SPOOLDIR;
1179 vmu->passwordlocation = OPT_PWLOC_VOICEMAILCONF;
1181 } else if (!strcasecmp(var, "options")) {
1182 apply_options(vmu, value);
1186 static char *vm_check_password_shell(char *command, char *buf, size_t len)
1188 int fds[2], pid = 0;
1190 memset(buf, 0, len);
1193 snprintf(buf, len, "FAILURE: Pipe failed: %s", strerror(errno));
1196 pid = ast_safe_fork(0);
1202 snprintf(buf, len, "FAILURE: Fork failed");
1206 if (read(fds[0], buf, len) < 0) {
1207 ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
1212 AST_DECLARE_APP_ARGS(arg,
1215 char *mycmd = ast_strdupa(command);
1218 dup2(fds[1], STDOUT_FILENO);
1220 ast_close_fds_above_n(STDOUT_FILENO);
1222 AST_NONSTANDARD_APP_ARGS(arg, mycmd, ' ');
1224 execv(arg.v[0], arg.v);
1225 printf("FAILURE: %s", strerror(errno));
1233 * \brief Check that password meets minimum required length
1234 * \param vmu The voicemail user to change the password for.
1235 * \param password The password string to check
1237 * \return zero on ok, 1 on not ok.
1239 static int check_password(struct ast_vm_user *vmu, char *password)
1241 /* check minimum length */
1242 if (strlen(password) < minpassword)
1244 /* check that password does not contain '*' character */
1245 if (!ast_strlen_zero(password) && password[0] == '*')
1247 if (!ast_strlen_zero(ext_pass_check_cmd)) {
1248 char cmd[255], buf[255];
1250 ast_debug(1, "Verify password policies for %s\n", password);
1252 snprintf(cmd, sizeof(cmd), "%s %s %s %s %s", ext_pass_check_cmd, vmu->mailbox, vmu->context, vmu->password, password);
1253 if (vm_check_password_shell(cmd, buf, sizeof(buf))) {
1254 ast_debug(5, "Result: %s\n", buf);
1255 if (!strncasecmp(buf, "VALID", 5)) {
1256 ast_debug(3, "Passed password check: '%s'\n", buf);
1258 } else if (!strncasecmp(buf, "FAILURE", 7)) {
1259 ast_log(AST_LOG_WARNING, "Unable to execute password validation script: '%s'.\n", buf);
1262 ast_log(AST_LOG_NOTICE, "Password doesn't match policies for user %s %s\n", vmu->mailbox, password);
1271 * \brief Performs a change of the voicemail passowrd in the realtime engine.
1272 * \param vmu The voicemail user to change the password for.
1273 * \param password The new value to be set to the password for this user.
1275 * This only works if there is a realtime engine configured.
1276 * This is called from the (top level) vm_change_password.
1278 * \return zero on success, -1 on error.
1280 static int change_password_realtime(struct ast_vm_user *vmu, const char *password)
1283 if (!strcmp(vmu->password, password)) {
1284 /* No change (but an update would return 0 rows updated, so we opt out here) */
1288 if (strlen(password) > 10) {
1289 ast_realtime_require_field("voicemail", "password", RQ_CHAR, strlen(password), SENTINEL);
1291 if (ast_update2_realtime("voicemail", "context", vmu->context, "mailbox", vmu->mailbox, SENTINEL, "password", password, SENTINEL) > 0) {
1292 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: realtime engine updated with new password\r\nPasswordSource: realtime");
1293 ast_copy_string(vmu->password, password, sizeof(vmu->password));
1300 * \brief Destructively Parse options and apply.
1302 static void apply_options(struct ast_vm_user *vmu, const char *options)
1307 stringp = ast_strdupa(options);
1308 while ((s = strsep(&stringp, "|"))) {
1310 if ((var = strsep(&value, "=")) && value) {
1311 apply_option(vmu, var, value);
1317 * \brief Loads the options specific to a voicemail user.
1319 * This is called when a vm_user structure is being set up, such as from load_options.
1321 static void apply_options_full(struct ast_vm_user *retval, struct ast_variable *var)
1323 for (; var; var = var->next) {
1324 if (!strcasecmp(var->name, "vmsecret")) {
1325 ast_copy_string(retval->password, var->value, sizeof(retval->password));
1326 } else if (!strcasecmp(var->name, "secret") || !strcasecmp(var->name, "password")) { /* don't overwrite vmsecret if it exists */
1327 if (ast_strlen_zero(retval->password)) {
1328 if (!ast_strlen_zero(var->value) && var->value[0] == '*') {
1329 ast_log(LOG_WARNING, "Invalid password detected for mailbox %s. The password"
1330 "\n\tmust be reset in voicemail.conf.\n", retval->mailbox);
1332 ast_copy_string(retval->password, var->value, sizeof(retval->password));
1335 } else if (!strcasecmp(var->name, "uniqueid")) {
1336 ast_copy_string(retval->uniqueid, var->value, sizeof(retval->uniqueid));
1337 } else if (!strcasecmp(var->name, "pager")) {
1338 ast_copy_string(retval->pager, var->value, sizeof(retval->pager));
1339 } else if (!strcasecmp(var->name, "email")) {
1340 ast_copy_string(retval->email, var->value, sizeof(retval->email));
1341 } else if (!strcasecmp(var->name, "fullname")) {
1342 ast_copy_string(retval->fullname, var->value, sizeof(retval->fullname));
1343 } else if (!strcasecmp(var->name, "context")) {
1344 ast_copy_string(retval->context, var->value, sizeof(retval->context));
1345 } else if (!strcasecmp(var->name, "emailsubject")) {
1346 ast_free(retval->emailsubject);
1347 retval->emailsubject = ast_strdup(substitute_escapes(var->value));
1348 } else if (!strcasecmp(var->name, "emailbody")) {
1349 ast_free(retval->emailbody);
1350 retval->emailbody = ast_strdup(substitute_escapes(var->value));
1352 } else if (!strcasecmp(var->name, "imapuser")) {
1353 ast_copy_string(retval->imapuser, var->value, sizeof(retval->imapuser));
1354 retval->imapversion = imapversion;
1355 } else if (!strcasecmp(var->name, "imappassword") || !strcasecmp(var->name, "imapsecret")) {
1356 ast_copy_string(retval->imappassword, var->value, sizeof(retval->imappassword));
1357 retval->imapversion = imapversion;
1358 } else if (!strcasecmp(var->name, "imapfolder")) {
1359 ast_copy_string(retval->imapfolder, var->value, sizeof(retval->imapfolder));
1360 } else if (!strcasecmp(var->name, "imapvmshareid")) {
1361 ast_copy_string(retval->imapvmshareid, var->value, sizeof(retval->imapvmshareid));
1362 retval->imapversion = imapversion;
1365 apply_option(retval, var->name, var->value);
1370 * \brief Determines if a DTMF key entered is valid.
1371 * \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.
1373 * Tests the character entered against the set of valid DTMF characters.
1374 * \return 1 if the character entered is a valid DTMF digit, 0 if the character is invalid.
1376 static int is_valid_dtmf(const char *key)
1379 char *local_key = ast_strdupa(key);
1381 for (i = 0; i < strlen(key); ++i) {
1382 if (!strchr(VALID_DTMF, *local_key)) {
1383 ast_log(AST_LOG_WARNING, "Invalid DTMF key \"%c\" used in voicemail configuration file\n", *local_key);
1392 * \brief Finds a voicemail user from the realtime engine.
1397 * 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.
1399 * \return The ast_vm_user structure for the user that was found.
1401 static struct ast_vm_user *find_user_realtime(struct ast_vm_user *ivm, const char *context, const char *mailbox)
1403 struct ast_variable *var;
1404 struct ast_vm_user *retval;
1406 if ((retval = (ivm ? ivm : ast_calloc(1, sizeof(*retval))))) {
1408 ast_set_flag(retval, VM_ALLOCED);
1410 memset(retval, 0, sizeof(*retval));
1412 ast_copy_string(retval->mailbox, mailbox, sizeof(retval->mailbox));
1413 populate_defaults(retval);
1414 if (!context && ast_test_flag((&globalflags), VM_SEARCH))
1415 var = ast_load_realtime("voicemail", "mailbox", mailbox, SENTINEL);
1417 var = ast_load_realtime("voicemail", "mailbox", mailbox, "context", context, SENTINEL);
1419 apply_options_full(retval, var);
1420 ast_variables_destroy(var);
1431 * \brief Finds a voicemail user from the users file or the realtime engine.
1436 * \return The ast_vm_user structure for the user that was found.
1438 static struct ast_vm_user *find_user(struct ast_vm_user *ivm, const char *context, const char *mailbox)
1440 /* This function could be made to generate one from a database, too */
1441 struct ast_vm_user *vmu = NULL, *cur;
1442 AST_LIST_LOCK(&users);
1444 if (!context && !ast_test_flag((&globalflags), VM_SEARCH))
1445 context = "default";
1447 AST_LIST_TRAVERSE(&users, cur, list) {
1449 if (cur->imapversion != imapversion) {
1453 if (ast_test_flag((&globalflags), VM_SEARCH) && !strcasecmp(mailbox, cur->mailbox))
1455 if (context && (!strcasecmp(context, cur->context)) && (!strcasecmp(mailbox, cur->mailbox)))
1459 /* Make a copy, so that on a reload, we have no race */
1460 if ((vmu = (ivm ? ivm : ast_malloc(sizeof(*vmu))))) {
1463 vmu->emailbody = ast_strdup(cur->emailbody);
1464 vmu->emailsubject = ast_strdup(cur->emailsubject);
1466 ast_set2_flag(vmu, !ivm, VM_ALLOCED);
1467 AST_LIST_NEXT(vmu, list) = NULL;
1470 vmu = find_user_realtime(ivm, context, mailbox);
1471 AST_LIST_UNLOCK(&users);
1476 * \brief Resets a user password to a specified password.
1481 * This does the actual change password work, called by the vm_change_password() function.
1483 * \return zero on success, -1 on error.
1485 static int reset_user_pw(const char *context, const char *mailbox, const char *newpass)
1487 /* This function could be made to generate one from a database, too */
1488 struct ast_vm_user *cur;
1490 AST_LIST_LOCK(&users);
1491 AST_LIST_TRAVERSE(&users, cur, list) {
1492 if ((!context || !strcasecmp(context, cur->context)) &&
1493 (!strcasecmp(mailbox, cur->mailbox)))
1497 ast_copy_string(cur->password, newpass, sizeof(cur->password));
1500 AST_LIST_UNLOCK(&users);
1505 * \brief The handler for the change password option.
1506 * \param vmu The voicemail user to work with.
1507 * \param newpassword The new password (that has been gathered from the appropriate prompting).
1508 * 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.
1509 * It is also called when the user wants to change their password from menu option '5' on the mailbox options menu.
1511 static void vm_change_password(struct ast_vm_user *vmu, const char *newpassword)
1513 struct ast_config *cfg = NULL;
1514 struct ast_variable *var = NULL;
1515 struct ast_category *cat = NULL;
1516 char *category = NULL, *value = NULL, *new = NULL;
1517 const char *tmp = NULL;
1518 struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS };
1519 char secretfn[PATH_MAX] = "";
1522 if (!change_password_realtime(vmu, newpassword))
1525 /* check if we should store the secret in the spool directory next to the messages */
1526 switch (vmu->passwordlocation) {
1527 case OPT_PWLOC_SPOOLDIR:
1528 snprintf(secretfn, sizeof(secretfn), "%s%s/%s/secret.conf", VM_SPOOL_DIR, vmu->context, vmu->mailbox);
1529 if (write_password_to_file(secretfn, newpassword) == 0) {
1530 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: secret.conf updated with new password\r\nPasswordSource: secret.conf");
1531 ast_verb(4, "Writing voicemail password to file %s succeeded\n", secretfn);
1532 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1533 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1536 ast_verb(4, "Writing voicemail password to file %s failed, falling back to config file\n", secretfn);
1539 case OPT_PWLOC_VOICEMAILCONF:
1540 if ((cfg = ast_config_load(VOICEMAIL_CONFIG, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
1541 while ((category = ast_category_browse(cfg, category))) {
1542 if (!strcasecmp(category, vmu->context)) {
1543 if (!(tmp = ast_variable_retrieve(cfg, category, vmu->mailbox))) {
1544 ast_log(AST_LOG_WARNING, "We could not find the mailbox.\n");
1547 value = strstr(tmp, ",");
1549 new = alloca(strlen(newpassword)+1);
1550 sprintf(new, "%s", newpassword);
1552 new = alloca((strlen(value) + strlen(newpassword) + 1));
1553 sprintf(new, "%s%s", newpassword, value);
1555 if (!(cat = ast_category_get(cfg, category))) {
1556 ast_log(AST_LOG_WARNING, "Failed to get category structure.\n");
1559 ast_variable_update(cat, vmu->mailbox, new, NULL, 0);
1563 /* save the results */
1565 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: voicemail.conf updated with new password\r\nPasswordSource: voicemail.conf");
1566 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1567 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1568 ast_config_text_file_save(VOICEMAIL_CONFIG, cfg, "AppVoicemail");
1573 case OPT_PWLOC_USERSCONF:
1574 /* check users.conf and update the password stored for the mailbox */
1575 /* if no vmsecret entry exists create one. */
1576 if ((cfg = ast_config_load("users.conf", config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
1577 ast_debug(4, "we are looking for %s\n", vmu->mailbox);
1578 for (category = ast_category_browse(cfg, NULL); category; category = ast_category_browse(cfg, category)) {
1579 ast_debug(4, "users.conf: %s\n", category);
1580 if (!strcasecmp(category, vmu->mailbox)) {
1581 if (!(tmp = ast_variable_retrieve(cfg, category, "vmsecret"))) {
1582 ast_debug(3, "looks like we need to make vmsecret!\n");
1583 var = ast_variable_new("vmsecret", newpassword, "");
1587 new = alloca(strlen(newpassword) + 1);
1588 sprintf(new, "%s", newpassword);
1589 if (!(cat = ast_category_get(cfg, category))) {
1590 ast_debug(4, "failed to get category!\n");
1595 ast_variable_update(cat, "vmsecret", new, NULL, 0);
1597 ast_variable_append(cat, var);
1603 /* save the results and clean things up */
1605 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: users.conf updated with new password\r\nPasswordSource: users.conf");
1606 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1607 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1608 ast_config_text_file_save("users.conf", cfg, "AppVoicemail");
1614 static void vm_change_password_shell(struct ast_vm_user *vmu, char *newpassword)
1617 snprintf(buf, sizeof(buf), "%s %s %s %s", ext_pass_cmd, vmu->context, vmu->mailbox, newpassword);
1618 ast_debug(1, "External password: %s\n",buf);
1619 if (!ast_safe_system(buf)) {
1620 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: external script updated with new password\r\nPasswordSource: external");
1621 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1622 /* Reset the password in memory, too */
1623 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1628 * \brief Creates a file system path expression for a folder within the voicemail data folder and the appropriate context.
1629 * \param dest The variable to hold the output generated path expression. This buffer should be of size PATH_MAX.
1630 * \param len The length of the path string that was written out.
1635 * The path is constructed as
1636 * VM_SPOOL_DIRcontext/ext/folder
1638 * \return zero on success, -1 on error.
1640 static int make_dir(char *dest, int len, const char *context, const char *ext, const char *folder)
1642 return snprintf(dest, len, "%s%s/%s/%s", VM_SPOOL_DIR, context, ext, folder);
1646 * \brief Creates a file system path expression for a folder within the voicemail data folder and the appropriate context.
1647 * \param dest The variable to hold the output generated path expression. This buffer should be of size PATH_MAX.
1648 * \param len The length of the path string that was written out.
1652 * The path is constructed as
1653 * VM_SPOOL_DIRcontext/ext/folder
1655 * \return zero on success, -1 on error.
1657 static int make_file(char *dest, const int len, const char *dir, const int num)
1659 return snprintf(dest, len, "%s/msg%04d", dir, num);
1662 /* same as mkstemp, but return a FILE * */
1663 static FILE *vm_mkftemp(char *template)
1666 int pfd = mkstemp(template);
1667 chmod(template, VOICEMAIL_FILE_MODE & ~my_umask);
1669 p = fdopen(pfd, "w+");
1678 /*! \brief basically mkdir -p $dest/$context/$ext/$folder
1679 * \param dest String. base directory.
1680 * \param len Length of dest.
1681 * \param context String. Ignored if is null or empty string.
1682 * \param ext String. Ignored if is null or empty string.
1683 * \param folder String. Ignored if is null or empty string.
1684 * \return -1 on failure, 0 on success.
1686 static int create_dirpath(char *dest, int len, const char *context, const char *ext, const char *folder)
1688 mode_t mode = VOICEMAIL_DIR_MODE;
1691 make_dir(dest, len, context, ext, folder);
1692 if ((res = ast_mkdir(dest, mode))) {
1693 ast_log(AST_LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res));
1699 static const char * const mailbox_folders[] = {
1718 static const char *mbox(struct ast_vm_user *vmu, int id)
1721 if (vmu && id == 0) {
1722 return vmu->imapfolder;
1725 return (id >= 0 && id < ARRAY_LEN(mailbox_folders)) ? mailbox_folders[id] : "Unknown";
1728 static int get_folder_by_name(const char *name)
1732 for (i = 0; i < ARRAY_LEN(mailbox_folders); i++) {
1733 if (strcasecmp(name, mailbox_folders[i]) == 0) {
1741 static void free_user(struct ast_vm_user *vmu)
1743 if (ast_test_flag(vmu, VM_ALLOCED)) {
1745 ast_free(vmu->emailbody);
1746 vmu->emailbody = NULL;
1748 ast_free(vmu->emailsubject);
1749 vmu->emailsubject = NULL;
1755 static int vm_allocate_dh(struct vm_state *vms, struct ast_vm_user *vmu, int count_msg) {
1757 int arraysize = (vmu->maxmsg > count_msg ? vmu->maxmsg : count_msg);
1758 if (!vms->dh_arraysize) {
1759 /* initial allocation */
1760 if (!(vms->deleted = ast_calloc(arraysize, sizeof(int)))) {
1763 if (!(vms->heard = ast_calloc(arraysize, sizeof(int)))) {
1766 vms->dh_arraysize = arraysize;
1767 } else if (vms->dh_arraysize < arraysize) {
1768 if (!(vms->deleted = ast_realloc(vms->deleted, arraysize * sizeof(int)))) {
1771 if (!(vms->heard = ast_realloc(vms->heard, arraysize * sizeof(int)))) {
1774 memset(vms->deleted, 0, arraysize * sizeof(int));
1775 memset(vms->heard, 0, arraysize * sizeof(int));
1776 vms->dh_arraysize = arraysize;
1782 /* All IMAP-specific functions should go in this block. This
1783 * keeps them from being spread out all over the code */
1785 static void vm_imap_delete(char *file, int msgnum, struct ast_vm_user *vmu)
1788 struct vm_state *vms;
1789 unsigned long messageNum;
1791 /* If greetings aren't stored in IMAP, just delete the file */
1792 if (msgnum < 0 && !imapgreetings) {
1793 ast_filedelete(file, NULL);
1797 if (!(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && !(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
1798 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);
1802 /* find real message number based on msgnum */
1803 /* this may be an index into vms->msgArray based on the msgnum. */
1804 messageNum = vms->msgArray[msgnum];
1805 if (messageNum == 0) {
1806 ast_log(LOG_WARNING, "msgnum %d, mailbox message %lu is zero.\n", msgnum, messageNum);
1809 ast_debug(3, "deleting msgnum %d, which is mailbox message %lu\n", msgnum, messageNum);
1810 /* delete message */
1811 snprintf (arg, sizeof(arg), "%lu", messageNum);
1812 ast_mutex_lock(&vms->lock);
1813 mail_setflag (vms->mailstream, arg, "\\DELETED");
1814 mail_expunge(vms->mailstream);
1815 ast_mutex_unlock(&vms->lock);
1818 static int imap_retrieve_greeting(const char *dir, const int msgnum, struct ast_vm_user *vmu)
1820 struct vm_state *vms_p;
1821 char *file, *filename;
1826 /* This function is only used for retrieval of IMAP greetings
1827 * regular messages are not retrieved this way, nor are greetings
1828 * if they are stored locally*/
1829 if (msgnum > -1 || !imapgreetings) {
1832 file = strrchr(ast_strdupa(dir), '/');
1836 ast_debug(1, "Failed to procure file name from directory passed.\n");
1841 /* check if someone is accessing this box right now... */
1842 if (!(vms_p = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) &&
1843 !(vms_p = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
1844 /* Unlike when retrieving a message, it is reasonable not to be able to find a
1845 * vm_state for a mailbox when trying to retrieve a greeting. Just create one,
1846 * that's all we need to do.
1848 if (!(vms_p = create_vm_state_from_user(vmu))) {
1849 ast_log(LOG_NOTICE, "Unable to create vm_state object!\n");
1854 /* Greetings will never have a prepended message */
1855 *vms_p->introfn = '\0';
1857 ast_mutex_lock(&vms_p->lock);
1858 ret = init_mailstream(vms_p, GREETINGS_FOLDER);
1859 if (!vms_p->mailstream) {
1860 ast_log(AST_LOG_ERROR, "IMAP mailstream is NULL\n");
1861 ast_mutex_unlock(&vms_p->lock);
1865 /*XXX Yuck, this could probably be done a lot better */
1866 for (i = 0; i < vms_p->mailstream->nmsgs; i++) {
1867 mail_fetchstructure(vms_p->mailstream, i + 1, &body);
1868 /* We have the body, now we extract the file name of the first attachment. */
1869 if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
1870 attachment = ast_strdupa(body->nested.part->next->body.parameter->value);
1872 ast_log(AST_LOG_ERROR, "There is no file attached to this IMAP message.\n");
1873 ast_mutex_unlock(&vms_p->lock);
1876 filename = strsep(&attachment, ".");
1877 if (!strcmp(filename, file)) {
1878 ast_copy_string(vms_p->fn, dir, sizeof(vms_p->fn));
1879 vms_p->msgArray[vms_p->curmsg] = i + 1;
1880 save_body(body, vms_p, "2", attachment, 0);
1881 ast_mutex_unlock(&vms_p->lock);
1885 ast_mutex_unlock(&vms_p->lock);
1890 static int imap_retrieve_file(const char *dir, const int msgnum, const char *mailbox, const char *context)
1893 char *header_content;
1894 char *attachedfilefmt;
1896 struct vm_state *vms;
1897 char text_file[PATH_MAX];
1898 FILE *text_file_ptr;
1900 struct ast_vm_user *vmu;
1902 if (!(vmu = find_user(NULL, context, mailbox))) {
1903 ast_log(LOG_WARNING, "Couldn't find user with mailbox %s@%s\n", mailbox, context);
1908 if (imapgreetings) {
1909 res = imap_retrieve_greeting(dir, msgnum, vmu);
1917 /* Before anything can happen, we need a vm_state so that we can
1918 * actually access the imap server through the vms->mailstream
1920 if (!(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && !(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
1921 /* This should not happen. If it does, then I guess we'd
1922 * need to create the vm_state, extract which mailbox to
1923 * open, and then set up the msgArray so that the correct
1924 * IMAP message could be accessed. If I have seen correctly
1925 * though, the vms should be obtainable from the vmstates list
1926 * and should have its msgArray properly set up.
1928 ast_log(LOG_ERROR, "Couldn't find a vm_state for mailbox %s!!! Oh no!\n", vmu->mailbox);
1933 make_file(vms->fn, sizeof(vms->fn), dir, msgnum);
1934 snprintf(vms->introfn, sizeof(vms->introfn), "%sintro", vms->fn);
1936 /* Don't try to retrieve a message from IMAP if it already is on the file system */
1937 if (ast_fileexists(vms->fn, NULL, NULL) > 0) {
1942 ast_debug(3, "Before mail_fetchheaders, curmsg is: %d, imap messages is %lu\n", msgnum, vms->msgArray[msgnum]);
1943 if (vms->msgArray[msgnum] == 0) {
1944 ast_log(LOG_WARNING, "Trying to access unknown message\n");
1949 /* This will only work for new messages... */
1950 ast_mutex_lock(&vms->lock);
1951 header_content = mail_fetchheader (vms->mailstream, vms->msgArray[msgnum]);
1952 ast_mutex_unlock(&vms->lock);
1953 /* empty string means no valid header */
1954 if (ast_strlen_zero(header_content)) {
1955 ast_log(LOG_ERROR, "Could not fetch header for message number %ld\n", vms->msgArray[msgnum]);
1960 ast_mutex_lock(&vms->lock);
1961 mail_fetchstructure(vms->mailstream, vms->msgArray[msgnum], &body);
1962 ast_mutex_unlock(&vms->lock);
1964 /* We have the body, now we extract the file name of the first attachment. */
1965 if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
1966 attachedfilefmt = ast_strdupa(body->nested.part->next->body.parameter->value);
1968 ast_log(LOG_ERROR, "There is no file attached to this IMAP message.\n");
1973 /* Find the format of the attached file */
1975 strsep(&attachedfilefmt, ".");
1976 if (!attachedfilefmt) {
1977 ast_log(LOG_ERROR, "File format could not be obtained from IMAP message attachment\n");
1982 save_body(body, vms, "2", attachedfilefmt, 0);
1983 if (save_body(body, vms, "3", attachedfilefmt, 1)) {
1984 *vms->introfn = '\0';
1987 /* Get info from headers!! */
1988 snprintf(text_file, sizeof(text_file), "%s.%s", vms->fn, "txt");
1990 if (!(text_file_ptr = fopen(text_file, "w"))) {
1991 ast_log(LOG_WARNING, "Unable to open/create file %s: %s\n", text_file, strerror(errno));
1994 fprintf(text_file_ptr, "%s\n", "[message]");
1996 get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Name:", buf, sizeof(buf));
1997 fprintf(text_file_ptr, "callerid=\"%s\" ", S_OR(buf, ""));
1998 get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Num:", buf, sizeof(buf));
1999 fprintf(text_file_ptr, "<%s>\n", S_OR(buf, ""));
2000 get_header_by_tag(header_content, "X-Asterisk-VM-Context:", buf, sizeof(buf));
2001 fprintf(text_file_ptr, "context=%s\n", S_OR(buf, ""));
2002 get_header_by_tag(header_content, "X-Asterisk-VM-Orig-time:", buf, sizeof(buf));
2003 fprintf(text_file_ptr, "origtime=%s\n", S_OR(buf, ""));
2004 get_header_by_tag(header_content, "X-Asterisk-VM-Duration:", buf, sizeof(buf));
2005 fprintf(text_file_ptr, "duration=%s\n", S_OR(buf, ""));
2006 get_header_by_tag(header_content, "X-Asterisk-VM-Category:", buf, sizeof(buf));
2007 fprintf(text_file_ptr, "category=%s\n", S_OR(buf, ""));
2008 get_header_by_tag(header_content, "X-Asterisk-VM-Flag:", buf, sizeof(buf));
2009 fprintf(text_file_ptr, "flag=%s\n", S_OR(buf, ""));
2010 fclose(text_file_ptr);
2017 static int folder_int(const char *folder)
2019 /*assume a NULL folder means INBOX*/
2023 if (!strcasecmp(folder, imapfolder)) {
2025 } else if (!strcasecmp(folder, "Old")) {
2027 } else if (!strcasecmp(folder, "Work")) {
2029 } else if (!strcasecmp(folder, "Family")) {
2031 } else if (!strcasecmp(folder, "Friends")) {
2033 } else if (!strcasecmp(folder, "Cust1")) {
2035 } else if (!strcasecmp(folder, "Cust2")) {
2037 } else if (!strcasecmp(folder, "Cust3")) {
2039 } else if (!strcasecmp(folder, "Cust4")) {
2041 } else if (!strcasecmp(folder, "Cust5")) {
2043 } else if (!strcasecmp(folder, "Urgent")) {
2045 } else { /*assume they meant INBOX if folder is not found otherwise*/
2050 static int __messagecount(const char *context, const char *mailbox, const char *folder)
2055 struct ast_vm_user *vmu, vmus;
2056 struct vm_state *vms_p;
2058 int fold = folder_int(folder);
2061 /* If URGENT, then look at INBOX */
2067 if (ast_strlen_zero(mailbox))
2070 /* We have to get the user before we can open the stream! */
2071 vmu = find_user(&vmus, context, mailbox);
2073 ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailbox, context);
2076 /* No IMAP account available */
2077 if (vmu->imapuser[0] == '\0') {
2078 ast_log(AST_LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
2083 /* No IMAP account available */
2084 if (vmu->imapuser[0] == '\0') {
2085 ast_log(AST_LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
2090 /* check if someone is accessing this box right now... */
2091 vms_p = get_vm_state_by_imapuser(vmu->imapuser, 1);
2093 vms_p = get_vm_state_by_mailbox(mailbox, context, 1);
2096 ast_debug(3, "Returning before search - user is logged in\n");
2097 if (fold == 0) { /* INBOX */
2098 return urgent ? vms_p->urgentmessages : vms_p->newmessages;
2100 if (fold == 1) { /* Old messages */
2101 return vms_p->oldmessages;
2105 /* add one if not there... */
2106 vms_p = get_vm_state_by_imapuser(vmu->imapuser, 0);
2108 vms_p = get_vm_state_by_mailbox(mailbox, context, 0);
2112 vms_p = create_vm_state_from_user(vmu);
2114 ret = init_mailstream(vms_p, fold);
2115 if (!vms_p->mailstream) {
2116 ast_log(AST_LOG_ERROR, "Houston we have a problem - IMAP mailstream is NULL\n");
2120 ast_mutex_lock(&vms_p->lock);
2121 pgm = mail_newsearchpgm ();
2122 hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", (char *)(!ast_strlen_zero(vmu->imapvmshareid) ? vmu->imapvmshareid : mailbox));
2123 hdr->next = mail_newsearchheader("X-Asterisk-VM-Context", (char *) S_OR(context, "default"));
2125 if (fold != OLD_FOLDER) {
2129 /* In the special case where fold is 1 (old messages) we have to do things a bit
2130 * differently. Old messages are stored in the INBOX but are marked as "seen"
2136 /* look for urgent messages */
2137 if (fold == NEW_FOLDER) {
2149 vms_p->vmArrayIndex = 0;
2150 mail_search_full (vms_p->mailstream, NULL, pgm, NIL);
2151 if (fold == 0 && urgent == 0)
2152 vms_p->newmessages = vms_p->vmArrayIndex;
2154 vms_p->oldmessages = vms_p->vmArrayIndex;
2155 if (fold == 0 && urgent == 1)
2156 vms_p->urgentmessages = vms_p->vmArrayIndex;
2157 /*Freeing the searchpgm also frees the searchhdr*/
2158 mail_free_searchpgm(&pgm);
2159 ast_mutex_unlock(&vms_p->lock);
2161 return vms_p->vmArrayIndex;
2163 ast_mutex_lock(&vms_p->lock);
2164 mail_ping(vms_p->mailstream);
2165 ast_mutex_unlock(&vms_p->lock);
2170 static int imap_check_limits(struct ast_channel *chan, struct vm_state *vms, struct ast_vm_user *vmu, int msgnum)
2172 /* Check if mailbox is full */
2173 check_quota(vms, vmu->imapfolder);
2174 if (vms->quota_limit && vms->quota_usage >= vms->quota_limit) {
2175 ast_debug(1, "*** QUOTA EXCEEDED!! %u >= %u\n", vms->quota_usage, vms->quota_limit);
2176 ast_play_and_wait(chan, "vm-mailboxfull");
2180 /* Check if we have exceeded maxmsg */
2181 ast_debug(3, "Checking message number quota: mailbox has %d messages, maximum is set to %d, current messages %d\n", msgnum, vmu->maxmsg, inprocess_count(vmu->mailbox, vmu->context, 0));
2182 if (msgnum >= vmu->maxmsg - inprocess_count(vmu->mailbox, vmu->context, +1)) {
2183 ast_log(LOG_WARNING, "Unable to leave message since we will exceed the maximum number of messages allowed (%u >= %u)\n", msgnum, vmu->maxmsg);
2184 ast_play_and_wait(chan, "vm-mailboxfull");
2185 pbx_builtin_setvar_helper(chan, "VMSTATUS", "FAILED");
2193 * \brief Gets the number of messages that exist in a mailbox folder.
2198 * This method is used when IMAP backend is used.
2199 * \return The number of messages in this mailbox folder (zero or more).
2201 static int messagecount(const char *context, const char *mailbox, const char *folder)
2203 if (ast_strlen_zero(folder) || !strcmp(folder, "INBOX")) {
2204 return __messagecount(context, mailbox, "INBOX") + __messagecount(context, mailbox, "Urgent");
2206 return __messagecount(context, mailbox, folder);
2210 static int imap_store_file(const char *dir, const char *mailboxuser, const char *mailboxcontext, int msgnum, struct ast_channel *chan, struct ast_vm_user *vmu, char *fmt, int duration, struct vm_state *vms, const char *flag)
2212 char *myserveremail = serveremail;
2214 char introfn[PATH_MAX];
2218 char tmp[80] = "/tmp/astmail-XXXXXX";
2223 int ret; /* for better error checking */
2224 char *imap_flags = NIL;
2225 int msgcount = (messagecount(vmu->context, vmu->mailbox, "INBOX") + messagecount(vmu->context, vmu->mailbox, "Old"));
2226 int box = NEW_FOLDER;
2228 /* Back out early if this is a greeting and we don't want to store greetings in IMAP */
2230 if(!imapgreetings) {
2233 box = GREETINGS_FOLDER;
2237 if (imap_check_limits(chan, vms, vmu, msgcount)) {
2241 /* Set urgent flag for IMAP message */
2242 if (!ast_strlen_zero(flag) && !strcmp(flag, "Urgent")) {
2243 ast_debug(3, "Setting message flag \\\\FLAGGED.\n");
2244 imap_flags = "\\FLAGGED";
2247 /* Attach only the first format */
2248 fmt = ast_strdupa(fmt);
2250 strsep(&stringp, "|");
2252 if (!ast_strlen_zero(vmu->serveremail))
2253 myserveremail = vmu->serveremail;
2256 make_file(fn, sizeof(fn), dir, msgnum);
2258 ast_copy_string (fn, dir, sizeof(fn));
2260 snprintf(introfn, sizeof(introfn), "%sintro", fn);
2261 if (ast_fileexists(introfn, NULL, NULL) <= 0) {
2265 if (ast_strlen_zero(vmu->email)) {
2266 /* We need the vmu->email to be set when we call make_email_file, but
2267 * if we keep it set, a duplicate e-mail will be created. So at the end
2268 * of this function, we will revert back to an empty string if tempcopy
2271 ast_copy_string(vmu->email, vmu->imapuser, sizeof(vmu->email));
2275 if (!strcmp(fmt, "wav49"))
2277 ast_debug(3, "Storing file '%s', format '%s'\n", fn, fmt);
2279 /* Make a temporary file instead of piping directly to sendmail, in case the mail
2281 if (!(p = vm_mkftemp(tmp))) {
2282 ast_log(AST_LOG_WARNING, "Unable to store '%s' (can't create temporary file)\n", fn);
2284 *(vmu->email) = '\0';
2288 if (msgnum < 0 && imapgreetings) {
2289 if ((ret = init_mailstream(vms, GREETINGS_FOLDER))) {
2290 ast_log(AST_LOG_WARNING, "Unable to open mailstream.\n");
2293 imap_delete_old_greeting(fn, vms);
2296 make_email_file(p, myserveremail, vmu, msgnum, vmu->context, vmu->mailbox, "INBOX",
2297 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL),
2298 S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, NULL),
2299 fn, introfn, fmt, duration, 1, chan, NULL, 1, flag);
2300 /* read mail file to memory */
2303 if (!(buf = ast_malloc(len + 1))) {
2304 ast_log(AST_LOG_ERROR, "Can't allocate %ld bytes to read message\n", len + 1);
2307 *(vmu->email) = '\0';
2310 if (fread(buf, len, 1, p) < len) {
2312 ast_log(LOG_ERROR, "Short read while reading in mail file.\n");
2316 ((char *) buf)[len] = '\0';
2317 INIT(&str, mail_string, buf, len);
2318 ret = init_mailstream(vms, box);
2320 imap_mailbox_name(mailbox, sizeof(mailbox), vms, box, 1);
2321 ast_mutex_lock(&vms->lock);
2322 if(!mail_append_full(vms->mailstream, mailbox, imap_flags, NIL, &str))
2323 ast_log(LOG_ERROR, "Error while sending the message to %s\n", mailbox);
2324 ast_mutex_unlock(&vms->lock);
2329 ast_log(LOG_ERROR, "Could not initialize mailstream for %s\n", mailbox);
2335 ast_debug(3, "%s stored\n", fn);
2338 *(vmu->email) = '\0';
2339 inprocess_count(vmu->mailbox, vmu->context, -1);
2345 * \brief Gets the number of messages that exist in the inbox folder.
2346 * \param mailbox_context
2347 * \param newmsgs The variable that is updated with the count of new messages within this inbox.
2348 * \param oldmsgs The variable that is updated with the count of old messages within this inbox.
2349 * \param urgentmsgs The variable that is updated with the count of urgent messages within this inbox.
2351 * This method is used when IMAP backend is used.
2352 * Simultaneously determines the count of new,old, and urgent messages. The total messages would then be the sum of these three.
2354 * \return zero on success, -1 on error.
2357 static int inboxcount2(const char *mailbox_context, int *urgentmsgs, int *newmsgs, int *oldmsgs)
2359 char tmp[PATH_MAX] = "";
2371 ast_debug(3, "Mailbox is set to %s\n", mailbox_context);
2372 /* If no mailbox, return immediately */
2373 if (ast_strlen_zero(mailbox_context))
2376 ast_copy_string(tmp, mailbox_context, sizeof(tmp));
2377 context = strchr(tmp, '@');
2378 if (strchr(mailbox_context, ',')) {
2379 int tmpnew, tmpold, tmpurgent;
2380 ast_copy_string(tmp, mailbox_context, sizeof(tmp));
2382 while ((cur = strsep(&mb, ", "))) {
2383 if (!ast_strlen_zero(cur)) {
2384 if (inboxcount2(cur, urgentmsgs ? &tmpurgent : NULL, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
2392 *urgentmsgs += tmpurgent;
2403 context = "default";
2404 mailboxnc = (char *) mailbox_context;
2408 struct ast_vm_user *vmu = find_user(NULL, context, mailboxnc);
2410 ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailboxnc, context);
2413 if ((*newmsgs = __messagecount(context, mailboxnc, vmu->imapfolder)) < 0) {
2418 if ((*oldmsgs = __messagecount(context, mailboxnc, "Old")) < 0) {
2423 if ((*urgentmsgs = __messagecount(context, mailboxnc, "Urgent")) < 0) {
2431 * \brief Determines if the given folder has messages.
2432 * \param mailbox The @ delimited string for user@context. If no context is found, uses 'default' for the context.
2433 * \param folder the folder to look in
2435 * This function is used when the mailbox is stored in an IMAP back end.
2436 * This invokes the messagecount(). Here we are interested in the presence of messages (> 0) only, not the actual count.
2437 * \return 1 if the folder has one or more messages. zero otherwise.
2440 static int has_voicemail(const char *mailbox, const char *folder)
2442 char tmp[256], *tmp2, *box, *context;
2443 ast_copy_string(tmp, mailbox, sizeof(tmp));
2445 if (strchr(tmp2, ',') || strchr(tmp2, '&')) {
2446 while ((box = strsep(&tmp2, ",&"))) {
2447 if (!ast_strlen_zero(box)) {
2448 if (has_voicemail(box, folder)) {
2454 if ((context = strchr(tmp, '@'))) {
2457 context = "default";
2459 return __messagecount(context, tmp, folder) ? 1 : 0;
2463 * \brief Copies a message from one mailbox to another.
2473 * This works with IMAP storage based mailboxes.
2475 * \return zero on success, -1 on error.
2477 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)
2479 struct vm_state *sendvms = NULL, *destvms = NULL;
2480 char messagestring[10]; /*I guess this could be a problem if someone has more than 999999999 messages...*/
2481 if (msgnum >= recip->maxmsg) {
2482 ast_log(LOG_WARNING, "Unable to copy mail, mailbox %s is full\n", recip->mailbox);
2485 if (!(sendvms = get_vm_state_by_imapuser(vmu->imapuser, 0))) {
2486 ast_log(LOG_ERROR, "Couldn't get vm_state for originator's mailbox!!\n");
2489 if (!(destvms = get_vm_state_by_imapuser(recip->imapuser, 0))) {
2490 ast_log(LOG_ERROR, "Couldn't get vm_state for destination mailbox!\n");
2493 snprintf(messagestring, sizeof(messagestring), "%ld", sendvms->msgArray[msgnum]);
2494 ast_mutex_lock(&sendvms->lock);
2495 if ((mail_copy(sendvms->mailstream, messagestring, (char *) mbox(vmu, imbox)) == T)) {
2496 ast_mutex_unlock(&sendvms->lock);
2499 ast_mutex_unlock(&sendvms->lock);
2500 ast_log(LOG_WARNING, "Unable to copy message from mailbox %s to mailbox %s\n", vmu->mailbox, recip->mailbox);
2504 static void imap_mailbox_name(char *spec, size_t len, struct vm_state *vms, int box, int use_folder)
2506 char tmp[256], *t = tmp;
2507 size_t left = sizeof(tmp);
2509 if (box == OLD_FOLDER) {
2510 ast_copy_string(vms->curbox, mbox(NULL, NEW_FOLDER), sizeof(vms->curbox));
2512 ast_copy_string(vms->curbox, mbox(NULL, box), sizeof(vms->curbox));
2515 if (box == NEW_FOLDER) {
2516 ast_copy_string(vms->vmbox, "vm-INBOX", sizeof(vms->vmbox));
2518 snprintf(vms->vmbox, sizeof(vms->vmbox), "vm-%s", mbox(NULL, box));
2521 /* Build up server information */
2522 ast_build_string(&t, &left, "{%s:%s/imap", imapserver, imapport);
2524 /* Add authentication user if present */
2525 if (!ast_strlen_zero(authuser))
2526 ast_build_string(&t, &left, "/authuser=%s", authuser);
2528 /* Add flags if present */
2529 if (!ast_strlen_zero(imapflags))
2530 ast_build_string(&t, &left, "/%s", imapflags);
2532 /* End with username */
2534 ast_build_string(&t, &left, "/user=%s}", vms->imapuser);
2536 ast_build_string(&t, &left, "/user=%s/novalidate-cert}", vms->imapuser);
2538 if (box == NEW_FOLDER || box == OLD_FOLDER)
2539 snprintf(spec, len, "%s%s", tmp, use_folder? vms->imapfolder: "INBOX");
2540 else if (box == GREETINGS_FOLDER)
2541 snprintf(spec, len, "%s%s", tmp, greetingfolder);
2542 else { /* Other folders such as Friends, Family, etc... */
2543 if (!ast_strlen_zero(imapparentfolder)) {
2544 /* imapparentfolder would typically be set to INBOX */
2545 snprintf(spec, len, "%s%s%c%s", tmp, imapparentfolder, delimiter, mbox(NULL, box));
2547 snprintf(spec, len, "%s%s", tmp, mbox(NULL, box));
2552 static int init_mailstream(struct vm_state *vms, int box)
2554 MAILSTREAM *stream = NIL;
2559 ast_log(LOG_ERROR, "vm_state is NULL!\n");
2562 ast_debug(3, "vm_state user is:%s\n", vms->imapuser);
2563 if (vms->mailstream == NIL || !vms->mailstream) {
2564 ast_debug(1, "mailstream not set.\n");
2566 stream = vms->mailstream;
2568 /* debug = T; user wants protocol telemetry? */
2569 debug = NIL; /* NO protocol telemetry? */
2571 if (delimiter == '\0') { /* did not probe the server yet */
2573 #ifdef USE_SYSTEM_IMAP
2574 #include <imap/linkage.c>
2575 #elif defined(USE_SYSTEM_CCLIENT)
2576 #include <c-client/linkage.c>
2578 #include "linkage.c"
2580 /* Connect to INBOX first to get folders delimiter */
2581 imap_mailbox_name(tmp, sizeof(tmp), vms, 0, 1);
2582 ast_mutex_lock(&vms->lock);
2583 stream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
2584 ast_mutex_unlock(&vms->lock);
2585 if (stream == NIL) {
2586 ast_log(LOG_ERROR, "Can't connect to imap server %s\n", tmp);
2589 get_mailbox_delimiter(stream);
2590 /* update delimiter in imapfolder */
2591 for (cp = vms->imapfolder; *cp; cp++)
2595 /* Now connect to the target folder */
2596 imap_mailbox_name(tmp, sizeof(tmp), vms, box, 1);
2597 ast_debug(3, "Before mail_open, server: %s, box:%d\n", tmp, box);
2598 ast_mutex_lock(&vms->lock);
2599 vms->mailstream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
2600 ast_mutex_unlock(&vms->lock);
2601 if (vms->mailstream == NIL) {
2608 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box)
2612 int ret, urgent = 0;
2614 /* If Urgent, then look at INBOX */
2620 ast_copy_string(vms->imapuser, vmu->imapuser, sizeof(vms->imapuser));
2621 ast_copy_string(vms->imapfolder, vmu->imapfolder, sizeof(vms->imapfolder));
2622 vms->imapversion = vmu->imapversion;
2623 ast_debug(3, "Before init_mailstream, user is %s\n", vmu->imapuser);
2625 if ((ret = init_mailstream(vms, box)) || !vms->mailstream) {
2626 ast_log(AST_LOG_ERROR, "Could not initialize mailstream\n");
2630 create_dirpath(vms->curdir, sizeof(vms->curdir), vmu->context, vms->username, vms->curbox);
2634 ast_debug(3, "Mailbox name set to: %s, about to check quotas\n", mbox(vmu, box));
2635 check_quota(vms, (char *) mbox(vmu, box));
2638 ast_mutex_lock(&vms->lock);
2639 pgm = mail_newsearchpgm();
2641 /* Check IMAP folder for Asterisk messages only... */
2642 hdr = mail_newsearchheader("X-Asterisk-VM-Extension", (!ast_strlen_zero(vmu->imapvmshareid) ? vmu->imapvmshareid : vmu->mailbox));
2643 hdr->next = mail_newsearchheader("X-Asterisk-VM-Context", vmu->context);
2648 /* if box = NEW_FOLDER, check for new, if box = OLD_FOLDER, check for read */
2649 if (box == NEW_FOLDER && urgent == 1) {
2654 } else if (box == NEW_FOLDER && urgent == 0) {
2659 } else if (box == OLD_FOLDER) {
2664 ast_debug(3, "Before mail_search_full, user is %s\n", vmu->imapuser);
2666 vms->vmArrayIndex = 0;
2667 mail_search_full (vms->mailstream, NULL, pgm, NIL);
2668 vms->lastmsg = vms->vmArrayIndex - 1;
2669 mail_free_searchpgm(&pgm);
2670 /* Since IMAP storage actually stores both old and new messages in the same IMAP folder,
2671 * ensure to allocate enough space to account for all of them. Warn if old messages
2672 * have not been checked first as that is required.
2674 if (box == 0 && !vms->dh_arraysize) {
2675 ast_log(LOG_WARNING, "The code expects the old messages to be checked first, fix the code.\n");
2677 if (vm_allocate_dh(vms, vmu, box == 0 ? vms->vmArrayIndex + vms->oldmessages : vms->lastmsg)) {
2678 ast_mutex_unlock(&vms->lock);
2682 ast_mutex_unlock(&vms->lock);
2686 static void write_file(char *filename, char *buffer, unsigned long len)
2690 output = fopen (filename, "w");
2691 if (fwrite(buffer, len, 1, output) != 1) {
2692 if (ferror(output)) {
2693 ast_log(LOG_ERROR, "Short write while writing e-mail body: %s.\n", strerror(errno));
2699 static void update_messages_by_imapuser(const char *user, unsigned long number)
2701 struct vm_state *vms = get_vm_state_by_imapuser(user, 1);
2703 if (!vms && !(vms = get_vm_state_by_imapuser(user, 0))) {
2707 ast_debug(3, "saving mailbox message number %lu as message %d. Interactive set to %d\n", number, vms->vmArrayIndex, vms->interactive);
2708 vms->msgArray[vms->vmArrayIndex++] = number;
2711 void mm_searched(MAILSTREAM *stream, unsigned long number)
2713 char *mailbox = stream->mailbox, buf[1024] = "", *user;
2715 if (!(user = get_user_by_mailbox(mailbox, buf, sizeof(buf))))
2718 update_messages_by_imapuser(user, number);
2721 static struct ast_vm_user *find_user_realtime_imapuser(const char *imapuser)
2723 struct ast_variable *var;
2724 struct ast_vm_user *vmu;
2726 vmu = ast_calloc(1, sizeof *vmu);
2729 ast_set_flag(vmu, VM_ALLOCED);
2730 populate_defaults(vmu);
2732 var = ast_load_realtime("voicemail", "imapuser", imapuser, NULL);
2734 apply_options_full(vmu, var);
2735 ast_variables_destroy(var);
2743 /* Interfaces to C-client */
2745 void mm_exists(MAILSTREAM * stream, unsigned long number)
2747 /* mail_ping will callback here if new mail! */
2748 ast_debug(4, "Entering EXISTS callback for message %ld\n", number);
2749 if (number == 0) return;
2754 void mm_expunged(MAILSTREAM * stream, unsigned long number)
2756 /* mail_ping will callback here if expunged mail! */
2757 ast_debug(4, "Entering EXPUNGE callback for message %ld\n", number);
2758 if (number == 0) return;
2763 void mm_flags(MAILSTREAM * stream, unsigned long number)
2765 /* mail_ping will callback here if read mail! */
2766 ast_debug(4, "Entering FLAGS callback for message %ld\n", number);
2767 if (number == 0) return;
2772 void mm_notify(MAILSTREAM * stream, char *string, long errflg)
2774 ast_debug(5, "Entering NOTIFY callback, errflag is %ld, string is %s\n", errflg, string);
2775 mm_log (string, errflg);
2779 void mm_list(MAILSTREAM * stream, int delim, char *mailbox, long attributes)
2781 if (delimiter == '\0') {
2785 ast_debug(5, "Delimiter set to %c and mailbox %s\n", delim, mailbox);
2786 if (attributes & LATT_NOINFERIORS)
2787 ast_debug(5, "no inferiors\n");
2788 if (attributes & LATT_NOSELECT)
2789 ast_debug(5, "no select\n");
2790 if (attributes & LATT_MARKED)
2791 ast_debug(5, "marked\n");
2792 if (attributes & LATT_UNMARKED)
2793 ast_debug(5, "unmarked\n");
2797 void mm_lsub(MAILSTREAM * stream, int delim, char *mailbox, long attributes)
2799 ast_debug(5, "Delimiter set to %c and mailbox %s\n", delim, mailbox);
2800 if (attributes & LATT_NOINFERIORS)
2801 ast_debug(5, "no inferiors\n");
2802 if (attributes & LATT_NOSELECT)
2803 ast_debug(5, "no select\n");
2804 if (attributes & LATT_MARKED)
2805 ast_debug(5, "marked\n");
2806 if (attributes & LATT_UNMARKED)
2807 ast_debug(5, "unmarked\n");
2811 void mm_status(MAILSTREAM * stream, char *mailbox, MAILSTATUS * status)
2813 ast_log(AST_LOG_NOTICE, " Mailbox %s", mailbox);
2814 if (status->flags & SA_MESSAGES)
2815 ast_log(AST_LOG_NOTICE, ", %lu messages", status->messages);
2816 if (status->flags & SA_RECENT)
2817 ast_log(AST_LOG_NOTICE, ", %lu recent", status->recent);
2818 if (status->flags & SA_UNSEEN)
2819 ast_log(AST_LOG_NOTICE, ", %lu unseen", status->unseen);
2820 if (status->flags & SA_UIDVALIDITY)
2821 ast_log(AST_LOG_NOTICE, ", %lu UID validity", status->uidvalidity);
2822 if (status->flags & SA_UIDNEXT)
2823 ast_log(AST_LOG_NOTICE, ", %lu next UID", status->uidnext);
2824 ast_log(AST_LOG_NOTICE, "\n");
2828 void mm_log(char *string, long errflg)
2830 switch ((short) errflg) {
2832 ast_debug(1, "IMAP Info: %s\n", string);
2836 ast_log(AST_LOG_WARNING, "IMAP Warning: %s\n", string);
2839 ast_log(AST_LOG_ERROR, "IMAP Error: %s\n", string);
2845 void mm_dlog(char *string)
2847 ast_log(AST_LOG_NOTICE, "%s\n", string);
2851 void mm_login(NETMBX * mb, char *user, char *pwd, long trial)
2853 struct ast_vm_user *vmu;
2855 ast_debug(4, "Entering callback mm_login\n");
2857 ast_copy_string(user, mb->user, MAILTMPLEN);
2859 /* We should only do this when necessary */
2860 if (!ast_strlen_zero(authpassword)) {
2861 ast_copy_string(pwd, authpassword, MAILTMPLEN);
2863 AST_LIST_TRAVERSE(&users, vmu, list) {
2864 if (!strcasecmp(mb->user, vmu->imapuser)) {
2865 ast_copy_string(pwd, vmu->imappassword, MAILTMPLEN);
2870 if ((vmu = find_user_realtime_imapuser(mb->user))) {
2871 ast_copy_string(pwd, vmu->imappassword, MAILTMPLEN);
2879 void mm_critical(MAILSTREAM * stream)
2884 void mm_nocritical(MAILSTREAM * stream)
2889 long mm_diskerror(MAILSTREAM * stream, long errcode, long serious)
2891 kill (getpid (), SIGSTOP);
2896 void mm_fatal(char *string)
2898 ast_log(AST_LOG_ERROR, "IMAP access FATAL error: %s\n", string);
2901 /* C-client callback to handle quota */
2902 static void mm_parsequota(MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pquota)
2904 struct vm_state *vms;
2905 char *mailbox = stream->mailbox, *user;
2906 char buf[1024] = "";
2907 unsigned long usage = 0, limit = 0;
2910 usage = pquota->usage;
2911 limit = pquota->limit;
2912 pquota = pquota->next;
2915 if (!(user = get_user_by_mailbox(mailbox, buf, sizeof(buf))) || (!(vms = get_vm_state_by_imapuser(user, 2)) && !(vms = get_vm_state_by_imapuser(user, 0)))) {
2916 ast_log(AST_LOG_ERROR, "No state found.\n");
2920 ast_debug(3, "User %s usage is %lu, limit is %lu\n", user, usage, limit);
2922 vms->quota_usage = usage;
2923 vms->quota_limit = limit;
2926 static char *get_header_by_tag(char *header, char *tag, char *buf, size_t len)
2928 char *start, *eol_pnt;
2931 if (ast_strlen_zero(header) || ast_strlen_zero(tag))
2934 taglen = strlen(tag) + 1;
2938 if (!(start = strstr(header, tag)))
2941 /* Since we can be called multiple times we should clear our buffer */
2942 memset(buf, 0, len);
2944 ast_copy_string(buf, start+taglen, len);
2945 if ((eol_pnt = strchr(buf,'\r')) || (eol_pnt = strchr(buf,'\n')))
2950 static char *get_user_by_mailbox(char *mailbox, char *buf, size_t len)
2952 char *start, *quote, *eol_pnt;
2954 if (ast_strlen_zero(mailbox))
2957 if (!(start = strstr(mailbox, "/user=")))
2960 ast_copy_string(buf, start+6, len);
2962 if (!(quote = strchr(buf, '\"'))) {
2963 if (!(eol_pnt = strchr(buf, '/')))
2964 eol_pnt = strchr(buf,'}');
2968 eol_pnt = strchr(buf+1,'\"');
2974 static struct vm_state *create_vm_state_from_user(struct ast_vm_user *vmu)
2976 struct vm_state *vms_p;
2978 pthread_once(&ts_vmstate.once, ts_vmstate.key_init);
2979 if ((vms_p = pthread_getspecific(ts_vmstate.key)) && !strcmp(vms_p->imapuser, vmu->imapuser) && !strcmp(vms_p->username, vmu->mailbox)) {
2982 ast_debug(5, "Adding new vmstate for %s\n", vmu->imapuser);
2983 if (!(vms_p = ast_calloc(1, sizeof(*vms_p))))
2985 ast_copy_string(vms_p->imapuser, vmu->imapuser, sizeof(vms_p->imapuser));
2986 ast_copy_string(vms_p->imapfolder, vmu->imapfolder, sizeof(vms_p->imapfolder));
2987 ast_copy_string(vms_p->username, vmu->mailbox, sizeof(vms_p->username)); /* save for access from interactive entry point */
2988 ast_copy_string(vms_p->context, vmu->context, sizeof(vms_p->context));
2989 vms_p->mailstream = NIL; /* save for access from interactive entry point */
2990 vms_p->imapversion = vmu->imapversion;
2991 ast_debug(5, "Copied %s to %s\n", vmu->imapuser, vms_p->imapuser);
2993 /* set mailbox to INBOX! */
2994 ast_copy_string(vms_p->curbox, mbox(vmu, 0), sizeof(vms_p->curbox));
2995 init_vm_state(vms_p);
2996 vmstate_insert(vms_p);
3000 static struct vm_state *get_vm_state_by_imapuser(const char *user, int interactive)
3002 struct vmstate *vlist = NULL;
3005 struct vm_state *vms;
3006 pthread_once(&ts_vmstate.once, ts_vmstate.key_init);
3007 vms = pthread_getspecific(ts_vmstate.key);
3011 AST_LIST_LOCK(&vmstates);
3012 AST_LIST_TRAVERSE(&vmstates, vlist, list) {
3014 ast_debug(3, "error: vms is NULL for %s\n", user);
3017 if (vlist->vms->imapversion != imapversion) {
3020 if (!vlist->vms->imapuser) {
3021 ast_debug(3, "error: imapuser is NULL for %s\n", user);
3025 if (!strcmp(vlist->vms->imapuser, user) && (interactive == 2 || vlist->vms->interactive == interactive)) {
3026 AST_LIST_UNLOCK(&vmstates);
3030 AST_LIST_UNLOCK(&vmstates);
3032 ast_debug(3, "%s not found in vmstates\n", user);
3037 static struct vm_state *get_vm_state_by_mailbox(const char *mailbox, const char *context, int interactive)
3040 struct vmstate *vlist = NULL;
3041 const char *local_context = S_OR(context, "default");
3044 struct vm_state *vms;
3045 pthread_once(&ts_vmstate.once, ts_vmstate.key_init);
3046 vms = pthread_getspecific(ts_vmstate.key);
3050 AST_LIST_LOCK(&vmstates);
3051 AST_LIST_TRAVERSE(&vmstates, vlist, list) {
3053 ast_debug(3, "error: vms is NULL for %s\n", mailbox);
3056 if (vlist->vms->imapversion != imapversion) {
3059 if (!vlist->vms->username || !vlist->vms->context) {
3060 ast_debug(3, "error: username is NULL for %s\n", mailbox);
3064 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);
3066 if (!strcmp(vlist->vms->username, mailbox) && !strcmp(vlist->vms->context, local_context) && vlist->vms->interactive == interactive) {
3067 ast_debug(3, "Found it!\n");
3068 AST_LIST_UNLOCK(&vmstates);
3072 AST_LIST_UNLOCK(&vmstates);
3074 ast_debug(3, "%s not found in vmstates\n", mailbox);
3079 static void vmstate_insert(struct vm_state *vms)
3082 struct vm_state *altvms;
3084 /* If interactive, it probably already exists, and we should
3085 use the one we already have since it is more up to date.
3086 We can compare the username to find the duplicate */
3087 if (vms->interactive == 1) {
3088 altvms = get_vm_state_by_mailbox(vms->username, vms->context, 0);
3090 ast_debug(3, "Duplicate mailbox %s, copying message info...\n", vms->username);
3091 vms->newmessages = altvms->newmessages;
3092 vms->oldmessages = altvms->oldmessages;
3093 vms->vmArrayIndex = altvms->vmArrayIndex;
3094 vms->lastmsg = altvms->lastmsg;
3095 vms->curmsg = altvms->curmsg;
3096 /* get a pointer to the persistent store */
3097 vms->persist_vms = altvms;
3098 /* Reuse the mailstream? */
3099 #ifdef REALLY_FAST_EVEN_IF_IT_MEANS_RESOURCE_LEAKS
3100 vms->mailstream = altvms->mailstream;
3102 vms->mailstream = NIL;
3108 if (!(v = ast_calloc(1, sizeof(*v))))
3113 ast_debug(3, "Inserting vm_state for user:%s, mailbox %s\n", vms->imapuser, vms->username);