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