Add VoicemailRefresh AMI Action
[asterisk/asterisk.git] / apps / app_voicemail.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*!
20  * \file
21  * \author Mark Spencer <markster@digium.com>
22  * \brief Comedian Mail - Voicemail System
23  *
24  * unixODBC (http://www.unixodbc.org/)
25  * A source distribution of University of Washington's IMAP c-client
26  *         (http://www.washington.edu/imap/)
27  *
28  * \par See also
29  * \arg \ref Config_vm
30  * \note For information about voicemail IMAP storage, https://wiki.asterisk.org/wiki/display/AST/IMAP+Voicemail+Storage
31   * \ingroup applications
32  * \todo This module requires res_adsi to load. This needs to be optional
33  * during compilation.
34  *
35  * \todo 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.
38  */
39
40 /*** MODULEINFO
41         <use type="module">res_adsi</use>
42         <use type="module">res_smdi</use>
43         <support_level>core</support_level>
44  ***/
45
46 /*** MAKEOPTS
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>
53         </member>
54         <member name="ODBC_STORAGE" displayname="Storage of Voicemail using ODBC">
55                 <depend>generic_odbc</depend>
56                 <depend>ltdl</depend>
57                 <conflict>IMAP_STORAGE</conflict>
58                 <conflict>FILE_STORAGE</conflict>
59                 <defaultenabled>no</defaultenabled>
60                 <support_level>core</support_level>
61         </member>
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>
69         </member>
70 </category>
71 ***/
72
73 #include "asterisk.h"
74
75 #ifdef IMAP_STORAGE
76 #include <ctype.h>
77 #include <signal.h>
78 #include <pwd.h>
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>
87 #else
88 #include "c-client.h"
89 #include "imap4r1.h"
90 #include "linkage.h"
91 #endif
92 #endif
93
94 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
95
96 #include "asterisk/paths.h"     /* use ast_config_AST_SPOOL_DIR */
97 #include <sys/time.h>
98 #include <sys/stat.h>
99 #include <sys/mman.h>
100 #include <time.h>
101 #include <dirent.h>
102 #if defined(__FreeBSD__) || defined(__OpenBSD__)
103 #include <sys/wait.h>
104 #endif
105
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/strings.h"
123 #include "asterisk/smdi.h"
124 #include "asterisk/astobj2.h"
125 #include "asterisk/event.h"
126 #include "asterisk/taskprocessor.h"
127 #include "asterisk/test.h"
128
129 #ifdef ODBC_STORAGE
130 #include "asterisk/res_odbc.h"
131 #endif
132
133 #ifdef IMAP_STORAGE
134 #include "asterisk/threadstorage.h"
135 #endif
136
137 /*** DOCUMENTATION
138         <application name="VoiceMail" language="en_US">
139                 <synopsis>
140                         Leave a Voicemail message.
141                 </synopsis>
142                 <syntax>
143                         <parameter name="mailboxs" argsep="&amp;" required="true">
144                                 <argument name="mailbox1" argsep="@" required="true">
145                                         <argument name="mailbox" required="true" />
146                                         <argument name="context" />
147                                 </argument>
148                                 <argument name="mailbox2" argsep="@" multiple="true">
149                                         <argument name="mailbox" required="true" />
150                                         <argument name="context" />
151                                 </argument>
152                         </parameter>
153                         <parameter name="options">
154                                 <optionlist>
155                                         <option name="b">
156                                                 <para>Play the <literal>busy</literal> greeting to the calling party.</para>
157                                         </option>
158                                         <option name="d">
159                                                 <argument name="c" />
160                                                 <para>Accept digits for a new extension in context <replaceable>c</replaceable>,
161                                                 if played during the greeting. Context defaults to the current context.</para>
162                                         </option>
163                                         <option name="g">
164                                                 <argument name="#" required="true" />
165                                                 <para>Use the specified amount of gain when recording the voicemail
166                                                 message. The units are whole-number decibels (dB). Only works on supported
167                                                 technologies, which is DAHDI only.</para>
168                                         </option>
169                                         <option name="s">
170                                                 <para>Skip the playback of instructions for leaving a message to the
171                                                 calling party.</para>
172                                         </option>
173                                         <option name="u">
174                                                 <para>Play the <literal>unavailable</literal> greeting.</para>
175                                         </option>
176                                         <option name="U">
177                                                 <para>Mark message as <literal>URGENT</literal>.</para>
178                                         </option>
179                                         <option name="P">
180                                                 <para>Mark message as <literal>PRIORITY</literal>.</para>
181                                         </option>
182                                 </optionlist>
183                         </parameter>
184                 </syntax>
185                 <description>
186                         <para>This application allows the calling party to leave a message for the specified
187                         list of mailboxes. When multiple mailboxes are specified, the greeting will be taken from
188                         the first mailbox specified. Dialplan execution will stop if the specified mailbox does not
189                         exist.</para>
190                         <para>The Voicemail application will exit if any of the following DTMF digits are received:</para>
191                         <enumlist>
192                                 <enum name="0">
193                                         <para>Jump to the <literal>o</literal> extension in the current dialplan context.</para>
194                                 </enum>
195                                 <enum name="*">
196                                         <para>Jump to the <literal>a</literal> extension in the current dialplan context.</para>
197                                 </enum>
198                         </enumlist>
199                         <para>This application will set the following channel variable upon completion:</para>
200                         <variablelist>
201                                 <variable name="VMSTATUS">
202                                         <para>This indicates the status of the execution of the VoiceMail application.</para>
203                                         <value name="SUCCESS" />
204                                         <value name="USEREXIT" />
205                                         <value name="FAILED" />
206                                 </variable>
207                         </variablelist>
208                 </description>
209                 <see-also>
210                         <ref type="application">VoiceMailMain</ref>
211                 </see-also>
212         </application>
213         <application name="VoiceMailMain" language="en_US">
214                 <synopsis>
215                         Check Voicemail messages.
216                 </synopsis>
217                 <syntax>
218                         <parameter name="mailbox" required="true" argsep="@">
219                                 <argument name="mailbox" />
220                                 <argument name="context" />
221                         </parameter>
222                         <parameter name="options">
223                                 <optionlist>
224                                         <option name="p">
225                                                 <para>Consider the <replaceable>mailbox</replaceable> parameter as a prefix to
226                                                 the mailbox that is entered by the caller.</para>
227                                         </option>
228                                         <option name="g">
229                                                 <argument name="#" required="true" />
230                                                 <para>Use the specified amount of gain when recording a voicemail message.
231                                                 The units are whole-number decibels (dB).</para>
232                                         </option>
233                                         <option name="s">
234                                                 <para>Skip checking the passcode for the mailbox.</para>
235                                         </option>
236                                         <option name="a">
237                                                 <argument name="folder" required="true" />
238                                                 <para>Skip folder prompt and go directly to <replaceable>folder</replaceable> specified.
239                                                 Defaults to <literal>INBOX</literal> (or <literal>0</literal>).</para>
240                                                 <enumlist>
241                                                         <enum name="0"><para>INBOX</para></enum>
242                                                         <enum name="1"><para>Old</para></enum>
243                                                         <enum name="2"><para>Work</para></enum>
244                                                         <enum name="3"><para>Family</para></enum>
245                                                         <enum name="4"><para>Friends</para></enum>
246                                                         <enum name="5"><para>Cust1</para></enum>
247                                                         <enum name="6"><para>Cust2</para></enum>
248                                                         <enum name="7"><para>Cust3</para></enum>
249                                                         <enum name="8"><para>Cust4</para></enum>
250                                                         <enum name="9"><para>Cust5</para></enum>
251                                                 </enumlist>
252                                         </option>
253                                 </optionlist>
254                         </parameter>
255                 </syntax>
256                 <description>
257                         <para>This application allows the calling party to check voicemail messages. A specific
258                         <replaceable>mailbox</replaceable>, and optional corresponding <replaceable>context</replaceable>,
259                         may be specified. If a <replaceable>mailbox</replaceable> is not provided, the calling party will
260                         be prompted to enter one. If a <replaceable>context</replaceable> is not specified, the
261                         <literal>default</literal> context will be used.</para>
262                         <para>The VoiceMailMain application will exit if the following DTMF digit is entered as Mailbox
263                         or Password, and the extension exists:</para>
264                         <enumlist>
265                                 <enum name="*">
266                                         <para>Jump to the <literal>a</literal> extension in the current dialplan context.</para>
267                                 </enum>
268                         </enumlist>
269                 </description>
270                 <see-also>
271                         <ref type="application">VoiceMail</ref>
272                 </see-also>
273         </application>
274         <application name="MailboxExists" language="en_US">
275                 <synopsis>
276                         Check to see if Voicemail mailbox exists.
277                 </synopsis>
278                 <syntax>
279                         <parameter name="mailbox" required="true" argsep="@">
280                                 <argument name="mailbox" required="true" />
281                                 <argument name="context" />
282                         </parameter>
283                         <parameter name="options">
284                                 <para>None options.</para>
285                         </parameter>
286                 </syntax>
287                 <description>
288                         <note><para>DEPRECATED. Use VM_INFO(mailbox[@context],exists) instead.</para></note>
289                         <para>Check to see if the specified <replaceable>mailbox</replaceable> exists. If no voicemail
290                         <replaceable>context</replaceable> is specified, the <literal>default</literal> context
291                         will be used.</para>
292                         <para>This application will set the following channel variable upon completion:</para>
293                         <variablelist>
294                                 <variable name="VMBOXEXISTSSTATUS">
295                                         <para>This will contain the status of the execution of the MailboxExists application.
296                                         Possible values include:</para>
297                                         <value name="SUCCESS" />
298                                         <value name="FAILED" />
299                                 </variable>
300                         </variablelist>
301                 </description>
302                 <see-also>
303                         <ref type="function">VM_INFO</ref>
304                 </see-also>
305         </application>
306         <application name="VMAuthenticate" language="en_US">
307                 <synopsis>
308                         Authenticate with Voicemail passwords.
309                 </synopsis>
310                 <syntax>
311                         <parameter name="mailbox" required="true" argsep="@">
312                                 <argument name="mailbox" />
313                                 <argument name="context" />
314                         </parameter>
315                         <parameter name="options">
316                                 <optionlist>
317                                         <option name="s">
318                                                 <para>Skip playing the initial prompts.</para>
319                                         </option>
320                                 </optionlist>
321                         </parameter>
322                 </syntax>
323                 <description>
324                         <para>This application behaves the same way as the Authenticate application, but the passwords
325                         are taken from <filename>voicemail.conf</filename>. If the <replaceable>mailbox</replaceable> is
326                         specified, only that mailbox's password will be considered valid. If the <replaceable>mailbox</replaceable>
327                         is not specified, the channel variable <variable>AUTH_MAILBOX</variable> will be set with the authenticated
328                         mailbox.</para>
329                         <para>The VMAuthenticate application will exit if the following DTMF digit is entered as Mailbox
330                         or Password, and the extension exists:</para>
331                         <enumlist>
332                                 <enum name="*">
333                                         <para>Jump to the <literal>a</literal> extension in the current dialplan context.</para>
334                                 </enum>
335                         </enumlist>
336                 </description>
337         </application>
338         <application name="VoiceMailPlayMsg" language="en_US">
339                 <synopsis>
340                         Play a single voice mail msg from a mailbox by msg id.
341                 </synopsis>
342                 <syntax>
343                         <parameter name="mailbox" required="true" argsep="@">
344                                 <argument name="mailbox" />
345                                 <argument name="context" />
346                         </parameter>
347                         <parameter name="msg_id" required="true">
348                                 <para>The msg id of the msg to play back. </para>
349                         </parameter>
350                 </syntax>
351                 <description>
352                         <para>This application sets the following channel variable upon completion:</para>
353                         <variablelist>
354                                 <variable name="VOICEMAIL_PLAYBACKSTATUS">
355                                         <para>The status of the playback attempt as a text string.</para>
356                                         <value name="SUCCESS"/>
357                                         <value name="FAILED"/>
358                                 </variable>
359                         </variablelist>
360                 </description>
361         </application>
362         <application name="VMSayName" language="en_US">
363                 <synopsis>
364                         Play the name of a voicemail user
365                 </synopsis>
366                 <syntax>
367                         <parameter name="mailbox" required="true" argsep="@">
368                                 <argument name="mailbox" />
369                                 <argument name="context" />
370                         </parameter>
371                 </syntax>
372                 <description>
373                         <para>This application will say the recorded name of the voicemail user specified as the
374                         argument to this application. If no context is provided, <literal>default</literal> is assumed.</para>
375                 </description>
376         </application>
377         <function name="MAILBOX_EXISTS" language="en_US">
378                 <synopsis>
379                         Tell if a mailbox is configured.
380                 </synopsis>
381                 <syntax argsep="@">
382                         <parameter name="mailbox" required="true" />
383                         <parameter name="context" />
384                 </syntax>
385                 <description>
386                         <note><para>DEPRECATED. Use VM_INFO(mailbox[@context],exists) instead.</para></note>
387                         <para>Returns a boolean of whether the corresponding <replaceable>mailbox</replaceable> exists.
388                         If <replaceable>context</replaceable> is not specified, defaults to the <literal>default</literal>
389                         context.</para>
390                 </description>
391                 <see-also>
392                         <ref type="function">VM_INFO</ref>
393                 </see-also>
394         </function>
395         <function name="VM_INFO" language="en_US">
396                 <synopsis>
397                         Returns the selected attribute from a mailbox.
398                 </synopsis>
399                 <syntax argsep=",">
400                         <parameter name="mailbox" argsep="@" required="true">
401                                 <argument name="mailbox" required="true" />
402                                 <argument name="context" />
403                         </parameter>
404                         <parameter name="attribute" required="true">
405                                 <optionlist>
406                                         <option name="count">
407                                                 <para>Count of messages in specified <replaceable>folder</replaceable>.
408                                                 If <replaceable>folder</replaceable> is not specified, defaults to <literal>INBOX</literal>.</para>
409                                         </option>
410                                         <option name="email">
411                                                 <para>E-mail address associated with the mailbox.</para>
412                                         </option>
413                                         <option name="exists">
414                                                 <para>Returns a boolean of whether the corresponding <replaceable>mailbox</replaceable> exists.</para>
415                                         </option>
416                                         <option name="fullname">
417                                                 <para>Full name associated with the mailbox.</para>
418                                         </option>
419                                         <option name="language">
420                                                 <para>Mailbox language if overridden, otherwise the language of the channel.</para>
421                                         </option>
422                                         <option name="locale">
423                                                 <para>Mailbox locale if overridden, otherwise global locale.</para>
424                                         </option>
425                                         <option name="pager">
426                                                 <para>Pager e-mail address associated with the mailbox.</para>
427                                         </option>
428                                         <option name="password">
429                                                 <para>Mailbox access password.</para>
430                                         </option>
431                                         <option name="tz">
432                                                 <para>Mailbox timezone if overridden, otherwise global timezone</para>
433                                         </option>
434                                 </optionlist>
435                         </parameter>
436                         <parameter name="folder" required="false">
437                                 <para>If not specified, <literal>INBOX</literal> is assumed.</para>
438                         </parameter>
439                 </syntax>
440                 <description>
441                         <para>Returns the selected attribute from the specified <replaceable>mailbox</replaceable>.
442                         If <replaceable>context</replaceable> is not specified, defaults to the <literal>default</literal>
443                         context. Where the <replaceable>folder</replaceable> can be specified, common folders
444                         include <literal>INBOX</literal>, <literal>Old</literal>, <literal>Work</literal>,
445                         <literal>Family</literal> and <literal>Friends</literal>.</para>
446                 </description>
447         </function>
448         <manager name="VoicemailUsersList" language="en_US">
449                 <synopsis>
450                         List All Voicemail User Information.
451                 </synopsis>
452                 <syntax>
453                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
454                 </syntax>
455                 <description>
456                 </description>
457         </manager>
458         <manager name="VoicemailRefresh" language="en_US">
459                 <synopsis>
460                         Tell Asterisk to poll mailboxes for a change
461                 </synopsis>
462                 <syntax>
463                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
464                         <parameter name="Context" />
465                         <parameter name="Mailbox" />
466                 </syntax>
467                 <description>
468                         <para>Normally, MWI indicators are only sent when Asterisk itself
469                         changes a mailbox.  With external programs that modify the content
470                         of a mailbox from outside the application, an option exists called
471                         <literal>pollmailboxes</literal> that will cause voicemail to
472                         continually scan all mailboxes on a system for changes.  This can
473                         cause a large amount of load on a system.  This command allows
474                         external applications to signal when a particular mailbox has
475                         changed, thus permitting external applications to modify mailboxes
476                         and MWI to work without introducing considerable CPU load.</para>
477                         <para>If <replaceable>Context</replaceable> is not specified, all
478                         mailboxes on the system will be polled for changes.  If
479                         <replaceable>Context</replaceable> is specified, but
480                         <replaceable>Mailbox</replaceable> is omitted, then all mailboxes
481                         within <replaceable>Context</replaceable> will be polled.
482                         Otherwise, only a single mailbox will be polled for changes.</para>
483                 </description>
484         </manager>
485  ***/
486
487 #ifdef IMAP_STORAGE
488 static char imapserver[48];
489 static char imapport[8];
490 static char imapflags[128];
491 static char imapfolder[64];
492 static char imapparentfolder[64] = "\0";
493 static char greetingfolder[64];
494 static char authuser[32];
495 static char authpassword[42];
496 static int imapversion = 1;
497
498 static int expungeonhangup = 1;
499 static int imapgreetings = 0;
500 static char delimiter = '\0';
501
502 struct vm_state;
503 struct ast_vm_user;
504
505 AST_THREADSTORAGE(ts_vmstate);
506
507 /* Forward declarations for IMAP */
508 static int init_mailstream(struct vm_state *vms, int box);
509 static void write_file(char *filename, char *buffer, unsigned long len);
510 static char *get_header_by_tag(char *header, char *tag, char *buf, size_t len);
511 static void vm_imap_delete(char *file, int msgnum, struct ast_vm_user *vmu);
512 static char *get_user_by_mailbox(char *mailbox, char *buf, size_t len);
513 static struct vm_state *get_vm_state_by_imapuser(const char *user, int interactive);
514 static struct vm_state *get_vm_state_by_mailbox(const char *mailbox, const char *context, int interactive);
515 static struct vm_state *create_vm_state_from_user(struct ast_vm_user *vmu);
516 static void vmstate_insert(struct vm_state *vms);
517 static void vmstate_delete(struct vm_state *vms);
518 static void set_update(MAILSTREAM * stream);
519 static void init_vm_state(struct vm_state *vms);
520 static int save_body(BODY *body, struct vm_state *vms, char *section, char *format, int is_intro);
521 static void get_mailbox_delimiter(struct vm_state *vms, MAILSTREAM *stream);
522 static void mm_parsequota (MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pquota);
523 static void imap_mailbox_name(char *spec, size_t len, struct vm_state *vms, int box, int target);
524 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, const char *msg_id);
525 static void vm_imap_update_msg_id(char *dir, int msgnum, const char *msg_id, struct ast_vm_user *vmu, struct ast_config *msg_cfg, int folder);
526 static void update_messages_by_imapuser(const char *user, unsigned long number);
527 static int vm_delete(char *file);
528
529 static int imap_remove_file (char *dir, int msgnum);
530 static int imap_retrieve_file (const char *dir, const int msgnum, const char *mailbox, const char *context);
531 static int imap_delete_old_greeting (char *dir, struct vm_state *vms);
532 static void check_quota(struct vm_state *vms, char *mailbox);
533 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box);
534 struct vmstate {
535         struct vm_state *vms;
536         AST_LIST_ENTRY(vmstate) list;
537 };
538
539 static AST_LIST_HEAD_STATIC(vmstates, vmstate);
540
541 #endif
542
543 #define SMDI_MWI_WAIT_TIMEOUT 1000 /* 1 second */
544
545 #define COMMAND_TIMEOUT 5000
546 /* Don't modify these here; set your umask at runtime instead */
547 #define VOICEMAIL_DIR_MODE      0777
548 #define VOICEMAIL_FILE_MODE     0666
549 #define CHUNKSIZE       65536
550
551 #define VOICEMAIL_CONFIG "voicemail.conf"
552 #define ASTERISK_USERNAME "asterisk"
553
554 /* Define fast-forward, pause, restart, and reverse keys
555  * while listening to a voicemail message - these are
556  * strings, not characters */
557 #define DEFAULT_LISTEN_CONTROL_FORWARD_KEY "#"
558 #define DEFAULT_LISTEN_CONTROL_REVERSE_KEY "*"
559 #define DEFAULT_LISTEN_CONTROL_PAUSE_KEY "0"
560 #define DEFAULT_LISTEN_CONTROL_RESTART_KEY "2"
561 #define DEFAULT_LISTEN_CONTROL_STOP_KEY "13456789"
562 #define VALID_DTMF "1234567890*#" /* Yes ABCD are valid dtmf but what phones have those? */
563
564 /* Default mail command to mail voicemail. Change it with the
565  * mailcmd= command in voicemail.conf */
566 #define SENDMAIL "/usr/sbin/sendmail -t"
567
568 #define INTRO "vm-intro"
569
570 #define MAXMSG 100
571 #define MAXMSGLIMIT 9999
572
573 #define MINPASSWORD 0 /*!< Default minimum mailbox password length */
574
575 #define BASELINELEN 72
576 #define BASEMAXINLINE 256
577 #ifdef IMAP_STORAGE
578 #define ENDL "\r\n"
579 #else
580 #define ENDL "\n"
581 #endif
582
583 #define MAX_DATETIME_FORMAT     512
584 #define MAX_NUM_CID_CONTEXTS 10
585
586 #define VM_REVIEW        (1 << 0)   /*!< After recording, permit the caller to review the recording before saving */
587 #define VM_OPERATOR      (1 << 1)   /*!< Allow 0 to be pressed to go to 'o' extension */
588 #define VM_SAYCID        (1 << 2)   /*!< Repeat the CallerID info during envelope playback */
589 #define VM_SVMAIL        (1 << 3)   /*!< Allow the user to compose a new VM from within VoicemailMain */
590 #define VM_ENVELOPE      (1 << 4)   /*!< Play the envelope information (who-from, time received, etc.) */
591 #define VM_SAYDURATION   (1 << 5)   /*!< Play the length of the message during envelope playback */
592 #define VM_SKIPAFTERCMD  (1 << 6)   /*!< After deletion, assume caller wants to go to the next message */
593 #define VM_FORCENAME     (1 << 7)   /*!< Have new users record their name */
594 #define VM_FORCEGREET    (1 << 8)   /*!< Have new users record their greetings */
595 #define VM_PBXSKIP       (1 << 9)   /*!< Skip the [PBX] preamble in the Subject line of emails */
596 #define VM_DIRECFORWARD  (1 << 10)  /*!< Permit caller to use the Directory app for selecting to which mailbox to forward a VM */
597 #define VM_ATTACH        (1 << 11)  /*!< Attach message to voicemail notifications? */
598 #define VM_DELETE        (1 << 12)  /*!< Delete message after sending notification */
599 #define VM_ALLOCED       (1 << 13)  /*!< Structure was malloc'ed, instead of placed in a return (usually static) buffer */
600 #define VM_SEARCH        (1 << 14)  /*!< Search all contexts for a matching mailbox */
601 #define VM_TEMPGREETWARN (1 << 15)  /*!< Remind user tempgreeting is set */
602 #define VM_MOVEHEARD     (1 << 16)  /*!< Move a "heard" message to Old after listening to it */
603 #define VM_MESSAGEWRAP   (1 << 17)  /*!< Wrap around from the last message to the first, and vice-versa */
604 #define VM_FWDURGAUTO    (1 << 18)  /*!< Autoset of Urgent flag on forwarded Urgent messages set globally */
605 #define ERROR_LOCK_PATH  -100
606 #define OPERATOR_EXIT     300
607
608 enum vm_box {
609         NEW_FOLDER,
610         OLD_FOLDER,
611         WORK_FOLDER,
612         FAMILY_FOLDER,
613         FRIENDS_FOLDER,
614         GREETINGS_FOLDER
615 };
616
617 enum vm_option_flags {
618         OPT_SILENT =           (1 << 0),
619         OPT_BUSY_GREETING =    (1 << 1),
620         OPT_UNAVAIL_GREETING = (1 << 2),
621         OPT_RECORDGAIN =       (1 << 3),
622         OPT_PREPEND_MAILBOX =  (1 << 4),
623         OPT_AUTOPLAY =         (1 << 6),
624         OPT_DTMFEXIT =         (1 << 7),
625         OPT_MESSAGE_Urgent =   (1 << 8),
626         OPT_MESSAGE_PRIORITY = (1 << 9)
627 };
628
629 enum vm_option_args {
630         OPT_ARG_RECORDGAIN = 0,
631         OPT_ARG_PLAYFOLDER = 1,
632         OPT_ARG_DTMFEXIT   = 2,
633         /* This *must* be the last value in this enum! */
634         OPT_ARG_ARRAY_SIZE = 3,
635 };
636
637 enum vm_passwordlocation {
638         OPT_PWLOC_VOICEMAILCONF = 0,
639         OPT_PWLOC_SPOOLDIR      = 1,
640         OPT_PWLOC_USERSCONF     = 2,
641 };
642
643 AST_APP_OPTIONS(vm_app_options, {
644         AST_APP_OPTION('s', OPT_SILENT),
645         AST_APP_OPTION('b', OPT_BUSY_GREETING),
646         AST_APP_OPTION('u', OPT_UNAVAIL_GREETING),
647         AST_APP_OPTION_ARG('g', OPT_RECORDGAIN, OPT_ARG_RECORDGAIN),
648         AST_APP_OPTION_ARG('d', OPT_DTMFEXIT, OPT_ARG_DTMFEXIT),
649         AST_APP_OPTION('p', OPT_PREPEND_MAILBOX),
650         AST_APP_OPTION_ARG('a', OPT_AUTOPLAY, OPT_ARG_PLAYFOLDER),
651         AST_APP_OPTION('U', OPT_MESSAGE_Urgent),
652         AST_APP_OPTION('P', OPT_MESSAGE_PRIORITY)
653 });
654
655 static const char * const mailbox_folders[] = {
656 #ifdef IMAP_STORAGE
657         imapfolder,
658 #else
659         "INBOX",
660 #endif
661         "Old",
662         "Work",
663         "Family",
664         "Friends",
665         "Cust1",
666         "Cust2",
667         "Cust3",
668         "Cust4",
669         "Cust5",
670         "Deleted",
671         "Urgent",
672 };
673
674 static int load_config(int reload);
675 #ifdef TEST_FRAMEWORK
676 static int load_config_from_memory(int reload, struct ast_config *cfg, struct ast_config *ucfg);
677 #endif
678 static int actual_load_config(int reload, struct ast_config *cfg, struct ast_config *ucfg);
679
680 /*! \page vmlang Voicemail Language Syntaxes Supported
681
682         \par Syntaxes supported, not really language codes.
683         \arg \b en    - English
684         \arg \b de    - German
685         \arg \b es    - Spanish
686         \arg \b fr    - French
687         \arg \b it    - Italian
688         \arg \b nl    - Dutch
689         \arg \b pt    - Portuguese
690         \arg \b pt_BR - Portuguese (Brazil)
691         \arg \b gr    - Greek
692         \arg \b no    - Norwegian
693         \arg \b se    - Swedish
694         \arg \b tw    - Chinese (Taiwan)
695         \arg \b ua - Ukrainian
696
697 German requires the following additional soundfile:
698 \arg \b 1F      einE (feminine)
699
700 Spanish requires the following additional soundfile:
701 \arg \b 1M      un (masculine)
702
703 Dutch, Portuguese & Spanish require the following additional soundfiles:
704 \arg \b vm-INBOXs       singular of 'new'
705 \arg \b vm-Olds         singular of 'old/heard/read'
706
707 NB these are plural:
708 \arg \b vm-INBOX        nieuwe (nl)
709 \arg \b vm-Old          oude (nl)
710
711 Polish uses:
712 \arg \b vm-new-a        'new', feminine singular accusative
713 \arg \b vm-new-e        'new', feminine plural accusative
714 \arg \b vm-new-ych      'new', feminine plural genitive
715 \arg \b vm-old-a        'old', feminine singular accusative
716 \arg \b vm-old-e        'old', feminine plural accusative
717 \arg \b vm-old-ych      'old', feminine plural genitive
718 \arg \b digits/1-a      'one', not always same as 'digits/1'
719 \arg \b digits/2-ie     'two', not always same as 'digits/2'
720
721 Swedish uses:
722 \arg \b vm-nytt         singular of 'new'
723 \arg \b vm-nya          plural of 'new'
724 \arg \b vm-gammalt      singular of 'old'
725 \arg \b vm-gamla        plural of 'old'
726 \arg \b digits/ett      'one', not always same as 'digits/1'
727
728 Norwegian uses:
729 \arg \b vm-ny           singular of 'new'
730 \arg \b vm-nye          plural of 'new'
731 \arg \b vm-gammel       singular of 'old'
732 \arg \b vm-gamle        plural of 'old'
733
734 Dutch also uses:
735 \arg \b nl-om           'at'?
736
737 Spanish also uses:
738 \arg \b vm-youhaveno
739
740 Italian requires the following additional soundfile:
741
742 For vm_intro_it:
743 \arg \b vm-nuovo        new
744 \arg \b vm-nuovi        new plural
745 \arg \b vm-vecchio      old
746 \arg \b vm-vecchi       old plural
747
748 Chinese (Taiwan) requires the following additional soundfile:
749 \arg \b vm-tong         A class-word for call (tong1)
750 \arg \b vm-ri           A class-word for day (ri4)
751 \arg \b vm-you          You (ni3)
752 \arg \b vm-haveno   Have no (mei2 you3)
753 \arg \b vm-have     Have (you3)
754 \arg \b vm-listen   To listen (yao4 ting1)
755
756
757 \note Don't use vm-INBOX or vm-Old, because they are the name of the INBOX and Old folders,
758 spelled among others when you have to change folder. For the above reasons, vm-INBOX
759 and vm-Old are spelled plural, to make them sound more as folder name than an adjective.
760
761 */
762
763 struct baseio {
764         int iocp;
765         int iolen;
766         int linelength;
767         int ateof;
768         unsigned char iobuf[BASEMAXINLINE];
769 };
770
771 /*! Structure for linked list of users 
772  * Use ast_vm_user_destroy() to free one of these structures. */
773 struct ast_vm_user {
774         char context[AST_MAX_CONTEXT];   /*!< Voicemail context */
775         char mailbox[AST_MAX_EXTENSION]; /*!< Mailbox id, unique within vm context */
776         char password[80];               /*!< Secret pin code, numbers only */
777         char fullname[80];               /*!< Full name, for directory app */
778         char email[80];                  /*!< E-mail address */
779         char *emailsubject;              /*!< E-mail subject */
780         char *emailbody;                 /*!< E-mail body */
781         char pager[80];                  /*!< E-mail address to pager (no attachment) */
782         char serveremail[80];            /*!< From: Mail address */
783         char language[MAX_LANGUAGE];     /*!< Config: Language setting */
784         char zonetag[80];                /*!< Time zone */
785         char locale[20];                 /*!< The locale (for presentation of date/time) */
786         char callback[80];
787         char dialout[80];
788         char uniqueid[80];               /*!< Unique integer identifier */
789         char exit[80];
790         char attachfmt[20];              /*!< Attachment format */
791         unsigned int flags;              /*!< VM_ flags */      
792         int saydurationm;
793         int minsecs;                     /*!< Minimum number of seconds per message for this mailbox */
794         int maxmsg;                      /*!< Maximum number of msgs per folder for this mailbox */
795         int maxdeletedmsg;               /*!< Maximum number of deleted msgs saved for this mailbox */
796         int maxsecs;                     /*!< Maximum number of seconds per message for this mailbox */
797         int passwordlocation;            /*!< Storage location of the password */
798 #ifdef IMAP_STORAGE
799         char imapserver[48];             /*!< IMAP server address */
800         char imapport[8];                /*!< IMAP server port */
801         char imapflags[128];             /*!< IMAP optional flags */
802         char imapuser[80];               /*!< IMAP server login */
803         char imappassword[80];           /*!< IMAP server password if authpassword not defined */
804         char imapfolder[64];             /*!< IMAP voicemail folder */
805         char imapvmshareid[80];          /*!< Shared mailbox ID to use rather than the dialed one */
806         int imapversion;                 /*!< If configuration changes, use the new values */
807 #endif
808         double volgain;                  /*!< Volume gain for voicemails sent via email */
809         AST_LIST_ENTRY(ast_vm_user) list;
810 };
811
812 /*! Voicemail time zones */
813 struct vm_zone {
814         AST_LIST_ENTRY(vm_zone) list;
815         char name[80];
816         char timezone[80];
817         char msg_format[512];
818 };
819
820 #define VMSTATE_MAX_MSG_ARRAY 256
821
822 /*! Voicemail mailbox state */
823 struct vm_state {
824         char curbox[80];
825         char username[80];
826         char context[80];
827         char curdir[PATH_MAX];
828         char vmbox[PATH_MAX];
829         char fn[PATH_MAX];
830         char intro[PATH_MAX];
831         int *deleted;
832         int *heard;
833         int dh_arraysize; /* used for deleted / heard allocation */
834         int curmsg;
835         int lastmsg;
836         int newmessages;
837         int oldmessages;
838         int urgentmessages;
839         int starting;
840         int repeats;
841 #ifdef IMAP_STORAGE
842         ast_mutex_t lock;
843         int updated;                         /*!< decremented on each mail check until 1 -allows delay */
844         long msgArray[VMSTATE_MAX_MSG_ARRAY];
845         MAILSTREAM *mailstream;
846         int vmArrayIndex;
847         char imapuser[80];                   /*!< IMAP server login */
848         char imapfolder[64];                 /*!< IMAP voicemail folder */
849         char imapserver[48];                 /*!< IMAP server address */
850         char imapport[8];                    /*!< IMAP server port */
851         char imapflags[128];                 /*!< IMAP optional flags */
852         int imapversion;
853         int interactive;
854         char introfn[PATH_MAX];              /*!< Name of prepended file */
855         unsigned int quota_limit;
856         unsigned int quota_usage;
857         struct vm_state *persist_vms;
858 #endif
859 };
860
861 #ifdef ODBC_STORAGE
862 static char odbc_database[80];
863 static char odbc_table[80];
864 #define RETRIEVE(a,b,c,d) retrieve_file(a,b)
865 #define DISPOSE(a,b) remove_file(a,b)
866 #define STORE(a,b,c,d,e,f,g,h,i,j,k) store_file(a,b,c,d)
867 #define EXISTS(a,b,c,d) (message_exists(a,b))
868 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(a,b,c,d,e,f))
869 #define COPY(a,b,c,d,e,f,g,h) (copy_file(a,b,c,d,e,f))
870 #define DELETE(a,b,c,d) (delete_file(a,b))
871 #define UPDATE_MSG_ID(a, b, c, d, e, f) (odbc_update_msg_id((a), (b), (c)))
872 #else
873 #ifdef IMAP_STORAGE
874 #define DISPOSE(a,b) (imap_remove_file(a,b))
875 #define STORE(a,b,c,d,e,f,g,h,i,j,k) (imap_store_file(a,b,c,d,e,f,g,h,i,j,k))
876 #define RETRIEVE(a,b,c,d) imap_retrieve_file(a,b,c,d)
877 #define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
878 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
879 #define COPY(a,b,c,d,e,f,g,h) (copy_file(g,h));
880 #define DELETE(a,b,c,d) (vm_imap_delete(a,b,d))
881 #define UPDATE_MSG_ID(a, b, c, d, e, f) (vm_imap_update_msg_id((a), (b), (c), (d), (e), (f)))
882 #else
883 #define RETRIEVE(a,b,c,d)
884 #define DISPOSE(a,b)
885 #define STORE(a,b,c,d,e,f,g,h,i,j,k)
886 #define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
887 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
888 #define COPY(a,b,c,d,e,f,g,h) (copy_plain_file(g,h)); 
889 #define DELETE(a,b,c,d) (vm_delete(c))
890 #define UPDATE_MSG_ID(a, b, c, d, e, f)
891 #endif
892 #endif
893
894 static char VM_SPOOL_DIR[PATH_MAX];
895
896 static char ext_pass_cmd[128];
897 static char ext_pass_check_cmd[128];
898
899 static int my_umask;
900
901 #define PWDCHANGE_INTERNAL (1 << 1)
902 #define PWDCHANGE_EXTERNAL (1 << 2)
903 static int pwdchange = PWDCHANGE_INTERNAL;
904
905 #ifdef ODBC_STORAGE
906 #define tdesc "Comedian Mail (Voicemail System) with ODBC Storage"
907 #else
908 # ifdef IMAP_STORAGE
909 # define tdesc "Comedian Mail (Voicemail System) with IMAP Storage"
910 # else
911 # define tdesc "Comedian Mail (Voicemail System)"
912 # endif
913 #endif
914
915 static char userscontext[AST_MAX_EXTENSION] = "default";
916
917 static char *addesc = "Comedian Mail";
918
919 /* Leave a message */
920 static char *app = "VoiceMail";
921
922 /* Check mail, control, etc */
923 static char *app2 = "VoiceMailMain";
924
925 static char *app3 = "MailboxExists";
926 static char *app4 = "VMAuthenticate";
927
928 static char *playmsg_app = "VoiceMailPlayMsg";
929
930 static char *sayname_app = "VMSayName";
931
932 static AST_LIST_HEAD_STATIC(users, ast_vm_user);
933 static AST_LIST_HEAD_STATIC(zones, vm_zone);
934 static char zonetag[80];
935 static char locale[20];
936 static int maxsilence;
937 static int maxmsg;
938 static int maxdeletedmsg;
939 static int silencethreshold = 128;
940 static char serveremail[80];
941 static char mailcmd[160];       /* Configurable mail cmd */
942 static char externnotify[160]; 
943 static struct ast_smdi_interface *smdi_iface = NULL;
944 static char vmfmts[80];
945 static double volgain;
946 static int vmminsecs;
947 static int vmmaxsecs;
948 static int maxgreet;
949 static int skipms;
950 static int maxlogins;
951 static int minpassword;
952 static int passwordlocation;
953
954 /*! Poll mailboxes for changes since there is something external to
955  *  app_voicemail that may change them. */
956 static unsigned int poll_mailboxes;
957
958 /*! Polling frequency */
959 static unsigned int poll_freq;
960 /*! By default, poll every 30 seconds */
961 #define DEFAULT_POLL_FREQ 30
962
963 AST_MUTEX_DEFINE_STATIC(poll_lock);
964 static ast_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
965 static pthread_t poll_thread = AST_PTHREADT_NULL;
966 static unsigned char poll_thread_run;
967
968 /*! Subscription to ... MWI event subscriptions */
969 static struct ast_event_sub *mwi_sub_sub;
970 /*! Subscription to ... MWI event un-subscriptions */
971 static struct ast_event_sub *mwi_unsub_sub;
972
973 /*!
974  * \brief An MWI subscription
975  *
976  * This is so we can keep track of which mailboxes are subscribed to.
977  * This way, we know which mailboxes to poll when the pollmailboxes
978  * option is being used.
979  */
980 struct mwi_sub {
981         AST_RWLIST_ENTRY(mwi_sub) entry;
982         int old_urgent;
983         int old_new;
984         int old_old;
985         uint32_t uniqueid;
986         char mailbox[1];
987 };
988
989 struct mwi_sub_task {
990         const char *mailbox;
991         const char *context;
992         uint32_t uniqueid;
993 };
994
995 static struct ast_taskprocessor *mwi_subscription_tps;
996
997 static AST_RWLIST_HEAD_STATIC(mwi_subs, mwi_sub);
998
999 /* custom audio control prompts for voicemail playback */
1000 static char listen_control_forward_key[12];
1001 static char listen_control_reverse_key[12];
1002 static char listen_control_pause_key[12];
1003 static char listen_control_restart_key[12];
1004 static char listen_control_stop_key[12];
1005
1006 /* custom password sounds */
1007 static char vm_password[80] = "vm-password";
1008 static char vm_newpassword[80] = "vm-newpassword";
1009 static char vm_passchanged[80] = "vm-passchanged";
1010 static char vm_reenterpassword[80] = "vm-reenterpassword";
1011 static char vm_mismatch[80] = "vm-mismatch";
1012 static char vm_invalid_password[80] = "vm-invalid-password";
1013 static char vm_pls_try_again[80] = "vm-pls-try-again";
1014
1015 /*
1016  * XXX If we have the time, motivation, etc. to fix up this prompt, one of the following would be appropriate:
1017  * 1. create a sound along the lines of "Please try again.  When done, press the pound key" which could be spliced
1018  * from existing sound clips.  This would require some programming changes in the area of vm_forward options and also
1019  * app.c's __ast_play_and_record function
1020  * 2. create a sound prompt saying "Please try again.  When done recording, press any key to stop and send the prepended
1021  * message."  At the time of this comment, I think this would require new voice work to be commissioned.
1022  * 3. Something way different like providing instructions before a time out or a post-recording menu.  This would require
1023  * more effort than either of the other two.
1024  */
1025 static char vm_prepend_timeout[80] = "vm-then-pound";
1026
1027 static struct ast_flags globalflags = {0};
1028
1029 static int saydurationminfo;
1030
1031 static char dialcontext[AST_MAX_CONTEXT] = "";
1032 static char callcontext[AST_MAX_CONTEXT] = "";
1033 static char exitcontext[AST_MAX_CONTEXT] = "";
1034
1035 static char cidinternalcontexts[MAX_NUM_CID_CONTEXTS][64];
1036
1037
1038 static char *emailbody = NULL;
1039 static char *emailsubject = NULL;
1040 static char *pagerbody = NULL;
1041 static char *pagersubject = NULL;
1042 static char fromstring[100];
1043 static char pagerfromstring[100];
1044 static char charset[32] = "ISO-8859-1";
1045
1046 static unsigned char adsifdn[4] = "\x00\x00\x00\x0F";
1047 static unsigned char adsisec[4] = "\x9B\xDB\xF7\xAC";
1048 static int adsiver = 1;
1049 static char emaildateformat[32] = "%A, %B %d, %Y at %r";
1050 static char pagerdateformat[32] = "%A, %B %d, %Y at %r";
1051
1052 /* Forward declarations - generic */
1053 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box);
1054 static int close_mailbox(struct vm_state *vms, struct ast_vm_user *vmu);
1055 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);
1056 static int dialout(struct ast_channel *chan, struct ast_vm_user *vmu, char *num, char *outgoing_context);
1057 static int play_record_review(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime,
1058                         char *fmt, int outsidecaller, struct ast_vm_user *vmu, int *duration, int *sound_duration, const char *unlockdir,
1059                         signed char record_gain, struct vm_state *vms, char *flag, const char *msg_id);
1060 static int vm_tempgreeting(struct ast_channel *chan, struct ast_vm_user *vmu, struct vm_state *vms, char *fmtc, signed char record_gain);
1061 static int vm_play_folder_name(struct ast_channel *chan, char *mbox);
1062 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);
1063 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, const char *msg_id);
1064 static void apply_options(struct ast_vm_user *vmu, const char *options);
1065 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);
1066 static int is_valid_dtmf(const char *key);
1067 static void read_password_from_file(const char *secretfn, char *password, int passwordlen);
1068 static int write_password_to_file(const char *secretfn, const char *password);
1069 static const char *substitute_escapes(const char *value);
1070 static int message_range_and_existence_check(struct vm_state *vms, const char *msg_ids [], size_t num_msgs, int *msg_nums, struct ast_vm_user *vmu);
1071 /*!
1072  * Place a message in the indicated folder
1073  *
1074  * \param vmu Voicemail user
1075  * \param vms Current voicemail state for the user
1076  * \param msg The message number to save
1077  * \param box The folder into which the message should be saved
1078  * \param[out] newmsg The new message number of the saved message
1079  * \param move Tells whether to copy or to move the message
1080  *
1081  * \note the "move" parameter is only honored for IMAP voicemail presently
1082  * \retval 0 Success
1083  * \retval other Failure
1084  */
1085 static int save_to_folder(struct ast_vm_user *vmu, struct vm_state *vms, int msg, int box, int *newmsg, int move);
1086
1087 static struct ast_vm_mailbox_snapshot *vm_mailbox_snapshot_create(const char *mailbox, const char *context, const char *folder, int descending, enum ast_vm_snapshot_sort_val sort_val, int combine_INBOX_and_OLD);
1088 static struct ast_vm_mailbox_snapshot *vm_mailbox_snapshot_destroy(struct ast_vm_mailbox_snapshot *mailbox_snapshot);
1089
1090 static int vm_msg_forward(const char *from_mailbox, const char *from_context, const char *from_folder, const char *to_mailbox, const char *to_context, const char *to_folder, size_t num_msgs, const char *msg_ids[], int delete_old);
1091 static int vm_msg_move(const char *mailbox, const char *context, size_t num_msgs, const char *oldfolder, const char *old_msg_ids[], const char *newfolder);
1092 static int vm_msg_remove(const char *mailbox, const char *context, size_t num_msgs, const char *folder, const char *msgs[]);
1093 static int vm_msg_play(struct ast_channel *chan, const char *mailbox, const char *context, const char *folder, const char *msg_num, ast_vm_msg_play_cb cb);
1094
1095 #ifdef TEST_FRAMEWORK
1096 static int vm_test_destroy_user(const char *context, const char *mailbox);
1097 static int vm_test_create_user(const char *context, const char *mailbox);
1098 #endif
1099
1100 struct ao2_container *inprocess_container;
1101
1102 struct inprocess {
1103         int count;
1104         char *context;
1105         char mailbox[0];
1106 };
1107
1108 static int inprocess_hash_fn(const void *obj, const int flags)
1109 {
1110         const struct inprocess *i = obj;
1111         return atoi(i->mailbox);
1112 }
1113
1114 static int inprocess_cmp_fn(void *obj, void *arg, int flags)
1115 {
1116         struct inprocess *i = obj, *j = arg;
1117         if (strcmp(i->mailbox, j->mailbox)) {
1118                 return 0;
1119         }
1120         return !strcmp(i->context, j->context) ? CMP_MATCH : 0;
1121 }
1122
1123 static int inprocess_count(const char *context, const char *mailbox, int delta)
1124 {
1125         struct inprocess *i, *arg = ast_alloca(sizeof(*arg) + strlen(context) + strlen(mailbox) + 2);
1126         arg->context = arg->mailbox + strlen(mailbox) + 1;
1127         strcpy(arg->mailbox, mailbox); /* SAFE */
1128         strcpy(arg->context, context); /* SAFE */
1129         ao2_lock(inprocess_container);
1130         if ((i = ao2_find(inprocess_container, arg, 0))) {
1131                 int ret = ast_atomic_fetchadd_int(&i->count, delta);
1132                 ao2_unlock(inprocess_container);
1133                 ao2_ref(i, -1);
1134                 return ret;
1135         }
1136         if (delta < 0) {
1137                 ast_log(LOG_WARNING, "BUG: ref count decrement on non-existing object???\n");
1138         }
1139         if (!(i = ao2_alloc(sizeof(*i) + strlen(context) + strlen(mailbox) + 2, NULL))) {
1140                 ao2_unlock(inprocess_container);
1141                 return 0;
1142         }
1143         i->context = i->mailbox + strlen(mailbox) + 1;
1144         strcpy(i->mailbox, mailbox); /* SAFE */
1145         strcpy(i->context, context); /* SAFE */
1146         i->count = delta;
1147         ao2_link(inprocess_container, i);
1148         ao2_unlock(inprocess_container);
1149         ao2_ref(i, -1);
1150         return 0;
1151 }
1152
1153 #if !(defined(ODBC_STORAGE) || defined(IMAP_STORAGE))
1154 static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit);
1155 #endif
1156
1157 /*!
1158  * \brief Strips control and non 7-bit clean characters from input string.
1159  *
1160  * \note To map control and none 7-bit characters to a 7-bit clean characters
1161  *  please use ast_str_encode_mine().
1162  */
1163 static char *strip_control_and_high(const char *input, char *buf, size_t buflen)
1164 {
1165         char *bufptr = buf;
1166         for (; *input; input++) {
1167                 if (*input < 32) {
1168                         continue;
1169                 }
1170                 *bufptr++ = *input;
1171                 if (bufptr == buf + buflen - 1) {
1172                         break;
1173                 }
1174         }
1175         *bufptr = '\0';
1176         return buf;
1177 }
1178
1179
1180 /*!
1181  * \brief Sets default voicemail system options to a voicemail user.
1182  *
1183  * This applies select global settings to a newly created (dynamic) instance of a voicemail user.
1184  * - all the globalflags
1185  * - the saydurationminfo
1186  * - the callcontext
1187  * - the dialcontext
1188  * - the exitcontext
1189  * - vmmaxsecs, vmmaxmsg, maxdeletedmsg
1190  * - volume gain.
1191  * - emailsubject, emailbody set to NULL
1192  */
1193 static void populate_defaults(struct ast_vm_user *vmu)
1194 {
1195         ast_copy_flags(vmu, (&globalflags), AST_FLAGS_ALL);
1196         vmu->passwordlocation = passwordlocation;
1197         if (saydurationminfo) {
1198                 vmu->saydurationm = saydurationminfo;
1199         }
1200         ast_copy_string(vmu->callback, callcontext, sizeof(vmu->callback));
1201         ast_copy_string(vmu->dialout, dialcontext, sizeof(vmu->dialout));
1202         ast_copy_string(vmu->exit, exitcontext, sizeof(vmu->exit));
1203         ast_copy_string(vmu->zonetag, zonetag, sizeof(vmu->zonetag));
1204         ast_copy_string(vmu->locale, locale, sizeof(vmu->locale));
1205         if (vmminsecs) {
1206                 vmu->minsecs = vmminsecs;
1207         }
1208         if (vmmaxsecs) {
1209                 vmu->maxsecs = vmmaxsecs;
1210         }
1211         if (maxmsg) {
1212                 vmu->maxmsg = maxmsg;
1213         }
1214         if (maxdeletedmsg) {
1215                 vmu->maxdeletedmsg = maxdeletedmsg;
1216         }
1217         vmu->volgain = volgain;
1218         ast_free(vmu->emailsubject);
1219         vmu->emailsubject = NULL;
1220         ast_free(vmu->emailbody);
1221         vmu->emailbody = NULL;
1222 #ifdef IMAP_STORAGE
1223         ast_copy_string(vmu->imapfolder, imapfolder, sizeof(vmu->imapfolder));
1224         ast_copy_string(vmu->imapserver, imapserver, sizeof(vmu->imapserver));
1225         ast_copy_string(vmu->imapport, imapport, sizeof(vmu->imapport));
1226         ast_copy_string(vmu->imapflags, imapflags, sizeof(vmu->imapflags));
1227 #endif
1228 }
1229
1230 /*!
1231  * \brief Sets a a specific property value.
1232  * \param vmu The voicemail user object to work with.
1233  * \param var The name of the property to be set.
1234  * \param value The value to be set to the property.
1235  * 
1236  * The property name must be one of the understood properties. See the source for details.
1237  */
1238 static void apply_option(struct ast_vm_user *vmu, const char *var, const char *value)
1239 {
1240         int x;
1241         if (!strcasecmp(var, "attach")) {
1242                 ast_set2_flag(vmu, ast_true(value), VM_ATTACH);
1243         } else if (!strcasecmp(var, "attachfmt")) {
1244                 ast_copy_string(vmu->attachfmt, value, sizeof(vmu->attachfmt));
1245         } else if (!strcasecmp(var, "serveremail")) {
1246                 ast_copy_string(vmu->serveremail, value, sizeof(vmu->serveremail));
1247         } else if (!strcasecmp(var, "emailbody")) {
1248                 ast_free(vmu->emailbody);
1249                 vmu->emailbody = ast_strdup(substitute_escapes(value));
1250         } else if (!strcasecmp(var, "emailsubject")) {
1251                 ast_free(vmu->emailsubject);
1252                 vmu->emailsubject = ast_strdup(substitute_escapes(value));
1253         } else if (!strcasecmp(var, "language")) {
1254                 ast_copy_string(vmu->language, value, sizeof(vmu->language));
1255         } else if (!strcasecmp(var, "tz")) {
1256                 ast_copy_string(vmu->zonetag, value, sizeof(vmu->zonetag));
1257         } else if (!strcasecmp(var, "locale")) {
1258                 ast_copy_string(vmu->locale, value, sizeof(vmu->locale));
1259 #ifdef IMAP_STORAGE
1260         } else if (!strcasecmp(var, "imapuser")) {
1261                 ast_copy_string(vmu->imapuser, value, sizeof(vmu->imapuser));
1262                 vmu->imapversion = imapversion;
1263         } else if (!strcasecmp(var, "imapserver")) {
1264                 ast_copy_string(vmu->imapserver, value, sizeof(vmu->imapserver));
1265                 vmu->imapversion = imapversion;
1266         } else if (!strcasecmp(var, "imapport")) {
1267                 ast_copy_string(vmu->imapport, value, sizeof(vmu->imapport));
1268                 vmu->imapversion = imapversion;
1269         } else if (!strcasecmp(var, "imapflags")) {
1270                 ast_copy_string(vmu->imapflags, value, sizeof(vmu->imapflags));
1271                 vmu->imapversion = imapversion;
1272         } else if (!strcasecmp(var, "imappassword") || !strcasecmp(var, "imapsecret")) {
1273                 ast_copy_string(vmu->imappassword, value, sizeof(vmu->imappassword));
1274                 vmu->imapversion = imapversion;
1275         } else if (!strcasecmp(var, "imapfolder")) {
1276                 ast_copy_string(vmu->imapfolder, value, sizeof(vmu->imapfolder));
1277                 vmu->imapversion = imapversion;
1278         } else if (!strcasecmp(var, "imapvmshareid")) {
1279                 ast_copy_string(vmu->imapvmshareid, value, sizeof(vmu->imapvmshareid));
1280                 vmu->imapversion = imapversion;
1281 #endif
1282         } else if (!strcasecmp(var, "delete") || !strcasecmp(var, "deletevoicemail")) {
1283                 ast_set2_flag(vmu, ast_true(value), VM_DELETE); 
1284         } else if (!strcasecmp(var, "saycid")){
1285                 ast_set2_flag(vmu, ast_true(value), VM_SAYCID); 
1286         } else if (!strcasecmp(var, "sendvoicemail")){
1287                 ast_set2_flag(vmu, ast_true(value), VM_SVMAIL); 
1288         } else if (!strcasecmp(var, "review")){
1289                 ast_set2_flag(vmu, ast_true(value), VM_REVIEW);
1290         } else if (!strcasecmp(var, "tempgreetwarn")){
1291                 ast_set2_flag(vmu, ast_true(value), VM_TEMPGREETWARN);  
1292         } else if (!strcasecmp(var, "messagewrap")){
1293                 ast_set2_flag(vmu, ast_true(value), VM_MESSAGEWRAP);    
1294         } else if (!strcasecmp(var, "operator")) {
1295                 ast_set2_flag(vmu, ast_true(value), VM_OPERATOR);       
1296         } else if (!strcasecmp(var, "envelope")){
1297                 ast_set2_flag(vmu, ast_true(value), VM_ENVELOPE);       
1298         } else if (!strcasecmp(var, "moveheard")){
1299                 ast_set2_flag(vmu, ast_true(value), VM_MOVEHEARD);
1300         } else if (!strcasecmp(var, "sayduration")){
1301                 ast_set2_flag(vmu, ast_true(value), VM_SAYDURATION);    
1302         } else if (!strcasecmp(var, "saydurationm")){
1303                 if (sscanf(value, "%30d", &x) == 1) {
1304                         vmu->saydurationm = x;
1305                 } else {
1306                         ast_log(AST_LOG_WARNING, "Invalid min duration for say duration\n");
1307                 }
1308         } else if (!strcasecmp(var, "forcename")){
1309                 ast_set2_flag(vmu, ast_true(value), VM_FORCENAME);      
1310         } else if (!strcasecmp(var, "forcegreetings")){
1311                 ast_set2_flag(vmu, ast_true(value), VM_FORCEGREET);     
1312         } else if (!strcasecmp(var, "callback")) {
1313                 ast_copy_string(vmu->callback, value, sizeof(vmu->callback));
1314         } else if (!strcasecmp(var, "dialout")) {
1315                 ast_copy_string(vmu->dialout, value, sizeof(vmu->dialout));
1316         } else if (!strcasecmp(var, "exitcontext")) {
1317                 ast_copy_string(vmu->exit, value, sizeof(vmu->exit));
1318         } else if (!strcasecmp(var, "minsecs")) {
1319                 if (sscanf(value, "%30d", &x) == 1 && x >= 0) {
1320                         vmu->minsecs = x;
1321                 } else {
1322                         ast_log(LOG_WARNING, "Invalid min message length of %s. Using global value %d\n", value, vmminsecs);
1323                         vmu->minsecs = vmminsecs;
1324                 }
1325         } else if (!strcasecmp(var, "maxmessage") || !strcasecmp(var, "maxsecs")) {
1326                 vmu->maxsecs = atoi(value);
1327                 if (vmu->maxsecs <= 0) {
1328                         ast_log(AST_LOG_WARNING, "Invalid max message length of %s. Using global value %d\n", value, vmmaxsecs);
1329                         vmu->maxsecs = vmmaxsecs;
1330                 } else {
1331                         vmu->maxsecs = atoi(value);
1332                 }
1333                 if (!strcasecmp(var, "maxmessage"))
1334                         ast_log(AST_LOG_WARNING, "Option 'maxmessage' has been deprecated in favor of 'maxsecs'.  Please make that change in your voicemail config.\n");
1335         } else if (!strcasecmp(var, "maxmsg")) {
1336                 vmu->maxmsg = atoi(value);
1337                 /* Accept maxmsg=0 (Greetings only voicemail) */
1338                 if (vmu->maxmsg < 0) {
1339                         ast_log(AST_LOG_WARNING, "Invalid number of messages per folder maxmsg=%s. Using default value %d\n", value, MAXMSG);
1340                         vmu->maxmsg = MAXMSG;
1341                 } else if (vmu->maxmsg > MAXMSGLIMIT) {
1342                         ast_log(AST_LOG_WARNING, "Maximum number of messages per folder is %d. Cannot accept value maxmsg=%s\n", MAXMSGLIMIT, value);
1343                         vmu->maxmsg = MAXMSGLIMIT;
1344                 }
1345         } else if (!strcasecmp(var, "nextaftercmd")) {
1346                 ast_set2_flag(vmu, ast_true(value), VM_SKIPAFTERCMD);
1347         } else if (!strcasecmp(var, "backupdeleted")) {
1348                 if (sscanf(value, "%30d", &x) == 1)
1349                         vmu->maxdeletedmsg = x;
1350                 else if (ast_true(value))
1351                         vmu->maxdeletedmsg = MAXMSG;
1352                 else
1353                         vmu->maxdeletedmsg = 0;
1354
1355                 if (vmu->maxdeletedmsg < 0) {
1356                         ast_log(AST_LOG_WARNING, "Invalid number of deleted messages saved per mailbox backupdeleted=%s. Using default value %d\n", value, MAXMSG);
1357                         vmu->maxdeletedmsg = MAXMSG;
1358                 } else if (vmu->maxdeletedmsg > MAXMSGLIMIT) {
1359                         ast_log(AST_LOG_WARNING, "Maximum number of deleted messages saved per mailbox is %d. Cannot accept value backupdeleted=%s\n", MAXMSGLIMIT, value);
1360                         vmu->maxdeletedmsg = MAXMSGLIMIT;
1361                 }
1362         } else if (!strcasecmp(var, "volgain")) {
1363                 sscanf(value, "%30lf", &vmu->volgain);
1364         } else if (!strcasecmp(var, "passwordlocation")) {
1365                 if (!strcasecmp(value, "spooldir")) {
1366                         vmu->passwordlocation = OPT_PWLOC_SPOOLDIR;
1367                 } else {
1368                         vmu->passwordlocation = OPT_PWLOC_VOICEMAILCONF;
1369                 }
1370         } else if (!strcasecmp(var, "options")) {
1371                 apply_options(vmu, value);
1372         }
1373 }
1374
1375 static char *vm_check_password_shell(char *command, char *buf, size_t len) 
1376 {
1377         int fds[2], pid = 0;
1378
1379         memset(buf, 0, len);
1380
1381         if (pipe(fds)) {
1382                 snprintf(buf, len, "FAILURE: Pipe failed: %s", strerror(errno));
1383         } else {
1384                 /* good to go*/
1385                 pid = ast_safe_fork(0);
1386
1387                 if (pid < 0) {
1388                         /* ok maybe not */
1389                         close(fds[0]);
1390                         close(fds[1]);
1391                         snprintf(buf, len, "FAILURE: Fork failed");
1392                 } else if (pid) {
1393                         /* parent */
1394                         close(fds[1]);
1395                         if (read(fds[0], buf, len) < 0) {
1396                                 ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
1397                         }
1398                         close(fds[0]);
1399                 } else {
1400                         /*  child */
1401                         AST_DECLARE_APP_ARGS(arg,
1402                                 AST_APP_ARG(v)[20];
1403                         );
1404                         char *mycmd = ast_strdupa(command);
1405
1406                         close(fds[0]);
1407                         dup2(fds[1], STDOUT_FILENO);
1408                         close(fds[1]);
1409                         ast_close_fds_above_n(STDOUT_FILENO);
1410
1411                         AST_NONSTANDARD_APP_ARGS(arg, mycmd, ' ');
1412
1413                         execv(arg.v[0], arg.v); 
1414                         printf("FAILURE: %s", strerror(errno));
1415                         _exit(0);
1416                 }
1417         }
1418         return buf;
1419 }
1420
1421 /*!
1422  * \brief Check that password meets minimum required length
1423  * \param vmu The voicemail user to change the password for.
1424  * \param password The password string to check
1425  *
1426  * \return zero on ok, 1 on not ok.
1427  */
1428 static int check_password(struct ast_vm_user *vmu, char *password)
1429 {
1430         /* check minimum length */
1431         if (strlen(password) < minpassword)
1432                 return 1;
1433         /* check that password does not contain '*' character */
1434         if (!ast_strlen_zero(password) && password[0] == '*')
1435                 return 1;
1436         if (!ast_strlen_zero(ext_pass_check_cmd)) {
1437                 char cmd[255], buf[255];
1438
1439                 ast_debug(1, "Verify password policies for %s\n", password);
1440
1441                 snprintf(cmd, sizeof(cmd), "%s %s %s %s %s", ext_pass_check_cmd, vmu->mailbox, vmu->context, vmu->password, password);
1442                 if (vm_check_password_shell(cmd, buf, sizeof(buf))) {
1443                         ast_debug(5, "Result: %s\n", buf);
1444                         if (!strncasecmp(buf, "VALID", 5)) {
1445                                 ast_debug(3, "Passed password check: '%s'\n", buf);
1446                                 return 0;
1447                         } else if (!strncasecmp(buf, "FAILURE", 7)) {
1448                                 ast_log(AST_LOG_WARNING, "Unable to execute password validation script: '%s'.\n", buf);
1449                                 return 0;
1450                         } else {
1451                                 ast_log(AST_LOG_NOTICE, "Password doesn't match policies for user %s %s\n", vmu->mailbox, password);
1452                                 return 1;
1453                         }
1454                 }
1455         }
1456         return 0;
1457 }
1458
1459 /*! 
1460  * \brief Performs a change of the voicemail passowrd in the realtime engine.
1461  * \param vmu The voicemail user to change the password for.
1462  * \param password The new value to be set to the password for this user.
1463  * 
1464  * This only works if there is a realtime engine configured.
1465  * This is called from the (top level) vm_change_password.
1466  *
1467  * \return zero on success, -1 on error.
1468  */
1469 static int change_password_realtime(struct ast_vm_user *vmu, const char *password)
1470 {
1471         int res = -1;
1472         if (!strcmp(vmu->password, password)) {
1473                 /* No change (but an update would return 0 rows updated, so we opt out here) */
1474                 return 0;
1475         }
1476
1477         if (strlen(password) > 10) {
1478                 ast_realtime_require_field("voicemail", "password", RQ_CHAR, strlen(password), SENTINEL);
1479         }
1480         if (ast_update2_realtime("voicemail", "context", vmu->context, "mailbox", vmu->mailbox, SENTINEL, "password", password, SENTINEL) > 0) {
1481                 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: realtime engine updated with new password\r\nPasswordSource: realtime");
1482                 ast_copy_string(vmu->password, password, sizeof(vmu->password));
1483                 res = 0;
1484         }
1485         return res;
1486 }
1487
1488 /*!
1489  * \brief Destructively Parse options and apply.
1490  */
1491 static void apply_options(struct ast_vm_user *vmu, const char *options)
1492 {       
1493         char *stringp;
1494         char *s;
1495         char *var, *value;
1496         stringp = ast_strdupa(options);
1497         while ((s = strsep(&stringp, "|"))) {
1498                 value = s;
1499                 if ((var = strsep(&value, "=")) && value) {
1500                         apply_option(vmu, var, value);
1501                 }
1502         }       
1503 }
1504
1505 /*!
1506  * \brief Loads the options specific to a voicemail user.
1507  * 
1508  * This is called when a vm_user structure is being set up, such as from load_options.
1509  */
1510 static void apply_options_full(struct ast_vm_user *retval, struct ast_variable *var)
1511 {
1512         for (; var; var = var->next) {
1513                 if (!strcasecmp(var->name, "vmsecret")) {
1514                         ast_copy_string(retval->password, var->value, sizeof(retval->password));
1515                 } else if (!strcasecmp(var->name, "secret") || !strcasecmp(var->name, "password")) { /* don't overwrite vmsecret if it exists */
1516                         if (ast_strlen_zero(retval->password)) {
1517                                 if (!ast_strlen_zero(var->value) && var->value[0] == '*') {
1518                                         ast_log(LOG_WARNING, "Invalid password detected for mailbox %s.  The password"
1519                                                 "\n\tmust be reset in voicemail.conf.\n", retval->mailbox);
1520                                 } else {
1521                                         ast_copy_string(retval->password, var->value, sizeof(retval->password));
1522                                 }
1523                         }
1524                 } else if (!strcasecmp(var->name, "uniqueid")) {
1525                         ast_copy_string(retval->uniqueid, var->value, sizeof(retval->uniqueid));
1526                 } else if (!strcasecmp(var->name, "pager")) {
1527                         ast_copy_string(retval->pager, var->value, sizeof(retval->pager));
1528                 } else if (!strcasecmp(var->name, "email")) {
1529                         ast_copy_string(retval->email, var->value, sizeof(retval->email));
1530                 } else if (!strcasecmp(var->name, "fullname")) {
1531                         ast_copy_string(retval->fullname, var->value, sizeof(retval->fullname));
1532                 } else if (!strcasecmp(var->name, "context")) {
1533                         ast_copy_string(retval->context, var->value, sizeof(retval->context));
1534                 } else if (!strcasecmp(var->name, "emailsubject")) {
1535                         ast_free(retval->emailsubject);
1536                         retval->emailsubject = ast_strdup(substitute_escapes(var->value));
1537                 } else if (!strcasecmp(var->name, "emailbody")) {
1538                         ast_free(retval->emailbody);
1539                         retval->emailbody = ast_strdup(substitute_escapes(var->value));
1540 #ifdef IMAP_STORAGE
1541                 } else if (!strcasecmp(var->name, "imapuser")) {
1542                         ast_copy_string(retval->imapuser, var->value, sizeof(retval->imapuser));
1543                         retval->imapversion = imapversion;
1544                 } else if (!strcasecmp(var->name, "imapserver")) {
1545                         ast_copy_string(retval->imapserver, var->value, sizeof(retval->imapserver));
1546                         retval->imapversion = imapversion;
1547                 } else if (!strcasecmp(var->name, "imapport")) {
1548                         ast_copy_string(retval->imapport, var->value, sizeof(retval->imapport));
1549                         retval->imapversion = imapversion;
1550                 } else if (!strcasecmp(var->name, "imapflags")) {
1551                         ast_copy_string(retval->imapflags, var->value, sizeof(retval->imapflags));
1552                         retval->imapversion = imapversion;
1553                 } else if (!strcasecmp(var->name, "imappassword") || !strcasecmp(var->name, "imapsecret")) {
1554                         ast_copy_string(retval->imappassword, var->value, sizeof(retval->imappassword));
1555                         retval->imapversion = imapversion;
1556                 } else if (!strcasecmp(var->name, "imapfolder")) {
1557                         ast_copy_string(retval->imapfolder, var->value, sizeof(retval->imapfolder));
1558                         retval->imapversion = imapversion;
1559                 } else if (!strcasecmp(var->name, "imapvmshareid")) {
1560                         ast_copy_string(retval->imapvmshareid, var->value, sizeof(retval->imapvmshareid));
1561                         retval->imapversion = imapversion;
1562 #endif
1563                 } else
1564                         apply_option(retval, var->name, var->value);
1565         }
1566 }
1567
1568 /*!
1569  * \brief Determines if a DTMF key entered is valid.
1570  * \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.
1571  *
1572  * Tests the character entered against the set of valid DTMF characters. 
1573  * \return 1 if the character entered is a valid DTMF digit, 0 if the character is invalid.
1574  */
1575 static int is_valid_dtmf(const char *key)
1576 {
1577         int i;
1578         char *local_key = ast_strdupa(key);
1579
1580         for (i = 0; i < strlen(key); ++i) {
1581                 if (!strchr(VALID_DTMF, *local_key)) {
1582                         ast_log(AST_LOG_WARNING, "Invalid DTMF key \"%c\" used in voicemail configuration file\n", *local_key);
1583                         return 0;
1584                 }
1585                 local_key++;
1586         }
1587         return 1;
1588 }
1589
1590 /*!
1591  * \brief Finds a voicemail user from the realtime engine.
1592  * \param ivm
1593  * \param context
1594  * \param mailbox
1595  *
1596  * 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.
1597  *
1598  * \return The ast_vm_user structure for the user that was found.
1599  */
1600 static struct ast_vm_user *find_user_realtime(struct ast_vm_user *ivm, const char *context, const char *mailbox)
1601 {
1602         struct ast_variable *var;
1603         struct ast_vm_user *retval;
1604
1605         if ((retval = (ivm ? ivm : ast_calloc(1, sizeof(*retval))))) {
1606                 if (ivm) {
1607                         memset(retval, 0, sizeof(*retval));
1608                 }
1609                 populate_defaults(retval);
1610                 if (!ivm) {
1611                         ast_set_flag(retval, VM_ALLOCED);
1612                 }
1613                 if (mailbox) {
1614                         ast_copy_string(retval->mailbox, mailbox, sizeof(retval->mailbox));
1615                 }
1616                 if (!context && ast_test_flag((&globalflags), VM_SEARCH)) {
1617                         var = ast_load_realtime("voicemail", "mailbox", mailbox, SENTINEL);
1618                 } else {
1619                         var = ast_load_realtime("voicemail", "mailbox", mailbox, "context", context, SENTINEL);
1620                 }
1621                 if (var) {
1622                         apply_options_full(retval, var);
1623                         ast_variables_destroy(var);
1624                 } else { 
1625                         if (!ivm) 
1626                                 ast_free(retval);
1627                         retval = NULL;
1628                 }       
1629         } 
1630         return retval;
1631 }
1632
1633 /*!
1634  * \brief Finds a voicemail user from the users file or the realtime engine.
1635  * \param ivm
1636  * \param context
1637  * \param mailbox
1638  * 
1639  * \return The ast_vm_user structure for the user that was found.
1640  */
1641 static struct ast_vm_user *find_user(struct ast_vm_user *ivm, const char *context, const char *mailbox)
1642 {
1643         /* This function could be made to generate one from a database, too */
1644         struct ast_vm_user *vmu = NULL, *cur;
1645         AST_LIST_LOCK(&users);
1646
1647         if (!context && !ast_test_flag((&globalflags), VM_SEARCH))
1648                 context = "default";
1649
1650         AST_LIST_TRAVERSE(&users, cur, list) {
1651 #ifdef IMAP_STORAGE
1652                 if (cur->imapversion != imapversion) {
1653                         continue;
1654                 }
1655 #endif
1656                 if (ast_test_flag((&globalflags), VM_SEARCH) && !strcasecmp(mailbox, cur->mailbox))
1657                         break;
1658                 if (context && (!strcasecmp(context, cur->context)) && (!strcasecmp(mailbox, cur->mailbox)))
1659                         break;
1660         }
1661         if (cur) {
1662                 /* Make a copy, so that on a reload, we have no race */
1663                 if ((vmu = (ivm ? ivm : ast_malloc(sizeof(*vmu))))) {
1664                         *vmu = *cur;
1665                         if (!ivm) {
1666                                 vmu->emailbody = ast_strdup(cur->emailbody);
1667                                 vmu->emailsubject = ast_strdup(cur->emailsubject);
1668                         }
1669                         ast_set2_flag(vmu, !ivm, VM_ALLOCED);
1670                         AST_LIST_NEXT(vmu, list) = NULL;
1671                 }
1672         } else
1673                 vmu = find_user_realtime(ivm, context, mailbox);
1674         AST_LIST_UNLOCK(&users);
1675         return vmu;
1676 }
1677
1678 /*!
1679  * \brief Resets a user password to a specified password.
1680  * \param context
1681  * \param mailbox
1682  * \param newpass
1683  *
1684  * This does the actual change password work, called by the vm_change_password() function.
1685  *
1686  * \return zero on success, -1 on error.
1687  */
1688 static int reset_user_pw(const char *context, const char *mailbox, const char *newpass)
1689 {
1690         /* This function could be made to generate one from a database, too */
1691         struct ast_vm_user *cur;
1692         int res = -1;
1693         AST_LIST_LOCK(&users);
1694         AST_LIST_TRAVERSE(&users, cur, list) {
1695                 if ((!context || !strcasecmp(context, cur->context)) &&
1696                         (!strcasecmp(mailbox, cur->mailbox)))
1697                                 break;
1698         }
1699         if (cur) {
1700                 ast_copy_string(cur->password, newpass, sizeof(cur->password));
1701                 res = 0;
1702         }
1703         AST_LIST_UNLOCK(&users);
1704         return res;
1705 }
1706
1707 /*! 
1708  * \brief The handler for the change password option.
1709  * \param vmu The voicemail user to work with.
1710  * \param newpassword The new password (that has been gathered from the appropriate prompting).
1711  * 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.
1712  * It is also called when the user wants to change their password from menu option '5' on the mailbox options menu.
1713  */
1714 static void vm_change_password(struct ast_vm_user *vmu, const char *newpassword)
1715 {
1716         struct ast_config   *cfg = NULL;
1717         struct ast_variable *var = NULL;
1718         struct ast_category *cat = NULL;
1719         char *category = NULL, *value = NULL, *new = NULL;
1720         const char *tmp = NULL;
1721         struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS };
1722         char secretfn[PATH_MAX] = "";
1723         int found = 0;
1724
1725         if (!change_password_realtime(vmu, newpassword))
1726                 return;
1727
1728         /* check if we should store the secret in the spool directory next to the messages */
1729         switch (vmu->passwordlocation) {
1730         case OPT_PWLOC_SPOOLDIR:
1731                 snprintf(secretfn, sizeof(secretfn), "%s%s/%s/secret.conf", VM_SPOOL_DIR, vmu->context, vmu->mailbox);
1732                 if (write_password_to_file(secretfn, newpassword) == 0) {
1733                         ast_test_suite_event_notify("PASSWORDCHANGED", "Message: secret.conf updated with new password\r\nPasswordSource: secret.conf");
1734                         ast_verb(4, "Writing voicemail password to file %s succeeded\n", secretfn);
1735                         reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1736                         ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1737                         break;
1738                 } else {
1739                         ast_verb(4, "Writing voicemail password to file %s failed, falling back to config file\n", secretfn);
1740                 }
1741                 /* Fall-through */
1742         case OPT_PWLOC_VOICEMAILCONF:
1743                 if ((cfg = ast_config_load(VOICEMAIL_CONFIG, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
1744                         while ((category = ast_category_browse(cfg, category))) {
1745                                 if (!strcasecmp(category, vmu->context)) {
1746                                         if (!(tmp = ast_variable_retrieve(cfg, category, vmu->mailbox))) {
1747                                                 ast_log(AST_LOG_WARNING, "We could not find the mailbox.\n");
1748                                                 break;
1749                                         }
1750                                         value = strstr(tmp, ",");
1751                                         if (!value) {
1752                                                 new = ast_alloca(strlen(newpassword)+1);
1753                                                 sprintf(new, "%s", newpassword);
1754                                         } else {
1755                                                 new = ast_alloca((strlen(value) + strlen(newpassword) + 1));
1756                                                 sprintf(new, "%s%s", newpassword, value);
1757                                         }
1758                                         if (!(cat = ast_category_get(cfg, category))) {
1759                                                 ast_log(AST_LOG_WARNING, "Failed to get category structure.\n");
1760                                                 break;
1761                                         }
1762                                         ast_variable_update(cat, vmu->mailbox, new, NULL, 0);
1763                                         found = 1;
1764                                 }
1765                         }
1766                         /* save the results */
1767                         if (found) {
1768                                 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: voicemail.conf updated with new password\r\nPasswordSource: voicemail.conf");
1769                                 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1770                                 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1771                                 ast_config_text_file_save(VOICEMAIL_CONFIG, cfg, "AppVoicemail");
1772                                 break;
1773                         }
1774                 }
1775                 /* Fall-through */
1776         case OPT_PWLOC_USERSCONF:
1777                 /* check users.conf and update the password stored for the mailbox */
1778                 /* if no vmsecret entry exists create one. */
1779                 if ((cfg = ast_config_load("users.conf", config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
1780                         ast_debug(4, "we are looking for %s\n", vmu->mailbox);
1781                         for (category = ast_category_browse(cfg, NULL); category; category = ast_category_browse(cfg, category)) {
1782                                 ast_debug(4, "users.conf: %s\n", category);
1783                                 if (!strcasecmp(category, vmu->mailbox)) {
1784                                         if (!ast_variable_retrieve(cfg, category, "vmsecret")) {
1785                                                 ast_debug(3, "looks like we need to make vmsecret!\n");
1786                                                 var = ast_variable_new("vmsecret", newpassword, "");
1787                                         } else {
1788                                                 var = NULL;
1789                                         }
1790                                         new = ast_alloca(strlen(newpassword) + 1);
1791                                         sprintf(new, "%s", newpassword);
1792                                         if (!(cat = ast_category_get(cfg, category))) {
1793                                                 ast_debug(4, "failed to get category!\n");
1794                                                 ast_free(var);
1795                                                 break;
1796                                         }
1797                                         if (!var) {
1798                                                 ast_variable_update(cat, "vmsecret", new, NULL, 0);
1799                                         } else {
1800                                                 ast_variable_append(cat, var);
1801                                         }
1802                                         found = 1;
1803                                         break;
1804                                 }
1805                         }
1806                         /* save the results and clean things up */
1807                         if (found) {
1808                                 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: users.conf updated with new password\r\nPasswordSource: users.conf");
1809                                 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1810                                 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1811                                 ast_config_text_file_save("users.conf", cfg, "AppVoicemail");
1812                         }
1813                 }
1814         }
1815 }
1816
1817 static void vm_change_password_shell(struct ast_vm_user *vmu, char *newpassword)
1818 {
1819         char buf[255];
1820         snprintf(buf, sizeof(buf), "%s %s %s %s", ext_pass_cmd, vmu->context, vmu->mailbox, newpassword);
1821         ast_debug(1, "External password: %s\n",buf);
1822         if (!ast_safe_system(buf)) {
1823                 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: external script updated with new password\r\nPasswordSource: external");
1824                 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1825                 /* Reset the password in memory, too */
1826                 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1827         }
1828 }
1829
1830 /*! 
1831  * \brief Creates a file system path expression for a folder within the voicemail data folder and the appropriate context.
1832  * \param dest The variable to hold the output generated path expression. This buffer should be of size PATH_MAX.
1833  * \param len The length of the path string that was written out.
1834  * \param context
1835  * \param ext 
1836  * \param folder 
1837  * 
1838  * The path is constructed as 
1839  *      VM_SPOOL_DIRcontext/ext/folder
1840  *
1841  * \return zero on success, -1 on error.
1842  */
1843 static int make_dir(char *dest, int len, const char *context, const char *ext, const char *folder)
1844 {
1845         return snprintf(dest, len, "%s%s/%s/%s", VM_SPOOL_DIR, context, ext, folder);
1846 }
1847
1848 /*! 
1849  * \brief Creates a file system path expression for a folder within the voicemail data folder and the appropriate context.
1850  * \param dest The variable to hold the output generated path expression. This buffer should be of size PATH_MAX.
1851  * \param len The length of the path string that was written out.
1852  * \param dir 
1853  * \param num 
1854  * 
1855  * The path is constructed as 
1856  *      VM_SPOOL_DIRcontext/ext/folder
1857  *
1858  * \return zero on success, -1 on error.
1859  */
1860 static int make_file(char *dest, const int len, const char *dir, const int num)
1861 {
1862         return snprintf(dest, len, "%s/msg%04d", dir, num);
1863 }
1864
1865 /* same as mkstemp, but return a FILE * */
1866 static FILE *vm_mkftemp(char *template)
1867 {
1868         FILE *p = NULL;
1869         int pfd = mkstemp(template);
1870         chmod(template, VOICEMAIL_FILE_MODE & ~my_umask);
1871         if (pfd > -1) {
1872                 p = fdopen(pfd, "w+");
1873                 if (!p) {
1874                         close(pfd);
1875                         pfd = -1;
1876                 }
1877         }
1878         return p;
1879 }
1880
1881 /*! \brief basically mkdir -p $dest/$context/$ext/$folder
1882  * \param dest    String. base directory.
1883  * \param len     Length of dest.
1884  * \param context String. Ignored if is null or empty string.
1885  * \param ext     String. Ignored if is null or empty string.
1886  * \param folder  String. Ignored if is null or empty string. 
1887  * \return -1 on failure, 0 on success.
1888  */
1889 static int create_dirpath(char *dest, int len, const char *context, const char *ext, const char *folder)
1890 {
1891         mode_t  mode = VOICEMAIL_DIR_MODE;
1892         int res;
1893
1894         make_dir(dest, len, context, ext, folder);
1895         if ((res = ast_mkdir(dest, mode))) {
1896                 ast_log(AST_LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res));
1897                 return -1;
1898         }
1899         return 0;
1900 }
1901
1902 static const char *mbox(struct ast_vm_user *vmu, int id)
1903 {
1904 #ifdef IMAP_STORAGE
1905         if (vmu && id == 0) {
1906                 return vmu->imapfolder;
1907         }
1908 #endif
1909         return (id >= 0 && id < ARRAY_LEN(mailbox_folders)) ? mailbox_folders[id] : "Unknown";
1910 }
1911
1912 static const char *vm_index_to_foldername(int id)
1913 {
1914         return mbox(NULL, id);
1915 }
1916
1917
1918 static int get_folder_by_name(const char *name)
1919 {
1920         size_t i;
1921
1922         for (i = 0; i < ARRAY_LEN(mailbox_folders); i++) {
1923                 if (strcasecmp(name, mailbox_folders[i]) == 0) {
1924                         return i;
1925                 }
1926         }
1927
1928         return -1;
1929 }
1930
1931 static void free_user(struct ast_vm_user *vmu)
1932 {
1933         if (ast_test_flag(vmu, VM_ALLOCED)) {
1934
1935                 ast_free(vmu->emailbody);
1936                 vmu->emailbody = NULL;
1937
1938                 ast_free(vmu->emailsubject);
1939                 vmu->emailsubject = NULL;
1940
1941                 ast_free(vmu);
1942         }
1943 }
1944
1945 static int vm_allocate_dh(struct vm_state *vms, struct ast_vm_user *vmu, int count_msg) {
1946
1947         int arraysize = (vmu->maxmsg > count_msg ? vmu->maxmsg : count_msg);
1948
1949         /* remove old allocation */
1950         if (vms->deleted) {
1951                 ast_free(vms->deleted);
1952                 vms->deleted = NULL;
1953         }
1954         if (vms->heard) {
1955                 ast_free(vms->heard);
1956                 vms->heard = NULL;
1957         }
1958         vms->dh_arraysize = 0;
1959
1960         if (arraysize > 0) {
1961                 if (!(vms->deleted = ast_calloc(arraysize, sizeof(int)))) {
1962                         return -1;
1963                 }
1964                 if (!(vms->heard = ast_calloc(arraysize, sizeof(int)))) {
1965                         ast_free(vms->deleted);
1966                         vms->deleted = NULL;
1967                         return -1;
1968                 }
1969                 vms->dh_arraysize = arraysize;
1970         }
1971
1972         return 0;
1973 }
1974
1975 /* All IMAP-specific functions should go in this block. This
1976  * keeps them from being spread out all over the code */
1977 #ifdef IMAP_STORAGE
1978 static void vm_imap_delete(char *file, int msgnum, struct ast_vm_user *vmu)
1979 {
1980         char arg[10];
1981         struct vm_state *vms;
1982         unsigned long messageNum;
1983
1984         /* If greetings aren't stored in IMAP, just delete the file */
1985         if (msgnum < 0 && !imapgreetings) {
1986                 ast_filedelete(file, NULL);
1987                 return;
1988         }
1989
1990         if (!(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && !(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
1991                 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);
1992                 return;
1993         }
1994
1995         if (msgnum < 0) {
1996                 imap_delete_old_greeting(file, vms);
1997                 return;
1998         }
1999
2000         /* find real message number based on msgnum */
2001         /* this may be an index into vms->msgArray based on the msgnum. */
2002         messageNum = vms->msgArray[msgnum];
2003         if (messageNum == 0) {
2004                 ast_log(LOG_WARNING, "msgnum %d, mailbox message %lu is zero.\n", msgnum, messageNum);
2005                 return;
2006         }
2007         ast_debug(3, "deleting msgnum %d, which is mailbox message %lu\n", msgnum, messageNum);
2008         /* delete message */
2009         snprintf (arg, sizeof(arg), "%lu", messageNum);
2010         ast_mutex_lock(&vms->lock);
2011         mail_setflag (vms->mailstream, arg, "\\DELETED");
2012         mail_expunge(vms->mailstream);
2013         ast_mutex_unlock(&vms->lock);
2014 }
2015
2016 static void vm_imap_update_msg_id(char *dir, int msgnum, const char *msg_id, struct ast_vm_user *vmu, struct ast_config *msg_cfg, int folder)
2017 {
2018         struct ast_channel *chan;
2019         char *cid;
2020         char *cid_name;
2021         char *cid_num;
2022         struct vm_state *vms;
2023         const char *duration_str;
2024         int duration = 0;
2025
2026         /*
2027          * First, get things initially set up. If any of this fails, then
2028          * back out before doing anything substantial
2029          */
2030         vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0);
2031         if (!vms) {
2032                 return;
2033         }
2034
2035         if (open_mailbox(vms, vmu, folder)) {
2036                 return;
2037         }
2038
2039         chan = ast_dummy_channel_alloc();
2040         if (!chan) {
2041                 close_mailbox(vms, vmu);
2042                 return;
2043         }
2044
2045         /*
2046          * We need to make sure the new message we save has the same
2047          * callerid, flag, and duration as the original message
2048          */
2049         cid = ast_strdupa(ast_variable_retrieve(msg_cfg, "message", "callerid"));
2050
2051         if (!ast_strlen_zero(cid)) {
2052                 ast_callerid_parse(cid, &cid_name, &cid_num);
2053                 ast_party_caller_init(ast_channel_caller(chan));
2054                 if (!ast_strlen_zero(cid_name)) {
2055                         ast_channel_caller(chan)->id.name.valid = 1;
2056                         ast_channel_caller(chan)->id.name.str = ast_strdup(cid_name);
2057                 }
2058                 if (!ast_strlen_zero(cid_num)) {
2059                         ast_channel_caller(chan)->id.number.valid = 1;
2060                         ast_channel_caller(chan)->id.number.str = ast_strdup(cid_num);
2061                 }
2062         }
2063
2064         duration_str = ast_variable_retrieve(msg_cfg, "message", "duration");
2065
2066         if (!ast_strlen_zero(duration_str)) {
2067                 sscanf(duration_str, "%30d", &duration);
2068         }
2069
2070         /*
2071          * IMAP messages cannot be altered once delivered. So we have to delete the
2072          * current message and then re-add it with the updated message ID.
2073          *
2074          * Furthermore, there currently is no atomic way to create a new message and to
2075          * store it in an arbitrary folder. So we have to save it to the INBOX and then
2076          * move to the appropriate folder.
2077          */
2078         if (!imap_store_file(dir, vmu->mailbox, vmu->context, msgnum, chan, vmu, vmfmts,
2079                         duration, vms, ast_variable_retrieve(msg_cfg, "message", "flag"), msg_id)) {
2080                 if (folder != NEW_FOLDER) {
2081                         save_to_folder(vmu, vms, msgnum, folder, NULL, 1);
2082                 }
2083                 vm_imap_delete(dir, msgnum, vmu);
2084         }
2085         close_mailbox(vms, vmu);
2086         ast_channel_unref(chan);
2087 }
2088
2089 static int imap_retrieve_greeting(const char *dir, const int msgnum, struct ast_vm_user *vmu)
2090 {
2091         struct vm_state *vms_p;
2092         char *file, *filename;
2093         char *attachment;
2094         int i;
2095         BODY *body;
2096
2097         /* This function is only used for retrieval of IMAP greetings
2098          * regular messages are not retrieved this way, nor are greetings
2099          * if they are stored locally*/
2100         if (msgnum > -1 || !imapgreetings) {
2101                 return 0;
2102         } else {
2103                 file = strrchr(ast_strdupa(dir), '/');
2104                 if (file)
2105                         *file++ = '\0';
2106                 else {
2107                         ast_debug(1, "Failed to procure file name from directory passed.\n");
2108                         return -1;
2109                 }
2110         }
2111
2112         /* check if someone is accessing this box right now... */
2113         if (!(vms_p = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && 
2114                 !(vms_p = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
2115                 /* Unlike when retrieving a message, it is reasonable not to be able to find a 
2116                 * vm_state for a mailbox when trying to retrieve a greeting. Just create one,
2117                 * that's all we need to do.
2118                 */
2119                 if (!(vms_p = create_vm_state_from_user(vmu))) {
2120                         ast_log(LOG_NOTICE, "Unable to create vm_state object!\n");
2121                         return -1;
2122                 }
2123         }
2124
2125         /* Greetings will never have a prepended message */
2126         *vms_p->introfn = '\0';
2127
2128         ast_mutex_lock(&vms_p->lock);
2129         if (init_mailstream(vms_p, GREETINGS_FOLDER) || !vms_p->mailstream) {
2130                 ast_log(AST_LOG_ERROR, "IMAP mailstream is NULL or can't init_mailstream\n");
2131                 ast_mutex_unlock(&vms_p->lock);
2132                 return -1;
2133         }
2134
2135         /*XXX Yuck, this could probably be done a lot better */
2136         for (i = 0; i < vms_p->mailstream->nmsgs; i++) {
2137                 mail_fetchstructure(vms_p->mailstream, i + 1, &body);
2138                 /* We have the body, now we extract the file name of the first attachment. */
2139                 if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
2140                         attachment = ast_strdupa(body->nested.part->next->body.parameter->value);
2141                 } else {
2142                         ast_log(AST_LOG_ERROR, "There is no file attached to this IMAP message.\n");
2143                         ast_mutex_unlock(&vms_p->lock);
2144                         return -1;
2145                 }
2146                 filename = strsep(&attachment, ".");
2147                 if (!strcmp(filename, file)) {
2148                         ast_copy_string(vms_p->fn, dir, sizeof(vms_p->fn));
2149                         vms_p->msgArray[vms_p->curmsg] = i + 1;
2150                         save_body(body, vms_p, "2", attachment, 0);
2151                         ast_mutex_unlock(&vms_p->lock);
2152                         return 0;
2153                 }
2154         }
2155         ast_mutex_unlock(&vms_p->lock);
2156
2157         return -1;
2158 }
2159
2160 static int imap_retrieve_file(const char *dir, const int msgnum, const char *mailbox, const char *context)
2161 {
2162         BODY *body;
2163         char *header_content;
2164         char *attachedfilefmt;
2165         char buf[80];
2166         struct vm_state *vms;
2167         char text_file[PATH_MAX];
2168         FILE *text_file_ptr;
2169         int res = 0;
2170         struct ast_vm_user *vmu;
2171
2172         if (!(vmu = find_user(NULL, context, mailbox))) {
2173                 ast_log(LOG_WARNING, "Couldn't find user with mailbox %s@%s\n", mailbox, context);
2174                 return -1;
2175         }
2176         
2177         if (msgnum < 0) {
2178                 if (imapgreetings) {
2179                         res = imap_retrieve_greeting(dir, msgnum, vmu);
2180                         goto exit;
2181                 } else {
2182                         res = 0;
2183                         goto exit;
2184                 }
2185         }
2186
2187         /* Before anything can happen, we need a vm_state so that we can
2188          * actually access the imap server through the vms->mailstream
2189          */
2190         if (!(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && !(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
2191                 /* This should not happen. If it does, then I guess we'd
2192                  * need to create the vm_state, extract which mailbox to
2193                  * open, and then set up the msgArray so that the correct
2194                  * IMAP message could be accessed. If I have seen correctly
2195                  * though, the vms should be obtainable from the vmstates list
2196                  * and should have its msgArray properly set up.
2197                  */
2198                 ast_log(LOG_ERROR, "Couldn't find a vm_state for mailbox %s!!! Oh no!\n", vmu->mailbox);
2199                 res = -1;
2200                 goto exit;
2201         }
2202
2203         make_file(vms->fn, sizeof(vms->fn), dir, msgnum);
2204         snprintf(vms->introfn, sizeof(vms->introfn), "%sintro", vms->fn);
2205
2206         /* Don't try to retrieve a message from IMAP if it already is on the file system */
2207         if (ast_fileexists(vms->fn, NULL, NULL) > 0) {
2208                 res = 0;
2209                 goto exit;
2210         }
2211
2212         ast_debug(3, "Before mail_fetchheaders, curmsg is: %d, imap messages is %lu\n", msgnum, vms->msgArray[msgnum]);
2213         if (vms->msgArray[msgnum] == 0) {
2214                 ast_log(LOG_WARNING, "Trying to access unknown message\n");
2215                 res = -1;
2216                 goto exit;
2217         }
2218
2219         /* This will only work for new messages... */
2220         ast_mutex_lock(&vms->lock);
2221         header_content = mail_fetchheader (vms->mailstream, vms->msgArray[msgnum]);
2222         ast_mutex_unlock(&vms->lock);
2223         /* empty string means no valid header */
2224         if (ast_strlen_zero(header_content)) {
2225                 ast_log(LOG_ERROR, "Could not fetch header for message number %ld\n", vms->msgArray[msgnum]);
2226                 res = -1;
2227                 goto exit;
2228         }
2229
2230         ast_mutex_lock(&vms->lock);
2231         mail_fetchstructure(vms->mailstream, vms->msgArray[msgnum], &body);
2232         ast_mutex_unlock(&vms->lock);
2233
2234         /* We have the body, now we extract the file name of the first attachment. */
2235         if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
2236                 attachedfilefmt = ast_strdupa(body->nested.part->next->body.parameter->value);
2237         } else {
2238                 ast_log(LOG_ERROR, "There is no file attached to this IMAP message.\n");
2239                 res = -1;
2240                 goto exit;
2241         }
2242         
2243         /* Find the format of the attached file */
2244
2245         strsep(&attachedfilefmt, ".");
2246         if (!attachedfilefmt) {
2247                 ast_log(LOG_ERROR, "File format could not be obtained from IMAP message attachment\n");
2248                 res = -1;
2249                 goto exit;
2250         }
2251         
2252         save_body(body, vms, "2", attachedfilefmt, 0);
2253         if (save_body(body, vms, "3", attachedfilefmt, 1)) {
2254                 *vms->introfn = '\0';
2255         }
2256
2257         /* Get info from headers!! */
2258         snprintf(text_file, sizeof(text_file), "%s.%s", vms->fn, "txt");
2259
2260         if (!(text_file_ptr = fopen(text_file, "w"))) {
2261                 ast_log(LOG_WARNING, "Unable to open/create file %s: %s\n", text_file, strerror(errno));
2262         }
2263
2264         fprintf(text_file_ptr, "%s\n", "[message]");
2265
2266         if (get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Name:", buf, sizeof(buf))) {
2267                 fprintf(text_file_ptr, "callerid=\"%s\" ", S_OR(buf, ""));
2268         }
2269         if (get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Num:", buf, sizeof(buf))) {
2270                 fprintf(text_file_ptr, "<%s>\n", S_OR(buf, ""));
2271         }
2272         if (get_header_by_tag(header_content, "X-Asterisk-VM-Context:", buf, sizeof(buf))) {
2273                 fprintf(text_file_ptr, "context=%s\n", S_OR(buf, ""));
2274         }
2275         if (get_header_by_tag(header_content, "X-Asterisk-VM-Orig-time:", buf, sizeof(buf))) {
2276                 fprintf(text_file_ptr, "origtime=%s\n", S_OR(buf, ""));
2277         }
2278         if (get_header_by_tag(header_content, "X-Asterisk-VM-Duration:", buf, sizeof(buf))) {
2279                 fprintf(text_file_ptr, "duration=%s\n", S_OR(buf, ""));
2280         }
2281         if (get_header_by_tag(header_content, "X-Asterisk-VM-Category:", buf, sizeof(buf))) {
2282                 fprintf(text_file_ptr, "category=%s\n", S_OR(buf, ""));
2283         }
2284         if (get_header_by_tag(header_content, "X-Asterisk-VM-Flag:", buf, sizeof(buf))) {
2285                 fprintf(text_file_ptr, "flag=%s\n", S_OR(buf, ""));
2286         }
2287         if (get_header_by_tag(header_content, "X-Asterisk-VM-Message-ID:", buf, sizeof(buf))) {
2288                 fprintf(text_file_ptr, "msg_id=%s\n", S_OR(buf, ""));
2289         }
2290         fclose(text_file_ptr);
2291
2292 exit:
2293         free_user(vmu);
2294         return res;
2295 }
2296
2297 static int folder_int(const char *folder)
2298 {
2299         /*assume a NULL folder means INBOX*/
2300         if (!folder) {
2301                 return 0;
2302         }
2303         if (!strcasecmp(folder, imapfolder)) {
2304                 return 0;
2305         } else if (!strcasecmp(folder, "Old")) {
2306                 return 1;
2307         } else if (!strcasecmp(folder, "Work")) {
2308                 return 2;
2309         } else if (!strcasecmp(folder, "Family")) {
2310                 return 3;
2311         } else if (!strcasecmp(folder, "Friends")) {
2312                 return 4;
2313         } else if (!strcasecmp(folder, "Cust1")) {
2314                 return 5;
2315         } else if (!strcasecmp(folder, "Cust2")) {
2316                 return 6;
2317         } else if (!strcasecmp(folder, "Cust3")) {
2318                 return 7;
2319         } else if (!strcasecmp(folder, "Cust4")) {
2320                 return 8;
2321         } else if (!strcasecmp(folder, "Cust5")) {
2322                 return 9;
2323         } else if (!strcasecmp(folder, "Urgent")) {
2324                 return 11;
2325         } else { /*assume they meant INBOX if folder is not found otherwise*/
2326                 return 0;
2327         }
2328 }
2329
2330 static int __messagecount(const char *context, const char *mailbox, const char *folder)
2331 {
2332         SEARCHPGM *pgm;
2333         SEARCHHEADER *hdr;
2334
2335         struct ast_vm_user *vmu, vmus;
2336         struct vm_state *vms_p;
2337         int ret = 0;
2338         int fold = folder_int(folder);
2339         int urgent = 0;
2340         
2341         /* If URGENT, then look at INBOX */
2342         if (fold == 11) {
2343                 fold = NEW_FOLDER;
2344                 urgent = 1;
2345         }
2346
2347         if (ast_strlen_zero(mailbox))
2348                 return 0;
2349
2350         /* We have to get the user before we can open the stream! */
2351         vmu = find_user(&vmus, context, mailbox);
2352         if (!vmu) {
2353                 ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailbox, context);
2354                 return -1;
2355         } else {
2356                 /* No IMAP account available */
2357                 if (vmu->imapuser[0] == '\0') {
2358                         ast_log(AST_LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
2359                         return -1;
2360                 }
2361         }
2362
2363         /* No IMAP account available */
2364         if (vmu->imapuser[0] == '\0') {
2365                 ast_log(AST_LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
2366                 free_user(vmu);
2367                 return -1;
2368         }
2369
2370         /* check if someone is accessing this box right now... */
2371         vms_p = get_vm_state_by_imapuser(vmu->imapuser, 1);
2372         if (!vms_p) {
2373                 vms_p = get_vm_state_by_mailbox(mailbox, context, 1);
2374         }
2375         if (vms_p) {
2376                 ast_debug(3, "Returning before search - user is logged in\n");
2377                 if (fold == 0) { /* INBOX */
2378                         return urgent ? vms_p->urgentmessages : vms_p->newmessages;
2379                 }
2380                 if (fold == 1) { /* Old messages */
2381                         return vms_p->oldmessages;
2382                 }
2383         }
2384
2385         /* add one if not there... */
2386         vms_p = get_vm_state_by_imapuser(vmu->imapuser, 0);
2387         if (!vms_p) {
2388                 vms_p = get_vm_state_by_mailbox(mailbox, context, 0);
2389         }
2390
2391         if (!vms_p) {
2392                 vms_p = create_vm_state_from_user(vmu);
2393         }
2394         ret = init_mailstream(vms_p, fold);
2395         if (!vms_p->mailstream) {
2396                 ast_log(AST_LOG_ERROR, "Houston we have a problem - IMAP mailstream is NULL\n");
2397                 return -1;
2398         }
2399         if (ret == 0) {
2400                 ast_mutex_lock(&vms_p->lock);
2401                 pgm = mail_newsearchpgm ();
2402                 hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", (char *)(!ast_strlen_zero(vmu->imapvmshareid) ? vmu->imapvmshareid : mailbox));
2403                 hdr->next = mail_newsearchheader("X-Asterisk-VM-Context", (char *) S_OR(context, "default"));
2404                 pgm->header = hdr;
2405                 if (fold != OLD_FOLDER) {
2406                         pgm->unseen = 1;
2407                         pgm->seen = 0;
2408                 }
2409                 /* In the special case where fold is 1 (old messages) we have to do things a bit
2410                  * differently. Old messages are stored in the INBOX but are marked as "seen"
2411                  */
2412                 else {
2413                         pgm->unseen = 0;
2414                         pgm->seen = 1;
2415                 }
2416                 /* look for urgent messages */
2417                 if (fold == NEW_FOLDER) {
2418                         if (urgent) {
2419                                 pgm->flagged = 1;
2420                                 pgm->unflagged = 0;
2421                         } else {
2422                                 pgm->flagged = 0;
2423                                 pgm->unflagged = 1;
2424                         }
2425                 }
2426                 pgm->undeleted = 1;
2427                 pgm->deleted = 0;
2428
2429                 vms_p->vmArrayIndex = 0;
2430                 mail_search_full (vms_p->mailstream, NULL, pgm, NIL);
2431                 if (fold == 0 && urgent == 0)
2432                         vms_p->newmessages = vms_p->vmArrayIndex;
2433                 if (fold == 1)
2434                         vms_p->oldmessages = vms_p->vmArrayIndex;
2435                 if (fold == 0 && urgent == 1)
2436                         vms_p->urgentmessages = vms_p->vmArrayIndex;
2437                 /*Freeing the searchpgm also frees the searchhdr*/
2438                 mail_free_searchpgm(&pgm);
2439                 ast_mutex_unlock(&vms_p->lock);
2440                 vms_p->updated = 0;
2441                 return vms_p->vmArrayIndex;
2442         } else {
2443                 ast_mutex_lock(&vms_p->lock);
2444                 mail_ping(vms_p->mailstream);
2445                 ast_mutex_unlock(&vms_p->lock);
2446         }
2447         return 0;
2448 }
2449
2450 static int imap_check_limits(struct ast_channel *chan, struct vm_state *vms, struct ast_vm_user *vmu, int msgnum)
2451 {
2452         /* Check if mailbox is full */
2453         check_quota(vms, vmu->imapfolder);
2454         if (vms->quota_limit && vms->quota_usage >= vms->quota_limit) {
2455                 ast_debug(1, "*** QUOTA EXCEEDED!! %u >= %u\n", vms->quota_usage, vms->quota_limit);
2456                 if (chan) {
2457                         ast_play_and_wait(chan, "vm-mailboxfull");
2458                 }
2459                 return -1;
2460         }
2461
2462         /* Check if we have exceeded maxmsg */
2463         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));
2464         if (msgnum >= vmu->maxmsg - inprocess_count(vmu->mailbox, vmu->context, +1)) {
2465                 ast_log(LOG_WARNING, "Unable to leave message since we will exceed the maximum number of messages allowed (%u >= %u)\n", msgnum, vmu->maxmsg);
2466                 if (chan) {
2467                         ast_play_and_wait(chan, "vm-mailboxfull");
2468                         pbx_builtin_setvar_helper(chan, "VMSTATUS", "FAILED");
2469                 }
2470                 return -1;
2471         }
2472
2473         return 0;
2474 }
2475
2476 /*!
2477  * \brief Gets the number of messages that exist in a mailbox folder.
2478  * \param context
2479  * \param mailbox
2480  * \param folder
2481  * 
2482  * This method is used when IMAP backend is used.
2483  * \return The number of messages in this mailbox folder (zero or more).
2484  */
2485 static int messagecount(const char *context, const char *mailbox, const char *folder)
2486 {
2487         if (ast_strlen_zero(folder) || !strcmp(folder, "INBOX")) {
2488                 return __messagecount(context, mailbox, "INBOX") + __messagecount(context, mailbox, "Urgent");
2489         } else {
2490                 return __messagecount(context, mailbox, folder);
2491         }
2492 }
2493
2494 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, const char *msg_id)
2495 {
2496         char *myserveremail = serveremail;
2497         char fn[PATH_MAX];
2498         char introfn[PATH_MAX];
2499         char mailbox[256];
2500         char *stringp;
2501         FILE *p = NULL;
2502         char tmp[80] = "/tmp/astmail-XXXXXX";
2503         long len;
2504         void *buf;
2505         int tempcopy = 0;
2506         STRING str;
2507         int ret; /* for better error checking */
2508         char *imap_flags = NIL;
2509         int msgcount = (messagecount(vmu->context, vmu->mailbox, "INBOX") + messagecount(vmu->context, vmu->mailbox, "Old"));
2510         int box = NEW_FOLDER;
2511
2512         /* Back out early if this is a greeting and we don't want to store greetings in IMAP */
2513         if (msgnum < 0) {
2514                 if(!imapgreetings) {
2515                         return 0;
2516                 } else {
2517                         box = GREETINGS_FOLDER;
2518                 }
2519         }
2520
2521         if (imap_check_limits(chan, vms, vmu, msgcount)) {
2522                 return -1;
2523         }
2524
2525         /* Set urgent flag for IMAP message */
2526         if (!ast_strlen_zero(flag) && !strcmp(flag, "Urgent")) {
2527                 ast_debug(3, "Setting message flag \\\\FLAGGED.\n");
2528                 imap_flags = "\\FLAGGED";
2529         }
2530
2531         /* Attach only the first format */
2532         fmt = ast_strdupa(fmt);
2533         stringp = fmt;
2534         strsep(&stringp, "|");
2535
2536         if (!ast_strlen_zero(vmu->serveremail))
2537                 myserveremail = vmu->serveremail;
2538
2539         if (msgnum > -1)
2540                 make_file(fn, sizeof(fn), dir, msgnum);
2541         else
2542                 ast_copy_string (fn, dir, sizeof(fn));
2543
2544         snprintf(introfn, sizeof(introfn), "%sintro", fn);
2545         if (ast_fileexists(introfn, NULL, NULL) <= 0) {
2546                 *introfn = '\0';
2547         }
2548
2549         if (ast_strlen_zero(vmu->email)) {
2550                 /* We need the vmu->email to be set when we call make_email_file, but
2551                  * if we keep it set, a duplicate e-mail will be created. So at the end
2552                  * of this function, we will revert back to an empty string if tempcopy
2553                  * is 1.
2554                  */
2555                 ast_copy_string(vmu->email, vmu->imapuser, sizeof(vmu->email));
2556                 tempcopy = 1;
2557         }
2558
2559         if (!strcmp(fmt, "wav49"))
2560                 fmt = "WAV";
2561         ast_debug(3, "Storing file '%s', format '%s'\n", fn, fmt);
2562
2563         /* Make a temporary file instead of piping directly to sendmail, in case the mail
2564            command hangs. */
2565         if (!(p = vm_mkftemp(tmp))) {
2566                 ast_log(AST_LOG_WARNING, "Unable to store '%s' (can't create temporary file)\n", fn);
2567                 if (tempcopy)
2568                         *(vmu->email) = '\0';
2569                 return -1;
2570         }
2571
2572         if (msgnum < 0 && imapgreetings) {
2573                 if ((ret = init_mailstream(vms, GREETINGS_FOLDER))) {
2574                         ast_log(AST_LOG_WARNING, "Unable to open mailstream.\n");
2575                         return -1;
2576                 }
2577                 imap_delete_old_greeting(fn, vms);
2578         }
2579
2580         make_email_file(p, myserveremail, vmu, msgnum, vmu->context, vmu->mailbox, "INBOX",
2581                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
2582                 S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL),
2583                 fn, introfn, fmt, duration, 1, chan, NULL, 1, flag, msg_id);
2584         /* read mail file to memory */
2585         len = ftell(p);
2586         rewind(p);
2587         if (!(buf = ast_malloc(len + 1))) {
2588                 ast_log(AST_LOG_ERROR, "Can't allocate %ld bytes to read message\n", len + 1);
2589                 fclose(p);
2590                 if (tempcopy)
2591                         *(vmu->email) = '\0';
2592                 return -1;
2593         }
2594         if (fread(buf, len, 1, p) < len) {
2595                 if (ferror(p)) {
2596                         ast_log(LOG_ERROR, "Short read while reading in mail file.\n");
2597                         return -1;
2598                 }
2599         }
2600         ((char *) buf)[len] = '\0';
2601         INIT(&str, mail_string, buf, len);
2602         ret = init_mailstream(vms, box);
2603         if (ret == 0) {
2604                 imap_mailbox_name(mailbox, sizeof(mailbox), vms, box, 1);
2605                 ast_mutex_lock(&vms->lock);
2606                 if(!mail_append_full(vms->mailstream, mailbox, imap_flags, NIL, &str))
2607                         ast_log(LOG_ERROR, "Error while sending the message to %s\n", mailbox);
2608                 ast_mutex_unlock(&vms->lock);
2609                 fclose(p);
2610                 unlink(tmp);
2611                 ast_free(buf);
2612         } else {
2613                 ast_log(LOG_ERROR, "Could not initialize mailstream for %s\n", mailbox);
2614                 fclose(p);
2615                 unlink(tmp);
2616                 ast_free(buf);
2617                 return -1;
2618         }
2619         ast_debug(3, "%s stored\n", fn);
2620
2621         if (tempcopy)
2622                 *(vmu->email) = '\0';
2623         inprocess_count(vmu->mailbox, vmu->context, -1);
2624         return 0;
2625
2626 }
2627
2628 /*!
2629  * \brief Gets the number of messages that exist in the inbox folder.
2630  * \param mailbox_context
2631  * \param newmsgs The variable that is updated with the count of new messages within this inbox.
2632  * \param oldmsgs The variable that is updated with the count of old messages within this inbox.
2633  * \param urgentmsgs The variable that is updated with the count of urgent messages within this inbox.
2634  * 
2635  * This method is used when IMAP backend is used.
2636  * Simultaneously determines the count of new,old, and urgent messages. The total messages would then be the sum of these three.
2637  *
2638  * \return zero on success, -1 on error.
2639  */
2640
2641 static int inboxcount2(const char *mailbox_context, int *urgentmsgs, int *newmsgs, int *oldmsgs)
2642 {
2643         char tmp[PATH_MAX] = "";
2644         char *mailboxnc;
2645         char *context;
2646         char *mb;
2647         char *cur;
2648         if (newmsgs)
2649                 *newmsgs = 0;
2650         if (oldmsgs)
2651                 *oldmsgs = 0;
2652         if (urgentmsgs)
2653                 *urgentmsgs = 0;
2654
2655         ast_debug(3, "Mailbox is set to %s\n", mailbox_context);
2656         /* If no mailbox, return immediately */
2657         if (ast_strlen_zero(mailbox_context))
2658                 return 0;
2659
2660         ast_copy_string(tmp, mailbox_context, sizeof(tmp));
2661         context = strchr(tmp, '@');
2662         if (strchr(mailbox_context, ',')) {
2663                 int tmpnew, tmpold, tmpurgent;
2664                 ast_copy_string(tmp, mailbox_context, sizeof(tmp));
2665                 mb = tmp;
2666                 while ((cur = strsep(&mb, ", "))) {
2667                         if (!ast_strlen_zero(cur)) {
2668                                 if (inboxcount2(cur, urgentmsgs ? &tmpurgent : NULL, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
2669                                         return -1;
2670                                 else {
2671                                         if (newmsgs)
2672                                                 *newmsgs += tmpnew; 
2673                                         if (oldmsgs)
2674                                                 *oldmsgs += tmpold;
2675                                         if (urgentmsgs)
2676                                                 *urgentmsgs += tmpurgent;
2677                                 }
2678                         }
2679                 }
2680                 return 0;
2681         }
2682         if (context) {
2683                 *context = '\0';
2684                 mailboxnc = tmp;
2685                 context++;
2686         } else {
2687                 context = "default";
2688                 mailboxnc = (char *) mailbox_context;
2689         }
2690
2691         if (newmsgs) {
2692                 struct ast_vm_user *vmu = find_user(NULL, context, mailboxnc);
2693                 if (!vmu) {
2694                         ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailboxnc, context);
2695                         return -1;
2696                 }
2697                 if ((*newmsgs = __messagecount(context, mailboxnc, vmu->imapfolder)) < 0) {
2698                         free_user(vmu);
2699                         return -1;
2700                 }
2701                 free_user(vmu);
2702         }
2703         if (oldmsgs) {
2704                 if ((*oldmsgs = __messagecount(context, mailboxnc, "Old")) < 0) {
2705                         return -1;
2706                 }
2707         }
2708         if (urgentmsgs) {
2709                 if ((*urgentmsgs = __messagecount(context, mailboxnc, "Urgent")) < 0) {
2710                         return -1;
2711                 }
2712         }
2713         return 0;
2714 }
2715
2716 /** 
2717  * \brief Determines if the given folder has messages.
2718  * \param mailbox The @ delimited string for user@context. If no context is found, uses 'default' for the context.
2719  * \param folder the folder to look in
2720  *
2721  * This function is used when the mailbox is stored in an IMAP back end.
2722  * This invokes the messagecount(). Here we are interested in the presence of messages (> 0) only, not the actual count.
2723  * \return 1 if the folder has one or more messages. zero otherwise.
2724  */
2725
2726 static int has_voicemail(const char *mailbox, const char *folder)
2727 {
2728         char tmp[256], *tmp2, *box, *context;
2729         ast_copy_string(tmp, mailbox, sizeof(tmp));
2730         tmp2 = tmp;
2731         if (strchr(tmp2, ',') || strchr(tmp2, '&')) {
2732                 while ((box = strsep(&tmp2, ",&"))) {
2733                         if (!ast_strlen_zero(box)) {
2734                                 if (has_voicemail(box, folder)) {
2735                                         return 1;
2736                                 }
2737                         }
2738                 }
2739         }
2740         if ((context = strchr(tmp, '@'))) {
2741                 *context++ = '\0';
2742         } else {
2743                 context = "default";
2744         }
2745         return __messagecount(context, tmp, folder) ? 1 : 0;
2746 }
2747
2748 /*!
2749  * \brief Copies a message from one mailbox to another.
2750  * \param chan
2751  * \param vmu
2752  * \param imbox
2753  * \param msgnum
2754  * \param duration
2755  * \param recip
2756  * \param fmt
2757  * \param dir
2758  *
2759  * This works with IMAP storage based mailboxes.
2760  *
2761  * \return zero on success, -1 on error.
2762  */
2763 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, const char *dest_folder)
2764 {
2765         struct vm_state *sendvms = NULL;
2766         char messagestring[10]; /*I guess this could be a problem if someone has more than 999999999 messages...*/
2767         if (msgnum >= recip->maxmsg) {
2768                 ast_log(LOG_WARNING, "Unable to copy mail, mailbox %s is full\n", recip->mailbox);
2769                 return -1;
2770         }
2771         if (!(sendvms = get_vm_state_by_imapuser(vmu->imapuser, 0))) {
2772                 ast_log(LOG_ERROR, "Couldn't get vm_state for originator's mailbox!!\n");
2773                 return -1;
2774         }
2775         if (!get_vm_state_by_imapuser(recip->imapuser, 0)) {
2776                 ast_log(LOG_ERROR, "Couldn't get vm_state for destination mailbox!\n");
2777                 return -1;
2778         }
2779         snprintf(messagestring, sizeof(messagestring), "%ld", sendvms->msgArray[msgnum]);
2780         ast_mutex_lock(&sendvms->lock);
2781         if ((mail_copy(sendvms->mailstream, messagestring, (char *) mbox(vmu, imbox)) == T)) {
2782                 ast_mutex_unlock(&sendvms->lock);
2783                 return 0;
2784         }
2785         ast_mutex_unlock(&sendvms->lock);
2786         ast_log(LOG_WARNING, "Unable to copy message from mailbox %s to mailbox %s\n", vmu->mailbox, recip->mailbox);
2787         return -1;
2788 }
2789
2790 static void imap_mailbox_name(char *spec, size_t len, struct vm_state *vms, int box, int use_folder)
2791 {
2792         char tmp[256], *t = tmp;
2793         size_t left = sizeof(tmp);
2794         
2795         if (box == OLD_FOLDER) {
2796                 ast_copy_string(vms->curbox, mbox(NULL, NEW_FOLDER), sizeof(vms->curbox));
2797         } else {
2798                 ast_copy_string(vms->curbox, mbox(NULL, box), sizeof(vms->curbox));
2799         }
2800
2801         if (box == NEW_FOLDER) {
2802                 ast_copy_string(vms->vmbox, "vm-INBOX", sizeof(vms->vmbox));
2803         } else {
2804                 snprintf(vms->vmbox, sizeof(vms->vmbox), "vm-%s", mbox(NULL, box));
2805         }
2806
2807         /* Build up server information */
2808         ast_build_string(&t, &left, "{%s:%s/imap", S_OR(vms->imapserver, imapserver), S_OR(vms->imapport, imapport));
2809
2810         /* Add authentication user if present */
2811         if (!ast_strlen_zero(authuser))
2812                 ast_build_string(&t, &left, "/authuser=%s", authuser);
2813
2814         /* Add flags if present */
2815         if (!ast_strlen_zero(imapflags) || !(ast_strlen_zero(vms->imapflags))) {
2816                 ast_build_string(&t, &left, "/%s", S_OR(vms->imapflags, imapflags));
2817         }
2818
2819         /* End with username */
2820 #if 1
2821         ast_build_string(&t, &left, "/user=%s}", vms->imapuser);
2822 #else
2823         ast_build_string(&t, &left, "/user=%s/novalidate-cert}", vms->imapuser);
2824 #endif
2825         if (box == NEW_FOLDER || box == OLD_FOLDER)
2826                 snprintf(spec, len, "%s%s", tmp, use_folder? vms->imapfolder: "INBOX");
2827         else if (box == GREETINGS_FOLDER)
2828                 snprintf(spec, len, "%s%s", tmp, greetingfolder);
2829         else {  /* Other folders such as Friends, Family, etc... */
2830                 if (!ast_strlen_zero(imapparentfolder)) {
2831                         /* imapparentfolder would typically be set to INBOX */
2832                         snprintf(spec, len, "%s%s%c%s", tmp, imapparentfolder, delimiter, mbox(NULL, box));
2833                 } else {
2834                         snprintf(spec, len, "%s%s", tmp, mbox(NULL, box));
2835                 }
2836         }
2837 }
2838
2839 static int init_mailstream(struct vm_state *vms, int box)
2840 {
2841         MAILSTREAM *stream = NIL;
2842         long debug;
2843         char tmp[256];
2844
2845         if (!vms) {
2846                 ast_log(LOG_ERROR, "vm_state is NULL!\n");
2847                 return -1;
2848         }
2849         ast_debug(3, "vm_state user is:%s\n", vms->imapuser);
2850         if (vms->mailstream == NIL || !vms->mailstream) {
2851                 ast_debug(1, "mailstream not set.\n");
2852         } else {
2853                 stream = vms->mailstream;
2854         }
2855         /* debug = T;  user wants protocol telemetry? */
2856         debug = NIL;  /* NO protocol telemetry? */
2857
2858         if (delimiter == '\0') {                /* did not probe the server yet */
2859                 char *cp;
2860 #ifdef USE_SYSTEM_IMAP
2861 #include <imap/linkage.c>
2862 #elif defined(USE_SYSTEM_CCLIENT)
2863 #include <c-client/linkage.c>
2864 #else
2865 #include "linkage.c"
2866 #endif
2867                 /* Connect to INBOX first to get folders delimiter */
2868                 imap_mailbox_name(tmp, sizeof(tmp), vms, 0, 1);
2869                 ast_mutex_lock(&vms->lock);
2870                 stream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
2871                 ast_mutex_unlock(&vms->lock);
2872                 if (stream == NIL) {
2873                         ast_log(LOG_ERROR, "Can't connect to imap server %s\n", tmp);
2874                         return -1;
2875                 }
2876                 get_mailbox_delimiter(vms, stream);
2877                 /* update delimiter in imapfolder */
2878                 for (cp = vms->imapfolder; *cp; cp++)
2879                         if (*cp == '/')
2880                                 *cp = delimiter;
2881         }
2882         /* Now connect to the target folder */
2883         imap_mailbox_name(tmp, sizeof(tmp), vms, box, 1);
2884         ast_debug(3, "Before mail_open, server: %s, box:%d\n", tmp, box);
2885         ast_mutex_lock(&vms->lock);
2886         vms->mailstream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
2887         ast_mutex_unlock(&vms->lock);
2888         if (vms->mailstream == NIL) {
2889                 return -1;
2890         } else {
2891                 return 0;
2892         }
2893 }
2894
2895 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box)
2896 {
2897         SEARCHPGM *pgm;
2898         SEARCHHEADER *hdr;
2899         int urgent = 0;
2900
2901         /* If Urgent, then look at INBOX */
2902         if (box == 11) {
2903                 box = NEW_FOLDER;
2904                 urgent = 1;
2905         }
2906
2907         ast_copy_string(vms->imapuser, vmu->imapuser, sizeof(vms->imapuser));
2908         ast_copy_string(vms->imapfolder, vmu->imapfolder, sizeof(vms->imapfolder));
2909         ast_copy_string(vms->imapserver, vmu->imapserver, sizeof(vms->imapserver));
2910         ast_copy_string(vms->imapport, vmu->imapport, sizeof(vms->imapport));
2911         ast_copy_string(vms->imapflags, vmu->imapflags, sizeof(vms->imapflags));
2912         vms->imapversion = vmu->imapversion;
2913         ast_debug(3, "Before init_mailstream, user is %s\n", vmu->imapuser);
2914
2915         if (init_mailstream(vms, box) || !vms->mailstream) {
2916                 ast_log(AST_LOG_ERROR, "Could not initialize mailstream\n");
2917                 return -1;
2918         }
2919
2920         create_dirpath(vms->curdir, sizeof(vms->curdir), vmu->context, vms->username, vms->curbox);
2921
2922         /* Check Quota */
2923         if  (box == 0)  {
2924                 ast_debug(3, "Mailbox name set to: %s, about to check quotas\n", mbox(vmu, box));
2925                 check_quota(vms, (char *) mbox(vmu, box));
2926         }
2927
2928         ast_mutex_lock(&vms->lock);
2929         pgm = mail_newsearchpgm();
2930
2931         /* Check IMAP folder for Asterisk messages only... */
2932         hdr = mail_newsearchheader("X-Asterisk-VM-Extension", (!ast_strlen_zero(vmu->imapvmshareid) ? vmu->imapvmshareid : vmu->mailbox));
2933         hdr->next = mail_newsearchheader("X-Asterisk-VM-Context", vmu->context);
2934         pgm->header = hdr;
2935         pgm->deleted = 0;
2936         pgm->undeleted = 1;
2937
2938         /* if box = NEW_FOLDER, check for new, if box = OLD_FOLDER, check for read */
2939         if (box == NEW_FOLDER && urgent == 1) {
2940                 pgm->unseen = 1;
2941                 pgm->seen = 0;
2942                 pgm->flagged = 1;
2943                 pgm->unflagged = 0;
2944         } else if (box == NEW_FOLDER && urgent == 0) {
2945                 pgm->unseen = 1;
2946                 pgm->seen = 0;
2947                 pgm->flagged = 0;
2948                 pgm->unflagged = 1;
2949         } else if (box == OLD_FOLDER) {
2950                 pgm->seen = 1;
2951                 pgm->unseen = 0;
2952         }
2953
2954         ast_debug(3, "Before mail_search_full, user is %s\n", vmu->imapuser);
2955
2956         vms->vmArrayIndex = 0;
2957         mail_search_full (vms->mailstream, NULL, pgm, NIL);
2958         vms->lastmsg = vms->vmArrayIndex - 1;
2959         mail_free_searchpgm(&pgm);
2960         /* Since IMAP storage actually stores both old and new messages in the same IMAP folder,
2961          * ensure to allocate enough space to account for all of them. Warn if old messages
2962          * have not been checked first as that is required.
2963          */
2964         if (box == 0 && !vms->dh_arraysize) {
2965                 ast_log(LOG_WARNING, "The code expects the old messages to be checked first, fix the code.\n");
2966         }
2967         if (vm_allocate_dh(vms, vmu, box == 0 ? vms->vmArrayIndex + vms->oldmessages : vms->lastmsg)) {
2968                 ast_mutex_unlock(&vms->lock);
2969                 return -1;
2970         }
2971
2972         ast_mutex_unlock(&vms->lock);
2973         return 0;
2974 }
2975
2976 static void write_file(char *filename, char *buffer, unsigned long len)
2977 {
2978         FILE *output;
2979
2980         output = fopen (filename, "w");
2981         if (fwrite(buffer, len, 1, output) != 1) {
2982                 if (ferror(output)) {
2983                         ast_log(LOG_ERROR, "Short write while writing e-mail body: %s.\n", strerror(errno));
2984                 }
2985         }
2986         fclose (output);
2987 }
2988
2989 static void update_messages_by_imapuser(const char *user, unsigned long number)
2990 {
2991         struct vm_state *vms = get_vm_state_by_imapuser(user, 1);
2992
2993         if (!vms && !(vms = get_vm_state_by_imapuser(user, 0))) {
2994                 return;
2995         }
2996
2997         ast_debug(3, "saving mailbox message number %lu as message %d. Interactive set to %d\n", number, vms->vmArrayIndex, vms->interactive);
2998         vms->msgArray[vms->vmArrayIndex++] = number;
2999 }
3000
3001 void mm_searched(MAILSTREAM *stream, unsigned long number)
3002 {
3003         char *mailbox = stream->mailbox, buf[1024] = "", *user;
3004
3005         if (!(user = get_user_by_mailbox(mailbox, buf, sizeof(buf))))
3006                 return;
3007
3008         update_messages_by_imapuser(user, number);
3009 }
3010
3011 static struct ast_vm_user *find_user_realtime_imapuser(const char *imapuser)
3012 {
3013         struct ast_variable *var;
3014         struct ast_vm_user *vmu;
3015
3016         vmu = ast_calloc(1, sizeof *vmu);
3017         if (!vmu)
3018                 return NULL;
3019
3020         populate_defaults(vmu);
3021         ast_set_flag(vmu, VM_ALLOCED);
3022
3023         var = ast_load_realtime("voicemail", "imapuser", imapuser, NULL);
3024         if (var) {
3025                 apply_options_full(vmu, var);
3026                 ast_variables_destroy(var);
3027                 return vmu;
3028         } else {
3029                 ast_free(vmu);
3030                 return NULL;
3031         }
3032 }
3033
3034 /* Interfaces to C-client */
3035
3036 void mm_exists(MAILSTREAM * stream, unsigned long number)
3037 {
3038         /* mail_ping will callback here if new mail! */
3039         ast_debug(4, "Entering EXISTS callback for message %ld\n", number);
3040         if (number == 0) return;
3041         set_update(stream);
3042 }
3043
3044
3045 void mm_expunged(MAILSTREAM * stream, unsigned long number)
3046 {
3047         /* mail_ping will callback here if expunged mail! */
3048         ast_debug(4, "Entering EXPUNGE callback for message %ld\n", number);
3049         if (number == 0) return;
3050         set_update(stream);
3051 }
3052
3053
3054 void mm_flags(MAILSTREAM * stream, unsigned long number)
3055 {