5e342bda08335f696880791f2bc358c1321c60ee
[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>res_adsi</use>
42         <use>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>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         if (!ast_strlen_zero(ext_pass_check_cmd)) {
1219                 char cmd[255], buf[255];
1220
1221                 ast_debug(1, "Verify password policies for %s\n", password);
1222
1223                 snprintf(cmd, sizeof(cmd), "%s %s %s %s %s", ext_pass_check_cmd, vmu->mailbox, vmu->context, vmu->password, password);
1224                 if (vm_check_password_shell(cmd, buf, sizeof(buf))) {
1225                         ast_debug(5, "Result: %s\n", buf);
1226                         if (!strncasecmp(buf, "VALID", 5)) {
1227                                 ast_debug(3, "Passed password check: '%s'\n", buf);
1228                                 return 0;
1229                         } else if (!strncasecmp(buf, "FAILURE", 7)) {
1230                                 ast_log(AST_LOG_WARNING, "Unable to execute password validation script: '%s'.\n", buf);
1231                                 return 0;
1232                         } else {
1233                                 ast_log(AST_LOG_NOTICE, "Password doesn't match policies for user %s %s\n", vmu->mailbox, password);
1234                                 return 1;
1235                         }
1236                 }
1237         }
1238         return 0;
1239 }
1240
1241 /*! 
1242  * \brief Performs a change of the voicemail passowrd in the realtime engine.
1243  * \param vmu The voicemail user to change the password for.
1244  * \param password The new value to be set to the password for this user.
1245  * 
1246  * This only works if there is a realtime engine configured.
1247  * This is called from the (top level) vm_change_password.
1248  *
1249  * \return zero on success, -1 on error.
1250  */
1251 static int change_password_realtime(struct ast_vm_user *vmu, const char *password)
1252 {
1253         int res = -1;
1254         if (!strcmp(vmu->password, password)) {
1255                 /* No change (but an update would return 0 rows updated, so we opt out here) */
1256                 return 0;
1257         }
1258
1259         if (strlen(password) > 10) {
1260                 ast_realtime_require_field("voicemail", "password", RQ_CHAR, strlen(password), SENTINEL);
1261         }
1262         if (ast_update2_realtime("voicemail", "context", vmu->context, "mailbox", vmu->mailbox, SENTINEL, "password", password, SENTINEL) > 0) {
1263                 ast_copy_string(vmu->password, password, sizeof(vmu->password));
1264                 res = 0;
1265         }
1266         return res;
1267 }
1268
1269 /*!
1270  * \brief Destructively Parse options and apply.
1271  */
1272 static void apply_options(struct ast_vm_user *vmu, const char *options)
1273 {       
1274         char *stringp;
1275         char *s;
1276         char *var, *value;
1277         stringp = ast_strdupa(options);
1278         while ((s = strsep(&stringp, "|"))) {
1279                 value = s;
1280                 if ((var = strsep(&value, "=")) && value) {
1281                         apply_option(vmu, var, value);
1282                 }
1283         }       
1284 }
1285
1286 /*!
1287  * \brief Loads the options specific to a voicemail user.
1288  * 
1289  * This is called when a vm_user structure is being set up, such as from load_options.
1290  */
1291 static void apply_options_full(struct ast_vm_user *retval, struct ast_variable *var)
1292 {
1293         for (; var; var = var->next) {
1294                 if (!strcasecmp(var->name, "vmsecret")) {
1295                         ast_copy_string(retval->password, var->value, sizeof(retval->password));
1296                 } else if (!strcasecmp(var->name, "secret") || !strcasecmp(var->name, "password")) { /* don't overwrite vmsecret if it exists */
1297                         if (ast_strlen_zero(retval->password))
1298                                 ast_copy_string(retval->password, var->value, sizeof(retval->password));
1299                 } else if (!strcasecmp(var->name, "uniqueid")) {
1300                         ast_copy_string(retval->uniqueid, var->value, sizeof(retval->uniqueid));
1301                 } else if (!strcasecmp(var->name, "pager")) {
1302                         ast_copy_string(retval->pager, var->value, sizeof(retval->pager));
1303                 } else if (!strcasecmp(var->name, "email")) {
1304                         ast_copy_string(retval->email, var->value, sizeof(retval->email));
1305                 } else if (!strcasecmp(var->name, "fullname")) {
1306                         ast_copy_string(retval->fullname, var->value, sizeof(retval->fullname));
1307                 } else if (!strcasecmp(var->name, "context")) {
1308                         ast_copy_string(retval->context, var->value, sizeof(retval->context));
1309                 } else if (!strcasecmp(var->name, "emailsubject")) {
1310                         retval->emailsubject = ast_strdup(var->value);
1311                 } else if (!strcasecmp(var->name, "emailbody")) {
1312                         retval->emailbody = ast_strdup(var->value);
1313 #ifdef IMAP_STORAGE
1314                 } else if (!strcasecmp(var->name, "imapuser")) {
1315                         ast_copy_string(retval->imapuser, var->value, sizeof(retval->imapuser));
1316                         retval->imapversion = imapversion;
1317                 } else if (!strcasecmp(var->name, "imappassword") || !strcasecmp(var->name, "imapsecret")) {
1318                         ast_copy_string(retval->imappassword, var->value, sizeof(retval->imappassword));
1319                         retval->imapversion = imapversion;
1320                 } else if (!strcasecmp(var->name, "imapfolder")) {
1321                         ast_copy_string(retval->imapfolder, var->value, sizeof(retval->imapfolder));
1322                 } else if (!strcasecmp(var->name, "imapvmshareid")) {
1323                         ast_copy_string(retval->imapvmshareid, var->value, sizeof(retval->imapvmshareid));
1324                         retval->imapversion = imapversion;
1325 #endif
1326                 } else
1327                         apply_option(retval, var->name, var->value);
1328         }
1329 }
1330
1331 /*!
1332  * \brief Determines if a DTMF key entered is valid.
1333  * \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.
1334  *
1335  * Tests the character entered against the set of valid DTMF characters. 
1336  * \return 1 if the character entered is a valid DTMF digit, 0 if the character is invalid.
1337  */
1338 static int is_valid_dtmf(const char *key)
1339 {
1340         int i;
1341         char *local_key = ast_strdupa(key);
1342
1343         for (i = 0; i < strlen(key); ++i) {
1344                 if (!strchr(VALID_DTMF, *local_key)) {
1345                         ast_log(AST_LOG_WARNING, "Invalid DTMF key \"%c\" used in voicemail configuration file\n", *local_key);
1346                         return 0;
1347                 }
1348                 local_key++;
1349         }
1350         return 1;
1351 }
1352
1353 /*!
1354  * \brief Finds a voicemail user from the realtime engine.
1355  * \param ivm
1356  * \param context
1357  * \param mailbox
1358  *
1359  * 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.
1360  *
1361  * \return The ast_vm_user structure for the user that was found.
1362  */
1363 static struct ast_vm_user *find_user_realtime(struct ast_vm_user *ivm, const char *context, const char *mailbox)
1364 {
1365         struct ast_variable *var;
1366         struct ast_vm_user *retval;
1367
1368         if ((retval = (ivm ? ivm : ast_calloc(1, sizeof(*retval))))) {
1369                 if (!ivm)
1370                         ast_set_flag(retval, VM_ALLOCED);       
1371                 else
1372                         memset(retval, 0, sizeof(*retval));
1373                 if (mailbox) 
1374                         ast_copy_string(retval->mailbox, mailbox, sizeof(retval->mailbox));
1375                 populate_defaults(retval);
1376                 if (!context && ast_test_flag((&globalflags), VM_SEARCH))
1377                         var = ast_load_realtime("voicemail", "mailbox", mailbox, SENTINEL);
1378                 else
1379                         var = ast_load_realtime("voicemail", "mailbox", mailbox, "context", context, SENTINEL);
1380                 if (var) {
1381                         apply_options_full(retval, var);
1382                         ast_variables_destroy(var);
1383                 } else { 
1384                         if (!ivm) 
1385                                 ast_free(retval);
1386                         retval = NULL;
1387                 }       
1388         } 
1389         return retval;
1390 }
1391
1392 /*!
1393  * \brief Finds a voicemail user from the users file or the realtime engine.
1394  * \param ivm
1395  * \param context
1396  * \param mailbox
1397  * 
1398  * \return The ast_vm_user structure for the user that was found.
1399  */
1400 static struct ast_vm_user *find_user(struct ast_vm_user *ivm, const char *context, const char *mailbox)
1401 {
1402         /* This function could be made to generate one from a database, too */
1403         struct ast_vm_user *vmu = NULL, *cur;
1404         AST_LIST_LOCK(&users);
1405
1406         if (!context && !ast_test_flag((&globalflags), VM_SEARCH))
1407                 context = "default";
1408
1409         AST_LIST_TRAVERSE(&users, cur, list) {
1410 #ifdef IMAP_STORAGE
1411                 if (cur->imapversion != imapversion) {
1412                         continue;
1413                 }
1414 #endif
1415                 if (ast_test_flag((&globalflags), VM_SEARCH) && !strcasecmp(mailbox, cur->mailbox))
1416                         break;
1417                 if (context && (!strcasecmp(context, cur->context)) && (!strcasecmp(mailbox, cur->mailbox)))
1418                         break;
1419         }
1420         if (cur) {
1421                 /* Make a copy, so that on a reload, we have no race */
1422                 if ((vmu = (ivm ? ivm : ast_malloc(sizeof(*vmu))))) {
1423                         memcpy(vmu, cur, sizeof(*vmu));
1424                         ast_set2_flag(vmu, !ivm, VM_ALLOCED);
1425                         AST_LIST_NEXT(vmu, list) = NULL;
1426                 }
1427         } else
1428                 vmu = find_user_realtime(ivm, context, mailbox);
1429         AST_LIST_UNLOCK(&users);
1430         return vmu;
1431 }
1432
1433 /*!
1434  * \brief Resets a user password to a specified password.
1435  * \param context
1436  * \param mailbox
1437  * \param newpass
1438  *
1439  * This does the actual change password work, called by the vm_change_password() function.
1440  *
1441  * \return zero on success, -1 on error.
1442  */
1443 static int reset_user_pw(const char *context, const char *mailbox, const char *newpass)
1444 {
1445         /* This function could be made to generate one from a database, too */
1446         struct ast_vm_user *cur;
1447         int res = -1;
1448         AST_LIST_LOCK(&users);
1449         AST_LIST_TRAVERSE(&users, cur, list) {
1450                 if ((!context || !strcasecmp(context, cur->context)) &&
1451                         (!strcasecmp(mailbox, cur->mailbox)))
1452                                 break;
1453         }
1454         if (cur) {
1455                 ast_copy_string(cur->password, newpass, sizeof(cur->password));
1456                 res = 0;
1457         }
1458         AST_LIST_UNLOCK(&users);
1459         return res;
1460 }
1461
1462 /*! 
1463  * \brief The handler for the change password option.
1464  * \param vmu The voicemail user to work with.
1465  * \param newpassword The new password (that has been gathered from the appropriate prompting).
1466  * 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.
1467  * It is also called when the user wants to change their password from menu option '5' on the mailbox options menu.
1468  */
1469 static void vm_change_password(struct ast_vm_user *vmu, const char *newpassword)
1470 {
1471         struct ast_config   *cfg = NULL;
1472         struct ast_variable *var = NULL;
1473         struct ast_category *cat = NULL;
1474         char *category = NULL, *value = NULL, *new = NULL;
1475         const char *tmp = NULL;
1476         struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS };
1477         char secretfn[PATH_MAX] = "";
1478         int found = 0;
1479
1480         if (!change_password_realtime(vmu, newpassword))
1481                 return;
1482
1483         /* check if we should store the secret in the spool directory next to the messages */
1484         switch (vmu->passwordlocation) {
1485         case OPT_PWLOC_SPOOLDIR:
1486                 snprintf(secretfn, sizeof(secretfn), "%s%s/%s/secret.conf", VM_SPOOL_DIR, vmu->context, vmu->mailbox);
1487                 if (write_password_to_file(secretfn, newpassword) == 0) {
1488                         ast_verb(4, "Writing voicemail password to file %s succeeded\n", secretfn);
1489                         reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1490                         ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1491                         break;
1492                 } else {
1493                         ast_verb(4, "Writing voicemail password to file %s failed, falling back to config file\n", secretfn);
1494                 }
1495                 /* Fall-through */
1496         case OPT_PWLOC_VOICEMAILCONF:
1497                 if ((cfg = ast_config_load(VOICEMAIL_CONFIG, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
1498                         while ((category = ast_category_browse(cfg, category))) {
1499                                 if (!strcasecmp(category, vmu->context)) {
1500                                         if (!(tmp = ast_variable_retrieve(cfg, category, vmu->mailbox))) {
1501                                                 ast_log(AST_LOG_WARNING, "We could not find the mailbox.\n");
1502                                                 break;
1503                                         }
1504                                         value = strstr(tmp, ",");
1505                                         if (!value) {
1506                                                 new = alloca(strlen(newpassword)+1);
1507                                                 sprintf(new, "%s", newpassword);
1508                                         } else {
1509                                                 new = alloca((strlen(value) + strlen(newpassword) + 1));
1510                                                 sprintf(new, "%s%s", newpassword, value);
1511                                         }
1512                                         if (!(cat = ast_category_get(cfg, category))) {
1513                                                 ast_log(AST_LOG_WARNING, "Failed to get category structure.\n");
1514                                                 break;
1515                                         }
1516                                         ast_variable_update(cat, vmu->mailbox, new, NULL, 0);
1517                                         found = 1;
1518                                 }
1519                         }
1520                         /* save the results */
1521                         if (found) {
1522                                 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1523                                 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1524                                 ast_config_text_file_save(VOICEMAIL_CONFIG, cfg, "AppVoicemail");
1525                                 break;
1526                         }
1527                 }
1528                 /* Fall-through */
1529         case OPT_PWLOC_USERSCONF:
1530                 /* check users.conf and update the password stored for the mailbox */
1531                 /* if no vmsecret entry exists create one. */
1532                 if ((cfg = ast_config_load("users.conf", config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
1533                         ast_debug(4, "we are looking for %s\n", vmu->mailbox);
1534                         for (category = ast_category_browse(cfg, NULL); category; category = ast_category_browse(cfg, category)) {
1535                                 ast_debug(4, "users.conf: %s\n", category);
1536                                 if (!strcasecmp(category, vmu->mailbox)) {
1537                                         if (!(tmp = ast_variable_retrieve(cfg, category, "vmsecret"))) {
1538                                                 ast_debug(3, "looks like we need to make vmsecret!\n");
1539                                                 var = ast_variable_new("vmsecret", newpassword, "");
1540                                         } else {
1541                                                 var = NULL;
1542                                         }
1543                                         new = alloca(strlen(newpassword) + 1);
1544                                         sprintf(new, "%s", newpassword);
1545                                         if (!(cat = ast_category_get(cfg, category))) {
1546                                                 ast_debug(4, "failed to get category!\n");
1547                                                 ast_free(var);
1548                                                 break;
1549                                         }
1550                                         if (!var) {
1551                                                 ast_variable_update(cat, "vmsecret", new, NULL, 0);
1552                                         } else {
1553                                                 ast_variable_append(cat, var);
1554                                         }
1555                                         found = 1;
1556                                         break;
1557                                 }
1558                         }
1559                         /* save the results and clean things up */
1560                         if (found) {
1561                                 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1562                                 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1563                                 ast_config_text_file_save("users.conf", cfg, "AppVoicemail");
1564                         }
1565                 }
1566         }
1567 }
1568
1569 static void vm_change_password_shell(struct ast_vm_user *vmu, char *newpassword)
1570 {
1571         char buf[255];
1572         snprintf(buf, sizeof(buf), "%s %s %s %s", ext_pass_cmd, vmu->context, vmu->mailbox, newpassword);
1573         if (!ast_safe_system(buf)) {
1574                 ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
1575                 /* Reset the password in memory, too */
1576                 reset_user_pw(vmu->context, vmu->mailbox, newpassword);
1577         }
1578 }
1579
1580 /*! 
1581  * \brief Creates a file system path expression for a folder within the voicemail data folder and the appropriate context.
1582  * \param dest The variable to hold the output generated path expression. This buffer should be of size PATH_MAX.
1583  * \param len The length of the path string that was written out.
1584  * \param context
1585  * \param ext 
1586  * \param folder 
1587  * 
1588  * The path is constructed as 
1589  *      VM_SPOOL_DIRcontext/ext/folder
1590  *
1591  * \return zero on success, -1 on error.
1592  */
1593 static int make_dir(char *dest, int len, const char *context, const char *ext, const char *folder)
1594 {
1595         return snprintf(dest, len, "%s%s/%s/%s", VM_SPOOL_DIR, context, ext, folder);
1596 }
1597
1598 /*! 
1599  * \brief Creates a file system path expression for a folder within the voicemail data folder and the appropriate context.
1600  * \param dest The variable to hold the output generated path expression. This buffer should be of size PATH_MAX.
1601  * \param len The length of the path string that was written out.
1602  * \param dir 
1603  * \param num 
1604  * 
1605  * The path is constructed as 
1606  *      VM_SPOOL_DIRcontext/ext/folder
1607  *
1608  * \return zero on success, -1 on error.
1609  */
1610 static int make_file(char *dest, const int len, const char *dir, const int num)
1611 {
1612         return snprintf(dest, len, "%s/msg%04d", dir, num);
1613 }
1614
1615 /* same as mkstemp, but return a FILE * */
1616 static FILE *vm_mkftemp(char *template)
1617 {
1618         FILE *p = NULL;
1619         int pfd = mkstemp(template);
1620         chmod(template, VOICEMAIL_FILE_MODE & ~my_umask);
1621         if (pfd > -1) {
1622                 p = fdopen(pfd, "w+");
1623                 if (!p) {
1624                         close(pfd);
1625                         pfd = -1;
1626                 }
1627         }
1628         return p;
1629 }
1630
1631 /*! \brief basically mkdir -p $dest/$context/$ext/$folder
1632  * \param dest    String. base directory.
1633  * \param len     Length of dest.
1634  * \param context String. Ignored if is null or empty string.
1635  * \param ext     String. Ignored if is null or empty string.
1636  * \param folder  String. Ignored if is null or empty string. 
1637  * \return -1 on failure, 0 on success.
1638  */
1639 static int create_dirpath(char *dest, int len, const char *context, const char *ext, const char *folder)
1640 {
1641         mode_t  mode = VOICEMAIL_DIR_MODE;
1642         int res;
1643
1644         make_dir(dest, len, context, ext, folder);
1645         if ((res = ast_mkdir(dest, mode))) {
1646                 ast_log(AST_LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res));
1647                 return -1;
1648         }
1649         return 0;
1650 }
1651
1652 static const char * const mailbox_folders[] = {
1653 #ifdef IMAP_STORAGE
1654         imapfolder,
1655 #else
1656         "INBOX",
1657 #endif
1658         "Old",
1659         "Work",
1660         "Family",
1661         "Friends",
1662         "Cust1",
1663         "Cust2",
1664         "Cust3",
1665         "Cust4",
1666         "Cust5",
1667         "Deleted",
1668         "Urgent",
1669 };
1670
1671 static const char *mbox(struct ast_vm_user *vmu, int id)
1672 {
1673 #ifdef IMAP_STORAGE
1674         if (vmu && id == 0) {
1675                 return vmu->imapfolder;
1676         }
1677 #endif
1678         return (id >= 0 && id < ARRAY_LEN(mailbox_folders)) ? mailbox_folders[id] : "Unknown";
1679 }
1680
1681 static int get_folder_by_name(const char *name)
1682 {
1683         size_t i;
1684
1685         for (i = 0; i < ARRAY_LEN(mailbox_folders); i++) {
1686                 if (strcasecmp(name, mailbox_folders[i]) == 0) {
1687                         return i;
1688                 }
1689         }
1690
1691         return -1;
1692 }
1693
1694 static void free_user(struct ast_vm_user *vmu)
1695 {
1696         if (ast_test_flag(vmu, VM_ALLOCED)) {
1697                 if (vmu->emailbody != NULL) {
1698                         ast_free(vmu->emailbody);
1699                         vmu->emailbody = NULL;
1700                 }
1701                 if (vmu->emailsubject != NULL) {
1702                         ast_free(vmu->emailsubject);
1703                         vmu->emailsubject = NULL;
1704                 }
1705                 ast_free(vmu);
1706         }
1707 }
1708
1709 static int vm_allocate_dh(struct vm_state *vms, struct ast_vm_user *vmu, int count_msg) {
1710
1711         int arraysize = (vmu->maxmsg > count_msg ? vmu->maxmsg : count_msg);
1712         if (!vms->dh_arraysize) {
1713                 /* initial allocation */
1714                 if (!(vms->deleted = ast_calloc(arraysize, sizeof(int)))) {
1715                         return -1;
1716                 }
1717                 if (!(vms->heard = ast_calloc(arraysize, sizeof(int)))) {
1718                         return -1;
1719                 }
1720                 vms->dh_arraysize = arraysize;
1721         } else if (vms->dh_arraysize < arraysize) {
1722                 if (!(vms->deleted = ast_realloc(vms->deleted, arraysize * sizeof(int)))) {
1723                         return -1;
1724                 }
1725                 if (!(vms->heard = ast_realloc(vms->heard, arraysize * sizeof(int)))) {
1726                         return -1;
1727                 }
1728                 memset(vms->deleted, 0, arraysize * sizeof(int));
1729                 memset(vms->heard, 0, arraysize * sizeof(int));
1730                 vms->dh_arraysize = arraysize;
1731         }
1732
1733         return 0;
1734 }
1735
1736 /* All IMAP-specific functions should go in this block. This
1737  * keeps them from being spread out all over the code */
1738 #ifdef IMAP_STORAGE
1739 static void vm_imap_delete(char *file, int msgnum, struct ast_vm_user *vmu)
1740 {
1741         char arg[10];
1742         struct vm_state *vms;
1743         unsigned long messageNum;
1744
1745         /* If greetings aren't stored in IMAP, just delete the file */
1746         if (msgnum < 0 && !imapgreetings) {
1747                 ast_filedelete(file, NULL);
1748                 return;
1749         }
1750
1751         if (!(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && !(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
1752                 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);
1753                 return;
1754         }
1755
1756         /* find real message number based on msgnum */
1757         /* this may be an index into vms->msgArray based on the msgnum. */
1758         messageNum = vms->msgArray[msgnum];
1759         if (messageNum == 0) {
1760                 ast_log(LOG_WARNING, "msgnum %d, mailbox message %lu is zero.\n", msgnum, messageNum);
1761                 return;
1762         }
1763         ast_debug(3, "deleting msgnum %d, which is mailbox message %lu\n", msgnum, messageNum);
1764         /* delete message */
1765         snprintf (arg, sizeof(arg), "%lu", messageNum);
1766         ast_mutex_lock(&vms->lock);
1767         mail_setflag (vms->mailstream, arg, "\\DELETED");
1768         mail_expunge(vms->mailstream);
1769         ast_mutex_unlock(&vms->lock);
1770 }
1771
1772 static int imap_retrieve_greeting(const char *dir, const int msgnum, struct ast_vm_user *vmu)
1773 {
1774         struct vm_state *vms_p;
1775         char *file, *filename;
1776         char *attachment;
1777         int ret = 0, i;
1778         BODY *body;
1779
1780         /* This function is only used for retrieval of IMAP greetings
1781          * regular messages are not retrieved this way, nor are greetings
1782          * if they are stored locally*/
1783         if (msgnum > -1 || !imapgreetings) {
1784                 return 0;
1785         } else {
1786                 file = strrchr(ast_strdupa(dir), '/');
1787                 if (file)
1788                         *file++ = '\0';
1789                 else {
1790                         ast_debug(1, "Failed to procure file name from directory passed.\n");
1791                         return -1;
1792                 }
1793         }
1794
1795         /* check if someone is accessing this box right now... */
1796         if (!(vms_p = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && 
1797                 !(vms_p = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
1798                 /* Unlike when retrieving a message, it is reasonable not to be able to find a 
1799                 * vm_state for a mailbox when trying to retrieve a greeting. Just create one,
1800                 * that's all we need to do.
1801                 */
1802                 if (!(vms_p = create_vm_state_from_user(vmu))) {
1803                         ast_log(LOG_NOTICE, "Unable to create vm_state object!\n");
1804                         return -1;
1805                 }
1806         }
1807         
1808         /* Greetings will never have a prepended message */
1809         *vms_p->introfn = '\0';
1810
1811         ast_mutex_lock(&vms_p->lock);
1812         ret = init_mailstream(vms_p, GREETINGS_FOLDER);
1813         if (!vms_p->mailstream) {
1814                 ast_log(AST_LOG_ERROR, "IMAP mailstream is NULL\n");
1815                 ast_mutex_unlock(&vms_p->lock);
1816                 return -1;
1817         }
1818
1819         /*XXX Yuck, this could probably be done a lot better */
1820         for (i = 0; i < vms_p->mailstream->nmsgs; i++) {
1821                 mail_fetchstructure(vms_p->mailstream, i + 1, &body);
1822                 /* We have the body, now we extract the file name of the first attachment. */
1823                 if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
1824                         attachment = ast_strdupa(body->nested.part->next->body.parameter->value);
1825                 } else {
1826                         ast_log(AST_LOG_ERROR, "There is no file attached to this IMAP message.\n");
1827                         ast_mutex_unlock(&vms_p->lock);
1828                         return -1;
1829                 }
1830                 filename = strsep(&attachment, ".");
1831                 if (!strcmp(filename, file)) {
1832                         ast_copy_string(vms_p->fn, dir, sizeof(vms_p->fn));
1833                         vms_p->msgArray[vms_p->curmsg] = i + 1;
1834                         save_body(body, vms_p, "2", attachment, 0);
1835                         ast_mutex_unlock(&vms_p->lock);
1836                         return 0;
1837                 }
1838         }
1839         ast_mutex_unlock(&vms_p->lock);
1840
1841         return -1;
1842 }
1843
1844 static int imap_retrieve_file(const char *dir, const int msgnum, const char *mailbox, const char *context)
1845 {
1846         BODY *body;
1847         char *header_content;
1848         char *attachedfilefmt;
1849         char buf[80];
1850         struct vm_state *vms;
1851         char text_file[PATH_MAX];
1852         FILE *text_file_ptr;
1853         int res = 0;
1854         struct ast_vm_user *vmu;
1855
1856         if (!(vmu = find_user(NULL, context, mailbox))) {
1857                 ast_log(LOG_WARNING, "Couldn't find user with mailbox %s@%s\n", mailbox, context);
1858                 return -1;
1859         }
1860         
1861         if (msgnum < 0) {
1862                 if (imapgreetings) {
1863                         res = imap_retrieve_greeting(dir, msgnum, vmu);
1864                         goto exit;
1865                 } else {
1866                         res = 0;
1867                         goto exit;
1868                 }
1869         }
1870
1871         /* Before anything can happen, we need a vm_state so that we can
1872          * actually access the imap server through the vms->mailstream
1873          */
1874         if (!(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 1)) && !(vms = get_vm_state_by_mailbox(vmu->mailbox, vmu->context, 0))) {
1875                 /* This should not happen. If it does, then I guess we'd
1876                  * need to create the vm_state, extract which mailbox to
1877                  * open, and then set up the msgArray so that the correct
1878                  * IMAP message could be accessed. If I have seen correctly
1879                  * though, the vms should be obtainable from the vmstates list
1880                  * and should have its msgArray properly set up.
1881                  */
1882                 ast_log(LOG_ERROR, "Couldn't find a vm_state for mailbox %s!!! Oh no!\n", vmu->mailbox);
1883                 res = -1;
1884                 goto exit;
1885         }
1886
1887         make_file(vms->fn, sizeof(vms->fn), dir, msgnum);
1888         snprintf(vms->introfn, sizeof(vms->introfn), "%sintro", vms->fn);
1889
1890         /* Don't try to retrieve a message from IMAP if it already is on the file system */
1891         if (ast_fileexists(vms->fn, NULL, NULL) > 0) {
1892                 res = 0;
1893                 goto exit;
1894         }
1895
1896         ast_debug(3, "Before mail_fetchheaders, curmsg is: %d, imap messages is %lu\n", msgnum, vms->msgArray[msgnum]);
1897         if (vms->msgArray[msgnum] == 0) {
1898                 ast_log(LOG_WARNING, "Trying to access unknown message\n");
1899                 res = -1;
1900                 goto exit;
1901         }
1902
1903         /* This will only work for new messages... */
1904         ast_mutex_lock(&vms->lock);
1905         header_content = mail_fetchheader (vms->mailstream, vms->msgArray[msgnum]);
1906         ast_mutex_unlock(&vms->lock);
1907         /* empty string means no valid header */
1908         if (ast_strlen_zero(header_content)) {
1909                 ast_log(LOG_ERROR, "Could not fetch header for message number %ld\n", vms->msgArray[msgnum]);
1910                 res = -1;
1911                 goto exit;
1912         }
1913
1914         ast_mutex_lock(&vms->lock);
1915         mail_fetchstructure(vms->mailstream, vms->msgArray[msgnum], &body);
1916         ast_mutex_unlock(&vms->lock);
1917
1918         /* We have the body, now we extract the file name of the first attachment. */
1919         if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
1920                 attachedfilefmt = ast_strdupa(body->nested.part->next->body.parameter->value);
1921         } else {
1922                 ast_log(LOG_ERROR, "There is no file attached to this IMAP message.\n");
1923                 res = -1;
1924                 goto exit;
1925         }
1926         
1927         /* Find the format of the attached file */
1928
1929         strsep(&attachedfilefmt, ".");
1930         if (!attachedfilefmt) {
1931                 ast_log(LOG_ERROR, "File format could not be obtained from IMAP message attachment\n");
1932                 res = -1;
1933                 goto exit;
1934         }
1935         
1936         save_body(body, vms, "2", attachedfilefmt, 0);
1937         if (save_body(body, vms, "3", attachedfilefmt, 1)) {
1938                 *vms->introfn = '\0';
1939         }
1940
1941         /* Get info from headers!! */
1942         snprintf(text_file, sizeof(text_file), "%s.%s", vms->fn, "txt");
1943
1944         if (!(text_file_ptr = fopen(text_file, "w"))) {
1945                 ast_log(LOG_WARNING, "Unable to open/create file %s: %s\n", text_file, strerror(errno));
1946         }
1947
1948         fprintf(text_file_ptr, "%s\n", "[message]");
1949
1950         get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Name:", buf, sizeof(buf));
1951         fprintf(text_file_ptr, "callerid=\"%s\" ", S_OR(buf, ""));
1952         get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Num:", buf, sizeof(buf));
1953         fprintf(text_file_ptr, "<%s>\n", S_OR(buf, ""));
1954         get_header_by_tag(header_content, "X-Asterisk-VM-Context:", buf, sizeof(buf));
1955         fprintf(text_file_ptr, "context=%s\n", S_OR(buf, ""));
1956         get_header_by_tag(header_content, "X-Asterisk-VM-Orig-time:", buf, sizeof(buf));
1957         fprintf(text_file_ptr, "origtime=%s\n", S_OR(buf, ""));
1958         get_header_by_tag(header_content, "X-Asterisk-VM-Duration:", buf, sizeof(buf));
1959         fprintf(text_file_ptr, "duration=%s\n", S_OR(buf, ""));
1960         get_header_by_tag(header_content, "X-Asterisk-VM-Category:", buf, sizeof(buf));
1961         fprintf(text_file_ptr, "category=%s\n", S_OR(buf, ""));
1962         get_header_by_tag(header_content, "X-Asterisk-VM-Flag:", buf, sizeof(buf));
1963         fprintf(text_file_ptr, "flag=%s\n", S_OR(buf, ""));
1964         fclose(text_file_ptr);
1965
1966 exit:
1967         free_user(vmu);
1968         return res;
1969 }
1970
1971 static int folder_int(const char *folder)
1972 {
1973         /*assume a NULL folder means INBOX*/
1974         if (!folder) {
1975                 return 0;
1976         }
1977         if (!strcasecmp(folder, imapfolder)) {
1978                 return 0;
1979         } else if (!strcasecmp(folder, "Old")) {
1980                 return 1;
1981         } else if (!strcasecmp(folder, "Work")) {
1982                 return 2;
1983         } else if (!strcasecmp(folder, "Family")) {
1984                 return 3;
1985         } else if (!strcasecmp(folder, "Friends")) {
1986                 return 4;
1987         } else if (!strcasecmp(folder, "Cust1")) {
1988                 return 5;
1989         } else if (!strcasecmp(folder, "Cust2")) {
1990                 return 6;
1991         } else if (!strcasecmp(folder, "Cust3")) {
1992                 return 7;
1993         } else if (!strcasecmp(folder, "Cust4")) {
1994                 return 8;
1995         } else if (!strcasecmp(folder, "Cust5")) {
1996                 return 9;
1997         } else if (!strcasecmp(folder, "Urgent")) {
1998                 return 11;
1999         } else { /*assume they meant INBOX if folder is not found otherwise*/
2000                 return 0;
2001         }
2002 }
2003
2004 static int __messagecount(const char *context, const char *mailbox, const char *folder)
2005 {
2006         SEARCHPGM *pgm;
2007         SEARCHHEADER *hdr;
2008
2009         struct ast_vm_user *vmu, vmus;
2010         struct vm_state *vms_p;
2011         int ret = 0;
2012         int fold = folder_int(folder);
2013         int urgent = 0;
2014         
2015         /* If URGENT, then look at INBOX */
2016         if (fold == 11) {
2017                 fold = NEW_FOLDER;
2018                 urgent = 1;
2019         }
2020
2021         if (ast_strlen_zero(mailbox))
2022                 return 0;
2023
2024         /* We have to get the user before we can open the stream! */
2025         vmu = find_user(&vmus, context, mailbox);
2026         if (!vmu) {
2027                 ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailbox, context);
2028                 return -1;
2029         } else {
2030                 /* No IMAP account available */
2031                 if (vmu->imapuser[0] == '\0') {
2032                         ast_log(AST_LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
2033                         return -1;
2034                 }
2035         }
2036
2037         /* No IMAP account available */
2038         if (vmu->imapuser[0] == '\0') {
2039                 ast_log(AST_LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
2040                 free_user(vmu);
2041                 return -1;
2042         }
2043
2044         /* check if someone is accessing this box right now... */
2045         vms_p = get_vm_state_by_imapuser(vmu->imapuser, 1);
2046         if (!vms_p) {
2047                 vms_p = get_vm_state_by_mailbox(mailbox, context, 1);
2048         }
2049         if (vms_p) {
2050                 ast_debug(3, "Returning before search - user is logged in\n");
2051                 if (fold == 0) { /* INBOX */
2052                         return urgent ? vms_p->urgentmessages : vms_p->newmessages;
2053                 }
2054                 if (fold == 1) { /* Old messages */
2055                         return vms_p->oldmessages;
2056                 }
2057         }
2058
2059         /* add one if not there... */
2060         vms_p = get_vm_state_by_imapuser(vmu->imapuser, 0);
2061         if (!vms_p) {
2062                 vms_p = get_vm_state_by_mailbox(mailbox, context, 0);
2063         }
2064
2065         if (!vms_p) {
2066                 vms_p = create_vm_state_from_user(vmu);
2067         }
2068         ret = init_mailstream(vms_p, fold);
2069         if (!vms_p->mailstream) {
2070                 ast_log(AST_LOG_ERROR, "Houston we have a problem - IMAP mailstream is NULL\n");
2071                 return -1;
2072         }
2073         if (ret == 0) {
2074                 ast_mutex_lock(&vms_p->lock);
2075                 pgm = mail_newsearchpgm ();
2076                 hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", (char *)(!ast_strlen_zero(vmu->imapvmshareid) ? vmu->imapvmshareid : mailbox));
2077                 hdr->next = mail_newsearchheader("X-Asterisk-VM-Context", (char *) S_OR(context, "default"));
2078                 pgm->header = hdr;
2079                 if (fold != OLD_FOLDER) {
2080                         pgm->unseen = 1;
2081                         pgm->seen = 0;
2082                 }
2083                 /* In the special case where fold is 1 (old messages) we have to do things a bit
2084                  * differently. Old messages are stored in the INBOX but are marked as "seen"
2085                  */
2086                 else {
2087                         pgm->unseen = 0;
2088                         pgm->seen = 1;
2089                 }
2090                 /* look for urgent messages */
2091                 if (fold == NEW_FOLDER) {
2092                         if (urgent) {
2093                                 pgm->flagged = 1;
2094                                 pgm->unflagged = 0;
2095                         } else {
2096                                 pgm->flagged = 0;
2097                                 pgm->unflagged = 1;
2098                         }
2099                 }
2100                 pgm->undeleted = 1;
2101                 pgm->deleted = 0;
2102
2103                 vms_p->vmArrayIndex = 0;
2104                 mail_search_full (vms_p->mailstream, NULL, pgm, NIL);
2105                 if (fold == 0 && urgent == 0)
2106                         vms_p->newmessages = vms_p->vmArrayIndex;
2107                 if (fold == 1)
2108                         vms_p->oldmessages = vms_p->vmArrayIndex;
2109                 if (fold == 0 && urgent == 1)
2110                         vms_p->urgentmessages = vms_p->vmArrayIndex;
2111                 /*Freeing the searchpgm also frees the searchhdr*/
2112                 mail_free_searchpgm(&pgm);
2113                 ast_mutex_unlock(&vms_p->lock);
2114                 vms_p->updated = 0;
2115                 return vms_p->vmArrayIndex;
2116         } else {
2117                 ast_mutex_lock(&vms_p->lock);
2118                 mail_ping(vms_p->mailstream);
2119                 ast_mutex_unlock(&vms_p->lock);
2120         }
2121         return 0;
2122 }
2123
2124 static int imap_check_limits(struct ast_channel *chan, struct vm_state *vms, struct ast_vm_user *vmu, int msgnum)
2125 {
2126         /* Check if mailbox is full */
2127         check_quota(vms, vmu->imapfolder);
2128         if (vms->quota_limit && vms->quota_usage >= vms->quota_limit) {
2129                 ast_debug(1, "*** QUOTA EXCEEDED!! %u >= %u\n", vms->quota_usage, vms->quota_limit);
2130                 ast_play_and_wait(chan, "vm-mailboxfull");
2131                 return -1;
2132         }
2133
2134         /* Check if we have exceeded maxmsg */
2135         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));
2136         if (msgnum >= vmu->maxmsg - inprocess_count(vmu->mailbox, vmu->context, +1)) {
2137                 ast_log(LOG_WARNING, "Unable to leave message since we will exceed the maximum number of messages allowed (%u >= %u)\n", msgnum, vmu->maxmsg);
2138                 ast_play_and_wait(chan, "vm-mailboxfull");
2139                 pbx_builtin_setvar_helper(chan, "VMSTATUS", "FAILED");
2140                 return -1;
2141         }
2142
2143         return 0;
2144 }
2145
2146 /*!
2147  * \brief Gets the number of messages that exist in a mailbox folder.
2148  * \param context
2149  * \param mailbox
2150  * \param folder
2151  * 
2152  * This method is used when IMAP backend is used.
2153  * \return The number of messages in this mailbox folder (zero or more).
2154  */
2155 static int messagecount(const char *context, const char *mailbox, const char *folder)
2156 {
2157         if (ast_strlen_zero(folder) || !strcmp(folder, "INBOX")) {
2158                 return __messagecount(context, mailbox, "INBOX") + __messagecount(context, mailbox, "Urgent");
2159         } else {
2160                 return __messagecount(context, mailbox, folder);
2161         }
2162 }
2163
2164 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)
2165 {
2166         char *myserveremail = serveremail;
2167         char fn[PATH_MAX];
2168         char introfn[PATH_MAX];
2169         char mailbox[256];
2170         char *stringp;
2171         FILE *p = NULL;
2172         char tmp[80] = "/tmp/astmail-XXXXXX";
2173         long len;
2174         void *buf;
2175         int tempcopy = 0;
2176         STRING str;
2177         int ret; /* for better error checking */
2178         char *imap_flags = NIL;
2179         int msgcount = (messagecount(vmu->context, vmu->mailbox, "INBOX") + messagecount(vmu->context, vmu->mailbox, "Old"));
2180         int box = NEW_FOLDER;
2181
2182         /* Back out early if this is a greeting and we don't want to store greetings in IMAP */
2183         if (msgnum < 0) {
2184                 if(!imapgreetings) {
2185                         return 0;
2186                 } else {
2187                         box = GREETINGS_FOLDER;
2188                 }
2189         }
2190
2191         if (imap_check_limits(chan, vms, vmu, msgcount)) {
2192                 return -1;
2193         }
2194
2195         /* Set urgent flag for IMAP message */
2196         if (!ast_strlen_zero(flag) && !strcmp(flag, "Urgent")) {
2197                 ast_debug(3, "Setting message flag \\\\FLAGGED.\n");
2198                 imap_flags = "\\FLAGGED";
2199         }
2200
2201         /* Attach only the first format */
2202         fmt = ast_strdupa(fmt);
2203         stringp = fmt;
2204         strsep(&stringp, "|");
2205
2206         if (!ast_strlen_zero(vmu->serveremail))
2207                 myserveremail = vmu->serveremail;
2208
2209         if (msgnum > -1)
2210                 make_file(fn, sizeof(fn), dir, msgnum);
2211         else
2212                 ast_copy_string (fn, dir, sizeof(fn));
2213
2214         snprintf(introfn, sizeof(introfn), "%sintro", fn);
2215         if (ast_fileexists(introfn, NULL, NULL) <= 0) {
2216                 *introfn = '\0';
2217         }
2218
2219         if (ast_strlen_zero(vmu->email)) {
2220                 /* We need the vmu->email to be set when we call make_email_file, but
2221                  * if we keep it set, a duplicate e-mail will be created. So at the end
2222                  * of this function, we will revert back to an empty string if tempcopy
2223                  * is 1.
2224                  */
2225                 ast_copy_string(vmu->email, vmu->imapuser, sizeof(vmu->email));
2226                 tempcopy = 1;
2227         }
2228
2229         if (!strcmp(fmt, "wav49"))
2230                 fmt = "WAV";
2231         ast_debug(3, "Storing file '%s', format '%s'\n", fn, fmt);
2232
2233         /* Make a temporary file instead of piping directly to sendmail, in case the mail
2234            command hangs. */
2235         if (!(p = vm_mkftemp(tmp))) {
2236                 ast_log(AST_LOG_WARNING, "Unable to store '%s' (can't create temporary file)\n", fn);
2237                 if (tempcopy)
2238                         *(vmu->email) = '\0';
2239                 return -1;
2240         }
2241
2242         if (msgnum < 0 && imapgreetings) {
2243                 if ((ret = init_mailstream(vms, GREETINGS_FOLDER))) {
2244                         ast_log(AST_LOG_WARNING, "Unable to open mailstream.\n");
2245                         return -1;
2246                 }
2247                 imap_delete_old_greeting(fn, vms);
2248         }
2249
2250         make_email_file(p, myserveremail, vmu, msgnum, vmu->context, vmu->mailbox, "INBOX",
2251                 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL),
2252                 S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, NULL),
2253                 fn, introfn, fmt, duration, 1, chan, NULL, 1, flag);
2254         /* read mail file to memory */
2255         len = ftell(p);
2256         rewind(p);
2257         if (!(buf = ast_malloc(len + 1))) {
2258                 ast_log(AST_LOG_ERROR, "Can't allocate %ld bytes to read message\n", len + 1);
2259                 fclose(p);
2260                 if (tempcopy)
2261                         *(vmu->email) = '\0';
2262                 return -1;
2263         }
2264         if (fread(buf, len, 1, p) < len) {
2265                 if (ferror(p)) {
2266                         ast_log(LOG_ERROR, "Short read while reading in mail file.\n");
2267                         return -1;
2268                 }
2269         }
2270         ((char *) buf)[len] = '\0';
2271         INIT(&str, mail_string, buf, len);
2272         ret = init_mailstream(vms, box);
2273         if (ret == 0) {
2274                 imap_mailbox_name(mailbox, sizeof(mailbox), vms, box, 1);
2275                 ast_mutex_lock(&vms->lock);
2276                 if(!mail_append_full(vms->mailstream, mailbox, imap_flags, NIL, &str))
2277                         ast_log(LOG_ERROR, "Error while sending the message to %s\n", mailbox);
2278                 ast_mutex_unlock(&vms->lock);
2279                 fclose(p);
2280                 unlink(tmp);
2281                 ast_free(buf);
2282         } else {
2283                 ast_log(LOG_ERROR, "Could not initialize mailstream for %s\n", mailbox);
2284                 fclose(p);
2285                 unlink(tmp);
2286                 ast_free(buf);
2287                 return -1;
2288         }
2289         ast_debug(3, "%s stored\n", fn);
2290
2291         if (tempcopy)
2292                 *(vmu->email) = '\0';
2293         inprocess_count(vmu->mailbox, vmu->context, -1);
2294         return 0;
2295
2296 }
2297
2298 /*!
2299  * \brief Gets the number of messages that exist in the inbox folder.
2300  * \param mailbox_context
2301  * \param newmsgs The variable that is updated with the count of new messages within this inbox.
2302  * \param oldmsgs The variable that is updated with the count of old messages within this inbox.
2303  * \param urgentmsgs The variable that is updated with the count of urgent messages within this inbox.
2304  * 
2305  * This method is used when IMAP backend is used.
2306  * Simultaneously determines the count of new,old, and urgent messages. The total messages would then be the sum of these three.
2307  *
2308  * \return zero on success, -1 on error.
2309  */
2310
2311 static int inboxcount2(const char *mailbox_context, int *urgentmsgs, int *newmsgs, int *oldmsgs)
2312 {
2313         char tmp[PATH_MAX] = "";
2314         char *mailboxnc;
2315         char *context;
2316         char *mb;
2317         char *cur;
2318         if (newmsgs)
2319                 *newmsgs = 0;
2320         if (oldmsgs)
2321                 *oldmsgs = 0;
2322         if (urgentmsgs)
2323                 *urgentmsgs = 0;
2324
2325         ast_debug(3, "Mailbox is set to %s\n", mailbox_context);
2326         /* If no mailbox, return immediately */
2327         if (ast_strlen_zero(mailbox_context))
2328                 return 0;
2329
2330         ast_copy_string(tmp, mailbox_context, sizeof(tmp));
2331         context = strchr(tmp, '@');
2332         if (strchr(mailbox_context, ',')) {
2333                 int tmpnew, tmpold, tmpurgent;
2334                 ast_copy_string(tmp, mailbox_context, sizeof(tmp));
2335                 mb = tmp;
2336                 while ((cur = strsep(&mb, ", "))) {
2337                         if (!ast_strlen_zero(cur)) {
2338                                 if (inboxcount2(cur, urgentmsgs ? &tmpurgent : NULL, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
2339                                         return -1;
2340                                 else {
2341                                         if (newmsgs)
2342                                                 *newmsgs += tmpnew; 
2343                                         if (oldmsgs)
2344                                                 *oldmsgs += tmpold;
2345                                         if (urgentmsgs)
2346                                                 *urgentmsgs += tmpurgent;
2347                                 }
2348                         }
2349                 }
2350                 return 0;
2351         }
2352         if (context) {
2353                 *context = '\0';
2354                 mailboxnc = tmp;
2355                 context++;
2356         } else {
2357                 context = "default";
2358                 mailboxnc = (char *) mailbox_context;
2359         }
2360
2361         if (newmsgs) {
2362                 struct ast_vm_user *vmu = find_user(NULL, context, mailboxnc);
2363                 if (!vmu) {
2364                         ast_log(AST_LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailboxnc, context);
2365                         return -1;
2366                 }
2367                 if ((*newmsgs = __messagecount(context, mailboxnc, vmu->imapfolder)) < 0) {
2368                         return -1;
2369                 }
2370         }
2371         if (oldmsgs) {
2372                 if ((*oldmsgs = __messagecount(context, mailboxnc, "Old")) < 0) {
2373                         return -1;
2374                 }
2375         }
2376         if (urgentmsgs) {
2377                 if ((*urgentmsgs = __messagecount(context, mailboxnc, "Urgent")) < 0) {
2378                         return -1;
2379                 }
2380         }
2381         return 0;
2382 }
2383
2384 /** 
2385  * \brief Determines if the given folder has messages.
2386  * \param mailbox The @ delimited string for user@context. If no context is found, uses 'default' for the context.
2387  * \param folder the folder to look in
2388  *
2389  * This function is used when the mailbox is stored in an IMAP back end.
2390  * This invokes the messagecount(). Here we are interested in the presence of messages (> 0) only, not the actual count.
2391  * \return 1 if the folder has one or more messages. zero otherwise.
2392  */
2393
2394 static int has_voicemail(const char *mailbox, const char *folder)
2395 {
2396         char tmp[256], *tmp2, *box, *context;
2397         ast_copy_string(tmp, mailbox, sizeof(tmp));
2398         tmp2 = tmp;
2399         if (strchr(tmp2, ',') || strchr(tmp2, '&')) {
2400                 while ((box = strsep(&tmp2, ",&"))) {
2401                         if (!ast_strlen_zero(box)) {
2402                                 if (has_voicemail(box, folder)) {
2403                                         return 1;
2404                                 }
2405                         }
2406                 }
2407         }
2408         if ((context = strchr(tmp, '@'))) {
2409                 *context++ = '\0';
2410         } else {
2411                 context = "default";
2412         }
2413         return __messagecount(context, tmp, folder) ? 1 : 0;
2414 }
2415
2416 /*!
2417  * \brief Copies a message from one mailbox to another.
2418  * \param chan
2419  * \param vmu
2420  * \param imbox
2421  * \param msgnum
2422  * \param duration
2423  * \param recip
2424  * \param fmt
2425  * \param dir
2426  *
2427  * This works with IMAP storage based mailboxes.
2428  *
2429  * \return zero on success, -1 on error.
2430  */
2431 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)
2432 {
2433         struct vm_state *sendvms = NULL, *destvms = NULL;
2434         char messagestring[10]; /*I guess this could be a problem if someone has more than 999999999 messages...*/
2435         if (msgnum >= recip->maxmsg) {
2436                 ast_log(LOG_WARNING, "Unable to copy mail, mailbox %s is full\n", recip->mailbox);
2437                 return -1;
2438         }
2439         if (!(sendvms = get_vm_state_by_imapuser(vmu->imapuser, 0))) {
2440                 ast_log(LOG_ERROR, "Couldn't get vm_state for originator's mailbox!!\n");
2441                 return -1;
2442         }
2443         if (!(destvms = get_vm_state_by_imapuser(recip->imapuser, 0))) {
2444                 ast_log(LOG_ERROR, "Couldn't get vm_state for destination mailbox!\n");
2445                 return -1;
2446         }
2447         snprintf(messagestring, sizeof(messagestring), "%ld", sendvms->msgArray[msgnum]);
2448         ast_mutex_lock(&sendvms->lock);
2449         if ((mail_copy(sendvms->mailstream, messagestring, (char *) mbox(vmu, imbox)) == T)) {
2450                 ast_mutex_unlock(&sendvms->lock);
2451                 return 0;
2452         }
2453         ast_mutex_unlock(&sendvms->lock);
2454         ast_log(LOG_WARNING, "Unable to copy message from mailbox %s to mailbox %s\n", vmu->mailbox, recip->mailbox);
2455         return -1;
2456 }
2457
2458 static void imap_mailbox_name(char *spec, size_t len, struct vm_state *vms, int box, int use_folder)
2459 {
2460         char tmp[256], *t = tmp;
2461         size_t left = sizeof(tmp);
2462         
2463         if (box == OLD_FOLDER) {
2464                 ast_copy_string(vms->curbox, mbox(NULL, NEW_FOLDER), sizeof(vms->curbox));
2465         } else {
2466                 ast_copy_string(vms->curbox, mbox(NULL, box), sizeof(vms->curbox));
2467         }
2468
2469         if (box == NEW_FOLDER) {
2470                 ast_copy_string(vms->vmbox, "vm-INBOX", sizeof(vms->vmbox));
2471         } else {
2472                 snprintf(vms->vmbox, sizeof(vms->vmbox), "vm-%s", mbox(NULL, box));
2473         }
2474
2475         /* Build up server information */
2476         ast_build_string(&t, &left, "{%s:%s/imap", imapserver, imapport);
2477
2478         /* Add authentication user if present */
2479         if (!ast_strlen_zero(authuser))
2480                 ast_build_string(&t, &left, "/authuser=%s", authuser);
2481
2482         /* Add flags if present */
2483         if (!ast_strlen_zero(imapflags))
2484                 ast_build_string(&t, &left, "/%s", imapflags);
2485
2486         /* End with username */
2487 #if 1
2488         ast_build_string(&t, &left, "/user=%s}", vms->imapuser);
2489 #else
2490         ast_build_string(&t, &left, "/user=%s/novalidate-cert}", vms->imapuser);
2491 #endif
2492         if (box == NEW_FOLDER || box == OLD_FOLDER)
2493                 snprintf(spec, len, "%s%s", tmp, use_folder? vms->imapfolder: "INBOX");
2494         else if (box == GREETINGS_FOLDER)
2495                 snprintf(spec, len, "%s%s", tmp, greetingfolder);
2496         else {  /* Other folders such as Friends, Family, etc... */
2497                 if (!ast_strlen_zero(imapparentfolder)) {
2498                         /* imapparentfolder would typically be set to INBOX */
2499                         snprintf(spec, len, "%s%s%c%s", tmp, imapparentfolder, delimiter, mbox(NULL, box));
2500                 } else {
2501                         snprintf(spec, len, "%s%s", tmp, mbox(NULL, box));
2502                 }
2503         }
2504 }
2505
2506 static int init_mailstream(struct vm_state *vms, int box)
2507 {
2508         MAILSTREAM *stream = NIL;
2509         long debug;
2510         char tmp[256];
2511
2512         if (!vms) {
2513                 ast_log(LOG_ERROR, "vm_state is NULL!\n");
2514                 return -1;
2515         }
2516         ast_debug(3, "vm_state user is:%s\n", vms->imapuser);
2517         if (vms->mailstream == NIL || !vms->mailstream) {
2518                 ast_debug(1, "mailstream not set.\n");
2519         } else {
2520                 stream = vms->mailstream;
2521         }
2522         /* debug = T;  user wants protocol telemetry? */
2523         debug = NIL;  /* NO protocol telemetry? */
2524
2525         if (delimiter == '\0') {                /* did not probe the server yet */
2526                 char *cp;
2527 #ifdef USE_SYSTEM_IMAP
2528 #include <imap/linkage.c>
2529 #elif defined(USE_SYSTEM_CCLIENT)
2530 #include <c-client/linkage.c>
2531 #else
2532 #include "linkage.c"
2533 #endif
2534                 /* Connect to INBOX first to get folders delimiter */
2535                 imap_mailbox_name(tmp, sizeof(tmp), vms, 0, 1);
2536                 ast_mutex_lock(&vms->lock);
2537                 stream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
2538                 ast_mutex_unlock(&vms->lock);
2539                 if (stream == NIL) {
2540                         ast_log(LOG_ERROR, "Can't connect to imap server %s\n", tmp);
2541                         return -1;
2542                 }
2543                 get_mailbox_delimiter(stream);
2544                 /* update delimiter in imapfolder */
2545                 for (cp = vms->imapfolder; *cp; cp++)
2546                         if (*cp == '/')
2547                                 *cp = delimiter;
2548         }
2549         /* Now connect to the target folder */
2550         imap_mailbox_name(tmp, sizeof(tmp), vms, box, 1);
2551         ast_debug(3, "Before mail_open, server: %s, box:%d\n", tmp, box);
2552         ast_mutex_lock(&vms->lock);
2553         vms->mailstream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL);
2554         ast_mutex_unlock(&vms->lock);
2555         if (vms->mailstream == NIL) {
2556                 return -1;
2557         } else {
2558                 return 0;
2559         }
2560 }
2561
2562 static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box)
2563 {
2564         SEARCHPGM *pgm;
2565         SEARCHHEADER *hdr;
2566         int ret, urgent = 0;
2567
2568         /* If Urgent, then look at INBOX */
2569         if (box == 11) {
2570                 box = NEW_FOLDER;
2571                 urgent = 1;
2572         }
2573
2574         ast_copy_string(vms->imapuser, vmu->imapuser, sizeof(vms->imapuser));
2575         ast_copy_string(vms->imapfolder, vmu->imapfolder, sizeof(vms->imapfolder));
2576         vms->imapversion = vmu->imapversion;
2577         ast_debug(3, "Before init_mailstream, user is %s\n", vmu->imapuser);
2578
2579         if ((ret = init_mailstream(vms, box)) || !vms->mailstream) {
2580                 ast_log(AST_LOG_ERROR, "Could not initialize mailstream\n");
2581                 return -1;
2582         }
2583
2584         create_dirpath(vms->curdir, sizeof(vms->curdir), vmu->context, vms->username, vms->curbox);
2585
2586         /* Check Quota */
2587         if  (box == 0)  {
2588                 ast_debug(3, "Mailbox name set to: %s, about to check quotas\n", mbox(vmu, box));
2589                 check_quota(vms, (char *) mbox(vmu, box));
2590         }
2591
2592         ast_mutex_lock(&vms->lock);
2593         pgm = mail_newsearchpgm();
2594
2595         /* Check IMAP folder for Asterisk messages only... */
2596         hdr = mail_newsearchheader("X-Asterisk-VM-Extension", (!ast_strlen_zero(vmu->imapvmshareid) ? vmu->imapvmshareid : vmu->mailbox));
2597         hdr->next = mail_newsearchheader("X-Asterisk-VM-Context", vmu->context);
2598         pgm->header = hdr;
2599         pgm->deleted = 0;
2600         pgm->undeleted = 1;
2601
2602         /* if box = NEW_FOLDER, check for new, if box = OLD_FOLDER, check for read */
2603         if (box == NEW_FOLDER && urgent == 1) {
2604                 pgm->unseen = 1;
2605                 pgm->seen = 0;
2606                 pgm->flagged = 1;
2607                 pgm->unflagged = 0;
2608         } else if (box == NEW_FOLDER && urgent == 0) {
2609                 pgm->unseen = 1;
2610                 pgm->seen = 0;
2611                 pgm->flagged = 0;
2612                 pgm->unflagged = 1;
2613         } else if (box == OLD_FOLDER) {
2614                 pgm->seen = 1;
2615                 pgm->unseen = 0;
2616         }
2617
2618         ast_debug(3, "Before mail_search_full, user is %s\n", vmu->imapuser);
2619
2620         vms->vmArrayIndex = 0;
2621         mail_search_full (vms->mailstream, NULL, pgm, NIL);
2622         vms->lastmsg = vms->vmArrayIndex - 1;
2623         mail_free_searchpgm(&pgm);
2624         /* Since IMAP storage actually stores both old and new messages in the same IMAP folder,
2625          * ensure to allocate enough space to account for all of them. Warn if old messages
2626          * have not been checked first as that is required.
2627          */
2628         if (box == 0 && !vms->dh_arraysize) {
2629                 ast_log(LOG_WARNING, "The code expects the old messages to be checked first, fix the code.\n");
2630         }
2631         if (vm_allocate_dh(vms, vmu, box == 0 ? vms->vmArrayIndex + vms->oldmessages : vms->lastmsg)) {
2632                 ast_mutex_unlock(&vms->lock);
2633                 return -1;
2634         }
2635
2636         ast_mutex_unlock(&vms->lock);
2637         return 0;
2638 }
2639
2640 static void write_file(char *filename, char *buffer, unsigned long len)
2641 {
2642         FILE *output;
2643
2644         output = fopen (filename, "w");
2645         if (fwrite(buffer, len, 1, output) != 1) {
2646                 if (ferror(output)) {
2647                         ast_log(LOG_ERROR, "Short write while writing e-mail body: %s.\n", strerror(errno));
2648                 }
2649         }
2650         fclose (output);
2651 }
2652
2653 static void update_messages_by_imapuser(const char *user, unsigned long number)
2654 {
2655         struct vm_state *vms = get_vm_state_by_imapuser(user, 1);
2656
2657         if (!vms && !(vms = get_vm_state_by_imapuser(user, 0))) {
2658                 return;
2659         }
2660
2661         ast_debug(3, "saving mailbox message number %lu as message %d. Interactive set to %d\n", number, vms->vmArrayIndex, vms->interactive);
2662         vms->msgArray[vms->vmArrayIndex++] = number;
2663 }
2664
2665 void mm_searched(MAILSTREAM *stream, unsigned long number)
2666 {
2667         char *mailbox = stream->mailbox, buf[1024] = "", *user;
2668
2669         if (!(user = get_user_by_mailbox(mailbox, buf, sizeof(buf))))
2670                 return;
2671
2672         update_messages_by_imapuser(user, number);
2673 }
2674
2675 static struct ast_vm_user *find_user_realtime_imapuser(const char *imapuser)
2676 {
2677         struct ast_variable *var;
2678         struct ast_vm_user *vmu;
2679
2680         vmu = ast_calloc(1, sizeof *vmu);
2681         if (!vmu)
2682                 return NULL;
2683         ast_set_flag(vmu, VM_ALLOCED);
2684         populate_defaults(vmu);
2685
2686         var = ast_load_realtime("voicemail", "imapuser", imapuser, NULL);
2687         if (var) {
2688                 apply_options_full(vmu, var);
2689                 ast_variables_destroy(var);
2690                 return vmu;
2691         } else {
2692                 ast_free(vmu);
2693                 return NULL;
2694         }
2695 }
2696
2697 /* Interfaces to C-client */
2698
2699 void mm_exists(MAILSTREAM * stream, unsigned long number)
2700 {
2701         /* mail_ping will callback here if new mail! */
2702         ast_debug(4, "Entering EXISTS callback for message %ld\n", number);
2703         if (number == 0) return;
2704         set_update(stream);
2705 }
2706
2707
2708 void mm_expunged(MAILSTREAM * stream, unsigned long number)
2709 {
2710         /* mail_ping will callback here if expunged mail! */
2711         ast_debug(4, "Entering EXPUNGE callback for message %ld\n", number);
2712         if (number == 0) return;
2713         set_update(stream);
2714 }
2715
2716
2717 void mm_flags(MAILSTREAM * stream, unsigned long number)
2718 {
2719         /* mail_ping will callback here if read mail! */
2720         ast_debug(4, "Entering FLAGS callback for message %ld\n", number);
2721         if (number == 0) return;
2722         set_update(stream);
2723 }
2724
2725
2726 void mm_notify(MAILSTREAM * stream, char *string, long errflg)
2727 {
2728         ast_debug(5, "Entering NOTIFY callback, errflag is %ld, string is %s\n", errflg, string);
2729         mm_log (string, errflg);
2730 }
2731
2732
2733 void mm_list(MAILSTREAM * stream, int delim, char *mailbox, long attributes)
2734 {
2735         if (delimiter == '\0') {
2736                 delimiter = delim;
2737         }
2738
2739         ast_debug(5, "Delimiter set to %c and mailbox %s\n", delim, mailbox);
2740         if (attributes & LATT_NOINFERIORS)
2741                 ast_debug(5, "no inferiors\n");
2742         if (attributes & LATT_NOSELECT)
2743                 ast_debug(5, "no select\n");
2744         if (attributes & LATT_MARKED)
2745                 ast_debug(5, "marked\n");
2746         if (attributes & LATT_UNMARKED)
2747                 ast_debug(5, "unmarked\n");
2748 }
2749
2750
2751 void mm_lsub(MAILSTREAM * stream, int delim, char *mailbox, long attributes)
2752 {
2753         ast_debug(5, "Delimiter set to %c and mailbox %s\n", delim, mailbox);
2754         if (attributes & LATT_NOINFERIORS)
2755                 ast_debug(5, "no inferiors\n");
2756         if (attributes & LATT_NOSELECT)
2757                 ast_debug(5, "no select\n");
2758         if (attributes & LATT_MARKED)
2759                 ast_debug(5, "marked\n");
2760         if (attributes & LATT_UNMARKED)
2761                 ast_debug(5, "unmarked\n");
2762 }
2763
2764
2765 void mm_status(MAILSTREAM * stream, char *mailbox, MAILSTATUS * status)
2766 {
2767         ast_log(AST_LOG_NOTICE, " Mailbox %s", mailbox);
2768         if (status->flags & SA_MESSAGES)
2769                 ast_log(AST_LOG_NOTICE, ", %lu messages", status->messages);
2770         if (status->flags & SA_RECENT)
2771                 ast_log(AST_LOG_NOTICE, ", %lu recent", status->recent);
2772         if (status->flags & SA_UNSEEN)
2773                 ast_log(AST_LOG_NOTICE, ", %lu unseen", status->unseen);
2774         if (status->flags & SA_UIDVALIDITY)
2775                 ast_log(AST_LOG_NOTICE, ", %lu UID validity", status->uidvalidity);
2776         if (status->flags & SA_UIDNEXT)
2777                 ast_log(AST_LOG_NOTICE, ", %lu next UID", status->uidnext);
2778         ast_log(AST_LOG_NOTICE, "\n");
2779 }
2780
2781
2782 void mm_log(char *string, long errflg)
2783 {
2784         switch ((short) errflg) {
2785                 case NIL:
2786                         ast_debug(1, "IMAP Info: %s\n", string);
2787                         break;
2788                 case PARSE:
2789                 case WARN:
2790                         ast_log(AST_LOG_WARNING, "IMAP Warning: %s\n", string);
2791                         break;
2792                 case ERROR:
2793                         ast_log(AST_LOG_ERROR, "IMAP Error: %s\n", string);
2794                         break;
2795         }
2796 }
2797
2798
2799 void mm_dlog(char *string)
2800 {
2801         ast_log(AST_LOG_NOTICE, "%s\n", string);
2802 }
2803
2804
2805 void mm_login(NETMBX * mb, char *user, char *pwd, long trial)
2806 {
2807         struct ast_vm_user *vmu;
2808
2809         ast_debug(4, "Entering callback mm_login\n");
2810
2811         ast_copy_string(user, mb->user, MAILTMPLEN);
2812
2813         /* We should only do this when necessary */
2814         if (!ast_strlen_zero(authpassword)) {
2815                 ast_copy_string(pwd, authpassword, MAILTMPLEN);
2816         } else {
2817                 AST_LIST_TRAVERSE(&users, vmu, list) {
2818                         if (!strcasecmp(mb->user, vmu->imapuser)) {
2819                                 ast_copy_string(pwd, vmu->imappassword, MAILTMPLEN);
2820                                 break;
2821                         }
2822                 }
2823                 if (!vmu) {
2824                         if ((vmu = find_user_realtime_imapuser(mb->user))) {
2825                                 ast_copy_string(pwd, vmu->imappassword, MAILTMPLEN);
2826                                 free_user(vmu);
2827                         }
2828                 }
2829         }
2830 }
2831
2832
2833 void mm_critical(MAILSTREAM * stream)
2834 {
2835 }
2836
2837
2838 void mm_nocritical(MAILSTREAM * stream)
2839 {
2840 }
2841
2842
2843 long mm_diskerror(MAILSTREAM * stream, long errcode, long serious)
2844 {
2845         kill (getpid (), SIGSTOP);
2846         return NIL;
2847 }
2848
2849
2850 void mm_fatal(char *string)
2851 {
2852         ast_log(AST_LOG_ERROR, "IMAP access FATAL error: %s\n", string);
2853 }
2854
2855 /* C-client callback to handle quota */
2856 static void mm_parsequota(MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pquota)
2857 {
2858         struct vm_state *vms;
2859         char *mailbox = stream->mailbox, *user;
2860         char buf[1024] = "";
2861         unsigned long usage = 0, limit = 0;
2862
2863         while (pquota) {
2864                 usage = pquota->usage;
2865                 limit = pquota->limit;
2866                 pquota = pquota->next;
2867         }
2868
2869         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)))) {
2870                 ast_log(AST_LOG_ERROR, "No state found.\n");
2871                 return;
2872         }
2873
2874         ast_debug(3, "User %s usage is %lu, limit is %lu\n", user, usage, limit);
2875
2876         vms->quota_usage = usage;
2877         vms->quota_limit = limit;
2878 }
2879
2880 static char *get_header_by_tag(char *header, char *tag, char *buf, size_t len)
2881 {
2882         char *start, *eol_pnt;
2883         int taglen;
2884
2885         if (ast_strlen_zero(header) || ast_strlen_zero(tag))
2886                 return NULL;
2887
2888         taglen = strlen(tag) + 1;
2889         if (taglen < 1)
2890                 return NULL;
2891
2892         if (!(start = strstr(header, tag)))
2893                 return NULL;
2894
2895         /* Since we can be called multiple times we should clear our buffer */
2896         memset(buf, 0, len);
2897
2898         ast_copy_string(buf, start+taglen, len);
2899         if ((eol_pnt = strchr(buf,'\r')) || (eol_pnt = strchr(buf,'\n')))
2900                 *eol_pnt = '\0';
2901         return buf;
2902 }
2903
2904 static char *get_user_by_mailbox(char *mailbox, char *buf, size_t len)
2905 {
2906         char *start, *quote, *eol_pnt;
2907
2908         if (ast_strlen_zero(mailbox))
2909                 return NULL;
2910
2911         if (!(start = strstr(mailbox, "/user=")))
2912                 return NULL;
2913
2914         ast_copy_string(buf, start+6, len);
2915
2916         if (!(quote = strchr(buf, '\"'))) {
2917                 if (!(eol_pnt = strchr(buf, '/')))
2918                         eol_pnt = strchr(buf,'}');
2919                 *eol_pnt = '\0';
2920                 return buf;
2921         } else {
2922                 eol_pnt = strchr(buf+1,'\"');
2923                 *eol_pnt = '\0';
2924                 return buf+1;
2925         }
2926 }
2927
2928 static struct vm_state *create_vm_state_from_user(struct ast_vm_user *vmu)
2929 {
2930         struct vm_state *vms_p;
2931
2932         pthread_once(&ts_vmstate.once, ts_vmstate.key_init);
2933         if ((vms_p = pthread_getspecific(ts_vmstate.key)) && !strcmp(vms_p->imapuser, vmu->imapuser) && !strcmp(vms_p->username, vmu->mailbox)) {
2934                 return vms_p;
2935         }
2936         ast_debug(5, "Adding new vmstate for %s\n", vmu->imapuser);
2937         if (!(vms_p = ast_calloc(1, sizeof(*vms_p))))
2938                 return NULL;
2939         ast_copy_string(vms_p->imapuser, vmu->imapuser, sizeof(vms_p->imapuser));
2940         ast_copy_string(vms_p->imapfolder, vmu->imapfolder, sizeof(vms_p->imapfolder));
2941         ast_copy_string(vms_p->username, vmu->mailbox, sizeof(vms_p->username)); /* save for access from interactive entry point */
2942         ast_copy_string(vms_p->context, vmu->context, sizeof(vms_p->context));
2943         vms_p->mailstream = NIL; /* save for access from interactive entry point */
2944         vms_p->imapversion = vmu->imapversion;
2945         ast_debug(5, "Copied %s to %s\n", vmu->imapuser, vms_p->imapuser);
2946         vms_p->updated = 1;
2947         /* set mailbox to INBOX! */
2948         ast_copy_string(vms_p->curbox, mbox(vmu, 0), sizeof(vms_p->curbox));
2949         init_vm_state(vms_p);
2950         vmstate_insert(vms_p);
2951         return vms_p;
2952 }
2953
2954 static struct vm_state *get_vm_state_by_imapuser(const char *user, int interactive)
2955 {
2956         struct vmstate *vlist = NULL;
2957
2958         if (interactive) {
2959                 struct vm_state *vms;
2960                 pthread_once(&ts_vmstate.once, ts_vmstate.key_init);
2961                 vms = pthread_getspecific(ts_vmstate.key);
2962                 return vms;
2963         }
2964
2965         AST_LIST_LOCK(&vmstates);
2966         AST_LIST_TRAVERSE(&vmstates, vlist, list) {
2967                 if (!vlist->vms) {
2968                         ast_debug(3, "error: vms is NULL for %s\n", user);
2969                         continue;
2970                 }
2971                 if (vlist->vms->imapversion != imapversion) {
2972                         continue;
2973                 }
2974                 if (!vlist->vms->imapuser) {
2975                         ast_debug(3, "error: imapuser is NULL for %s\n", user);
2976                         continue;
2977                 }
2978
2979                 if (!strcmp(vlist->vms->imapuser, user) && (interactive == 2 || vlist->vms->interactive == interactive)) {
2980                         AST_LIST_UNLOCK(&vmstates);
2981                         return vlist->vms;
2982                 }
2983         }
2984         AST_LIST_UNLOCK(&vmstates);
2985
2986         ast_debug(3, "%s not found in vmstates\n", user);
2987
2988         return NULL;
2989 }
2990
2991 static struct vm_state *get_vm_state_by_mailbox(const char *mailbox, const char *context, int interactive)
2992 {
2993
2994         struct vmstate *vlist = NULL;
2995         const char *local_context = S_OR(context, "default");
2996
2997         if (interactive) {
2998                 struct vm_state *vms;
2999                 pthread_once(&ts_vmstate.once, ts_vmstate.key_init);
3000                 vms = pthread_getspecific(ts_vmstate.key);
3001                 return vms;
3002         }
3003
3004         AST_LIST_LOCK(&vmstates);
3005         AST_LIST_TRAVERSE(&vmstates, vlist, list) {
3006                 if (!vlist->vms) {
3007                         ast_debug(3, "error: vms is NULL for %s\n", mailbox);
3008                         continue;
3009                 }
3010                 if (vlist->vms->imapversion != imapversion) {
3011                         continue;
3012                 }
3013                 if (!vlist->vms->username || !vlist->vms->context) {
3014                         ast_debug(3, "error: username is NULL for %s\n", mailbox);
3015                         continue;
3016                 }
3017
3018                 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);
3019
3020                 if (!strcmp(vlist->vms->username, mailbox) && !strcmp(vlist->vms->context, local_context) && vlist->vms->interactive == interactive) {
3021                         ast_debug(3, "Found it!\n");
3022                         AST_LIST_UNLOCK(&vmstates);
3023                         return vlist->vms;
3024                 }
3025         }
3026         AST_LIST_UNLOCK(&vmstates);
3027
3028         ast_debug(3, "%s not found in vmstates\n", mailbox);
3029
3030         return NULL;
3031 }
3032
3033 static void vmstate_insert(struct vm_state *vms)
3034 {
3035         struct vmstate *v;
3036         struct vm_state *altvms;
3037
3038         /* If interactive, it probably already exists, and we should
3039            use the one we already have since it is more up to date.
3040            We can compare the username to find the duplicate */
3041         if (vms->interactive == 1) {
3042                 altvms = get_vm_state_by_mailbox(vms->username, vms->context, 0);
3043                 if (altvms) {
3044                         ast_debug(3, "Duplicate mailbox %s, copying message info...\n", vms->username);
3045                         vms->newmessages = altvms->newmessages;
3046                         vms->oldmessages = altvms->oldmessages;
3047                         vms->vmArrayIndex = altvms->vmArrayIndex;
3048                         vms->lastmsg = altvms->lastmsg;
3049                         vms->curmsg = altvms->curmsg;
3050                         /* get a pointer to the persistent store */
3051                         vms->persist_vms = altvms;
3052                         /* Reuse the mailstream? */
3053 #ifdef REALLY_FAST_EVEN_IF_IT_MEANS_RESOURCE_LEAKS
3054                         vms->mailstream = altvms->mailstream;
3055 #else
3056                         vms->mailstream = NIL;
3057 #endif
3058                 }
3059                 return;
3060         }
3061
3062         if (!(v = ast_calloc(1, sizeof(*v))))
3063                 return;
3064
3065         v->vms = vms;
3066
3067         ast_debug(3, "Inserting vm_state for user:%s, mailbox %s\n", vms->imapuser, vms->username);
3068
3069         AST_LIST_LOCK(&vmstates);
3070         AST_LIST_INSERT_TAIL(&vmstates, v, list);
3071         AST_LIST_UNLOCK(&vmstates);
3072 }
3073
3074 static void vmstate_delete(struct vm_state *vms)
3075 {
3076         struct vmstate *vc = NULL;
3077         struct vm_state *altvms = NULL;
3078
3079         /* If interactive, we should copy pertinent info
3080            back to the persistent state (to make update immediate) */
3081         if (vms->interactive == 1 && (altvms = vms->persist_vms)) {
3082                 ast_debug(3, "Duplicate mailbox %s, copying message info...\n", vms->username);
3083                 altvms->newmessages = vms->newmessages;
3084                 altvms->oldmessages = vms->oldmessages;
3085                 altvms->updated = 1;
3086                 vms->mailstream = mail_close(vms->mailstream);
3087
3088                 /* Interactive states are not stored within the persistent list */
3089                 return;
3090         }
3091
3092         ast_debug(3, "Removing vm_state for user:%s, mailbox %s\n", vms->imapuser, vms->username);
3093
3094         AST_LIST_LOCK(&vmstates);
3095         AST_LIST_TRAVERSE_SAFE_BEGIN(&vmstates, vc, list) {
3096                 if (vc->vms == vms) {
3097                         AST_LIST_REMOVE_CURRENT(list);
3098                         break;
3099                 }
3100         }
3101         AST_LIST_TRAVERSE_SAFE_END
3102         AST_LIST_UNLOCK(&vmstates);
3103         
3104         if (vc) {
3105                 ast_mutex_destroy(&vc->vms->lock);
3106                 ast_free(vc);
3107         }
3108         else
3109                 ast_log(AST_LOG_ERROR, "No vmstate found for user:%s, mailbox %s\n", vms->imapuser, vms->username);
3110 }
3111
3112 static void set_update(MAILSTREAM * stream)
3113 {
3114         struct vm_state *vms;
3115         char *mailbox = stream->mailbox, *user;
3116         char buf[1024] = "";
3117
3118         if (!(user = get_user_by_mailbox(mailbox, buf, sizeof(buf))) || !(vms = get_vm_state_by_imapuser(user, 0))) {
3119                 if (user && option_debug > 2)
3120                         ast_log(AST_LOG_WARNING, "User %s mailbox not found for update.\n", user);
3121                 return;
3122         }
3123
3124         ast_debug(3, "User %s mailbox set for update.\n", user);
3125
3126         vms->updated = 1; /* Set updated flag since mailbox changed */
3127 }
3128
3129 static void init_vm_state(struct vm_state *vms)
3130 {
3131         int x;
3132         vms->vmArrayIndex = 0;
3133         for (x = 0; x < VMSTATE_MAX_MSG_ARRAY; x++) {
3134                 vms->msgArray[x] = 0;
3135         }
3136         ast_mutex_init(&vms->lock);
3137 }
3138
3139 static int save_body(BODY *body, struct vm_state *vms, char *section, char *format, int is_intro)
3140 {
3141         char *body_content;
3142         char *body_decoded;
3143         char *fn = is_intro ? vms->introfn : vms->fn;
3144         unsigned long len;
3145         unsigned long newlen;
3146         char filename[256];
3147
3148         if (!body || body == NIL)
3149                 return -1;
3150
3151         ast_mutex_lock(&vms->lock);
3152         body_content = mail_fetchbody(vms->mailstream, vms->msgArray[vms->curmsg], section, &len);
3153         ast_mutex_unlock(&vms->lock);
3154         if (body_content != NIL) {
3155                 snprintf(filename, sizeof(filename), "%s.%s", fn, format);
3156                 /* ast_debug(1, body_content); */
3157                 body_decoded = rfc822_base64((unsigned char *) body_content, len, &newlen);
3158                 /* If the body of the file is empty, return an error */
3159                 if (!newlen) {
3160                         return -1;
3161                 }
3162                 write_file(filename, (char *) body_decoded, newlen);
3163         } else {
3164                 ast_debug(5, "Body of message is NULL.\n");
3165                 return -1;
3166         }
3167         return 0;
3168 }
3169
3170 /*! 
3171  * \brief Get delimiter via mm_list callback 
3172  * \param stream
3173  *
3174  * Determines the delimiter character that is used by the underlying IMAP based mail store.
3175  */
3176 /* MUTEX should already be held */
3177 static void get_mailbox_delimiter(MAILSTREAM *stream) {
3178         char tmp[50];
3179         snprintf(tmp, sizeof(tmp), "{%s}", imapserver);
3180         mail_list(stream, tmp, "*");
3181 }
3182
3183 /*! 
3184  * \brief Check Quota for user 
3185  * \param vms a pointer to a vm_state struct, will use the mailstream property of this.
3186  * \param mailbox the mailbox to check the quota for.
3187  *
3188  * Calls imap_getquotaroot, which will populate its results into the vm_state vms input structure.
3189  */
3190 static void check_quota(struct vm_state *vms, char *mailbox) {
3191         ast_mutex_lock(&vms->lock);
3192         mail_parameters(NULL, SET_QUOTA, (void *) mm_parsequota);
3193         ast_debug(3, "Mailbox name set to: %s, about to check quotas\n", mailbox);
3194         if (vms && vms->mailstream != NULL) {
3195                 imap_getquotaroot(vms->mailstream, mailbox);
3196         } else {
3197                 ast_log(AST_LOG_WARNING, "Mailstream not available for mailbox: %s\n", mailbox);
3198         }
3199         ast_mutex_unlock(&vms->lock);
3200 }
3201
3202 #endif /* IMAP_STORAGE */
3203
3204 /*! \brief Lock file path
3205     only return failure if ast_lock_path returns 'timeout',
3206    not if the path does not exist or any other reason
3207 */
3208 static int vm_lock_path(const char *path)
3209 {
3210         switch (ast_lock_path(path)) {
3211         case AST_LOCK_TIMEOUT:
3212                 return -1;
3213         default:
3214                 return 0;
3215         }
3216 }
3217
3218
3219 #ifdef ODBC_STORAGE
3220 struct generic_prepare_struct {
3221         char *sql;
3222         int argc;
3223         char **argv;
3224 };
3225
3226 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
3227 {
3228         struct generic_prepare_struct *gps = data;
3229         int res, i;
3230         SQLHSTMT stmt;
3231
3232         res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
3233         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3234                 ast_log(AST_LOG_WARNING, "SQL Alloc Handle failed!\n");
3235                 return NULL;
3236         }
3237         res = SQLPrepare(stmt, (unsigned char *) gps->sql, SQL_NTS);
3238         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3239                 ast_log(AST_LOG_WARNING, "SQL Prepare failed![%s]\n", gps->sql);
3240                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
3241                 return NULL;
3242         }
3243         for (i = 0; i < gps->argc; i++)
3244                 SQLBindParameter(stmt, i + 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(gps->argv[i]), 0, gps->argv[i], 0, NULL);
3245
3246         return stmt;
3247 }
3248
3249 /*!
3250  * \brief Retrieves a file from an ODBC data store.
3251  * \param dir the path to the file to be retreived.
3252  * \param msgnum the message number, such as within a mailbox folder.
3253  * 
3254  * This method is used by the RETRIEVE macro when mailboxes are stored in an ODBC back end.
3255  * The purpose is to get the message from the database store to the local file system, so that the message may be played, or the information file may be read.
3256  *
3257  * The file is looked up by invoking a SQL on the odbc_table (default 'voicemessages') using the dir and msgnum input parameters.
3258  * The output is the message information file with the name msgnum and the extension .txt
3259  * and the message file with the extension of its format, in the directory with base file name of the msgnum.
3260  * 
3261  * \return 0 on success, -1 on error.
3262  */
3263 static int retrieve_file(char *dir, int msgnum)
3264 {
3265         int x = 0;
3266         int res;
3267         int fd = -1;
3268         size_t fdlen = 0;
3269         void *fdm = MAP_FAILED;
3270         SQLSMALLINT colcount = 0;
3271         SQLHSTMT stmt;
3272         char sql[PATH_MAX];
3273         char fmt[80]="";
3274         char *c;
3275         char coltitle[256];
3276         SQLSMALLINT collen;
3277         SQLSMALLINT datatype;
3278         SQLSMALLINT decimaldigits;
3279         SQLSMALLINT nullable;
3280         SQLULEN colsize;
3281         SQLLEN colsize2;
3282         FILE *f = NULL;
3283         char rowdata[80];
3284         char fn[PATH_MAX];
3285         char full_fn[PATH_MAX];
3286         char msgnums[80];
3287         char *argv[] = { dir, msgnums };
3288         struct generic_prepare_struct gps = { .sql = sql, .argc = 2, .argv = argv };
3289
3290         struct odbc_obj *obj;
3291         obj = ast_odbc_request_obj(odbc_database, 0);
3292         if (obj) {
3293                 ast_copy_string(fmt, vmfmts, sizeof(fmt));
3294                 c = strchr(fmt, '|');
3295                 if (c)
3296                         *c = '\0';
3297                 if (!strcasecmp(fmt, "wav49"))
3298                         strcpy(fmt, "WAV");
3299                 snprintf(msgnums, sizeof(msgnums), "%d", msgnum);
3300                 if (msgnum > -1)
3301                         make_file(fn, sizeof(fn), dir, msgnum);
3302                 else
3303                         ast_copy_string(fn, dir, sizeof(fn));
3304
3305                 /* Create the information file */
3306                 snprintf(full_fn, sizeof(full_fn), "%s.txt", fn);
3307                 
3308                 if (!(f = fopen(full_fn, "w+"))) {
3309                         ast_log(AST_LOG_WARNING, "Failed to open/create '%s'\n", full_fn);
3310                         goto yuck;
3311                 }
3312                 
3313                 snprintf(full_fn, sizeof(full_fn), "%s.%s", fn, fmt);
3314                 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE dir=? AND msgnum=?", odbc_table);
3315                 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
3316                 if (!stmt) {
3317                         ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
3318                         ast_odbc_release_obj(obj);
3319                         goto yuck;
3320                 }
3321                 res = SQLFetch(stmt);
3322                 if (res == SQL_NO_DATA) {
3323                         SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3324                         ast_odbc_release_obj(obj);
3325                         goto yuck;
3326                 } else if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3327                         ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
3328                         SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3329                         ast_odbc_release_obj(obj);
3330                         goto yuck;
3331                 }
3332                 fd = open(full_fn, O_RDWR | O_CREAT | O_TRUNC, VOICEMAIL_FILE_MODE);
3333                 if (fd < 0) {
3334                         ast_log(AST_LOG_WARNING, "Failed to write '%s': %s\n", full_fn, strerror(errno));
3335                         SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3336                         ast_odbc_release_obj(obj);
3337                         goto yuck;
3338                 }
3339                 res = SQLNumResultCols(stmt, &colcount);
3340                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {   
3341                         ast_log(AST_LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
3342                         SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3343                         ast_odbc_release_obj(obj);
3344                         goto yuck;
3345                 }
3346                 if (f) 
3347                         fprintf(f, "[message]\n");
3348                 for (x = 0; x < colcount; x++) {
3349                         rowdata[0] = '\0';
3350                         colsize = 0;
3351                         collen = sizeof(coltitle);
3352                         res = SQLDescribeCol(stmt, x + 1, (unsigned char *) coltitle, sizeof(coltitle), &collen, 
3353                                                 &datatype, &colsize, &decimaldigits, &nullable);
3354                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3355                                 ast_log(AST_LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
3356                                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3357                                 ast_odbc_release_obj(obj);
3358                                 goto yuck;
3359                         }
3360                         if (!strcasecmp(coltitle, "recording")) {
3361                                 off_t offset;
3362                                 res = SQLGetData(stmt, x + 1, SQL_BINARY, rowdata, 0, &colsize2);
3363                                 fdlen = colsize2;
3364                                 if (fd > -1) {
3365                                         char tmp[1]="";
3366                                         lseek(fd, fdlen - 1, SEEK_SET);
3367                                         if (write(fd, tmp, 1) != 1) {
3368                                                 close(fd);
3369                                                 fd = -1;
3370                                                 continue;
3371                                         }
3372                                         /* Read out in small chunks */
3373                                         for (offset = 0; offset < colsize2; offset += CHUNKSIZE) {
3374                                                 if ((fdm = mmap(NULL, CHUNKSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset)) == MAP_FAILED) {
3375                                                         ast_log(AST_LOG_WARNING, "Could not mmap the output file: %s (%d)\n", strerror(errno), errno);
3376                                                         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
3377                                                         ast_odbc_release_obj(obj);
3378                                                         goto yuck;
3379                                                 } else {
3380                                                         res = SQLGetData(stmt, x + 1, SQL_BINARY, fdm, CHUNKSIZE, NULL);
3381                                                         munmap(fdm, CHUNKSIZE);
3382                                                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3383                                                                 ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
3384                                                                 unlink(full_fn);
3385                                                                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
3386                                                                 ast_odbc_release_obj(obj);
3387                                                                 goto yuck;
3388                                                         }
3389                                                 }
3390                                         }
3391                                         if (truncate(full_fn, fdlen) < 0) {
3392                                                 ast_log(LOG_WARNING, "Unable to truncate '%s': %s\n", full_fn, strerror(errno));
3393                                         }
3394                                 }
3395                         } else {
3396                                 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
3397                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3398                                         ast_log(AST_LOG_WARNING, "SQL Get Data error! coltitle=%s\n[%s]\n\n", coltitle, sql);
3399                                         SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3400                                         ast_odbc_release_obj(obj);
3401                                         goto yuck;
3402                                 }
3403                                 if (strcasecmp(coltitle, "msgnum") && strcasecmp(coltitle, "dir") && f)
3404                                         fprintf(f, "%s=%s\n", coltitle, rowdata);
3405                         }
3406                 }
3407                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3408                 ast_odbc_release_obj(obj);
3409         } else
3410                 ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
3411 yuck:
3412         if (f)
3413                 fclose(f);
3414         if (fd > -1)
3415                 close(fd);
3416         return x - 1;
3417 }
3418
3419 /*!
3420  * \brief Determines the highest message number in use for a given user and mailbox folder.
3421  * \param vmu 
3422  * \param dir the folder the mailbox folder to look for messages. Used to construct the SQL where clause.
3423  *
3424  * This method is used when mailboxes are stored in an ODBC back end.
3425  * Typical use to set the msgnum would be to take the value returned from this method and add one to it.
3426  *
3427  * \return the value of zero or greater to indicate the last message index in use, -1 to indicate none.
3428
3429  */
3430 static int last_message_index(struct ast_vm_user *vmu, char *dir)
3431 {
3432         int x = 0;
3433         int res;
3434         SQLHSTMT stmt;
3435         char sql[PATH_MAX];
3436         char rowdata[20];
3437         char *argv[] = { dir };
3438         struct generic_prepare_struct gps = { .sql = sql, .argc = 1, .argv = argv };
3439
3440         struct odbc_obj *obj;
3441         obj = ast_odbc_request_obj(odbc_database, 0);
3442         if (obj) {
3443                 snprintf(sql, sizeof(sql), "SELECT msgnum FROM %s WHERE dir=? order by msgnum desc limit 1", odbc_table);
3444
3445                 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
3446                 if (!stmt) {
3447                         ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
3448                         ast_odbc_release_obj(obj);
3449                         goto yuck;
3450                 }
3451                 res = SQLFetch(stmt);
3452                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3453                         if (res == SQL_NO_DATA) {
3454                                 ast_log(AST_LOG_DEBUG, "Directory '%s' has no messages and therefore no index was retrieved.\n", dir);
3455                         } else {
3456                                 ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
3457                         }
3458
3459                         SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3460                         ast_odbc_release_obj(obj);
3461                         goto yuck;
3462                 }
3463                 res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
3464                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3465                         ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
3466                         SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3467                         ast_odbc_release_obj(obj);
3468                         goto yuck;
3469                 }
3470                 if (sscanf(rowdata, "%30d", &x) != 1)
3471                         ast_log(AST_LOG_WARNING, "Failed to read message index!\n");
3472                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3473                 ast_odbc_release_obj(obj);
3474                 return x;
3475         } else
3476                 ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
3477 yuck:
3478         return x - 1;
3479 }
3480
3481 /*!
3482  * \brief Determines if the specified message exists.
3483  * \param dir the folder the mailbox folder to look for messages. 
3484  * \param msgnum the message index to query for.
3485  *
3486  * This method is used when mailboxes are stored in an ODBC back end.
3487  *
3488  * \return greater than zero if the message exists, zero when the message does not exist or on error.
3489  */
3490 static int message_exists(char *dir, int msgnum)
3491 {
3492         int x = 0;
3493         int res;
3494         SQLHSTMT stmt;
3495         char sql[PATH_MAX];
3496         char rowdata[20];
3497         char msgnums[20];
3498         char *argv[] = { dir, msgnums };
3499         struct generic_prepare_struct gps = { .sql = sql, .argc = 2, .argv = argv };
3500
3501         struct odbc_obj *obj;
3502         obj = ast_odbc_request_obj(odbc_database, 0);
3503         if (obj) {
3504                 snprintf(msgnums, sizeof(msgnums), "%d", msgnum);
3505                 snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=? AND msgnum=?", odbc_table);
3506                 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
3507                 if (!stmt) {
3508                         ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
3509                         ast_odbc_release_obj(obj);
3510                         goto yuck;
3511                 }
3512                 res = SQLFetch(stmt);
3513                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3514                         ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
3515                         SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3516                         ast_odbc_release_obj(obj);
3517                         goto yuck;
3518                 }
3519                 res = SQLGetData(stmt, 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
3520                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3521                         ast_log(AST_LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
3522                         SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3523                         ast_odbc_release_obj(obj);
3524                         goto yuck;
3525                 }
3526                 if (sscanf(rowdata, "%30d", &x) != 1)
3527                         ast_log(AST_LOG_WARNING, "Failed to read message count!\n");
3528                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
3529                 ast_odbc_release_obj(obj);
3530         } else
3531                 ast_log(AST_LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
3532 yuck:
3533         return x;
3534 }
3535
3536 /*!
3537  * \brief returns the number of messages found.
3538  * \param vmu
3539  * \param dir the folder the mailbox folder to look for messages. Used to construct the SQL where clause.
3540  *
3541  * This method is used when mailboxes are stored in an ODBC back end.
3542  *
3543  * \return The count of messages being zero or more, less than zero on error.
3544  */
3545 static int count_messages(struct ast_vm_user *vmu, char *dir)
3546 {
3547         int x = 0;
3548         int res;
3549         SQLHSTMT stmt;
3550         char sql[PATH_MAX];
3551         char rowdata[20];
3552         char *argv[] = { dir };
3553         struct generic_prepare_struct gps = { .sql = sql, .argc = 1, .argv = argv };
3554
3555         struct odbc_obj *obj;
3556         obj = ast_odbc_request_obj(odbc_database, 0);
3557         if (obj) {
3558                 snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=?", odbc_table);
3559                 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
3560                 if (!stmt) {
3561                         ast_log(AST_LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
3562                         ast_odbc_release_obj(obj);
3563                         goto yuck;
3564                 }
3565                 res = SQLFetch(stmt);
3566                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
3567                         ast_log(AST_LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
3568                         SQLFreeHandle (SQL_HANDLE_STMT, stmt);