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