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