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