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