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