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