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