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