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