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