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