app_voicemail: Fix the "test_voicemail_vm_info" unit test.
[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[VMSTATE_MAX_MSG_ARRAY];
865         MAILSTREAM *mailstream;
866         int vmArrayIndex;
867         char imapuser[80];                   /*!< IMAP server login */
868         char imapfolder[64];                 /*!< IMAP voicemail folder */
869         char imapserver[48];                 /*!< IMAP server address */
870         char imapport[8];                    /*!< IMAP server port */
871         char imapflags[128];                 /*!< IMAP optional flags */
872         int imapversion;
873         int interactive;
874         char introfn[PATH_MAX];              /*!< Name of prepended file */
875         unsigned int quota_limit;
876         unsigned int quota_usage;
877         struct vm_state *persist_vms;
878 #endif
879 };
880
881 #ifdef ODBC_STORAGE
882 static char odbc_database[80];
883 static char odbc_table[80];
884 #define RETRIEVE(a,b,c,d) retrieve_file(a,b)
885 #define DISPOSE(a,b) remove_file(a,b)
886 #define STORE(a,b,c,d,e,f,g,h,i,j,k) store_file(a,b,c,d)
887 #define EXISTS(a,b,c,d) (message_exists(a,b))
888 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(a,b,c,d,e,f))
889 #define COPY(a,b,c,d,e,f,g,h) (copy_file(a,b,c,d,e,f))
890 #define DELETE(a,b,c,d) (delete_file(a,b))
891 #define UPDATE_MSG_ID(a, b, c, d, e, f) (odbc_update_msg_id((a), (b), (c)))
892 #else
893 #ifdef IMAP_STORAGE
894 #define DISPOSE(a,b) (imap_remove_file(a,b))
895 #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))
896 #define RETRIEVE(a,b,c,d) imap_retrieve_file(a,b,c,d)
897 #define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
898 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
899 #define COPY(a,b,c,d,e,f,g,h) (copy_file(g,h));
900 #define DELETE(a,b,c,d) (vm_imap_delete(a,b,d))
901 #define UPDATE_MSG_ID(a, b, c, d, e, f) (vm_imap_update_msg_id((a), (b), (c), (d), (e), (f)))
902 #else
903 #define RETRIEVE(a,b,c,d)
904 #define DISPOSE(a,b)
905 #define STORE(a,b,c,d,e,f,g,h,i,j,k)
906 #define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
907 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
908 #define COPY(a,b,c,d,e,f,g,h) (copy_plain_file(g,h)); 
909 #define DELETE(a,b,c,d) (vm_delete(c))
910 #define UPDATE_MSG_ID(a, b, c, d, e, f)
911 #endif
912 #endif
913
914 static char VM_SPOOL_DIR[PATH_MAX];
915
916 static char ext_pass_cmd[128];
917 static char ext_pass_check_cmd[128];
918
919 static int my_umask;
920
921 #define PWDCHANGE_INTERNAL (1 << 1)
922 #define PWDCHANGE_EXTERNAL (1 << 2)
923 static int pwdchange = PWDCHANGE_INTERNAL;
924
925 #ifdef ODBC_STORAGE
926 #define tdesc "Comedian Mail (Voicemail System) with ODBC Storage"
927 #else
928 # ifdef IMAP_STORAGE
929 # define tdesc "Comedian Mail (Voicemail System) with IMAP Storage"
930 # else
931 # define tdesc "Comedian Mail (Voicemail System)"
932 # endif
933 #endif
934
935 static char userscontext[AST_MAX_EXTENSION] = "default";
936
937 static char *addesc = "Comedian Mail";
938
939 /* Leave a message */
940 static char *app = "VoiceMail";
941
942 /* Check mail, control, etc */
943 static char *app2 = "VoiceMailMain";
944
945 static char *app3 = "MailboxExists";
946 static char *app4 = "VMAuthenticate";
947
948 static char *playmsg_app = "VoiceMailPlayMsg";
949
950 static char *sayname_app = "VMSayName";
951
952 static AST_LIST_HEAD_STATIC(users, ast_vm_user);
953 static AST_LIST_HEAD_STATIC(zones, vm_zone);
954 static char zonetag[80];
955 static char locale[20];
956 static int maxsilence;
957 static int maxmsg;
958 static int maxdeletedmsg;
959 static int silencethreshold = 128;
960 static char serveremail[80];
961 static char mailcmd[160];       /* Configurable mail cmd */
962 static char externnotify[160]; 
963 static struct ast_smdi_interface *smdi_iface = NULL;
964 static char vmfmts[80];
965 static double volgain;
966 static int vmminsecs;
967 static int vmmaxsecs;
968 static int maxgreet;
969 static int skipms;
970 static int maxlogins;
971 static int minpassword;
972 static int passwordlocation;
973
974 /*! Poll mailboxes for changes since there is something external to
975  *  app_voicemail that may change them. */
976 static unsigned int poll_mailboxes;
977
978 /*! Polling frequency */
979 static unsigned int poll_freq;
980 /*! By default, poll every 30 seconds */
981 #define DEFAULT_POLL_FREQ 30
982
983 AST_MUTEX_DEFINE_STATIC(poll_lock);
984 static ast_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
985 static pthread_t poll_thread = AST_PTHREADT_NULL;
986 static unsigned char poll_thread_run;
987
988 /*! Subscription to MWI event subscription changes */
989 static struct stasis_subscription *mwi_sub_sub;
990
991 /*!
992  * \brief An MWI subscription
993  *
994  * This is so we can keep track of which mailboxes are subscribed to.
995  * This way, we know which mailboxes to poll when the pollmailboxes
996  * option is being used.
997  */
998 struct mwi_sub {
999         AST_RWLIST_ENTRY(mwi_sub) entry;
1000         int old_urgent;
1001         int old_new;
1002         int old_old;
1003         char *uniqueid;
1004         char mailbox[1];
1005 };
1006
1007 struct mwi_sub_task {
1008         const char *mailbox;
1009         const char *context;
1010         const char *uniqueid;
1011 };
1012
1013 static void mwi_sub_task_dtor(struct mwi_sub_task *mwist)
1014 {
1015         ast_free((void *) mwist->mailbox);
1016         ast_free((void *) mwist->context);
1017         ast_free((void *) mwist->uniqueid);
1018         ast_free(mwist);
1019 }
1020
1021 static struct ast_taskprocessor *mwi_subscription_tps;
1022
1023 static AST_RWLIST_HEAD_STATIC(mwi_subs, mwi_sub);
1024
1025 /* custom audio control prompts for voicemail playback */
1026 static char listen_control_forward_key[12];
1027 static char listen_control_reverse_key[12];
1028 static char listen_control_pause_key[12];
1029 static char listen_control_restart_key[12];
1030 static char listen_control_stop_key[12];
1031
1032 /* custom password sounds */
1033 static char vm_password[80] = "vm-password";
1034 static char vm_newpassword[80] = "vm-newpassword";
1035 static char vm_passchanged[80] = "vm-passchanged";
1036 static char vm_reenterpassword[80] = "vm-reenterpassword";
1037 static char vm_mismatch[80] = "vm-mismatch";
1038 static char vm_invalid_password[80] = "vm-invalid-password";
1039 static char vm_pls_try_again[80] = "vm-pls-try-again";
1040
1041 /*
1042  * XXX If we have the time, motivation, etc. to fix up this prompt, one of the following would be appropriate:
1043  * 1. create a sound along the lines of "Please try again.  When done, press the pound key" which could be spliced
1044  * from existing sound clips.  This would require some programming changes in the area of vm_forward options and also
1045  * app.c's __ast_play_and_record function
1046  * 2. create a sound prompt saying "Please try again.  When done recording, press any key to stop and send the prepended
1047  * message."  At the time of this comment, I think this would require new voice work to be commissioned.
1048  * 3. Something way different like providing instructions before a time out or a post-recording menu.  This would require
1049  * more effort than either of the other two.
1050  */
1051 static char vm_prepend_timeout[80] = "vm-then-pound";
1052
1053 static struct ast_flags globalflags = {0};
1054
1055 static int saydurationminfo;
1056
1057 static char dialcontext[AST_MAX_CONTEXT] = "";
1058 static char callcontext[AST_MAX_CONTEXT] = "";
1059 static char exitcontext[AST_MAX_CONTEXT] = "";
1060
1061 static char cidinternalcontexts[MAX_NUM_CID_CONTEXTS][64];
1062
1063
1064 static char *emailbody = NULL;
1065 static char *emailsubject = NULL;
1066 static char *pagerbody = NULL;
1067 static char *pagersubject = NULL;
1068 static char fromstring[100];
1069 static char pagerfromstring[100];
1070 static char charset[32] = "ISO-8859-1";
1071
1072 static unsigned char adsifdn[4] = "\x00\x00\x00\x0F";
1073 static unsigned char adsisec[4] = "\x9B\xDB\xF7\xAC";
1074 static int adsiver = 1;
1075 static char emaildateformat[32] = "%A, %B %d, %Y at %r";
1076 static char pagerdateformat[32] = "%A, %B %d, %Y at %r";
1077
1078 /* Forward declarations - generic */
1079 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box);
1080 static int close_mailbox(struct vm_state *vms, struct ast_vm_user *vmu);
1081 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);
1082 static int dialout(struct ast_channel *chan, struct ast_vm_user *vmu, char *num, char *outgoing_context);
1083 static int play_record_review(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime,
1084                         char *fmt, int outsidecaller, struct ast_vm_user *vmu, int *duration, int *sound_duration, const char *unlockdir,
1085                         signed char record_gain, struct vm_state *vms, char *flag, const char *msg_id);
1086 static int vm_tempgreeting(struct ast_channel *chan, struct ast_vm_user *vmu, struct vm_state *vms, char *fmtc, signed char record_gain);
1087 static int vm_play_folder_name(struct ast_channel *chan, char *mbox);
1088 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);
1089 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);
1090 static void apply_options(struct ast_vm_user *vmu, const char *options);
1091 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);
1092 static int is_valid_dtmf(const char *key);
1093 static void read_password_from_file(const char *secretfn, char *password, int passwordlen);
1094 static int write_password_to_file(const char *secretfn, const char *password);
1095 static const char *substitute_escapes(const char *value);
1096 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);
1097 /*!
1098  * Place a message in the indicated folder
1099  *
1100  * \param vmu Voicemail user
1101  * \param vms Current voicemail state for the user
1102  * \param msg The message number to save
1103  * \param box The folder into which the message should be saved
1104  * \param[out] newmsg The new message number of the saved message
1105  * \param move Tells whether to copy or to move the message
1106  *
1107  * \note the "move" parameter is only honored for IMAP voicemail presently
1108  * \retval 0 Success
1109  * \retval other Failure
1110  */
1111 static int save_to_folder(struct ast_vm_user *vmu, struct vm_state *vms, int msg, int box, int *newmsg, int move);
1112
1113 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);
1114 static struct ast_vm_mailbox_snapshot *vm_mailbox_snapshot_destroy(struct ast_vm_mailbox_snapshot *mailbox_snapshot);
1115
1116 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);
1117 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);
1118 static int vm_msg_remove(const char *mailbox, const char *context, size_t num_msgs, const char *folder, const char *msgs[]);
1119 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);
1120
1121 #ifdef TEST_FRAMEWORK
1122 static int vm_test_destroy_user(const char *context, const char *mailbox);
1123 static int vm_test_create_user(const char *context, const char *mailbox);
1124 #endif
1125
1126 /*!
1127  * \internal
1128  * \brief Parse the given mailbox_id into mailbox and context.
1129  * \since 12.0.0
1130  *
1131  * \param mailbox_id The mailbox@context string to separate.
1132  * \param mailbox Where the mailbox part will start.
1133  * \param context Where the context part will start.  ("default" if not present)
1134  *
1135  * \retval 0 on success.
1136  * \retval -1 on error.
1137  */
1138 static int separate_mailbox(char *mailbox_id, char **mailbox, char **context)
1139 {
1140         if (ast_strlen_zero(mailbox_id) || !mailbox || !context) {
1141                 return -1;
1142         }
1143         *context = mailbox_id;
1144         *mailbox = strsep(context, "@");
1145         if (ast_strlen_zero(*mailbox)) {
1146                 return -1;
1147         }
1148         if (ast_strlen_zero(*context)) {
1149                 *context = "default";
1150         }
1151         return 0;
1152 }
1153
1154 struct ao2_container *inprocess_container;
1155
1156 struct inprocess {
1157         int count;
1158         char *context;
1159         char mailbox[0];
1160 };
1161
1162 static int inprocess_hash_fn(const void *obj, const int flags)
1163 {
1164         const struct inprocess *i = obj;
1165         return atoi(i->mailbox);
1166 }
1167
1168 static int inprocess_cmp_fn(void *obj, void *arg, int flags)
1169 {
1170         struct inprocess *i = obj, *j = arg;
1171         if (strcmp(i->mailbox, j->mailbox)) {
1172                 return 0;
1173         }
1174         return !strcmp(i->context, j->context) ? CMP_MATCH : 0;
1175 }
1176
1177 static int inprocess_count(const char *context, const char *mailbox, int delta)
1178 {
1179         struct inprocess *i, *arg = ast_alloca(sizeof(*arg) + strlen(context) + strlen(mailbox) + 2);
1180         arg->context = arg->mailbox + strlen(mailbox) + 1;
1181         strcpy(arg->mailbox, mailbox); /* SAFE */
1182         strcpy(arg->context, context); /* SAFE */
1183         ao2_lock(inprocess_container);
1184         if ((i = ao2_find(inprocess_container, arg, 0))) {
1185                 int ret = ast_atomic_fetchadd_int(&i->count, delta);
1186                 ao2_unlock(inprocess_container);
1187                 ao2_ref(i, -1);
1188                 return ret;
1189         }
1190         if (delta < 0) {
1191                 ast_log(LOG_WARNING, "BUG: ref count decrement on non-existing object???\n");
1192         }
1193         if (!(i = ao2_alloc(sizeof(*i) + strlen(context) + strlen(mailbox) + 2, NULL))) {
1194                 ao2_unlock(inprocess_container);
1195                 return 0;
1196         }
1197         i->context = i->mailbox + strlen(mailbox) + 1;
1198         strcpy(i->mailbox, mailbox); /* SAFE */
1199         strcpy(i->context, context); /* SAFE */
1200         i->count = delta;
1201         ao2_link(inprocess_container, i);
1202         ao2_unlock(inprocess_container);
1203         ao2_ref(i, -1);
1204         return 0;
1205 }
1206
1207 #if !(defined(ODBC_STORAGE) || defined(IMAP_STORAGE))
1208 static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit);
1209 #endif
1210
1211 /*!
1212  * \brief Strips control and non 7-bit clean characters from input string.
1213  *
1214  * \note To map control and none 7-bit characters to a 7-bit clean characters
1215  *  please use ast_str_encode_mine().
1216  */
1217 static char *strip_control_and_high(const char *input, char *buf, size_t buflen)
1218 {
1219         char *bufptr = buf;
1220         for (; *input; input++) {
1221                 if (*input < 32) {
1222                         continue;
1223                 }
1224                 *bufptr++ = *input;
1225                 if (bufptr == buf + buflen - 1) {
1226                         break;
1227                 }
1228         }
1229         *bufptr = '\0';
1230         return buf;
1231 }
1232
1233
1234 /*!
1235  * \brief Sets default voicemail system options to a voicemail user.
1236  *
1237  * This applies select global settings to a newly created (dynamic) instance of a voicemail user.
1238  * - all the globalflags
1239  * - the saydurationminfo
1240  * - the callcontext
1241  * - the dialcontext
1242  * - the exitcontext
1243  * - vmmaxsecs, vmmaxmsg, maxdeletedmsg
1244  * - volume gain.
1245  * - emailsubject, emailbody set to NULL
1246  */
1247 static void populate_defaults(struct ast_vm_user *vmu)
1248 {
1249         ast_copy_flags(vmu, (&globalflags), AST_FLAGS_ALL);
1250         vmu->passwordlocation = passwordlocation;
1251         if (saydurationminfo) {
1252                 vmu->saydurationm = saydurationminfo;
1253         }
1254         ast_copy_string(vmu->callback, callcontext, sizeof(vmu->callback));
1255         ast_copy_string(vmu->dialout, dialcontext, sizeof(vmu->dialout));
1256         ast_copy_string(vmu->exit, exitcontext, sizeof(vmu->exit));
1257         ast_copy_string(vmu->zonetag, zonetag, sizeof(vmu->zonetag));
1258         ast_copy_string(vmu->locale, locale, sizeof(vmu->locale));
1259         if (vmminsecs) {
1260                 vmu->minsecs = vmminsecs;
1261         }
1262         if (vmmaxsecs) {
1263                 vmu->maxsecs = vmmaxsecs;
1264         }
1265         if (maxmsg) {
1266                 vmu->maxmsg = maxmsg;
1267         }
1268         if (maxdeletedmsg) {
1269                 vmu->maxdeletedmsg = maxdeletedmsg;
1270         }
1271         vmu->volgain = volgain;
1272         ast_free(vmu->email);
1273         vmu->email = NULL;
1274         ast_free(vmu->emailsubject);
1275         vmu->emailsubject = NULL;
1276         ast_free(vmu->emailbody);
1277         vmu->emailbody = NULL;
1278 #ifdef IMAP_STORAGE
1279         ast_copy_string(vmu->imapfolder, imapfolder, sizeof(vmu->imapfolder));
1280         ast_copy_string(vmu->imapserver, imapserver, sizeof(vmu->imapserver));
1281         ast_copy_string(vmu->imapport, imapport, sizeof(vmu->imapport));
1282         ast_copy_string(vmu->imapflags, imapflags, sizeof(vmu->imapflags));
1283 #endif
1284 }
1285
1286 /*!
1287  * \brief Sets a a specific property value.
1288  * \param vmu The voicemail user object to work with.
1289  * \param var The name of the property to be set.
1290  * \param value The value to be set to the property.
1291  * 
1292  * The property name must be one of the understood properties. See the source for details.
1293  */
1294 static void apply_option(struct ast_vm_user *vmu, const char *var, const char *value)
1295 {
1296         int x;
1297         if (!strcasecmp(var, "attach")) {
1298                 ast_set2_flag(vmu, ast_true(value), VM_ATTACH);
1299         } else if (!strcasecmp(var, "attachfmt")) {
1300                 ast_copy_string(vmu->attachfmt, value, sizeof(vmu->attachfmt));
1301         } else if (!strcasecmp(var, "serveremail")) {
1302                 ast_copy_string(vmu->serveremail, value, sizeof(vmu->serveremail));
1303         } else if (!strcasecmp(var, "emailbody")) {
1304                 ast_free(vmu->emailbody);
1305                 vmu->emailbody = ast_strdup(substitute_escapes(value));
1306         } else if (!strcasecmp(var, "emailsubject")) {
1307                 ast_free(vmu->emailsubject);
1308                 vmu->emailsubject = ast_strdup(substitute_escapes(value));
1309         } else if (!strcasecmp(var, "language")) {
1310                 ast_copy_string(vmu->language, value, sizeof(vmu->language));
1311         } else if (!strcasecmp(var, "tz")) {
1312                 ast_copy_string(vmu->zonetag, value, sizeof(vmu->zonetag));
1313         } else if (!strcasecmp(var, "locale")) {
1314                 ast_copy_string(vmu->locale, value, sizeof(vmu->locale));
1315 #ifdef IMAP_STORAGE
1316         } else if (!strcasecmp(var, "imapuser")) {
1317                 ast_copy_string(vmu->imapuser, value, sizeof(vmu->imapuser));
1318                 vmu->imapversion = imapversion;
1319         } else if (!strcasecmp(var, "imapserver")) {
1320                 ast_copy_string(vmu->imapserver, value, sizeof(vmu->imapserver));
1321                 vmu->imapversion = imapversion;
1322         } else if (!strcasecmp(var, "imapport")) {
1323                 ast_copy_string(vmu->imapport, value, sizeof(vmu->imapport));
1324                 vmu->imapversion = imapversion;
1325         } else if (!strcasecmp(var, "imapflags")) {
1326                 ast_copy_string(vmu->imapflags, value, sizeof(vmu->imapflags));
1327                 vmu->imapversion = imapversion;
1328         } else if (!strcasecmp(var, "imappassword") || !strcasecmp(var, "imapsecret")) {
1329                 ast_copy_string(vmu->imappassword, value, sizeof(vmu->imappassword));
1330                 vmu->imapversion = imapversion;
1331         } else if (!strcasecmp(var, "imapfolder")) {
1332                 ast_copy_string(vmu->imapfolder, value, sizeof(vmu->imapfolder));
1333                 vmu->imapversion = imapversion;
1334         } else if (!strcasecmp(var, "imapvmshareid")) {
1335                 ast_copy_string(vmu->imapvmshareid, value, sizeof(vmu->imapvmshareid));
1336                 vmu->imapversion = imapversion;
1337 #endif
1338         } else if (!strcasecmp(var, "delete") || !strcasecmp(var, "deletevoicemail")) {
1339                 ast_set2_flag(vmu, ast_true(value), VM_DELETE); 
1340         } else if (!strcasecmp(var, "saycid")){
1341                 ast_set2_flag(vmu, ast_true(value), VM_SAYCID); 
1342         } else if (!strcasecmp(var, "sendvoicemail")){
1343                 ast_set2_flag(vmu, ast_true(value), VM_SVMAIL); 
1344         } else if (!strcasecmp(var, "review")){
1345                 ast_set2_flag(vmu, ast_true(value), VM_REVIEW);
1346         } else if (!strcasecmp(var, "tempgreetwarn")){
1347                 ast_set2_flag(vmu, ast_true(value), VM_TEMPGREETWARN);  
1348         } else if (!strcasecmp(var, "messagewrap")){
1349                 ast_set2_flag(vmu, ast_true(value), VM_MESSAGEWRAP);    
1350         } else if (!strcasecmp(var, "operator")) {
1351                 ast_set2_flag(vmu, ast_true(value), VM_OPERATOR);       
1352         } else if (!strcasecmp(var, "envelope")){
1353                 ast_set2_flag(vmu, ast_true(value), VM_ENVELOPE);       
1354         } else if (!strcasecmp(var, "moveheard")){
1355                 ast_set2_flag(vmu, ast_true(value), VM_MOVEHEARD);
1356         } else if (!strcasecmp(var, "sayduration")){
1357                 ast_set2_flag(vmu, ast_true(value), VM_SAYDURATION);    
1358         } else if (!strcasecmp(var, "saydurationm")){
1359                 if (sscanf(value, "%30d", &x) == 1) {
1360                         vmu->saydurationm = x;
1361                 } else {
1362                         ast_log(AST_LOG_WARNING, "Invalid min duration for say duration\n");
1363                 }
1364         } else if (!strcasecmp(var, "forcename")){
1365                 ast_set2_flag(vmu, ast_true(value), VM_FORCENAME);      
1366         } else if (!strcasecmp(var, "forcegreetings")){
1367                 ast_set2_flag(vmu, ast_true(value), VM_FORCEGREET);     
1368         } else if (!strcasecmp(var, "callback")) {
1369                 ast_copy_string(vmu->callback, value, sizeof(vmu->callback));
1370         } else if (!strcasecmp(var, "dialout")) {
1371                 ast_copy_string(vmu->dialout, value, sizeof(vmu->dialout));
1372         } else if (!strcasecmp(var, "exitcontext")) {
1373                 ast_copy_string(vmu->exit, value, sizeof(vmu->exit));
1374         } else if (!strcasecmp(var, "minsecs")) {
1375                 if (sscanf(value, "%30d", &x) == 1 && x >= 0) {
1376                         vmu->minsecs = x;
1377                 } else {
1378                         ast_log(LOG_WARNING, "Invalid min message length of %s. Using global value %d\n", value, vmminsecs);
1379                         vmu->minsecs = vmminsecs;
1380                 }
1381         } else if (!strcasecmp(var, "maxmessage") || !strcasecmp(var, "maxsecs")) {
1382                 vmu->maxsecs = atoi(value);
1383                 if (vmu->maxsecs <= 0) {
1384                         ast_log(AST_LOG_WARNING, "Invalid max message length of %s. Using global value %d\n", value, vmmaxsecs);
1385                         vmu->maxsecs = vmmaxsecs;
1386                 } else {
1387                         vmu->maxsecs = atoi(value);
1388                 }
1389                 if (!strcasecmp(var, "maxmessage"))
1390                         ast_log(AST_LOG_WARNING, "Option 'maxmessage' has been deprecated in favor of 'maxsecs'.  Please make that change in your voicemail config.\n");
1391         } else if (!strcasecmp(var, "maxmsg")) {
1392                 vmu->maxmsg = atoi(value);
1393                 /* Accept maxmsg=0 (Greetings only voicemail) */
1394                 if (vmu->maxmsg < 0) {
1395                         ast_log(AST_LOG_WARNING, "Invalid number of messages per folder maxmsg=%s. Using default value %d\n", value, MAXMSG);
1396                         vmu->maxmsg = MAXMSG;
1397                 } else if (vmu->maxmsg > MAXMSGLIMIT) {
1398                         ast_log(AST_LOG_WARNING, "Maximum number of messages per folder is %d. Cannot accept value maxmsg=%s\n", MAXMSGLIMIT, value);
1399                         vmu->maxmsg = MAXMSGLIMIT;
1400                 }
1401         } else if (!strcasecmp(var, "nextaftercmd")) {
1402                 ast_set2_flag(vmu, ast_true(value), VM_SKIPAFTERCMD);
1403         } else if (!strcasecmp(var, "backupdeleted")) {
1404                 if (sscanf(value, "%30d", &x) == 1)
1405                         vmu->maxdeletedmsg = x;
1406                 else if (ast_true(value))
1407                         vmu->maxdeletedmsg = MAXMSG;
1408                 else
1409                         vmu->maxdeletedmsg = 0;
1410
1411                 if (vmu->maxdeletedmsg < 0) {
1412                         ast_log(AST_LOG_WARNING, "Invalid number of deleted messages saved per mailbox backupdeleted=%s. Using default value %d\n", value, MAXMSG);
1413                         vmu->maxdeletedmsg = MAXMSG;
1414                 } else if (vmu->maxdeletedmsg > MAXMSGLIMIT) {
1415                         ast_log(AST_LOG_WARNING, "Maximum number of deleted messages saved per mailbox is %d. Cannot accept value backupdeleted=%s\n", MAXMSGLIMIT, value);
1416                         vmu->maxdeletedmsg = MAXMSGLIMIT;
1417                 }
1418         } else if (!strcasecmp(var, "volgain")) {
1419                 sscanf(value, "%30lf", &vmu->volgain);
1420         } else if (!strcasecmp(var, "passwordlocation")) {
1421                 if (!strcasecmp(value, "spooldir")) {
1422                         vmu->passwordlocation = OPT_PWLOC_SPOOLDIR;
1423                 } else {
1424                         vmu->passwordlocation = OPT_PWLOC_VOICEMAILCONF;
1425                 }
1426         } else if (!strcasecmp(var, "options")) {
1427                 apply_options(vmu, value);
1428         }
1429 }
1430
1431 static char *vm_check_password_shell(char *command, char *buf, size_t len) 
1432 {
1433         int fds[2], pid = 0;
1434
1435         memset(buf, 0, len);
1436
1437         if (pipe(fds)) {
1438                 snprintf(buf, len, "FAILURE: Pipe failed: %s", strerror(errno));
1439         } else {
1440                 /* good to go*/
1441                 pid = ast_safe_fork(0);
1442
1443                 if (pid < 0) {
1444                         /* ok maybe not */
1445                         close(fds[0]);
1446                         close(fds[1]);
1447                         snprintf(buf, len, "FAILURE: Fork failed");
1448                 } else if (pid) {
1449                         /* parent */
1450                         close(fds[1]);
1451                         if (read(fds[0], buf, len) < 0) {
1452                                 ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
1453                         }
1454                         close(fds[0]);
1455                 } else {
1456                         /*  child */
1457                         AST_DECLARE_APP_ARGS(arg,
1458                                 AST_APP_ARG(v)[20];
1459                         );
1460                         char *mycmd = ast_strdupa(command);
1461
1462                         close(fds[0]);
1463                         dup2(fds[1], STDOUT_FILENO);
1464                         close(fds[1]);
1465                         ast_close_fds_above_n(STDOUT_FILENO);
1466
1467                         AST_NONSTANDARD_APP_ARGS(arg, mycmd, ' ');
1468
1469                         execv(arg.v[0], arg.v); 
1470                         printf("FAILURE: %s", strerror(errno));
1471                         _exit(0);
1472                 }
1473         }
1474         return buf;
1475 }
1476
1477 /*!
1478  * \brief Check that password meets minimum required length
1479  * \param vmu The voicemail user to change the password for.
1480  * \param password The password string to check
1481  *
1482  * \return zero on ok, 1 on not ok.
1483  */
1484 static int check_password(struct ast_vm_user *vmu, char *password)
1485 {
1486         /* check minimum length */
1487         if (strlen(password) < minpassword)
1488                 return 1;
1489         /* check that password does not contain '*' character */
1490         if (!ast_strlen_zero(password) && password[0] == '*')
1491                 return 1;
1492         if (!ast_strlen_zero(ext_pass_check_cmd)) {
1493                 char cmd[255], buf[255];
1494
1495                 ast_debug(1, "Verify password policies for %s\n", password);
1496
1497                 snprintf(cmd, sizeof(cmd), "%s %s %s %s %s", ext_pass_check_cmd, vmu->mailbox, vmu->context, vmu->password, password);
1498                 if (vm_check_password_shell(cmd, buf, sizeof(buf))) {
1499                         ast_debug(5, "Result: %s\n", buf);
1500                         if (!strncasecmp(buf, "VALID", 5)) {
1501                                 ast_debug(3, "Passed password check: '%s'\n", buf);
1502                                 return 0;
1503                         } else if (!strncasecmp(buf, "FAILURE", 7)) {
1504                                 ast_log(AST_LOG_WARNING, "Unable to execute password validation script: '%s'.\n", buf);
1505                                 return 0;
1506                         } else {
1507                                 ast_log(AST_LOG_NOTICE, "Password doesn't match policies for user %s %s\n", vmu->mailbox, password);
1508                                 return 1;
1509                         }
1510                 }
1511         }
1512         return 0;
1513 }
1514
1515 /*! 
1516  * \brief Performs a change of the voicemail passowrd in the realtime engine.
1517  * \param vmu The voicemail user to change the password for.
1518  * \param password The new value to be set to the password for this user.
1519  * 
1520  * This only works if there is a realtime engine configured.
1521  * This is called from the (top level) vm_change_password.
1522  *
1523  * \return zero on success, -1 on error.
1524  */
1525 static int change_password_realtime(struct ast_vm_user *vmu, const char *password)
1526 {
1527         int res = -1;
1528         if (!strcmp(vmu->password, password)) {
1529                 /* No change (but an update would return 0 rows updated, so we opt out here) */
1530                 return 0;
1531         }
1532
1533         if (strlen(password) > 10) {
1534                 ast_realtime_require_field("voicemail", "password", RQ_CHAR, strlen(password), SENTINEL);
1535         }
1536         if (ast_update2_realtime("voicemail", "context", vmu->context, "mailbox", vmu->mailbox, SENTINEL, "password", password, SENTINEL) > 0) {
1537                 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: realtime engine updated with new password\r\nPasswordSource: realtime");
1538                 ast_copy_string(vmu->password, password, sizeof(vmu->password));
1539                 res = 0;
1540         }
1541         return res;
1542 }
1543
1544 /*!
1545  * \brief Destructively Parse options and apply.
1546  */
1547 static void apply_options(struct ast_vm_user *vmu, const char *options)
1548 {       
1549         char *stringp;
1550         char *s;
1551         char *var, *value;
1552         stringp = ast_strdupa(options);
1553         while ((s = strsep(&stringp, "|"))) {
1554                 value = s;
1555                 if ((var = strsep(&value, "=")) && value) {
1556                         apply_option(vmu, var, value);
1557                 }
1558         }       
1559 }
1560
1561 /*!
1562  * \brief Loads the options specific to a voicemail user.
1563  * 
1564  * This is called when a vm_user structure is being set up, such as from load_options.
1565  */
1566 static void apply_options_full(struct ast_vm_user *retval, struct ast_variable *var)
1567 {
1568         for (; var; var = var->next) {
1569                 if (!strcasecmp(var->name, "vmsecret")) {
1570                         ast_copy_string(retval->password, var->value, sizeof(retval->password));
1571                 } else if (!strcasecmp(var->name, "secret") || !strcasecmp(var->name, "password")) { /* don't overwrite vmsecret if it exists */
1572                         if (ast_strlen_zero(retval->password)) {
1573                                 if (!ast_strlen_zero(var->value) && var->value[0] == '*') {
1574                                         ast_log(LOG_WARNING, "Invalid password detected for mailbox %s.  The password"
1575                                                 "\n\tmust be reset in voicemail.conf.\n", retval->mailbox);
1576                                 } else {
1577                                         ast_copy_string(retval->password, var->value, sizeof(retval->password));
1578                                 }
1579                         }
1580                 } else if (!strcasecmp(var->name, "uniqueid")) {
1581                         ast_copy_string(retval->uniqueid, var->value, sizeof(retval->uniqueid));
1582                 } else if (!strcasecmp(var->name, "pager")) {
1583                         ast_copy_string(retval->pager, var->value, sizeof(retval->pager));
1584                 } else if (!strcasecmp(var->name, "email")) {
1585                         ast_free(retval->email);
1586                         retval->email = ast_strdup(var->value);
1587                 } else if (!strcasecmp(var->name, "fullname")) {
1588                         ast_copy_string(retval->fullname, var->value, sizeof(retval->fullname));
1589                 } else if (!strcasecmp(var->name, "context")) {
1590                         ast_copy_string(retval->context, var->value, sizeof(retval->context));
1591                 } else if (!strcasecmp(var->name, "emailsubject")) {
1592                         ast_free(retval->emailsubject);
1593                         retval->emailsubject = ast_strdup(substitute_escapes(var->value));
1594                 } else if (!strcasecmp(var->name, "emailbody")) {
1595                         ast_free(retval->emailbody);
1596                         retval->emailbody = ast_strdup(substitute_escapes(var->value));
1597 #ifdef IMAP_STORAGE
1598                 } else if (!strcasecmp(var->name, "imapuser")) {
1599                         ast_copy_string(retval->imapuser, var->value, sizeof(retval->imapuser));
1600                         retval->imapversion = imapversion;
1601                 } else if (!strcasecmp(var->name, "imapserver")) {
1602                         ast_copy_string(retval->imapserver, var->value, sizeof(retval->imapserver));
1603                         retval->imapversion = imapversion;
1604                 } else if (!strcasecmp(var->name, "imapport")) {
1605                         ast_copy_string(retval->imapport, var->value, sizeof(retval->imapport));
1606                         retval->imapversion = imapversion;
1607                 } else if (!strcasecmp(var->name, "imapflags")) {
1608                         ast_copy_string(retval->imapflags, var->value, sizeof(retval->imapflags));
1609                         retval->imapversion = imapversion;
1610                 } else if (!strcasecmp(var->name, "imappassword") || !strcasecmp(var->name, "imapsecret")) {
1611                         ast_copy_string(retval->imappassword, var->value, sizeof(retval->imappassword));
1612                         retval->imapversion = imapversion;
1613                 } else if (!strcasecmp(var->name, "imapfolder")) {
1614                         ast_copy_string(retval->imapfolder, var->value, sizeof(retval->imapfolder));
1615                         retval->imapversion = imapversion;
1616                 } else if (!strcasecmp(var->name, "imapvmshareid")) {
1617                         ast_copy_string(retval->imapvmshareid, var->value, sizeof(retval->imapvmshareid));
1618                         retval->imapversion = imapversion;
1619 #endif
1620                 } else
1621                         apply_option(retval, var->name, var->value);
1622         }
1623 }
1624
1625 /*!
1626  * \brief Determines if a DTMF key entered is valid.
1627  * \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.
1628  *
1629  * Tests the character entered against the set of valid DTMF characters. 
1630  * \return 1 if the character entered is a valid DTMF digit, 0 if the character is invalid.
1631  */
1632 static int is_valid_dtmf(const char *key)
1633 {
1634         int i;
1635         char *local_key = ast_strdupa(key);
1636
1637         for (i = 0; i < strlen(key); ++i) {
1638                 if (!strchr(VALID_DTMF, *local_key)) {
1639                         ast_log(AST_LOG_WARNING, "Invalid DTMF key \"%c\" used in voicemail configuration file\n", *local_key);
1640                         return 0;
1641                 }
1642                 local_key++;
1643         }
1644         return 1;
1645 }
1646
1647 /*!
1648  * \brief Finds a voicemail user from the realtime engine.
1649  * \param ivm
1650  * \param context
1651  * \param mailbox
1652  *
1653  * 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.
1654  *
1655  * \return The ast_vm_user structure for the user that was found.
1656  */
1657 static struct ast_vm_user *find_user_realtime(struct ast_vm_user *ivm, const char *context, const char *mailbox)
1658 {
1659         struct ast_variable *var;
1660         struct ast_vm_user *retval;
1661
1662         if ((retval = (ivm ? ivm : ast_calloc(1, sizeof(*retval))))) {
1663                 if (ivm) {
1664                         memset(retval, 0, sizeof(*retval));
1665                 }
1666                 populate_defaults(retval);
1667                 if (!ivm) {
1668                         ast_set_flag(retval, VM_ALLOCED);
1669                 }
1670                 if (mailbox) {
1671                         ast_copy_string(retval->mailbox, mailbox, sizeof(retval->mailbox));
1672                 }
1673                 if (!context && ast_test_flag((&globalflags), VM_SEARCH)) {
1674                         var = ast_load_realtime("voicemail", "mailbox", mailbox, SENTINEL);
1675                 } else {
1676                         var = ast_load_realtime("voicemail", "mailbox", mailbox, "context", context, SENTINEL);
1677                 }
1678                 if (var) {
1679                         apply_options_full(retval, var);
1680                         ast_variables_destroy(var);
1681                 } else { 
1682                         if (!ivm) 
1683                                 ast_free(retval);
1684                         retval = NULL;
1685                 }       
1686         } 
1687         return retval;
1688 }
1689
1690 /*!
1691  * \brief Finds a voicemail user from the users file or the realtime engine.
1692  * \param ivm
1693  * \param context
1694  * \param mailbox
1695  * 
1696  * \return The ast_vm_user structure for the user that was found.
1697  */
1698 static struct ast_vm_user *find_user(struct ast_vm_user *ivm, const char *context, const char *mailbox)
1699 {
1700         /* This function could be made to generate one from a database, too */
1701         struct ast_vm_user *vmu = NULL, *cur;
1702         AST_LIST_LOCK(&users);
1703
1704         if (!context && !ast_test_flag((&globalflags), VM_SEARCH))
1705                 context = "default";
1706
1707         AST_LIST_TRAVERSE(&users, cur, list) {
1708 #ifdef IMAP_STORAGE
1709                 if (cur->imapversion != imapversion) {
1710                         continue;
1711                 }
1712 #endif
1713                 if (ast_test_flag((&globalflags), VM_SEARCH) && !strcasecmp(mailbox, cur->mailbox))
1714                         break;
1715                 if (context && (!strcasecmp(context, cur->context)) && (!strcasecmp(mailbox, cur->mailbox)))
1716                         break;
1717         }
1718         if (cur) {
1719                 /* Make a copy, so that on a reload, we have no race */
1720                 if ((vmu = (ivm ? ivm : ast_malloc(sizeof(*vmu))))) {
1721                         *vmu = *cur;
1722                         if (!ivm) {
1723                                 vmu->email = ast_strdup(cur->email);
1724                                 vmu->emailbody = ast_strdup(cur->emailbody);
1725                                 vmu->emailsubject = ast_strdup(cur->emailsubject);
1726                         }
1727                         ast_set2_flag(vmu, !ivm, VM_ALLOCED);
1728                         AST_LIST_NEXT(vmu, list) = NULL;
1729                 }
1730         } else
1731                 vmu = find_user_realtime(ivm, context, mailbox);
1732         AST_LIST_UNLOCK(&users);
1733         return vmu;
1734 }
1735
1736 /*!
1737  * \brief Resets a user password to a specified password.
1738  * \param context
1739  * \param mailbox
1740  * \param newpass
1741  *
1742  * This does the actual change password work, called by the vm_change_password() function.
1743  *
1744  * \return zero on success, -1 on error.
1745  */
1746 static int reset_user_pw(const char *context, const char *mailbox, const char *newpass)
1747 {
1748         /* This function could be made to generate one from a database, too */
1749         struct ast_vm_user *cur;
1750         int res = -1;
1751         AST_LIST_LOCK(&users);
1752         AST_LIST_TRAVERSE(&users, cur, list) {
1753                 if ((!context || !strcasecmp(context, cur->context)) &&
1754                         (!strcasecmp(mailbox, cur->mailbox)))
1755                                 break;
1756         }
1757         if (cur) {
1758                 ast_copy_string(cur->password, newpass, sizeof(cur->password));
1759                 res = 0;
1760         }
1761         AST_LIST_UNLOCK(&users);
1762         return res;
1763 }
1764
1765 /*!
1766  * \brief Check if configuration file is valid
1767  */
1768 static inline int valid_config(const struct ast_config *cfg)
1769 {
1770         return cfg && cfg != CONFIG_STATUS_FILEINVALID;
1771 }
1772
1773 /*! 
1774  * \brief The handler for the change password option.
1775  * \param vmu The voicemail user to work with.
1776  * \param newpassword The new password (that has been gathered from the appropriate prompting).
1777  * 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.
1778  * It is also called when the user wants to change their password from menu option '5' on the mailbox options menu.
1779  */
1780 static void vm_change_password(struct ast_vm_user *vmu, const char *newpassword)
1781 {
1782         struct ast_config   *cfg = NULL;
1783         struct ast_variable *var = NULL;
1784         struct ast_category *cat = NULL;
1785         char *category = NULL, *value = NULL, *new = NULL;
1786         const char *tmp = NULL;
1787         struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS };
1788         char secretfn[PATH_MAX] = "";
1789         int found = 0;
1790
1791         if (!change_password_realtime(vmu, newpassword))
1792                 return;
1793
1794         /* check if we should store the secret in the spool directory next to the messages */
1795         switch (vmu->passwordlocation) {
1796         case OPT_PWLOC_SPOOLDIR:
1797                 snprintf(secretfn, sizeof(secretfn), "%s%s/%s/secret.conf", VM_SPOOL_DIR, vmu->context, vmu->mailbox);
1798                 if (write_password_to_file(secretfn, newpassword) == 0) {
1799                         ast_test_suite_event_notify("PASSWORDCHANGED", "Message: secret.conf updated with new password\r\nPasswordSource: secret.conf");
1800                         ast_verb(4, "Writing voicemail password to file %s succeeded\n", secretfn);
1801                         reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1802                         ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1803                         break;
1804                 } else {
1805                         ast_verb(4, "Writing voicemail password to file %s failed, falling back to config file\n", secretfn);
1806                 }
1807                 /* Fall-through */
1808         case OPT_PWLOC_VOICEMAILCONF:
1809                 if ((cfg = ast_config_load(VOICEMAIL_CONFIG, config_flags)) && valid_config(cfg)) {
1810                         while ((category = ast_category_browse(cfg, category))) {
1811                                 if (!strcasecmp(category, vmu->context)) {
1812                                         if (!(tmp = ast_variable_retrieve(cfg, category, vmu->mailbox))) {
1813                                                 ast_log(AST_LOG_WARNING, "We could not find the mailbox.\n");
1814                                                 break;
1815                                         }
1816                                         value = strstr(tmp, ",");
1817                                         if (!value) {
1818                                                 new = ast_alloca(strlen(newpassword)+1);
1819                                                 sprintf(new, "%s", newpassword);
1820                                         } else {
1821                                                 new = ast_alloca((strlen(value) + strlen(newpassword) + 1));
1822                                                 sprintf(new, "%s%s", newpassword, value);
1823                                         }
1824                                         if (!(cat = ast_category_get(cfg, category))) {
1825                                                 ast_log(AST_LOG_WARNING, "Failed to get category structure.\n");
1826                                                 break;
1827                                         }
1828                                         ast_variable_update(cat, vmu->mailbox, new, NULL, 0);
1829                                         found = 1;
1830                                 }
1831                         }
1832                         /* save the results */
1833                         if (found) {
1834                                 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: voicemail.conf updated with new password\r\nPasswordSource: voicemail.conf");
1835                                 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1836                                 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1837                                 ast_config_text_file_save(VOICEMAIL_CONFIG, cfg, "app_voicemail");
1838                                 ast_config_destroy(cfg);
1839                                 break;
1840                         }
1841
1842                         ast_config_destroy(cfg);
1843                 }
1844                 /* Fall-through */
1845         case OPT_PWLOC_USERSCONF:
1846                 /* check users.conf and update the password stored for the mailbox */
1847                 /* if no vmsecret entry exists create one. */
1848                 if ((cfg = ast_config_load("users.conf", config_flags)) && valid_config(cfg)) {
1849                         ast_debug(4, "we are looking for %s\n", vmu->mailbox);
1850                         for (category = ast_category_browse(cfg, NULL); category; category = ast_category_browse(cfg, category)) {
1851                                 ast_debug(4, "users.conf: %s\n", category);
1852                                 if (!strcasecmp(category, vmu->mailbox)) {
1853                                         if (!ast_variable_retrieve(cfg, category, "vmsecret")) {
1854                                                 ast_debug(3, "looks like we need to make vmsecret!\n");
1855                                                 var = ast_variable_new("vmsecret", newpassword, "");
1856                                         } else {
1857                                                 var = NULL;
1858                                         }
1859                                         new = ast_alloca(strlen(newpassword) + 1);
1860                                         sprintf(new, "%s", newpassword);
1861                                         if (!(cat = ast_category_get(cfg, category))) {
1862                                                 ast_debug(4, "failed to get category!\n");
1863                                                 ast_free(var);
1864                                                 break;
1865                                         }
1866                                         if (!var) {
1867                                                 ast_variable_update(cat, "vmsecret", new, NULL, 0);
1868                                         } else {
1869                                                 ast_variable_append(cat, var);
1870                                         }
1871                                         found = 1;
1872                                         break;
1873                                 }
1874                         }
1875                         /* save the results and clean things up */
1876                         if (found) {
1877                                 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: users.conf updated with new password\r\nPasswordSource: users.conf");
1878                                 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1879                                 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1880                                 ast_config_text_file_save("users.conf", cfg, "app_voicemail");
1881                         }
1882
1883                         ast_config_destroy(cfg);
1884                 }
1885         }
1886 }
1887
1888 static void vm_change_password_shell(struct ast_vm_user *vmu, char *newpassword)
1889 {
1890         char buf[255];
1891         snprintf(buf, sizeof(buf), "%s %s %s %s", ext_pass_cmd, vmu->context, vmu->mailbox, newpassword);
1892         ast_debug(1, "External password: %s\n",buf);
1893         if (!ast_safe_system(buf)) {
1894                 ast_test_suite_event_notify("PASSWORDCHANGED", "Message: external script updated with new password\r\nPasswordSource: external");
1895                 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1896                 /* Reset the password in memory, too */
1897                 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1898         }
1899 }
1900
1901 /*! 
1902  * \brief Creates a file system path expression for a folder within the voicemail data folder and the appropriate context.
1903  * \param dest The variable to hold the output generated path expression. This buffer should be of size PATH_MAX.
1904  * \param len The length of the path string that was written out.
1905  * \param context
1906  * \param ext 
1907  * \param folder 
1908  * 
1909  * The path is constructed as 
1910  *      VM_SPOOL_DIRcontext/ext/folder
1911  *
1912  * \return zero on success, -1 on error.
1913  */
1914 static int make_dir(char *dest, int len, const char *context, const char *ext, const char *folder)
1915 {
1916         return snprintf(dest, len, "%s%s/%s/%s", VM_SPOOL_DIR, context, ext, folder);
1917 }
1918
1919 /*! 
1920  * \brief Creates a file system path expression for a folder within the voicemail data folder and the appropriate context.
1921  * \param dest The variable to hold the output generated path expression. This buffer should be of size PATH_MAX.
1922  * \param len The length of the path string that was written out.
1923  * \param dir 
1924  * \param num 
1925  * 
1926  * The path is constructed as 
1927  *      VM_SPOOL_DIRcontext/ext/folder
1928  *
1929  * \return zero on success, -1 on error.
1930  */
1931 static int make_file(char *dest, const int len, const char *dir, const int num)
1932 {
1933         return snprintf(dest, len, "%s/msg%04d", dir, num);
1934 }
1935
1936 /* same as mkstemp, but return a FILE * */
1937 static FILE *vm_mkftemp(char *template)
1938 {
1939         FILE *p = NULL;
1940         int pfd = mkstemp(template);
1941         chmod(template, VOICEMAIL_FILE_MODE & ~my_umask);
1942         if (pfd > -1) {
1943                 p = fdopen(pfd, "w+");
1944                 if (!p) {
1945                         close(pfd);
1946                         pfd = -1;
1947                 }
1948         }
1949         return p;
1950 }
1951
1952 /*! \brief basically mkdir -p $dest/$context/$ext/$folder
1953  * \param dest    String. base directory.
1954  * \param len     Length of dest.
1955  * \param context String. Ignored if is null or empty string.
1956  * \param ext     String. Ignored if is null or empty string.
1957  * \param folder  String. Ignored if is null or empty string. 
1958  * \return -1 on failure, 0 on success.
1959  */
1960 static int create_dirpath(char *dest, int len, const char *context, const char *ext, const char *folder)
1961 {
1962         mode_t  mode = VOICEMAIL_DIR_MODE;
1963         int res;
1964
1965         make_dir(dest, len, context, ext, folder);
1966         if ((res = ast_mkdir(dest, mode))) {
1967                 ast_log(AST_LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res));
1968                 return -1;
1969         }
1970         return 0;
1971 }
1972
1973 static const char *mbox(struct ast_vm_user *vmu, int id)
1974 {
1975 #ifdef IMAP_STORAGE
1976         if (vmu && id == 0) {
1977                 return vmu->imapfolder;
1978         }
1979 #endif
1980         return (id >= 0 && id < ARRAY_LEN(mailbox_folders)) ? mailbox_folders[id] : "Unknown";
1981 }
1982
1983 static const char *vm_index_to_foldername(int id)
1984 {
1985         return mbox(NULL, id);
1986 }
1987
1988
1989 static int get_folder_by_name(const char *name)
1990 {
1991         size_t i;
1992
1993         for (i = 0; i < ARRAY_LEN(mailbox_folders); i++) {
1994                 if (strcasecmp(name, mailbox_folders[i]) == 0) {
1995                         return i;
1996                 }
1997         }
1998
1999         return -1;
2000 }
2001
2002 static void free_user(struct ast_vm_user *vmu)
2003 {
2004         if (ast_test_flag(vmu, VM_ALLOCED)) {
2005
2006                 ast_free(vmu->email);
2007                 vmu->email = NULL;
2008
2009                 ast_free(vmu->emailbody);
2010                 vmu->emailbody = NULL;
2011
2012                 ast_free(vmu->emailsubject);
2013                 vmu->emailsubject = NULL;
2014
2015                 ast_free(vmu);
2016         }
2017 }
2018
2019 static int vm_allocate_dh(struct vm_state *vms, struct ast_vm_user *vmu, int count_msg) {
2020
2021         int arraysize = (vmu->maxmsg > count_msg ? vmu->maxmsg : count_msg);
2022
2023         /* remove old allocation */
2024         if (vms->deleted) {
2025                 ast_free(vms->deleted);
2026                 vms->deleted = NULL;
2027         }
2028         if (vms->heard) {
2029                 ast_free(vms->heard);
2030                 vms->heard = NULL;
2031         }
2032         vms->dh_arraysize = 0;
2033
2034         if (arraysize > 0) {
2035                 if (!(vms->deleted = ast_calloc(arraysize, sizeof(int)))) {
2036                         return -1;
2037                 }
2038                 if (!(vms->heard = ast_calloc(arraysize, sizeof(int)))) {
2039                         ast_free(vms->deleted);
2040                         vms->deleted = NULL;
2041                         return -1;
2042                 }
2043                 vms->dh_arraysize = arraysize;
2044         }
2045
2046         return 0;
2047 }
2048
2049 /* All IMAP-specific functions should go in this block. This
2050  * keeps them from being spread out all over the code */
2051 #ifdef IMAP_STORAGE
2052 static void vm_imap_delete(char *file, int msgnum, struct ast_vm_user *vmu)
2053 {
2054         char arg[10];
2055         struct vm_state *vms;
2056         unsigned long messageNum;
2057
2058         /* If greetings aren't stored in IMAP, just delete the file */
2059         if (msgnum < 0 && !imapgreetings) {
2060                 ast_filedelete(file, NULL);
2061                 return;
2062         }
2063
2064         if (!(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && !(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
2065                 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);
2066                 return;
2067         }
2068
2069         if (msgnum < 0) {
2070                 imap_delete_old_greeting(file, vms);
2071                 return;
2072         }
2073
2074         /* find real message number based on msgnum */
2075         /* this may be an index into vms->msgArray based on the msgnum. */
2076         messageNum = vms->msgArray[msgnum];
2077         if (messageNum == 0) {
2078                 ast_log(LOG_WARNING, "msgnum %d, mailbox message %lu is zero.\n", msgnum, messageNum);
2079                 return;
2080         }
2081         ast_debug(3, "deleting msgnum %d, which is mailbox message %lu\n", msgnum, messageNum);
2082         /* delete message */
2083         snprintf (arg, sizeof(arg), "%lu", messageNum);
2084         ast_mutex_lock(&vms->lock);
2085         mail_setflag (vms->mailstream, arg, "\\DELETED");
2086         mail_expunge(vms->mailstream);
2087         ast_mutex_unlock(&vms->lock);
2088 }
2089
2090 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)
2091 {
2092         struct ast_channel *chan;
2093         char *cid;
2094         char *cid_name;
2095         char *cid_num;
2096         struct vm_state *vms;
2097         const char *duration_str;
2098         int duration = 0;
2099
2100         /*
2101          * First, get things initially set up. If any of this fails, then
2102          * back out before doing anything substantial
2103          */
2104         vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0);
2105         if (!vms) {
2106                 return;
2107         }
2108
2109         if (open_mailbox(vms, vmu, folder)) {
2110                 return;
2111         }
2112
2113         chan = ast_dummy_channel_alloc();
2114         if (!chan) {
2115                 close_mailbox(vms, vmu);
2116                 return;
2117         }
2118
2119         /*
2120          * We need to make sure the new message we save has the same
2121          * callerid, flag, and duration as the original message
2122          */
2123         cid = ast_strdupa(ast_variable_retrieve(msg_cfg, "message", "callerid"));
2124
2125         if (!ast_strlen_zero(cid)) {
2126                 ast_callerid_parse(cid, &cid_name, &cid_num);
2127                 ast_party_caller_init(ast_channel_caller(chan));
2128                 if (!ast_strlen_zero(cid_name)) {
2129                         ast_channel_caller(chan)->id.name.valid = 1;
2130                         ast_channel_caller(chan)->id.name.str = ast_strdup(cid_name);
2131                 }
2132                 if (!ast_strlen_zero(cid_num)) {
2133                         ast_channel_caller(chan)->id.number.valid = 1;
2134                         ast_channel_caller(chan)->id.number.str = ast_strdup(cid_num);
2135                 }
2136         }
2137
2138         duration_str = ast_variable_retrieve(msg_cfg, "message", "duration");
2139
2140         if (!ast_strlen_zero(duration_str)) {
2141                 sscanf(duration_str, "%30d", &duration);
2142         }
2143
2144         /*
2145          * IMAP messages cannot be altered once delivered. So we have to delete the
2146          * current message and then re-add it with the updated message ID.
2147          *
2148          * Furthermore, there currently is no atomic way to create a new message and to
2149          * store it in an arbitrary folder. So we have to save it to the INBOX and then
2150          * move to the appropriate folder.
2151          */
2152         if (!imap_store_file(dir, vmu->mailbox, vmu->context, msgnum, chan, vmu, vmfmts,
2153                         duration, vms, ast_variable_retrieve(msg_cfg, "message", "flag"), msg_id)) {
2154                 if (folder != NEW_FOLDER) {
2155                         save_to_folder(vmu, vms, msgnum, folder, NULL, 1);
2156                 }
2157                 vm_imap_delete(dir, msgnum, vmu);
2158         }
2159         close_mailbox(vms, vmu);
2160         ast_channel_unref(chan);
2161 }
2162
2163 static int imap_retrieve_greeting(const char *dir, const int msgnum, struct ast_vm_user *vmu)
2164 {
2165         struct vm_state *vms_p;
2166         char *file, *filename;
2167         char *attachment;
2168         int i;
2169         BODY *body;
2170
2171         /* This function is only used for retrieval of IMAP greetings
2172          * regular messages are not retrieved this way, nor are greetings
2173          * if they are stored locally*/
2174         if (msgnum > -1 || !imapgreetings) {
2175                 return 0;
2176         } else {
2177                 file = strrchr(ast_strdupa(dir), '/');
2178                 if (file)
2179                         *file++ = '\0';
2180                 else {
2181                         ast_debug(1, "Failed to procure file name from directory passed.\n");
2182                         return -1;
2183                 }
2184         }
2185
2186         /* check if someone is accessing this box right now... */
2187         if (!(vms_p = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && 
2188                 !(vms_p = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
2189                 /* Unlike when retrieving a message, it is reasonable not to be able to find a 
2190                 * vm_state for a mailbox when trying to retrieve a greeting. Just create one,
2191                 * that's all we need to do.
2192                 */
2193                 if (!(vms_p = create_vm_state_from_user(vmu))) {
2194                         ast_log(LOG_NOTICE, "Unable to create vm_state object!\n");
2195                         return -1;
2196                 }
2197         }
2198
2199         /* Greetings will never have a prepended message */
2200         *vms_p->introfn = '\0';
2201
2202         ast_mutex_lock(&vms_p->lock);
2203         if (init_mailstream(vms_p, GREETINGS_FOLDER) || !vms_p->mailstream) {
2204                 ast_log(AST_LOG_ERROR, "IMAP mailstream is NULL or can't init_mailstream\n");
2205                 ast_mutex_unlock(&vms_p->lock);
2206                 return -1;
2207         }
2208
2209         /*XXX Yuck, this could probably be done a lot better */
2210         for (i = 0; i < vms_p->mailstream->nmsgs; i++) {
2211                 mail_fetchstructure(vms_p->mailstream, i + 1, &body);
2212                 /* We have the body, now we extract the file name of the first attachment. */
2213                 if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
2214                         attachment = ast_strdupa(body->nested.part->next->body.parameter->value);
2215                 } else {
2216                         ast_log(AST_LOG_ERROR, "There is no file attached to this IMAP message.\n");
2217                         ast_mutex_unlock(&vms_p->lock);
2218                         return -1;
2219                 }
2220                 filename = strsep(&attachment, ".");
2221                 if (!strcmp(filename, file)) {
2222                         ast_copy_string(vms_p->fn, dir, sizeof(vms_p->fn));
2223                         vms_p->msgArray[vms_p->curmsg] = i + 1;
2224                         save_body(body, vms_p, "2", attachment, 0);
2225                         ast_mutex_unlock(&vms_p->lock);
2226                         return 0;
2227                 }
2228         }
2229         ast_mutex_unlock(&vms_p->lock);
2230
2231         return -1;
2232 }
2233
2234 static int imap_retrieve_file(const char *dir, const int msgnum, const char *mailbox, const char *context)
2235 {
2236         BODY *body;
2237         char *header_content;
2238         char *attachedfilefmt;
2239         char buf[80];
2240         struct vm_state *vms;
2241         char text_file[PATH_MAX];
2242         FILE *text_file_ptr;
2243         int res = 0;
2244         struct ast_vm_user *vmu;
2245
2246         if (!(vmu = find_user(NULL, context, mailbox))) {
2247                 ast_log(LOG_WARNING, "Couldn't find user with mailbox %s@%s\n", mailbox, context);
2248                 return -1;
2249         }
2250         
2251         if (msgnum < 0) {
2252                 if (imapgreetings) {
2253                         res = imap_retrieve_greeting(dir, msgnum, vmu);
2254                         goto exit;
2255                 } else {
2256                         res = 0;
2257                         goto exit;
2258                 }
2259         }
2260
2261         /* Before anything can happen, we need a vm_state so that we can
2262          * actually access the imap server through the vms->mailstream
2263          */
2264         if (!(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && !(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
2265                 /* This should not happen. If it does, then I guess we'd
2266                  * need to create the vm_state, extract which mailbox to
2267                  * open, and then set up the msgArray so that the correct
2268                  * IMAP message could be accessed. If I have seen correctly
2269                  * though, the vms should be obtainable from the vmstates list
2270                  * and should have its msgArray properly set up.
2271                  */
2272                 ast_log(LOG_ERROR, "Couldn't find a vm_state for mailbox %s!!! Oh no!\n", vmu->mailbox);
2273                 res = -1;
2274                 goto exit;
2275         }
2276
2277         make_file(vms->fn, sizeof(vms->fn), dir, msgnum);
2278         snprintf(vms->introfn, sizeof(vms->introfn), "%sintro", vms->fn);
2279
2280         /* Don't try to retrieve a message from IMAP if it already is on the file system */
2281         if (ast_fileexists(vms->fn, NULL, NULL) > 0) {
2282                 res = 0;
2283                 goto exit;
2284         }
2285
2286         ast_debug(3, "Before mail_fetchheaders, curmsg is: %d, imap messages is %lu\n", msgnum, vms->msgArray[msgnum]);
2287         if (vms->msgArray[msgnum] == 0) {
2288                 ast_log(LOG_WARNING, "Trying to access unknown message\n");
2289                 res = -1;
2290                 goto exit;
2291         }
2292
2293         /* This will only work for new messages... */
2294         ast_mutex_lock(&vms->lock);
2295         header_content = mail_fetchheader (vms->mailstream, vms->msgArray[msgnum]);
2296         ast_mutex_unlock(&vms->lock);
2297         /* empty string means no valid header */
2298         if (ast_strlen_zero(header_content)) {
2299                 ast_log(LOG_ERROR, "Could not fetch header for message number %ld\n", vms->msgArray[msgnum]);
2300                 res = -1;
2301                 goto exit;
2302         }
2303
2304         ast_mutex_lock(&vms->lock);
2305         mail_fetchstructure(vms->mailstream, vms->msgArray[msgnum], &body);
2306         ast_mutex_unlock(&vms->lock);
2307
2308         /* We have the body, now we extract the file name of the first attachment. */
2309         if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
2310                 attachedfilefmt = ast_strdupa(body->nested.part->next->body.parameter->value);
2311         } else {
2312                 ast_log(LOG_ERROR, "There is no file attached to this IMAP message.\n");
2313                 res = -1;
2314                 goto exit;
2315         }
2316         
2317         /* Find the format of the attached file */
2318
2319         strsep(&attachedfilefmt, ".");
2320         if (!attachedfilefmt) {
2321                 ast_log(LOG_ERROR, "File format could not be obtained from IMAP message attachment\n");
2322                 res = -1;
2323                 goto exit;
2324         }
2325         
2326         save_body(body, vms, "2", attachedfilefmt, 0);
2327         if (save_body(body, vms, "3", attachedfilefmt, 1)) {
2328                 *vms->introfn = '\0';
2329         }
2330
2331         /* Get info from headers!! */
2332         snprintf(text_file, sizeof(text_file), "%s.%s", vms->fn, "txt");
2333
2334         if (!(text_file_ptr = fopen(text_file, "w"))) {
2335                 ast_log(LOG_WARNING, "Unable to open/create file %s: %s\n", text_file, strerror(errno));
2336         }
2337
2338         fprintf(text_file_ptr, "%s\n", "[message]");
2339
2340         if (get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Name:", buf, sizeof(buf))) {
2341                 fprintf(text_file_ptr, "callerid=\"%s\" ", S_OR(buf, ""));
2342         }
2343         if (get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Num:", buf, sizeof(buf))) {
2344                 fprintf(text_file_ptr, "<%s>\n", S_OR(buf, ""));
2345         }
2346         if (get_header_by_tag(header_content, "X-Asterisk-VM-Context:", buf, sizeof(buf))) {
2347                 fprintf(text_file_ptr, "context=%s\n", S_OR(buf, ""));
2348         }
2349         if (get_header_by_tag(header_content, "X-Asterisk-VM-Orig-time:", buf, sizeof(buf))) {
2350                 fprintf(text_file_ptr, "origtime=%s\n", S_OR(buf, ""));
2351         }
2352         if (get_header_by_tag(header_content, "X-Asterisk-VM-Duration:", buf, sizeof(buf))) {
2353                 fprintf(text_file_ptr, "duration=%s\n", S_OR(buf, ""));
2354         }
2355         if (get_header_by_tag(header_content, "X-Asterisk-VM-Category:", buf, sizeof(buf))) {
2356                 fprintf(text_file_ptr, "category=%s\n", S_OR(buf, ""));
2357         }
2358         if (get_header_by_tag(header_content, "X-Asterisk-VM-Flag:", buf, sizeof(buf))) {
2359                 fprintf(text_file_ptr, "flag=%s\n", S_OR(buf, ""));
2360         }
2361         if (get_header_by_tag(header_content, "X-Asterisk-VM-Message-ID:", buf, sizeof(buf))) {
2362                 fprintf(text_file_ptr, "msg_id=%s\n", S_OR(buf, ""));
2363         }
2364         fclose(text_file_ptr);
2365
2366 exit:
2367         free_user(vmu);
2368         return res;
2369 }
2370
2371 static int folder_int(const char *folder)
2372 {
2373         /*assume a NULL folder means INBOX*/
2374         if (!folder) {
2375                 return 0;
2376         }
2377         if (!strcasecmp(folder, imapfolder)) {
2378                 return 0;
2379         } else if (!strcasecmp(folder, "Old")) {
2380                 return 1;
2381         } else if (!strcasecmp(folder, "Work")) {
2382                 return 2;
2383         } else if (!strcasecmp(folder, "Family")) {
2384                 return 3;
2385         } else if (!strcasecmp(folder, "Friends")) {
2386                 return 4;
2387         } else if (!strcasecmp(folder, "Cust1")) {
2388                 return 5;
2389         } else if (!strcasecmp(folder, "Cust2")) {
2390                 return 6;
2391         } else if (!strcasecmp(folder, "Cust3")) {
2392                 return 7;
2393         } else if (!strcasecmp(folder, "Cust4")) {
2394                 return 8;
2395         } else if (!strcasecmp(folder, "Cust5")) {
2396                 return 9;
2397         } else if (!strcasecmp(folder, "Urgent")) {
2398                 return 11;
2399         } else { /*assume they meant INBOX if folder is not found otherwise*/
2400                 return 0;
2401         }
2402 }
2403
2404 static int __messagecount(const char *context, const char *mailbox, const char *folder)
2405 {
2406         SEARCHPGM *pgm;
2407         SEARCHHEADER *hdr;
2408
2409         struct ast_vm_user *vmu, vmus;
2410         struct vm_state *vms_p;
2411         int ret = 0;
2412         int fold = folder_int(folder);
2413         int urgent = 0;
2414         
2415         /* If URGENT, then look at INBOX */
2416         if (fold == 11) {
2417                 fold = NEW_FOLDER;
2418                 urgent = 1;
2419         }
2420
2421         if (ast_strlen_zero(mailbox))
2422                 return 0;
2423
2424         /* We have to get the user before we can open the stream! */
2425         vmu = find_user(&vmus, context, mailbox);
2426         if (!vmu) {
2427                 ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailbox, context);
2428                 return -1;
2429         } else {
2430                 /* No IMAP account available */
2431                 if (vmu->imapuser[0] == '\0') {
2432                         ast_log(AST_LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
2433                         return -1;
2434                 }
2435         }
2436
2437         /* No IMAP account available */
2438         if (vmu->imapuser[0] == '\0') {
2439                 ast_log(AST_LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
2440                 free_user(vmu);
2441                 return -1;
2442         }
2443
2444         /* check if someone is accessing this box right now... */
2445         vms_p = get_vm_state_by_imapuser(vmu->imapuser, 1);
2446         if (!vms_p) {
2447                 vms_p = get_vm_state_by_mailbox(mailbox, context, 1);
2448         }
2449         if (vms_p) {
2450                 ast_debug(3, "Returning before search - user is logged in\n");
2451                 if (fold == 0) { /* INBOX */
2452                         return urgent ? vms_p->urgentmessages : vms_p->newmessages;
2453                 }
2454                 if (fold == 1) { /* Old messages */
2455                         return vms_p->oldmessages;
2456                 }
2457         }
2458
2459         /* add one if not there... */
2460         vms_p = get_vm_state_by_imapuser(vmu->imapuser, 0);
2461         if (!vms_p) {
2462                 vms_p = get_vm_state_by_mailbox(mailbox, context, 0);
2463         }
2464
2465         if (!vms_p) {
2466                 vms_p = create_vm_state_from_user(vmu);
2467         }
2468         ret = init_mailstream(vms_p, fold);
2469         if (!vms_p->mailstream) {
2470                 ast_log(AST_LOG_ERROR, "Houston we have a problem - IMAP mailstream is NULL\n");
2471                 return -1;
2472         }
2473         if (ret == 0) {
2474                 ast_mutex_lock(&vms_p->lock);
2475                 pgm = mail_newsearchpgm ();
2476                 hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", (char *)(!ast_strlen_zero(vmu->imapvmshareid) ? vmu->imapvmshareid : mailbox));
2477                 hdr->next = mail_newsearchheader("X-Asterisk-VM-Context", (char *) S_OR(context, "default"));
2478                 pgm->header = hdr;
2479                 if (fold != OLD_FOLDER) {
2480                         pgm->unseen = 1;
2481                         pgm->seen = 0;
2482                 }
2483                 /* In the special case where fold is 1 (old messages) we have to do things a bit
2484                  * differently. Old messages are stored in the INBOX but are marked as "seen"
2485                  */
2486                 else {
2487                         pgm->unseen = 0;
2488                         pgm->seen = 1;
2489                 }
2490                 /* look for urgent messages */
2491                 if (fold == NEW_FOLDER) {
2492                         if (urgent) {
2493                                 pgm->flagged = 1;
2494                                 pgm->unflagged = 0;
2495                         } else {
2496                                 pgm->flagged = 0;
2497                                 pgm->unflagged = 1;
2498                         }
2499                 }
2500                 pgm->undeleted = 1;
2501                 pgm->deleted = 0;
2502
2503                 vms_p->vmArrayIndex = 0;
2504                 mail_search_full (vms_p->mailstream, NULL, pgm, NIL);
2505                 if (fold == 0 && urgent == 0)
2506                         vms_p->newmessages = vms_p->vmArrayIndex;
2507                 if (fold == 1)
2508                         vms_p->oldmessages = vms_p->vmArrayIndex;
2509                 if (fold == 0 && urgent == 1)
2510                         vms_p->urgentmessages = vms_p->vmArrayIndex;
2511                 /*Freeing the searchpgm also frees the searchhdr*/
2512                 mail_free_searchpgm(&pgm);
2513                 ast_mutex_unlock(&vms_p->lock);
2514                 vms_p->updated = 0;
2515                 return vms_p->vmArrayIndex;
2516         } else {
2517                 ast_mutex_lock(&vms_p->lock);
2518                 mail_ping(vms_p->mailstream);
2519                 ast_mutex_unlock(&vms_p->lock);
2520         }
2521         return 0;
2522 }
2523
2524 static int imap_check_limits(struct ast_channel *chan, struct vm_state *vms, struct ast_vm_user *vmu, int msgnum)
2525 {
2526         /* Check if mailbox is full */
2527         check_quota(vms, vmu->imapfolder);
2528         if (vms->quota_limit && vms->quota_usage >= vms->quota_limit) {
2529                 ast_debug(1, "*** QUOTA EXCEEDED!! %u >= %u\n", vms->quota_usage, vms->quota_limit);
2530                 if (chan) {
2531                         ast_play_and_wait(chan, "vm-mailboxfull");
2532                 }
2533                 return -1;
2534         }
2535
2536         /* Check if we have exceeded maxmsg */
2537         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));
2538         if (msgnum >= vmu->maxmsg - inprocess_count(vmu->mailbox, vmu->context, +1)) {
2539                 ast_log(LOG_WARNING, "Unable to leave message since we will exceed the maximum number of messages allowed (%u >= %u)\n", msgnum, vmu->maxmsg);
2540                 if (chan) {
2541                         ast_play_and_wait(chan, "vm-mailboxfull");
2542                         pbx_builtin_setvar_helper(chan, "VMSTATUS", "FAILED");
2543                 }
2544                 return -1;
2545         }
2546
2547         return 0;
2548 }
2549
2550 /*!
2551  * \brief Gets the number of messages that exist in a mailbox folder.
2552  * \param mailbox_id
2553  * \param folder
2554  * 
2555  * This method is used when IMAP backend is used.
2556  * \return The number of messages in this mailbox folder (zero or more).
2557  */
2558 static int messagecount(const char *mailbox_id, const char *folder)
2559 {
2560         char *context;
2561         char *mailbox;
2562
2563         if (ast_strlen_zero(mailbox_id)
2564                 || separate_mailbox(ast_strdupa(mailbox_id), &mailbox, &context)) {
2565                 return 0;
2566         }
2567
2568         if (ast_strlen_zero(folder) || !strcmp(folder, "INBOX")) {
2569                 return __messagecount(context, mailbox, "INBOX") + __messagecount(context, mailbox, "Urgent");
2570         } else {
2571                 return __messagecount(context, mailbox, folder);
2572         }
2573 }
2574
2575 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)
2576 {
2577         char *myserveremail = serveremail;
2578         char fn[PATH_MAX];
2579         char introfn[PATH_MAX];
2580         char mailbox[256];
2581         char *stringp;
2582         FILE *p = NULL;
2583         char tmp[80] = "/tmp/astmail-XXXXXX";
2584         long len;
2585         void *buf;
2586         int tempcopy = 0;
2587         STRING str;
2588         int ret; /* for better error checking */
2589         char *imap_flags = NIL;
2590         int msgcount;
2591         int box = NEW_FOLDER;
2592
2593         snprintf(mailbox, sizeof(mailbox), "%s@%s", vmu->mailbox, vmu->context);
2594         msgcount = messagecount(mailbox, "INBOX") + messagecount(mailbox, "Old");
2595
2596         /* Back out early if this is a greeting and we don't want to store greetings in IMAP */
2597         if (msgnum < 0) {
2598                 if(!imapgreetings) {
2599                         return 0;
2600                 } else {
2601                         box = GREETINGS_FOLDER;
2602                 }
2603         }
2604
2605         if (imap_check_limits(chan, vms, vmu, msgcount)) {
2606                 return -1;
2607         }
2608
2609         /* Set urgent flag for IMAP message */
2610         if (!ast_strlen_zero(flag) && !strcmp(flag, "Urgent")) {
2611                 ast_debug(3, "Setting message flag \\\\FLAGGED.\n");
2612                 imap_flags = "\\FLAGGED";
2613         }
2614
2615         /* Attach only the first format */
2616         fmt = ast_strdupa(fmt);
2617         stringp = fmt;
2618         strsep(&stringp, "|");
2619
2620         if (!ast_strlen_zero(vmu->serveremail))
2621                 myserveremail = vmu->serveremail;
2622
2623         if (msgnum > -1)
2624                 make_file(fn, sizeof(fn), dir, msgnum);
2625         else
2626                 ast_copy_string (fn, dir, sizeof(fn));
2627
2628         snprintf(introfn, sizeof(introfn), "%sintro", fn);
2629         if (ast_fileexists(introfn, NULL, NULL) <= 0) {
2630                 *introfn = '\0';
2631         }
2632
2633         if (ast_strlen_zero(vmu->email)) {
2634                 /* We need the vmu->email to be set when we call make_email_file, but
2635                  * if we keep it set, a duplicate e-mail will be created. So at the end
2636                  * of this function, we will revert back to an empty string if tempcopy
2637                  * is 1.
2638                  */
2639                 vmu->email = ast_strdup(vmu->imapuser);
2640                 tempcopy = 1;
2641         }
2642
2643         if (!strcmp(fmt, "wav49"))
2644                 fmt = "WAV";
2645         ast_debug(3, "Storing file '%s', format '%s'\n", fn, fmt);
2646
2647         /* Make a temporary file instead of piping directly to sendmail, in case the mail
2648            command hangs. */
2649         if (!(p = vm_mkftemp(tmp))) {
2650                 ast_log(AST_LOG_WARNING, "Unable to store '%s' (can't create temporary file)\n", fn);
2651                 if (tempcopy) {
2652                         ast_free(vmu->email);
2653                         vmu->email = NULL;
2654                 }
2655                 return -1;
2656         }
2657
2658         if (msgnum < 0 && imapgreetings) {
2659                 if ((ret = init_mailstream(vms, GREETINGS_FOLDER))) {
2660                         ast_log(AST_LOG_WARNING, "Unable to open mailstream.\n");
2661                         return -1;
2662                 }
2663                 imap_delete_old_greeting(fn, vms);
2664         }
2665
2666         make_email_file(p, myserveremail, vmu, msgnum, vmu->context, vmu->mailbox, "INBOX",
2667                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
2668                 S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL),
2669                 fn, introfn, fmt, duration, 1, chan, NULL, 1, flag, msg_id);
2670         /* read mail file to memory */
2671         len = ftell(p);
2672         rewind(p);
2673         if (!(buf = ast_malloc(len + 1))) {
2674                 ast_log(AST_LOG_ERROR, "Can't allocate %ld bytes to read message\n", len + 1);
2675                 fclose(p);
2676                 if (tempcopy)
2677                         *(vmu->email) = '\0';
2678                 return -1;
2679         }
2680         if (fread(buf, len, 1, p) < len) {
2681                 if (ferror(p)) {
2682                         ast_log(LOG_ERROR, "Short read while reading in mail file.\n");
2683                         return -1;
2684                 }
2685         }
2686         ((char *) buf)[len] = '\0';
2687         INIT(&str, mail_string, buf, len);
2688         ret = init_mailstream(vms, box);
2689         if (ret == 0) {
2690                 imap_mailbox_name(mailbox, sizeof(mailbox), vms, box, 1);
2691                 ast_mutex_lock(&vms->lock);
2692                 if(!mail_append_full(vms->mailstream, mailbox, imap_flags, NIL, &str))
2693                         ast_log(LOG_ERROR, "Error while sending the message to %s\n", mailbox);
2694                 ast_mutex_unlock(&vms->lock);
2695                 fclose(p);
2696                 unlink(tmp);
2697                 ast_free(buf);
2698         } else {
2699                 ast_log(LOG_ERROR, "Could not initialize mailstream for %s\n", mailbox);
2700                 fclose(p);
2701                 unlink(tmp);
2702                 ast_free(buf);
2703                 return -1;
2704         }
2705         ast_debug(3, "%s stored\n", fn);
2706
2707         if (tempcopy)
2708                 *(vmu->email) = '\0';
2709         inprocess_count(vmu->mailbox, vmu->context, -1);
2710         return 0;
2711
2712 }
2713
2714 /*!
2715  * \brief Gets the number of messages that exist in the inbox folder.
2716  * \param mailbox_context
2717  * \param newmsgs The variable that is updated with the count of new messages within this inbox.
2718  * \param oldmsgs The variable that is updated with the count of old messages within this inbox.
2719  * \param urgentmsgs The variable that is updated with the count of urgent messages within this inbox.
2720  * 
2721  * This method is used when IMAP backend is used.
2722  * Simultaneously determines the count of new,old, and urgent messages. The total messages would then be the sum of these three.
2723  *
2724  * \return zero on success, -1 on error.
2725  */
2726
2727 static int inboxcount2(const char *mailbox_context, int *urgentmsgs, int *newmsgs, int *oldmsgs)
2728 {
2729         char tmp[PATH_MAX] = "";
2730         char *mailboxnc;
2731         char *context;
2732         char *mb;
2733         char *cur;
2734         if (newmsgs)
2735                 *newmsgs = 0;
2736         if (oldmsgs)
2737                 *oldmsgs = 0;
2738         if (urgentmsgs)
2739                 *urgentmsgs = 0;
2740
2741         ast_debug(3, "Mailbox is set to %s\n", mailbox_context);
2742         /* If no mailbox, return immediately */
2743         if (ast_strlen_zero(mailbox_context))
2744                 return 0;
2745
2746         ast_copy_string(tmp, mailbox_context, sizeof(tmp));
2747         context = strchr(tmp, '@');
2748         if (strchr(mailbox_context, ',')) {
2749                 int tmpnew, tmpold, tmpurgent;
2750                 ast_copy_string(tmp, mailbox_context, sizeof(tmp));
2751                 mb = tmp;
2752                 while ((cur = strsep(&mb, ", "))) {
2753                         if (!ast_strlen_zero(cur)) {
2754                                 if (inboxcount2(cur, urgentmsgs ? &tmpurgent : NULL, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
2755                                         return -1;
2756                                 else {
2757                                         if (newmsgs)
2758                                                 *newmsgs += tmpnew; 
2759                                         if (oldmsgs)
2760                                                 *oldmsgs += tmpold;
2761                                         if (urgentmsgs)
2762                                                 *urgentmsgs += tmpurgent;
2763                                 }
2764                         }
2765                 }
2766                 return 0;
2767         }
2768         if (context) {
2769                 *context = '\0';
2770                 mailboxnc = tmp;
2771                 context++;
2772         } else {
2773                 context = "default";
2774                 mailboxnc = (char *) mailbox_context;
2775         }
2776
2777         if (newmsgs) {
2778                 struct ast_vm_user *vmu = find_user(NULL, context, mailboxnc);
2779                 if (!vmu) {
2780                         ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailboxnc, context);
2781                         return -1;
2782                 }
2783                 if ((*newmsgs = __messagecount(context, mailboxnc, vmu->imapfolder)) < 0) {
2784                         free_user(vmu);
2785                         return -1;
2786                 }
2787                 free_user(vmu);
2788         }
2789         if (oldmsgs) {
2790                 if ((*oldmsgs = __messagecount(context, mailboxnc, "Old")) < 0) {
2791                         return -1;
2792                 }
2793         }
2794         if (urgentmsgs) {
2795                 if ((*urgentmsgs = __messagecount(context, mailboxnc, "Urgent")) < 0) {
2796                         return -1;
2797                 }
2798         }
2799         return 0;
2800 }
2801
2802 /** 
2803  * \brief Determines if the given folder has messages.
2804  * \param mailbox The @ delimited string for user@context. If no context is found, uses 'default' for the context.
2805  * \param folder the folder to look in
2806  *
2807  * This function is used when the mailbox is stored in an IMAP back end.
2808  * This invokes the messagecount(). Here we are interested in the presence of messages (> 0) only, not the actual count.
2809  * \return 1 if the folder has one or more messages. zero otherwise.
2810  */
2811
2812 static int has_voicemail(const char *mailbox, const char *folder)
2813 {
2814         char tmp[256], *tmp2, *box, *context;
2815         ast_copy_string(tmp, mailbox, sizeof(tmp));
2816         tmp2 = tmp;
2817         if (strchr(tmp2, ',') || strchr(tmp2, '&')) {
2818                 while ((box = strsep(&tmp2, ",&"))) {
2819                         if (!ast_strlen_zero(box)) {
2820                                 if (has_voicemail(box, folder)) {
2821                                         return 1;
2822                                 }
2823                         }
2824                 }
2825         }
2826         if ((context = strchr(tmp, '@'))) {
2827                 *context++ = '\0';
2828         } else {
2829                 context = "default";
2830         }
2831         return __messagecount(context, tmp, folder) ? 1 : 0;
2832 }
2833
2834 /*!
2835  * \brief Copies a message from one mailbox to another.
2836  * \param chan
2837  * \param vmu
2838  * \param imbox
2839  * \param msgnum
2840  * \param duration
2841  * \param recip
2842  * \param fmt
2843  * \param dir
2844  *
2845  * This works with IMAP storage based mailboxes.
2846  *
2847  * \return zero on success, -1 on error.
2848  */
2849 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)
2850 {
2851         struct vm_state *sendvms = NULL;
2852         char messagestring[10]; /*I guess this could be a problem if someone has more than 999999999 messages...*/
2853         if (msgnum >= recip->maxmsg) {
2854                 ast_log(LOG_WARNING, "Unable to copy mail, mailbox %s is full\n", recip->mailbox);
2855                 return -1;
2856         }
2857         if (!(sendvms = get_vm_state_by_imapuser(vmu->imapuser, 0))) {
2858                 ast_log(LOG_ERROR, "Couldn't get vm_state for originator's mailbox!!\n");
2859                 return -1;
2860         }
2861         if (!get_vm_state_by_imapuser(recip->imapuser, 0)) {
2862                 ast_log(LOG_ERROR, "Couldn't get vm_state for destination mailbox!\n");
2863                 return -1;
2864         }
2865         snprintf(messagestring, sizeof(messagestring), "%ld", sendvms->msgArray[msgnum]);
2866         ast_mutex_lock(&sendvms->lock);
2867         if ((mail_copy(sendvms->mailstream, messagestring, (char *) mbox(vmu, imbox)) == T)) {
2868                 ast_mutex_unlock(&sendvms->lock);
2869                 return 0;
2870         }
2871         ast_mutex_unlock(&sendvms->lock);
2872         ast_log(LOG_WARNING, "Unable to copy message from mailbox %s to mailbox %s\n", vmu->mailbox, recip->mailbox);
2873         return -1;
2874 }
2875
2876 static void imap_mailbox_name(char *spec, size_t len, struct vm_state *vms, int box, int use_folder)
2877 {
2878         char tmp[256], *t = tmp;
2879         size_t left = sizeof(tmp);
2880         
2881         if (box == OLD_FOLDER) {
2882                 ast_copy_string(vms->curbox, mbox(NULL, NEW_FOLDER), sizeof(vms->curbox));
2883         } else {
2884                 ast_copy_string(vms->curbox, mbox(NULL, box), sizeof(vms->curbox));
2885         }
2886
2887         if (box == NEW_FOLDER) {
2888                 ast_copy_string(vms->vmbox, "vm-INBOX", sizeof(vms->vmbox));
2889         } else {
2890                 snprintf(vms->vmbox, sizeof(vms->vmbox), "vm-%s", mbox(NULL, box));
2891         }
2892
2893         /* Build up server information */
2894         ast_build_string(&t, &left, "{%s:%s/imap", S_OR(vms->imapserver, imapserver), S_OR(vms->imapport, imapport));
2895
2896         /* Add authentication user if present */
2897         if (!ast_strlen_zero(authuser))
2898                 ast_build_string(&t, &left, "/authuser=%s", authuser);
2899
2900         /* Add flags if present */
2901         if (!ast_strlen_zero(imapflags) || !(ast_strlen_zero(vms->imapflags))) {
2902                 ast_build_string(&t, &left, "/%s", S_OR(vms->imapflags, imapflags));
2903         }
2904
2905         /* End with username */
2906 #if 1
2907         ast_build_string(&t, &left, "/user=%s}", vms->imapuser);
2908 #else
2909         ast_build_string(&t, &left, "/user=%s/novalidate-cert}", vms->imapuser);
2910 #endif
2911         if (box == NEW_FOLDER || box == OLD_FOLDER)
2912                 snprintf(spec, len, "%s%s", tmp, use_folder? vms->imapfolder: "INBOX");
2913         else if (box == GREETINGS_FOLDER)
2914                 snprintf(spec, len, "%s%s", tmp, greetingfolder);
2915         else {  /* Other folders such as Friends, Family, etc... */
2916                 if (!ast_strlen_zero(imapparentfolder)) {
2917                         /* imapparentfolder would typically be set to INBOX */
2918                         snprintf(spec, len, "%s%s%c%s", tmp, imapparentfolder, delimiter, mbox(NULL, box));
2919                 } else {
2920                         snprintf(spec, len, "%s%s", tmp, mbox(NULL, box));
2921                 }
2922         }
2923 }
2924
2925 static int init_mailstream(struct vm_state *vms, int box)
2926 {
2927         MAILSTREAM *stream = NIL;
2928         long debug;
2929         char tmp[256];
2930
2931         if (!vms) {
2932                 ast_log(LOG_ERROR, "vm_state is NULL!\n");
2933                 return -1;
2934         }
2935         ast_debug(3, "vm_state user is:%s\n", vms->imapuser);
2936         if (vms->mailstream == NIL || !vms->mailstream) {
2937                 ast_debug(1, "mailstream not set.\n");
2938         } else {
2939                 stream = vms->mailstream;
2940         }
2941         /* debug = T;  user wants protocol telemetry? */
2942         debug = NIL;  /* NO protocol telemetry? */
2943
2944         if (delimiter == '\0') {                /* did not probe the server yet */
2945                 char *cp;
2946 #ifdef USE_SYSTEM_IMAP
2947 #include <imap/linkage.c>
2948 #elif defined(USE_SYSTEM_CCLIENT)
2949 #include <c-client/linkage.c>
2950 #else
2951 #include "linkage.c"
2952 #endif
2953                 /* Connect to INBOX first to get folders delimiter */
2954                 imap_mailbox_name(tmp, sizeof(tmp), vms, 0, 1);
2955                 ast_mutex_lock(&vms->lock);
2956                 stream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
2957                 ast_mutex_unlock(&vms->lock);
2958                 if (stream == NIL) {
2959                         ast_log(LOG_ERROR, "Can't connect to imap server %s\n", tmp);
2960                         return -1;
2961                 }
2962                 get_mailbox_delimiter(vms, stream);
2963                 /* update delimiter in imapfolder */
2964                 for (cp = vms->imapfolder; *cp; cp++)
2965                         if (*cp == '/')
2966                                 *cp = delimiter;
2967         }
2968         /* Now connect to the target folder */
2969         imap_mailbox_name(tmp, sizeof(tmp), vms, box, 1);
2970         ast_debug(3, "Before mail_open, server: %s, box:%d\n", tmp, box);
2971         ast_mutex_lock(&vms->lock);
2972         vms->mailstream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
2973         ast_mutex_unlock(&vms->lock);
2974         if (vms->mailstream == NIL) {
2975                 return -1;
2976         } else {
2977                 return 0;
2978         }
2979 }
2980
2981 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box)
2982 {
2983         SEARCHPGM *pgm;
2984         SEARCHHEADER *hdr;
2985         int urgent = 0;
2986
2987         /* If Urgent, then look at INBOX */
2988         if (box == 11) {
2989                 box = NEW_FOLDER;
2990                 urgent = 1;
2991         }
2992
2993         ast_copy_string(vms->imapuser, vmu->imapuser, sizeof(vms->imapuser));
2994         ast_copy_string(vms->imapfolder, vmu->imapfolder, sizeof(vms->imapfolder));
2995         ast_copy_string(vms->imapserver, vmu->imapserver, sizeof(vms->imapserver));
2996         ast_copy_string(vms->imapport, vmu->imapport, sizeof(vms->imapport));
2997         ast_copy_string(vms->imapflags, vmu->imapflags, sizeof(vms->imapflags));
2998         vms->imapversion = vmu->imapversion;
2999         ast_debug(3, "Before init_mailstream, user is %s\n", vmu->imapuser);
3000
3001         if (init_mailstream(vms, box) || !vms->mailstream) {
3002                 ast_log(AST_LOG_ERROR, "Could not initialize mailstream\n");
3003                 return -1;
3004         }
3005
3006         create_dirpath(vms->curdir, sizeof(vms->curdir), vmu->context, vms->username, vms->curbox);
3007
3008         /* Check Quota */
3009         if  (box == 0)  {
3010                 ast_debug(3, "Mailbox name set to: %s, about to check quotas\n", mbox(vmu, box));
3011                 check_quota(vms, (char *) mbox(vmu, box));
3012         }
3013
3014         ast_mutex_lock(&vms->lock);
3015         pgm = mail_newsearchpgm();
3016
3017         /* Check IMAP folder for Asterisk messages only... */
3018         hdr = mail_newsearchheader("X-Asterisk-VM-Extension", (!ast_strlen_zero(vmu->imapvmshareid) ? vmu->imapvmshareid : vmu->mailbox));
3019         hdr->next = mail_newsearchheader("X-Asterisk-VM-Context", vmu->context);
3020         pgm->header = hdr;
3021         pgm->deleted = 0;
3022         pgm->undeleted = 1;
3023
3024         /* if box = NEW_FOLDER, check for new, if box = OLD_FOLDER, check for read */
3025         if (box == NEW_FOLDER && urgent == 1) {
3026                 pgm->unseen = 1;
3027                 pgm->seen = 0;
3028                 pgm->flagged = 1;
3029                 pgm->unflagged = 0;
3030         } else if (box == NEW_FOLDER && urgent == 0) {
3031                 pgm->unseen = 1;
3032                 pgm->seen = 0;
3033                 pgm->flagged = 0;
3034                 pgm->unflagged = 1;
3035         } else if (box == OLD_FOLDER) {
3036                 pgm->seen = 1;
3037                 pgm->unseen = 0;
3038         }
3039
3040         ast_debug(3, "Before mail_search_full, user is %s\n", vmu->imapuser);
3041
3042         vms->vmArrayIndex = 0;
3043         mail_search_full (vms->mailstream, NULL, pgm, NIL);
3044         vms->lastmsg = vms->vmArrayIndex - 1;
3045         mail_free_searchpgm(&pgm);
3046         /* Since IMAP storage actually stores both old and new messages in the same IMAP folder,
3047          * ensure to allocate enough space to account for all of them. Warn if old messages
3048          * have not been checked first as that is required.
3049          */
3050         if (box == 0 && !vms->dh_arraysize) {
3051                 ast_log(LOG_WARNING, "The code expects the old messages to be checked first, fix the code.\n");
3052         }
3053         if (vm_allocate_dh(vms, vmu, box == 0 ? vms->vmArrayIndex + vms->oldmessages : vms->lastmsg)) {
3054                 ast_mutex_unlock(&vms->lock);
3055                 return -1;
3056         }
3057
3058         ast_mutex_unlock(&vms->lock);
3059         return 0;
3060 }
3061
3062 static void write_file(char *filename, char *buffer, unsigned long len)
3063 {
3064         FILE *output;
3065
3066         output = fopen (filename, "w");
3067         if (fwrite(buffer, len, 1, output) != 1) {
3068                 if (ferror(output)) {
3069                         ast_log(LOG_ERROR, "Short write while writing e-mail body: %s.\n", strerror(errno));
3070                 }
3071         }
3072         fclose (output);
3073 }
3074
3075 static void update_messages_by_imapuser(const char *user, unsigned long number)
3076 {
3077         struct vm_state *vms = get_vm_state_by_imapuser(user, 1);
3078
3079         if (!vms && !(vms = get_vm_state_by_imapuser(user, 0))) {
3080                 return;
3081         }
3082
3083         ast_debug(3, "saving mailbox message number %lu as message %d. Interactive set to %d\n", number, vms->vmArrayIndex, vms->interactive);
3084         vms->msgArray[vms->vmArrayIndex++] = number;
3085 }
3086
3087 void mm_searched(MAILSTREAM *stream, unsigned long number)
3088 {
3089         char *mailbox = stream->mailbox, buf[1024] = "", *user;
3090
3091         if (!(user = get_user_by_mailbox(mailbox, buf, sizeof(buf))))
3092                 return;
3093
3094         update_messages_by_imapuser(user, number);
3095 }
3096
3097 static struct ast_vm_user *find_user_realtime_imapuser(const char *imapuser)
3098 {
3099         struct ast_variable *var;
3100         struct ast_vm_user *vmu;
3101
3102         vmu = ast_calloc(1, sizeof *vmu);
3103         if (!vmu)
3104                 return NULL;
3105
3106         populate_defaults(vmu);
3107         ast_set_flag(vmu, VM_ALLOCED);
3108
3109         var = ast_load_realtime("voicemail", "imapuser", imapuser, NULL);
3110         if (var) {
3111                 apply_options_full(vmu, var);
3112                 ast_variables_destroy(var);
3113                 return vmu;
3114         } else {
3115                 ast_free(vmu);
3116                 return NULL;
3117         }
3118 }
3119
3120 /* Interfaces to C-client */
3121
3122 void mm_exists(MAILSTREAM * stream, unsigned long number)
3123 {
3124         /* mail_ping will callback here if new mail! */
3125         ast_debug(4, "Entering EXISTS callback for message %ld\n", number);
3126         if (number == 0) return;
3127         set_update(stream);
3128 }
3129
3130
3131 void mm_expunged(MAILSTREAM * stream, unsigned long number)
3132 {
3133         /* mail_ping will callback here if expunged mail! */
3134         ast_debug(4, "Entering EXPUNGE callback for message %ld\n", number);
3135         if (number == 0) return;
3136         set_update(stream);
3137 }
3138
3139
3140 void mm_flags(MAILSTREAM * stream, unsigned long number)
3141 {
3142         /* mail_ping will callback here if read mail! */
3143         ast_debug(4, "Entering FLAGS callback for message %ld\n", number);
3144         if (number == 0) return;
3145         set_update(stream);
3146 }
3147
3148
3149 void mm_notify(MAILSTREAM * stream, char *string, long errflg)
3150 {
3151         ast_debug(5, "Entering NOTIFY callback, errflag is %ld, string is %s\n", errflg, string);
3152         mm_log (string, errflg);
3153 }
3154
3155
3156 void mm_list(MAILSTREAM * stream, int delim, char *mailbox, long attributes)
3157 {
3158         if (delimiter == '\0') {
3159                 delimiter = delim;
3160         }
3161
3162         ast_debug(5, "Delimiter set to %c and mailbox %s\n", delim, mailbox);
3163         if (attributes & LATT_NOINFERIORS)
3164                 ast_debug(5, "no inferiors\n");
3165         if (attributes & LATT_NOSELECT)
3166                 ast_debug(5, "no select\n");
3167         if (attributes & LATT_MARKED)
3168                 ast_debug(5, "marked\n");
3169         if (attributes & LATT_UNMARKED)
3170                 ast_debug(5, "unmarked\n");
3171 }
3172
3173
3174 void mm_lsub(MAILSTREAM * stream, int delim, char *mailbox, long attributes)
3175 {
3176         ast_debug(5, "Delimiter set to %c and mailbox %s\n", delim, mailbox);
3177         if (attributes & LATT_NOINFERIORS)
3178                 ast_debug(5, "no inferiors\n");
3179         if (attributes & LATT_NOSELECT)
3180                 ast_debug(5, "no select\n");
3181         if (attributes & LATT_MARKED)
3182                 ast_debug(5, "marked\n");
3183         if (attributes & LATT_UNMARKED)
3184                 ast_debug(5, "unmarked\n");
3185 }
3186
3187
3188 void mm_status(MAILSTREAM * stream, char *mailbox, MAILSTATUS * status)
3189 {
3190         ast_log(AST_LOG_NOTICE, " Mailbox %s", mailbox);
3191         if (status->flags & SA_MESSAGES)
3192                 ast_log(AST_LOG_NOTICE, ", %lu messages", status->messages);
3193         if (status->flags & SA_RECENT)
3194                 ast_log(AST_LOG_NOTICE, ", %lu recent", status->recent);
3195         if (status->flags & SA_UNSEEN)
3196                 ast_log(AST_LOG_NOTICE, ", %lu unseen", status->unseen);
3197         if (status->flags & SA_UIDVALIDITY)
3198                 ast_log(AST_LOG_NOTICE, ", %lu UID validity", status->uidvalidity);
3199         if (status->flags & SA_UIDNEXT)
3200                 ast_log(AST_LOG_NOTICE, ", %lu next UID", status->uidnext);
3201         ast_log(AST_LOG_NOTICE, "\n");
3202 }
3203
3204
3205 void mm_log(char *string, long errflg)
3206 {
3207         switch ((short) errflg) {
3208                 case NIL:
3209                         ast_debug(1, "IMAP Info: %s\n", string);
3210                         break;
3211                 case PARSE:
3212                 case WARN:
3213                         ast_log(AST_LOG_WARNING, "IMAP Warning: %s\n", string);
3214                         break;
3215                 case ERROR:
3216                         ast_log(AST_LOG_ERROR, "IMAP Error: %s\n", string);
3217                         break;
3218         }
3219 }
3220
3221
3222 void mm_dlog(char *string)
3223 {
3224         ast_log(AST_LOG_NOTICE, "%s\n", string);
3225 }
3226
3227
3228 void mm_login(NETMBX * mb, char *user, char *pwd, long trial)
3229 {
3230         struct ast_vm_user *vmu;
3231
3232         ast_debug(4, "Entering callback mm_login\n");
3233
3234         ast_copy_string(user, mb->user, MAILTMPLEN);
3235
3236         /* We should only do this when necessary */
3237         if (!ast_strlen_zero(authpassword)) {
3238                 ast_copy_string(pwd, authpassword, MAILTMPLEN);
3239         } else {
3240                 AST_LIST_TRAVERSE(&users, vmu, list) {
3241                         if (!strcasecmp(mb->user, vmu->imapuser)) {
3242                                 ast_copy_string(pwd, vmu->imappassword, MAILTMPLEN);
3243                                 break;
3244                         }
3245                 }
3246                 if (!vmu) {
3247                         if ((vmu = find_user_realtime_imapuser(mb->user))) {
3248                                 ast_copy_string(pwd, vmu->imappassword, MAILTMPLEN);
3249                                 free_user(vmu);
3250                         }
3251                 }
3252         }
3253 }
3254
3255
3256 void mm_critical(MAILSTREAM * stream)
3257 {
3258 }
3259
3260
3261 void mm_nocritical(MAILSTREAM * stream)
3262 {
3263 }
3264
3265
3266 long mm_diskerror(MAILSTREAM * stream, long errcode, long serious)
3267 {
3268         kill (getpid (), SIGSTOP);
3269         return NIL;
3270 }
3271
3272
3273 void mm_fatal(char *string)
3274 {
3275         ast_log(AST_LOG_ERROR, "IMAP access FATAL error: %s\n", string);
3276 }
3277
3278 /* C-client callback to handle quota */
3279 static void mm_parsequota(MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pquota)
3280 {
3281         struct vm_state *vms;
3282         char *mailbox = stream->mailbox, *user;
3283         char buf[1024] = "";
3284         unsigned long usage = 0, limit = 0;
3285
3286         while (pquota) {
3287                 usage = pquota->usage;
3288                 limit = pquota->limit;
3289                 pquota = pquota->next;
3290         }
3291
3292         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)))) {
3293                 ast_log(AST_LOG_ERROR, "No state found.\n");
3294                 return;
3295         }
3296
3297         ast_debug(3, "User %s usage is %lu, limit is %lu\n", user, usage, limit);
3298
3299         vms->quota_usage = usage;
3300         vms->quota_limit = limit;
3301 }
3302
3303 static char *get_header_by_tag(char *header, char *tag, char *buf, size_t len)
3304 {
3305         char *start, *eol_pnt;
3306         int taglen;
3307
3308         if (ast_strlen_zero(header) || ast_strlen_zero(tag))
3309                 return NULL;
3310
3311         taglen = strlen(tag) + 1;
3312         if (taglen < 1)
3313                 return NULL;
3314
3315         if (!(start = strstr(header, tag)))
3316                 return NULL;
3317
3318         /* Since we can be called multiple times we should clear our buffer */
3319         memset(buf, 0, len);
3320
3321         ast_copy_string(buf, start+taglen, len);
3322         if ((eol_pnt = strchr(buf,'\r')) || (eol_pnt = strchr(buf,'\n')))
3323                 *eol_pnt = '\0';
3324         return buf;
3325 }
3326
3327 static char *get_user_by_mailbox(char *mailbox, char *buf, size_t len)
3328 {
3329         char *start, *eol_pnt, *quote;
3330
3331         if (ast_strlen_zero(mailbox))
3332                 return NULL;
3333
3334         if (!(start = strstr(mailbox, "/user=")))
3335                 return NULL;
3336
3337         ast_copy_string(buf, start+6, len);
3338
3339         if (!(quote = strchr(buf, '"'))) {
3340                 if ((eol_pnt = strchr(buf, '/')) || (eol_pnt = strchr(buf, '}'))) {
3341                         *eol_pnt = '\0';
3342                 }
3343                 return buf;
3344         } else {
3345                 if ((eol_pnt = strchr(quote + 1, '"'))) {
3346                         *eol_pnt = '\0';
3347                 }
3348                 return quote + 1;
3349         }
3350 }
3351
3352 static struct vm_state *create_vm_state_from_user(struct ast_vm_user *vmu)
3353 {
3354         struct vm_state *vms_p;
3355
3356         pthread_once(&ts_vmstate.once, ts_vmstate.key_init);
3357         if ((vms_p = pthread_getspecific(ts_vmstate.key)) && !strcmp(vms_p->imapuser, vmu->imapuser) && !strcmp(vms_p->username, vmu->mailbox)) {
3358                 return vms_p;
3359         }
3360         ast_debug(5, "Adding new vmstate for %s\n", vmu->imapuser);
3361         if (!(vms_p = ast_calloc(1, sizeof(*vms_p))))
3362                 return NULL;
3363         ast_copy_string(vms_p->imapuser, vmu->imapuser, sizeof(vms_p->imapuser));
3364         ast_copy_string(vms_p->imapfolder, vmu->imapfolder, sizeof(vms_p->imapfolder));
3365         ast_copy_string(vms_p->imapserver, vmu->imapserver, sizeof(vms_p->imapserver));
3366         ast_copy_string(vms_p->imapport, vmu->imapport, sizeof(vms_p->imapport));
3367         ast_copy_string(vms_p->imapflags, vmu->imapflags, sizeof(vms_p->imapflags));
3368         ast_copy_string(vms_p->username, vmu->mailbox, sizeof(vms_p->username)); /* save for access from interactive entry point */
3369         ast_copy_string(vms_p->context, vmu->context, sizeof(vms_p->context));
3370         vms_p->mailstream = NIL; /* save for access from interactive entry point */
3371         vms_p->imapversion = vmu->imapversion;
3372         ast_debug(5, "Copied %s to %s\n", vmu->imapuser, vms_p->imapuser);
3373         vms_p->updated = 1;
3374         /* set mailbox to INBOX! */
3375         ast_copy_string(vms_p->curbox, mbox(vmu, 0), sizeof(vms_p->curbox));
3376         init_vm_state(vms_p);
3377         vmstate_insert(vms_p);
3378         return vms_p;
3379 }
3380
3381 static struct vm_state *get_vm_state_by_imapuser(const char *user, int interactive)
3382 {
3383         struct vmstate *vlist = NULL;
3384
3385         if (interactive) {
3386                 struct vm_state *vms;
3387                 pthread_once(&ts_vmstate.once, ts_vmstate.key_init);
3388                 vms = pthread_getspecific(ts_vmstate.key);
3389                 return vms;
3390         }
3391
3392         AST_LIST_LOCK(&vmstates);
3393         AST_LIST_TRAVERSE(&vmstates, vlist, list) {
3394                 if (!vlist->vms) {
3395                         ast_debug(3, "error: vms is NULL for %s\n", user);
3396                         continue;
3397                 }
3398                 if (vlist->vms->imapversion != imapversion) {
3399                         continue;
3400                 }
3401                 if (!vlist->vms->imapuser) {
3402                         ast_debug(3, "error: imapuser is NULL for %s\n", user);
3403                         continue;
3404                 }
3405
3406                 if (!strcmp(vlist->vms->imapuser, user) && (interactive == 2 || vlist->vms->interactive == interactive)) {
3407                         AST_LIST_UNLOCK(&vmstates);
3408                         return vlist->vms;
3409                 }
3410         }
3411         AST_LIST_UNLOCK(&vmstates);
3412
3413         ast_debug(3, "%s not found in vmstates\n", user);
3414
3415         return NULL;
3416 }
3417
3418 static struct vm_state *get_vm_state_by_mailbox(const char *mailbox, const char *context, int interactive)
3419 {
3420
3421         struct vmstate *vlist = NULL;
3422         const char *local_context = S_OR(context, "default");
3423
3424         if (interactive) {
3425                 struct vm_state *vms;
3426                 pthread_once(&ts_vmstate.once, ts_vmstate.key_init);
3427                 vms = pthread_getspecific(ts_vmstate.key);
3428                 return vms;
3429         }
3430
3431         AST_LIST_LOCK(&vmstates);
3432         AST_LIST_TRAVERSE(&vmstates, vlist, list) {
3433                 if (!vlist->vms) {
3434                         ast_debug(3, "error: vms is NULL for %s\n", mailbox);
3435                         continue;
3436                 }
3437                 if (vlist->vms->imapversion != imapversion) {
3438                         continue;
3439                 }
3440                 if (!vlist->vms->username || !vlist->vms->context) {
3441                         ast_debug(3, "error: username is NULL for %s\n", mailbox);
3442                         continue;
3443                 }
3444
3445                 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);
3446
3447                 if (!strcmp(vlist->vms->username, mailbox) && !strcmp(vlist->vms->context, local_context) && vlist->vms->interactive == interactive) {
3448                         ast_debug(3, "Found it!\n");
3449                         AST_LIST_UNLOCK(&vmstates);
3450                         return vlist->vms;
3451                 }
3452         }
3453         AST_LIST_UNLOCK(&vmstates);
3454
3455         ast_debug(3, "%s not found in vmstates\n", mailbox);
3456
3457         return NULL;
3458 }
3459
3460 static void vmstate_insert(struct vm_state *vms)
3461 {
3462         struct vmstate *v;
3463         struct vm_state *altvms;
3464
3465         /* If interactive, it probably already exists, and we should
3466            use the one we already have since it is more up to date.
3467            We can compare the username to find the duplicate */
3468         if (vms->interactive == 1) {
3469                 altvms = get_vm_state_by_mailbox(vms->username, vms->context, 0);
3470                 if (altvms) {
3471                         ast_debug(3, "Duplicate mailbox %s, copying message info...\n", vms->username);
3472                         vms->newmessages = altvms->newmessages;
3473                         vms->oldmessages = altvms->oldmessages;
3474                         vms->vmArrayIndex = altvms->vmArrayIndex;
3475                         vms->lastmsg = altvms->lastmsg;
3476                         vms->curmsg = altvms->curmsg;
3477                         /* get a pointer to the persistent store */
3478                         vms->persist_vms = altvms;
3479                         /* Reuse the mailstream? */
3480 #ifdef REALLY_FAST_EVEN_IF_IT_MEANS_RESOURCE_LEAKS
3481                         vms->mailstream = altvms->mailstream;
3482 #else
3483                         vms->mailstream = NIL;
3484 #endif
3485                 }
3486                 return;
3487         }
3488
3489         if (!(v = ast_calloc(1, sizeof(*v))))
3490                 return;
3491
3492         v->vms = vms;
3493
3494         ast_debug(3, "Inserting vm_state for user:%s, mailbox %s\n", vms->imapuser, vms->username);
3495
3496         AST_LIST_LOCK(&vmstates);
3497         AST_LIST_INSERT_TAIL(&vmstates, v, list);
3498         AST_LIST_UNLOCK(&vmstates);
3499 }
3500
3501 static void vmstate_delete(struct vm_state *vms)
3502 {
3503         struct vmstate *vc = NULL;
3504         struct vm_state *altvms = NULL;
3505
3506         /* If interactive, we should copy pertinent info
3507            back to the persistent state (to make update immediate) */
3508         if (vms->interactive == 1 && (altvms = vms->persist_vms)) {
3509                 ast_debug(3, "Duplicate mailbox %s, copying message info...\n", vms->username);
3510                 altvms->newmessages = vms->newmessages;
3511                 altvms->oldmessages = vms->oldmessages;
3512                 altvms->updated = 1;
3513                 vms->mailstream = mail_close(vms->mailstream);
3514
3515                 /* Interactive states are not stored within the persistent list */
3516                 return;
3517         }
3518
3519         ast_debug(3, "Removing vm_state for user:%s, mailbox %s\n", vms->imapuser, vms->username);
3520
3521         AST_LIST_LOCK(&vmstates);
3522         AST_LIST_TRAVERSE_SAFE_BEGIN(&vmstates, vc, list) {
3523                 if (vc->vms == vms) {
3524                         AST_LIST_REMOVE_CURRENT(list);
3525                         break;
3526                 }
3527         }
3528         AST_LIST_TRAVERSE_SAFE_END
3529         AST_LIST_UNLOCK(&vmstates);
3530         
3531         if (vc) {
3532                 ast_mutex_destroy(&vc->vms->lock);
3533                 ast_free(vc);
3534         }
3535         else
3536                 ast_log(AST_LOG_ERROR, "No vmstate found for user:%s, mailbox %s\n", vms->imapuser, vms->username);
3537 }
3538
3539 static void set_update(MAILSTREAM * stream)
3540 {
3541         struct vm_state *vms;
3542         char *mailbox = stream->mailbox, *user;