res_mwi_external: Clear the stasis cache entry when the external MWI is deleted.
[asterisk/asterisk.git] / include / asterisk / app.h
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17
18 /*! \file
19  * \brief Application convenience functions, designed to give consistent
20  *        look and feel to Asterisk apps.
21  */
22
23 #ifndef _ASTERISK_APP_H
24 #define _ASTERISK_APP_H
25
26 #include "asterisk/stringfields.h"
27 #include "asterisk/strings.h"
28 #include "asterisk/threadstorage.h"
29 #include "asterisk/file.h"
30 #include "asterisk/linkedlists.h"
31 #include "asterisk/utils.h"
32 #include "asterisk/stasis.h"
33
34 struct ast_flags64;
35
36 #if defined(__cplusplus) || defined(c_plusplus)
37 extern "C" {
38 #endif
39
40 AST_THREADSTORAGE_EXTERNAL(ast_str_thread_global_buf);
41
42 /* IVR stuff */
43
44 /*! \brief Callback function for IVR
45     \return returns 0 on completion, -1 on hangup or digit if interrupted
46   */
47 typedef int (ast_ivr_callback)(struct ast_channel *chan, char *option, void *cbdata);
48
49 typedef enum {
50         AST_ACTION_UPONE,       /*!< adata is unused */
51         AST_ACTION_EXIT,        /*!< adata is the return value for ast_ivr_menu_run if channel was not hungup */
52         AST_ACTION_CALLBACK,    /*!< adata is an ast_ivr_callback */
53         AST_ACTION_PLAYBACK,    /*!< adata is file to play */
54         AST_ACTION_BACKGROUND,  /*!< adata is file to play */
55         AST_ACTION_PLAYLIST,    /*!< adata is list of files, separated by ; to play */
56         AST_ACTION_MENU,        /*!< adata is a pointer to an ast_ivr_menu */
57         AST_ACTION_REPEAT,      /*!< adata is max # of repeats, cast to a pointer */
58         AST_ACTION_RESTART,     /*!< adata is like repeat, but resets repeats to 0 */
59         AST_ACTION_TRANSFER,    /*!< adata is a string with exten\verbatim[@context]\endverbatim */
60         AST_ACTION_WAITOPTION,  /*!< adata is a timeout, or 0 for defaults */
61         AST_ACTION_NOOP,        /*!< adata is unused */
62         AST_ACTION_BACKLIST,    /*!< adata is list of files separated by ; allows interruption */
63 } ast_ivr_action;
64
65 /*!
66     Special "options" are:
67    \arg "s" - "start here (one time greeting)"
68    \arg "g" - "greeting/instructions"
69    \arg "t" - "timeout"
70    \arg "h" - "hangup"
71    \arg "i" - "invalid selection"
72
73 */
74 struct ast_ivr_option {
75         char *option;
76         ast_ivr_action action;
77         void *adata;
78 };
79
80 struct ast_ivr_menu {
81         char *title;            /*!< Title of menu */
82         unsigned int flags;     /*!< Flags */
83         struct ast_ivr_option *options; /*!< All options */
84 };
85
86 /*!
87  * \brief Structure used for ast_copy_recording_to_vm in order to cleanly supply
88  * data needed for making the recording from the recorded file.
89  */
90 struct ast_vm_recording_data {
91         AST_DECLARE_STRING_FIELDS(
92                 AST_STRING_FIELD(context);
93                 AST_STRING_FIELD(mailbox);
94                 AST_STRING_FIELD(folder);
95                 AST_STRING_FIELD(recording_file);
96                 AST_STRING_FIELD(recording_ext);
97
98                 AST_STRING_FIELD(call_context);
99                 AST_STRING_FIELD(call_macrocontext);
100                 AST_STRING_FIELD(call_extension);
101                 AST_STRING_FIELD(call_callerchan);
102                 AST_STRING_FIELD(call_callerid);
103                 );
104         int call_priority;
105 };
106
107 #define AST_IVR_FLAG_AUTORESTART (1 << 0)
108
109 #define AST_IVR_DECLARE_MENU(holder, title, flags, foo...) \
110         static struct ast_ivr_option __options_##holder[] = foo;\
111         static struct ast_ivr_menu holder = { title, flags, __options_##holder }
112
113 enum ast_timelen {
114         TIMELEN_HOURS,
115         TIMELEN_MINUTES,
116         TIMELEN_SECONDS,
117         TIMELEN_MILLISECONDS,
118 };
119
120 /*!     \brief Runs an IVR menu
121         \return returns 0 on successful completion, -1 on hangup, or -2 on user error in menu */
122 int ast_ivr_menu_run(struct ast_channel *c, struct ast_ivr_menu *menu, void *cbdata);
123
124 /*! \brief Plays a stream and gets DTMF data from a channel
125  * \param c Which channel one is interacting with
126  * \param prompt File to pass to ast_streamfile (the one that you wish to play).
127  *        It is also valid for this to be multiple files concatenated by "&".
128  *        For example, "file1&file2&file3".
129  * \param s The location where the DTMF data will be stored
130  * \param maxlen Max Length of the data
131  * \param timeout Timeout length waiting for data(in milliseconds).  Set to 0 for standard timeout(six seconds), or -1 for no time out.
132  *
133  *  This function was designed for application programmers for situations where they need
134  *  to play a message and then get some DTMF data in response to the message.  If a digit
135  *  is pressed during playback, it will immediately break out of the message and continue
136  *  execution of your code.
137  */
138 int ast_app_getdata(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout);
139
140 /*! \brief Full version with audiofd and controlfd.  NOTE: returns '2' on ctrlfd available, not '1' like other full functions */
141 int ast_app_getdata_full(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd);
142
143 /*!
144  * \brief Run a macro on a channel, placing an optional second channel into autoservice.
145  * \since 11.0
146  *
147  * \details
148  * This is a shorthand method that makes it very easy to run a
149  * macro on any given channel.  It is perfectly reasonable to
150  * supply a NULL autoservice_chan here in case there is no
151  * channel to place into autoservice.
152  *
153  * \note Absolutely _NO_ channel locks should be held before calling this function.
154  *
155  * \param autoservice_chan A channel to place into autoservice while the macro is run
156  * \param macro_chan Channel to execute macro on.
157  * \param macro_args Macro application argument string.
158  *
159  * \retval 0 success
160  * \retval -1 on error
161  */
162 int ast_app_exec_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const char *macro_args);
163
164 /*!
165  * \since 1.8
166  * \brief Run a macro on a channel, placing an optional second channel into autoservice.
167  *
168  * \details
169  * This is a shorthand method that makes it very easy to run a
170  * macro on any given channel.  It is perfectly reasonable to
171  * supply a NULL autoservice_chan here in case there is no
172  * channel to place into autoservice.
173  *
174  * \note Absolutely _NO_ channel locks should be held before calling this function.
175  *
176  * \param autoservice_chan A channel to place into autoservice while the macro is run
177  * \param macro_chan Channel to execute macro on.
178  * \param macro_name The name of the macro to run.
179  * \param macro_args The arguments to pass to the macro.
180  *
181  * \retval 0 success
182  * \retval -1 on error
183  */
184 int ast_app_run_macro(struct ast_channel *autoservice_chan,
185         struct ast_channel *macro_chan, const char *macro_name, const char *macro_args);
186
187 /*!
188  * \brief Stack applications callback functions.
189  */
190 struct ast_app_stack_funcs {
191         /*!
192          * Module reference pointer so the module will stick around
193          * while a callback is active.
194          */
195         void *module;
196
197         /*!
198          * \brief Callback for the routine to run a subroutine on a channel.
199          *
200          * \note Absolutely _NO_ channel locks should be held before calling this function.
201          *
202          * \param chan Channel to execute subroutine on.
203          * \param args Gosub application argument string.
204          * \param ignore_hangup TRUE if a hangup does not stop execution of the routine.
205          *
206          * \retval 0 success
207          * \retval -1 on error
208          */
209         int (*run_sub)(struct ast_channel *chan, const char *args, int ignore_hangup);
210
211         /*!
212          * \brief Add missing context/exten to Gosub application argument string.
213          *
214          * \param chan Channel to obtain context/exten.
215          * \param args Gosub application argument string.
216          *
217          * \details
218          * Fills in the optional context and exten from the given channel.
219          *
220          * \retval New-args Gosub argument string on success.  Must be freed.
221          * \retval NULL on error.
222          */
223         const char *(*expand_sub_args)(struct ast_channel *chan, const char *args);
224
225         /* Add new API calls to the end here. */
226 };
227
228 /*!
229  * \since 11
230  * \brief Set stack application function callbacks
231  * \param funcs Stack applications callback functions.
232  */
233 void ast_install_stack_functions(const struct ast_app_stack_funcs *funcs);
234
235 /*!
236  * \brief Add missing context/exten to subroutine argument string.
237  *
238  * \param chan Channel to obtain context/exten.
239  * \param args Gosub application argument string.
240  *
241  * \details
242  * Fills in the optional context and exten from the given channel.
243  *
244  * \retval New-args Gosub argument string on success.  Must be freed.
245  * \retval NULL on error.
246  */
247 const char *ast_app_expand_sub_args(struct ast_channel *chan, const char *args);
248
249 /*!
250  * \since 11
251  * \brief Run a subroutine on a channel, placing an optional second channel into autoservice.
252  *
253  * \details
254  * This is a shorthand method that makes it very easy to run a
255  * subroutine on any given channel.  It is perfectly reasonable
256  * to supply a NULL autoservice_chan here in case there is no
257  * channel to place into autoservice.
258  *
259  * \note Absolutely _NO_ channel locks should be held before calling this function.
260  *
261  * \param autoservice_chan A channel to place into autoservice while the subroutine is run
262  * \param sub_chan Channel to execute subroutine on.
263  * \param sub_args Gosub application argument string.
264  * \param ignore_hangup TRUE if a hangup does not stop execution of the routine.
265  *
266  * \retval 0 success
267  * \retval -1 on error
268  */
269 int ast_app_exec_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const char *sub_args, int ignore_hangup);
270
271 /*!
272  * \since 11
273  * \brief Run a subroutine on a channel, placing an optional second channel into autoservice.
274  *
275  * \details
276  * This is a shorthand method that makes it very easy to run a
277  * subroutine on any given channel.  It is perfectly reasonable
278  * to supply a NULL autoservice_chan here in case there is no
279  * channel to place into autoservice.
280  *
281  * \note Absolutely _NO_ channel locks should be held before calling this function.
282  *
283  * \param autoservice_chan A channel to place into autoservice while the subroutine is run
284  * \param sub_chan Channel to execute subroutine on.
285  * \param sub_location The location of the subroutine to run.
286  * \param sub_args The arguments to pass to the subroutine.
287  * \param ignore_hangup TRUE if a hangup does not stop execution of the routine.
288  *
289  * \retval 0 success
290  * \retval -1 on error
291  */
292 int ast_app_run_sub(struct ast_channel *autoservice_chan,
293         struct ast_channel *sub_chan, const char *sub_location, const char *sub_args, int ignore_hangup);
294
295 enum ast_vm_snapshot_sort_val {
296         AST_VM_SNAPSHOT_SORT_BY_ID = 0,
297         AST_VM_SNAPSHOT_SORT_BY_TIME,
298 };
299
300 struct ast_vm_msg_snapshot {
301         AST_DECLARE_STRING_FIELDS(
302                 AST_STRING_FIELD(msg_id);
303                 AST_STRING_FIELD(callerid);
304                 AST_STRING_FIELD(callerchan);
305                 AST_STRING_FIELD(exten);
306                 AST_STRING_FIELD(origdate);
307                 AST_STRING_FIELD(origtime);
308                 AST_STRING_FIELD(duration);
309                 AST_STRING_FIELD(folder_name);
310                 AST_STRING_FIELD(flag);
311         );
312         unsigned int msg_number;
313
314         AST_LIST_ENTRY(ast_vm_msg_snapshot) msg;
315 };
316
317 struct ast_vm_mailbox_snapshot {
318         int total_msg_num;
319         int folders;
320         /* Things are not quite as they seem here.  This points to an allocated array of lists. */
321         AST_LIST_HEAD_NOLOCK(, ast_vm_msg_snapshot) *snapshots;
322 };
323
324 /*!
325  * \brief Voicemail playback callback function definition
326  *
327  * \param chan Channel to play the file back on.
328  * \param playfile Location of file on disk
329  * \param duration of file in seconds. This will be zero if msg is very short or
330  * has an unknown duration.
331  */
332 typedef void (ast_vm_msg_play_cb)(struct ast_channel *chan, const char *playfile, int duration);
333
334 /*!
335  * \brief Determines if the given folder has messages.
336  *
337  * \param mailboxes Comma or & delimited list of mailboxes (user@context).
338  *          If no context is found, uses 'default' for the context.
339  * \param folder The folder to look in.  Default is INBOX if not provided.
340  *
341  * \retval 1 if the folder has one or more messages.
342  * \retval 0 otherwise.
343  */
344 typedef int (ast_has_voicemail_fn)(const char *mailboxes, const char *folder);
345
346 /*!
347  * \brief Gets the number of messages that exist for the mailbox list.
348  *
349  * \param mailboxes Comma or space delimited list of mailboxes (user@context).
350  *          If no context is found, uses 'default' for the context.
351  * \param newmsgs Where to put the count of new messages. (Can be NULL)
352  * \param oldmsgs Where to put the count of old messages. (Can be NULL)
353  *
354  * \details
355  * Simultaneously determines the count of new + urgent and old
356  * messages.  The total messages would then be the sum of these.
357  *
358  * \retval 0 on success
359  * \retval -1 on failure
360  */
361 typedef int (ast_inboxcount_fn)(const char *mailboxes, int *newmsgs, int *oldmsgs);
362
363 /*!
364  * \brief Gets the number of messages that exist for the mailbox list.
365  *
366  * \param mailboxes Comma or space delimited list of mailboxes (user@context).
367  *          If no context is found, uses 'default' for the context.
368  * \param urgentmsgs Where to put the count of urgent messages. (Can be NULL)
369  * \param newmsgs Where to put the count of new messages. (Can be NULL)
370  * \param oldmsgs Where to put the count of old messages. (Can be NULL)
371  *
372  * \details
373  * Simultaneously determines the count of new, old, and urgent
374  * messages.  The total messages would then be the sum of these
375  * three.
376  *
377  * \retval 0 on success
378  * \retval -1 on failure
379  */
380 typedef int (ast_inboxcount2_fn)(const char *mailboxes, int *urgentmsgs, int *newmsgs, int *oldmsgs);
381
382 /*!
383  * \brief Gets the number of messages that exist in a mailbox folder.
384  *
385  * \param mailbox_id The mailbox name.
386  * \param folder The folder to look in.  Default is INBOX if not provided.
387  *
388  * \note If requesting INBOX then the returned count is INBOX + Urgent.
389  *
390  * \return The number of messages in the mailbox folder (zero or more).
391  */
392 typedef int (ast_messagecount_fn)(const char *mailbox_id, const char *folder);
393
394 /*!
395  * \brief Play a recorded user name for the mailbox.
396  *
397  * \param chan Where to play the recorded name file.
398  * \param user The user part of user@context.
399  * \param context The context part of user@context.  Must be explicit.
400  *
401  * \retval 0 Name played without interruption
402  * \retval dtmf ASCII value of the DTMF which interrupted playback
403  * \retval -1 on failure
404  */
405 typedef int (ast_sayname_fn)(struct ast_channel *chan, const char *user, const char *context);
406
407 /*!
408  * \brief Creates a voicemail based on a specified file to a mailbox.
409  *
410  * \param vm_rec_data A record containing filename and voicemail txt info.
411  *
412  * \retval 0 on success
413  * \retval -1 on failure
414  */
415 typedef int (ast_copy_recording_to_vm_fn)(struct ast_vm_recording_data *vm_rec_data);
416
417 /*!
418  * \brief Convert the mailbox folder id to a folder name.
419  *
420  * \param id Mailbox folder id to convert.
421  *
422  * \deprecated Nothing calls it and nothing ever should.
423  *
424  * \return The folder name associated with the id.
425  */
426 typedef const char *(ast_vm_index_to_foldername_fn)(int id);
427
428 /*!
429  * \brief Create a snapshot of a mailbox which contains information about every msg.
430  *
431  * \param user The user part of user@context.
432  * \param context The context part of user@context.  Must be explicit.
433  * \param folder When not NULL only msgs from the specified folder will be included.
434  * \param descending list the msgs in descending order rather than ascending order.
435  * \param sort_val What to sort in the snapshot.
436  * \param combine_INBOX_and_OLD When this argument is set, The OLD folder will be represented
437  *        in the INBOX folder of the snapshot. This allows the snapshot to represent the
438  *        OLD and INBOX messages in sorted order merged together.
439  *
440  * \note Only used by voicemail unit tests.
441  *
442  * \retval snapshot on success
443  * \retval NULL on failure
444  */
445 typedef struct ast_vm_mailbox_snapshot *(ast_vm_mailbox_snapshot_create_fn)(const char *user,
446         const char *context, const char *folder, int descending,
447         enum ast_vm_snapshot_sort_val sort_val, int combine_INBOX_and_OLD);
448
449 /*!
450  * \brief destroy a snapshot
451  *
452  * \param mailbox_snapshot The snapshot to destroy.
453  *
454  * \note Only used by voicemail unit tests.
455  *
456  * \retval NULL
457  */
458 typedef struct ast_vm_mailbox_snapshot *(ast_vm_mailbox_snapshot_destroy_fn)(struct ast_vm_mailbox_snapshot *mailbox_snapshot);
459
460 /*!
461  * \brief Move messages from one folder to another
462  *
463  * \param mailbox The mailbox to which the folders belong
464  * \param context The voicemail context for the mailbox
465  * \param num_msgs The number of messages to move
466  * \param oldfolder The folder from where messages should be moved
467  * \param old_msg_ids The message IDs of the messages to move
468  * \param newfolder The folder to which messages should be moved
469  *    new folder. This array must be num_msgs sized.
470  *
471  * \note Only used by voicemail unit tests.
472  *
473  * \retval -1 Failure
474  * \retval 0 Success
475  */
476 typedef int (ast_vm_msg_move_fn)(const char *mailbox, const char *context, size_t num_msgs,
477         const char *oldfolder, const char *old_msg_ids[], const char *newfolder);
478
479 /*!
480  * \brief Remove/delete messages from a mailbox folder.
481  *
482  * \param mailbox The mailbox from which to delete messages
483  * \param context The voicemail context for the mailbox
484  * \param num_msgs The number of messages to delete
485  * \param folder The folder from which to remove messages
486  * \param msgs The message IDs of the messages to delete
487  *
488  * \note Only used by voicemail unit tests.
489  *
490  * \retval -1 Failure
491  * \retval 0 Success
492  */
493 typedef int (ast_vm_msg_remove_fn)(const char *mailbox, const char *context, size_t num_msgs,
494         const char *folder, const char *msgs[]);
495
496 /*!
497  * \brief forward a message from one mailbox to another.
498  *
499  * \brief from_mailbox The original mailbox the message is being forwarded from
500  * \brief from_context The voicemail context of the from_mailbox
501  * \brief from_folder The folder from which the message is being forwarded
502  * \brief to_mailbox The mailbox to forward the message to
503  * \brief to_context The voicemail context of the to_mailbox
504  * \brief to_folder The folder to which the message is being forwarded
505  * \brief num_msgs The number of messages being forwarded
506  * \brief msg_ids The message IDs of the messages in from_mailbox to forward
507  * \brief delete_old If non-zero, the forwarded messages are also deleted from from_mailbox.
508  * Otherwise, the messages will remain in the from_mailbox.
509  *
510  * \note Only used by voicemail unit tests.
511  *
512  * \retval -1 Failure
513  * \retval 0 Success
514  */
515 typedef int (ast_vm_msg_forward_fn)(const char *from_mailbox, const char *from_context,
516         const char *from_folder, const char *to_mailbox, const char *to_context,
517         const char *to_folder, size_t num_msgs, const char *msg_ids[], int delete_old);
518
519 /*!
520  * \brief Play a voicemail msg back on a channel.
521  *
522  * \param chan
523  * \param mailbox msg is in.
524  * \param context of mailbox.
525  * \param folder voicemail folder to look in.
526  * \param msg_num message number in the voicemailbox to playback to the channel.
527  * \param cb
528  *
529  * \note Only used by voicemail unit tests.
530  *
531  * \retval 0 success
532  * \retval -1 failure
533  */
534 typedef int (ast_vm_msg_play_fn)(struct ast_channel *chan, const char *mailbox,
535         const char *context, const char *folder, const char *msg_num, ast_vm_msg_play_cb *cb);
536
537 #define VM_MODULE_VERSION 1
538
539 /*! \brief Voicemail function table definition. */
540 struct ast_vm_functions {
541         /*!
542          * \brief The version of this function table.
543          *
544          * \note If the ABI for this table changes, the module version
545          * (\ref VM_MODULE_VERSION) should be incremented.
546          */
547         unsigned int module_version;
548         /*! \brief The name of the module that provides the voicemail functionality */
549         const char *module_name;
550         /*! \brief The module for the voicemail provider */
551         struct ast_module *module;
552
553         ast_has_voicemail_fn *has_voicemail;
554         ast_inboxcount_fn *inboxcount;
555         ast_inboxcount2_fn *inboxcount2;
556         ast_messagecount_fn *messagecount;
557         ast_sayname_fn *sayname;
558         ast_copy_recording_to_vm_fn *copy_recording_to_vm;
559         ast_vm_index_to_foldername_fn *index_to_foldername;
560         ast_vm_mailbox_snapshot_create_fn *mailbox_snapshot_create;
561         ast_vm_mailbox_snapshot_destroy_fn *mailbox_snapshot_destroy;
562         ast_vm_msg_move_fn *msg_move;
563         ast_vm_msg_remove_fn *msg_remove;
564         ast_vm_msg_forward_fn *msg_forward;
565         ast_vm_msg_play_fn *msg_play;
566 };
567
568 /*!
569  * \brief Determine if a voicemail provider is registered.
570  * \since 12.0.0
571  *
572  * \retval 0 if no privider registered.
573  * \retval 1 if a privider is registered.
574  */
575 int ast_vm_is_registered(void);
576
577 /*!
578  * \brief Set voicemail function callbacks
579  *
580  * \param vm_table Voicemail function table to install.
581  * \param module Pointer to the module implementing the interface
582  *
583  * \retval 0 on success.
584  * \retval -1 on error.
585  */
586 int __ast_vm_register(const struct ast_vm_functions *vm_table, struct ast_module *module);
587
588 /*! \brief See \ref __ast_vm_register() */
589 #define ast_vm_register(vm_table) __ast_vm_register(vm_table, ast_module_info ? ast_module_info->self : NULL)
590
591 /*!
592  * \brief Unregister the specified voicemail provider
593  *
594  * \param The module name of the provider to unregister
595  *
596  * \return Nothing
597  */
598 void ast_vm_unregister(const char *module_name);
599
600 #ifdef TEST_FRAMEWORK
601 typedef int (ast_vm_test_create_user_fn)(const char *context, const char *user);
602 typedef int (ast_vm_test_destroy_user_fn)(const char *context, const char *user);
603
604 void ast_install_vm_test_functions(ast_vm_test_create_user_fn *vm_test_create_user_func,
605         ast_vm_test_destroy_user_fn *vm_test_destroy_user_func);
606
607 void ast_uninstall_vm_test_functions(void);
608 #endif
609
610 /*!
611  * \brief
612  * param[in] vm_rec_data Contains data needed to make the recording.
613  * retval 0 voicemail successfully created from recording.
614  * retval -1 Failure
615  */
616 int ast_app_copy_recording_to_vm(struct ast_vm_recording_data *vm_rec_data);
617
618 /*!
619  * \brief Determine if a given mailbox has any voicemail
620  * If folder is NULL, defaults to "INBOX".  If folder is "INBOX", includes the
621  * number of messages in the "Urgent" folder.
622  * \retval 1 Mailbox has voicemail
623  * \retval 0 No new voicemail in specified mailbox
624  * \retval -1 Failure
625  * \since 1.0
626  */
627 int ast_app_has_voicemail(const char *mailboxes, const char *folder);
628
629 /*!
630  * \brief Determine number of new/old messages in a mailbox
631  * \since 1.0
632  * \param[in] mailboxes Mailbox specification in the format
633  *      /code
634  *       mbox[\@context][&mbox2[\@context2]][...]
635  *      /code
636  * \param[out] newmsgs Number of messages in the "INBOX" folder.  Includes number of messages in the "Urgent" folder, if any.
637  * \param[out] oldmsgs Number of messages in the "Old" folder.
638  * \retval 0 Success
639  * \retval -1 Failure
640  */
641 int ast_app_inboxcount(const char *mailboxes, int *newmsgs, int *oldmsgs);
642
643 /*!
644  * \brief Determine number of urgent/new/old messages in a mailbox
645  * \param[in] mailboxes the mailbox context to use
646  * \param[out] urgentmsgs the urgent message count
647  * \param[out] newmsgs the new message count
648  * \param[out] oldmsgs the old message count
649  * \return Returns 0 for success, negative upon error
650  * \since 1.6.1
651  */
652 int ast_app_inboxcount2(const char *mailboxes, int *urgentmsgs, int *newmsgs, int *oldmsgs);
653
654 /*!
655  * \brief Given a mailbox and context, play that mailbox owner's name to the channel specified
656  * \param[in] chan Channel on which to play the name
657  * \param[in] mailbox Mailbox number from which to retrieve the recording
658  * \param[in] context Mailbox context from which to locate the mailbox number
659  * \retval 0 Name played without interruption
660  * \retval dtmf ASCII value of the DTMF which interrupted playback.
661  * \retval -1 Unable to locate mailbox or hangup occurred.
662  * \since 1.6.1
663  */
664 int ast_app_sayname(struct ast_channel *chan, const char *mailbox, const char *context);
665
666 /*!
667  * \brief Get the number of messages in a given mailbox folder
668  *
669  * \param[in] mailbox_id Mailbox name
670  * \param[in] folder The folder to look in.  Default is INBOX if not provided.
671  *
672  * \note If requesting INBOX then the returned count is INBOX + Urgent.
673  *
674  * \return The number of messages in the mailbox folder (zero or more).
675  */
676 int ast_app_messagecount(const char *mailbox_id, const char *folder);
677
678 /*!
679  * \brief Return name of folder, given an id
680  * \param[in] id Folder id
681  * \return Name of folder
682  */
683 const char *ast_vm_index_to_foldername(int id);
684
685 /*!
686  * \brief Create a snapshot of a mailbox which contains information about every msg.
687  *
688  * \param mailbox, the mailbox to look for
689  * \param context, the context to look for the mailbox in
690  * \param folder, OPTIONAL.  When not NULL only msgs from the specified folder will be included.
691  * \param descending, list the msgs in descending order rather than ascending order.
692  * \param combine_INBOX_and_OLD, When this argument is set, The OLD folder will be represented
693  *        in the INBOX folder of the snapshot. This allows the snapshot to represent the
694  *        OLD and INBOX messages in sorted order merged together.
695  *
696  * \retval snapshot on success
697  * \retval NULL on failure
698  */
699 struct ast_vm_mailbox_snapshot *ast_vm_mailbox_snapshot_create(const char *mailbox,
700         const char *context,
701         const char *folder,
702         int descending,
703         enum ast_vm_snapshot_sort_val sort_val,
704         int combine_INBOX_and_OLD);
705
706 /*!
707  * \brief destroy a snapshot
708  *
709  * \param mailbox_snapshot The snapshot to destroy.
710  * \retval NULL
711  */
712 struct ast_vm_mailbox_snapshot *ast_vm_mailbox_snapshot_destroy(struct ast_vm_mailbox_snapshot *mailbox_snapshot);
713
714 /*!
715  * \brief Move messages from one folder to another
716  *
717  * \param mailbox The mailbox to which the folders belong
718  * \param context The voicemail context for the mailbox
719  * \param num_msgs The number of messages to move
720  * \param oldfolder The folder from where messages should be moved
721  * \param old_msg_ids The message IDs of the messages to move
722  * \param newfolder The folder to which messages should be moved
723  * new folder. This array must be num_msgs sized.
724  *
725  * \retval -1 Failure
726  * \retval 0 Success
727  */
728 int ast_vm_msg_move(const char *mailbox,
729         const char *context,
730         size_t num_msgs,
731         const char *oldfolder,
732         const char *old_msg_ids[],
733         const char *newfolder);
734
735 /*!
736  * \brief Remove/delete messages from a mailbox folder.
737  *
738  * \param mailbox The mailbox from which to delete messages
739  * \param context The voicemail context for the mailbox
740  * \param num_msgs The number of messages to delete
741  * \param folder The folder from which to remove messages
742  * \param msgs The message IDs of the messages to delete
743  *
744  * \retval -1 Failure
745  * \retval 0 Success
746  */
747 int ast_vm_msg_remove(const char *mailbox,
748         const char *context,
749         size_t num_msgs,
750         const char *folder,
751         const char *msgs[]);
752
753 /*!
754  * \brief forward a message from one mailbox to another.
755  *
756  * \brief from_mailbox The original mailbox the message is being forwarded from
757  * \brief from_context The voicemail context of the from_mailbox
758  * \brief from_folder The folder from which the message is being forwarded
759  * \brief to_mailbox The mailbox to forward the message to
760  * \brief to_context The voicemail context of the to_mailbox
761  * \brief to_folder The folder to which the message is being forwarded
762  * \brief num_msgs The number of messages being forwarded
763  * \brief msg_ids The message IDs of the messages in from_mailbox to forward
764  * \brief delete_old If non-zero, the forwarded messages are also deleted from from_mailbox.
765  * Otherwise, the messages will remain in the from_mailbox.
766  *
767  * \retval -1 Failure
768  * \retval 0 Success
769  */
770 int ast_vm_msg_forward(const char *from_mailbox,
771         const char *from_context,
772         const char *from_folder,
773         const char *to_mailbox,
774         const char *to_context,
775         const char *to_folder,
776         size_t num_msgs,
777         const char *msg_ids[],
778         int delete_old);
779
780 /*!
781  * \brief Play a voicemail msg back on a channel.
782  *
783  * \param chan
784  * \param mailbox msg is in.
785  * \param context of mailbox.
786  * \param folder voicemail folder to look in.
787  * \param msg_num message number in the voicemailbox to playback to the channel.
788  * \param cb
789  *
790  * \retval 0 success
791  * \retval -1 failure
792  */
793 int ast_vm_msg_play(struct ast_channel *chan,
794         const char *mailbox,
795         const char *context,
796         const char *folder,
797         const char *msg_num,
798         ast_vm_msg_play_cb *cb);
799
800 #ifdef TEST_FRAMEWORK
801 int ast_vm_test_destroy_user(const char *context, const char *mailbox);
802 int ast_vm_test_create_user(const char *context, const char *mailbox);
803 #endif
804
805 /*! \brief Safely spawn an external program while closing file descriptors
806         \note This replaces the \b system call in all Asterisk modules
807 */
808 int ast_safe_system(const char *s);
809
810 /*!
811  * \brief Replace the SIGCHLD handler
812  *
813  * Normally, Asterisk has a SIGCHLD handler that is cleaning up all zombie
814  * processes from forking elsewhere in Asterisk.  However, if you want to
815  * wait*() on the process to retrieve information about it's exit status,
816  * then this signal handler needs to be temporarily replaced.
817  *
818  * Code that executes this function *must* call ast_unreplace_sigchld()
819  * after it is finished doing the wait*().
820  */
821 void ast_replace_sigchld(void);
822
823 /*!
824  * \brief Restore the SIGCHLD handler
825  *
826  * This function is called after a call to ast_replace_sigchld.  It restores
827  * the SIGCHLD handler that cleans up any zombie processes.
828  */
829 void ast_unreplace_sigchld(void);
830
831 /*!
832   \brief Send DTMF to a channel
833
834   \param chan    The channel that will receive the DTMF frames
835   \param peer    (optional) Peer channel that will be autoserviced while the
836                  primary channel is receiving DTMF
837   \param digits  This is a string of characters representing the DTMF digits
838                  to be sent to the channel.  Valid characters are
839                  "0123456789*#abcdABCD".  Note: You can pass arguments 'f' or
840                  'F', if you want to Flash the channel (if supported by the
841                  channel), or 'w' to add a 500 millisecond pause to the DTMF
842                  sequence.
843   \param between This is the number of milliseconds to wait in between each
844                  DTMF digit.  If zero milliseconds is specified, then the
845                  default value of 100 will be used.
846   \param duration This is the duration that each DTMF digit should have.
847 */
848 int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, const char *digits, int between, unsigned int duration);
849
850 /*! \brief Stream a filename (or file descriptor) as a generator. */
851 int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, int allowoverride);
852
853 /*!
854  * \brief Stream a file with fast forward, pause, reverse, restart.
855  * \param chan Channel
856  * \param file File to play.
857  * \param fwd, rev, stop, pause, restart DTMF keys for media control
858  * \param skipms Number of milliseconds to skip for fwd/rev.
859  * \param offsetms Number of milliseconds to skip when starting the media.
860  *
861  * Before calling this function, set this to be the number
862  * of ms to start from the beginning of the file.  When the function
863  * returns, it will be the number of ms from the beginning where the
864  * playback stopped.  Pass NULL if you don't care.
865  *
866  * \retval 0 on success
867  * \retval Non-zero on failure
868  */
869 int ast_control_streamfile(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *pause, const char *restart, int skipms, long *offsetms);
870
871 /*!
872  * \brief Version of ast_control_streamfile() which allows the language of the
873  * media file to be specified.
874  *
875  * \retval 0 on success
876  * \retval Non-zero on failure
877  */
878 int ast_control_streamfile_lang(struct ast_channel *chan, const char *file,
879         const char *fwd, const char *rev, const char *stop, const char *suspend,
880         const char *restart, int skipms, const char *lang, long *offsetms);
881
882 /*!
883  * \brief Stream a file with fast forward, pause, reverse, restart.
884  * \param chan
885  * \param file filename
886  * \param fwd, rev, stop, pause, restart, skipms, offsetms
887  * \param cb waitstream callback to invoke when fastforward or rewind occurrs.
888  *
889  * Before calling this function, set this to be the number
890  * of ms to start from the beginning of the file.  When the function
891  * returns, it will be the number of ms from the beginning where the
892  * playback stopped.  Pass NULL if you don't care.
893  */
894 int ast_control_streamfile_w_cb(struct ast_channel *chan,
895         const char *file,
896         const char *fwd,
897         const char *rev,
898         const char *stop,
899         const char *pause,
900         const char *restart,
901         int skipms,
902         long *offsetms,
903         ast_waitstream_fr_cb cb);
904
905 /*! \brief Play a stream and wait for a digit, returning the digit that was pressed */
906 int ast_play_and_wait(struct ast_channel *chan, const char *fn);
907
908 /*!
909  * Possible actions to take if a recording already exists
910  * \since 12
911  */
912 enum ast_record_if_exists {
913         /*! Fail the recording. */
914         AST_RECORD_IF_EXISTS_FAIL,
915         /*! Overwrite the existing recording. */
916         AST_RECORD_IF_EXISTS_OVERWRITE,
917         /*! Append to the existing recording. */
918         AST_RECORD_IF_EXISTS_APPEND,
919 };
920
921 /*!
922  * \brief Record a file based on input from a channel
923  *        This function will play "auth-thankyou" upon successful recording if
924  *        skip_confirmation_sound is false.
925  *
926  * \param chan the channel being recorded
927  * \param playfile Filename of sound to play before recording begins. A beep is also played when playfile completes, before the recording begins.
928  * \param recordfile Filename to save the recording
929  * \param maxtime_sec Longest possible message length in seconds
930  * \param fmt string containing all formats to be recorded delimited by '|'
931  * \param duration pointer to integer for storing length of the recording
932  * \param beep If true, play a beep before recording begins (and doesn't play \a playfile)
933  * \param sound_duration pointer to integer for storing length of the recording minus all silence
934  * \param silencethreshold tolerance of noise levels that can be considered silence for the purpose of silence timeout, -1 for default
935  * \param maxsilence_ms Length of time in milliseconds which will trigger a timeout from silence, -1 for default
936  * \param path Optional filesystem path to unlock
937  * \param acceptdtmf Character of DTMF to end and accept the recording
938  * \param canceldtmf Character of DTMF to end and cancel the recording
939  * \param skip_confirmation_sound If true, don't play auth-thankyou at end. Nice for custom recording prompts in apps.
940  * \param if_exists Action to take if recording already exists.
941  *
942  * \retval -1 failure or hangup
943  * \retval 'S' Recording ended from silence timeout
944  * \retval 't' Recording ended from the message exceeding the maximum duration
945  * \retval dtmfchar Recording ended via the return value's DTMF character for either cancel or accept.
946  */
947 int ast_play_and_record_full(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime_sec, const char *fmt, int *duration, int *sound_duration, int beep, int silencethreshold, int maxsilence_ms, const char *path, const char *acceptdtmf, const char *canceldtmf, int skip_confirmation_sound, enum ast_record_if_exists if_exists);
948
949 /*!
950  * \brief Record a file based on input from a channel. Use default accept and cancel DTMF.
951  *        This function will play "auth-thankyou" upon successful recording.
952  *
953  * \param chan the channel being recorded
954  * \param playfile Filename of sound to play before recording begins
955  * \param recordfile Filename to save the recording
956  * \param maxtime_sec Longest possible message length in seconds
957  * \param fmt string containing all formats to be recorded delimited by '|'
958  * \param duration pointer to integer for storing length of the recording
959  * \param sound_duration pointer to integer for storing length of the recording minus all silence
960  * \param silencethreshold tolerance of noise levels that can be considered silence for the purpose of silence timeout, -1 for default
961  * \param maxsilence_ms length of time in milliseconds which will trigger a timeout from silence, -1 for default
962  * \param path Optional filesystem path to unlock
963  *
964  * \retval -1 failure or hangup
965  * \retval 'S' Recording ended from silence timeout
966  * \retval 't' Recording ended from the message exceeding the maximum duration
967  * \retval dtmfchar Recording ended via the return value's DTMF character for either cancel or accept.
968  */
969 int ast_play_and_record(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime_sec, const char *fmt, int *duration, int *sound_duration, int silencethreshold, int maxsilence_ms, const char *path);
970
971 /*!
972  * \brief Record a file based on input frm a channel. Recording is performed in 'prepend' mode which works a little differently from normal recordings
973  *        This function will not play a success message due to post-recording control in the application this was added for.
974  *
975  * \param chan the channel being recorded
976  * \param playfile Filename of sound to play before recording begins
977  * \param recordfile Filename to save the recording
978  * \param maxtime_sec Longest possible message length in seconds
979  * \param fmt string containing all formats to be recorded delimited by '|'
980  * \param duration pointer to integer for storing length of the recording
981  * \param sound_duration pointer to integer for storing length of the recording minus all silence
982  * \param beep whether to play a beep to prompt the recording
983  * \param silencethreshold tolerance of noise levels that can be considered silence for the purpose of silence timeout, -1 for default
984  * \param maxsilence_ms length of time in milliseconds which will trigger a timeout from silence, -1 for default.
985  *
986  * \retval -1 failure or hangup
987  * \retval 'S' Recording ended from silence timeout
988  * \retval 't' Recording either exceeded maximum duration or the call was ended via DTMF
989  */
990 int ast_play_and_prepend(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime_sec, char *fmt, int *duration, int *sound_duration, int beep, int silencethreshold, int maxsilence_ms);
991
992 enum ast_getdata_result {
993         AST_GETDATA_FAILED = -1,
994         AST_GETDATA_COMPLETE = 0,
995         AST_GETDATA_TIMEOUT = 1,
996         AST_GETDATA_INTERRUPTED = 2,
997         /*! indicates a user terminated empty string rather than an empty string resulting
998          * from a timeout or other factors */
999         AST_GETDATA_EMPTY_END_TERMINATED = 3,
1000 };
1001
1002 enum AST_LOCK_RESULT {
1003         AST_LOCK_SUCCESS = 0,
1004         AST_LOCK_TIMEOUT = -1,
1005         AST_LOCK_PATH_NOT_FOUND = -2,
1006         AST_LOCK_FAILURE = -3,
1007 };
1008
1009 /*! \brief Type of locking to use in ast_lock_path / ast_unlock_path */
1010 enum AST_LOCK_TYPE {
1011         AST_LOCK_TYPE_LOCKFILE = 0,
1012         AST_LOCK_TYPE_FLOCK = 1,
1013 };
1014
1015 /*!
1016  * \brief Set the type of locks used by ast_lock_path()
1017  * \param type the locking type to use
1018  */
1019 void ast_set_lock_type(enum AST_LOCK_TYPE type);
1020
1021 /*!
1022  * \brief Lock a filesystem path.
1023  * \param path the path to be locked
1024  * \return one of \ref AST_LOCK_RESULT values
1025  */
1026 enum AST_LOCK_RESULT ast_lock_path(const char *path);
1027
1028 /*! \brief Unlock a path */
1029 int ast_unlock_path(const char *path);
1030
1031 /*! \brief Read a file into asterisk*/
1032 char *ast_read_textfile(const char *file);
1033
1034 struct ast_group_info;
1035
1036 /*! \brief Split a group string into group and category, returning a default category if none is provided. */
1037 int ast_app_group_split_group(const char *data, char *group, int group_max, char *category, int category_max);
1038
1039 /*! \brief Set the group for a channel, splitting the provided data into group and category, if specified. */
1040 int ast_app_group_set_channel(struct ast_channel *chan, const char *data);
1041
1042 /*! \brief Get the current channel count of the specified group and category. */
1043 int ast_app_group_get_count(const char *group, const char *category);
1044
1045 /*! \brief Get the current channel count of all groups that match the specified pattern and category. */
1046 int ast_app_group_match_get_count(const char *groupmatch, const char *category);
1047
1048 /*! \brief Discard all group counting for a channel */
1049 int ast_app_group_discard(struct ast_channel *chan);
1050
1051 /*! \brief Update all group counting for a channel to a new one */
1052 int ast_app_group_update(struct ast_channel *oldchan, struct ast_channel *newchan);
1053
1054 /*! \brief Write Lock the group count list */
1055 int ast_app_group_list_wrlock(void);
1056
1057 /*! \brief Read Lock the group count list */
1058 int ast_app_group_list_rdlock(void);
1059
1060 /*! \brief Get the head of the group count list */
1061 struct ast_group_info *ast_app_group_list_head(void);
1062
1063 /*! \brief Unlock the group count list */
1064 int ast_app_group_list_unlock(void);
1065
1066 /*!
1067   \brief Define an application argument
1068   \param name The name of the argument
1069 */
1070 #define AST_APP_ARG(name) char *name
1071
1072 /*!
1073   \brief Declare a structure to hold an application's arguments.
1074   \param name The name of the structure
1075   \param arglist The list of arguments, defined using AST_APP_ARG
1076
1077   This macro declares a structure intended to be used in a call
1078   to ast_app_separate_args(). The structure includes all the
1079   arguments specified, plus an argv array that overlays them and an
1080   argc argument counter. The arguments must be declared using AST_APP_ARG,
1081   and they will all be character pointers (strings).
1082
1083   \note The structure is <b>not</b> initialized, as the call to
1084   ast_app_separate_args() will perform that function before parsing
1085   the arguments.
1086  */
1087 #define AST_DECLARE_APP_ARGS(name, arglist) AST_DEFINE_APP_ARGS_TYPE(, arglist) name = { 0, }
1088
1089 /*!
1090   \brief Define a structure type to hold an application's arguments.
1091   \param type The name of the structure type
1092   \param arglist The list of arguments, defined using AST_APP_ARG
1093
1094   This macro defines a structure type intended to be used in a call
1095   to ast_app_separate_args(). The structure includes all the
1096   arguments specified, plus an argv array that overlays them and an
1097   argc argument counter. The arguments must be declared using AST_APP_ARG,
1098   and they will all be character pointers (strings).
1099
1100   \note This defines a structure type, but does not declare an instance
1101   of the structure. That must be done separately.
1102  */
1103 #define AST_DEFINE_APP_ARGS_TYPE(type, arglist) \
1104         struct type { \
1105                 unsigned int argc; \
1106                 char *argv[0]; \
1107                 arglist \
1108         }
1109
1110 /*!
1111   \brief Performs the 'standard' argument separation process for an application.
1112   \param args An argument structure defined using AST_DECLARE_APP_ARGS
1113   \param parse A modifiable buffer containing the input to be parsed
1114
1115   This function will separate the input string using the standard argument
1116   separator character ',' and fill in the provided structure, including
1117   the argc argument counter field.
1118  */
1119 #define AST_STANDARD_APP_ARGS(args, parse) \
1120         args.argc = __ast_app_separate_args(parse, ',', 1, args.argv, ((sizeof(args) - offsetof(typeof(args), argv)) / sizeof(args.argv[0])))
1121 #define AST_STANDARD_RAW_ARGS(args, parse) \
1122         args.argc = __ast_app_separate_args(parse, ',', 0, args.argv, ((sizeof(args) - offsetof(typeof(args), argv)) / sizeof(args.argv[0])))
1123
1124 /*!
1125   \brief Performs the 'nonstandard' argument separation process for an application.
1126   \param args An argument structure defined using AST_DECLARE_APP_ARGS
1127   \param parse A modifiable buffer containing the input to be parsed
1128   \param sep A nonstandard separator character
1129
1130   This function will separate the input string using the nonstandard argument
1131   separator character and fill in the provided structure, including
1132   the argc argument counter field.
1133  */
1134 #define AST_NONSTANDARD_APP_ARGS(args, parse, sep) \
1135         args.argc = __ast_app_separate_args(parse, sep, 1, args.argv, ((sizeof(args) - offsetof(typeof(args), argv)) / sizeof(args.argv[0])))
1136 #define AST_NONSTANDARD_RAW_ARGS(args, parse, sep) \
1137         args.argc = __ast_app_separate_args(parse, sep, 0, args.argv, ((sizeof(args) - offsetof(typeof(args), argv)) / sizeof(args.argv[0])))
1138
1139 /*!
1140   \brief Separate a string into arguments in an array
1141   \param buf The string to be parsed (this must be a writable copy, as it will be modified)
1142   \param delim The character to be used to delimit arguments
1143   \param remove_chars Remove backslashes and quote characters, while parsing
1144   \param array An array of 'char *' to be filled in with pointers to the found arguments
1145   \param arraylen The number of elements in the array (i.e. the number of arguments you will accept)
1146
1147   Note: if there are more arguments in the string than the array will hold, the last element of
1148   the array will contain the remaining arguments, not separated.
1149
1150   The array will be completely zeroed by this function before it populates any entries.
1151
1152   \return The number of arguments found, or zero if the function arguments are not valid.
1153 */
1154 unsigned int __ast_app_separate_args(char *buf, char delim, int remove_chars, char **array, int arraylen);
1155 #define ast_app_separate_args(a,b,c,d)  __ast_app_separate_args(a,b,1,c,d)
1156
1157 /*!
1158   \brief A structure to hold the description of an application 'option'.
1159
1160   Application 'options' are single-character flags that can be supplied
1161   to the application to affect its behavior; they can also optionally
1162   accept arguments enclosed in parenthesis.
1163
1164   These structures are used by the ast_app_parse_options function, uses
1165   this data to fill in a flags structure (to indicate which options were
1166   supplied) and array of argument pointers (for those options that had
1167   arguments supplied).
1168  */
1169 struct ast_app_option {
1170         /*! \brief The flag bit that represents this option. */
1171         uint64_t flag;
1172         /*! \brief The index of the entry in the arguments array
1173           that should be used for this option's argument. */
1174         unsigned int arg_index;
1175 };
1176
1177 #define BEGIN_OPTIONS {
1178 #define END_OPTIONS }
1179
1180 /*!
1181   \brief Declares an array of options for an application.
1182   \param holder The name of the array to be created
1183   \param options The actual options to be placed into the array
1184   \sa ast_app_parse_options
1185
1186   This macro declares a 'static const' array of \c struct \c ast_option
1187   elements to hold the list of available options for an application.
1188   Each option must be declared using either the AST_APP_OPTION()
1189   or AST_APP_OPTION_ARG() macros.
1190
1191   Example usage:
1192   \code
1193   enum my_app_option_flags {
1194         OPT_JUMP = (1 << 0),
1195         OPT_BLAH = (1 << 1),
1196         OPT_BLORT = (1 << 2),
1197   };
1198
1199   enum my_app_option_args {
1200         OPT_ARG_BLAH = 0,
1201         OPT_ARG_BLORT,
1202         !! this entry tells how many possible arguments there are,
1203            and must be the last entry in the list
1204         OPT_ARG_ARRAY_SIZE,
1205   };
1206
1207   AST_APP_OPTIONS(my_app_options, {
1208         AST_APP_OPTION('j', OPT_JUMP),
1209         AST_APP_OPTION_ARG('b', OPT_BLAH, OPT_ARG_BLAH),
1210         AST_APP_OPTION_BLORT('B', OPT_BLORT, OPT_ARG_BLORT),
1211   });
1212
1213   static int my_app_exec(struct ast_channel *chan, void *data)
1214   {
1215         char *options;
1216         struct ast_flags opts = { 0, };
1217         char *opt_args[OPT_ARG_ARRAY_SIZE];
1218
1219         ... do any argument parsing here ...
1220
1221         if (ast_app_parse_options(my_app_options, &opts, opt_args, options)) {
1222                 return -1;
1223         }
1224   }
1225   \endcode
1226  */
1227 #define AST_APP_OPTIONS(holder, options...) \
1228         static const struct ast_app_option holder[128] = options
1229
1230 /*!
1231   \brief Declares an application option that does not accept an argument.
1232   \param option The single character representing the option
1233   \param flagno The flag index to be set if this option is present
1234   \sa AST_APP_OPTIONS, ast_app_parse_options
1235  */
1236 #define AST_APP_OPTION(option, flagno) \
1237         [option] = { .flag = flagno }
1238
1239 /*!
1240   \brief Declares an application option that accepts an argument.
1241   \param option The single character representing the option
1242   \param flagno The flag index to be set if this option is present
1243   \param argno The index into the argument array where the argument should
1244   be placed
1245   \sa AST_APP_OPTIONS, ast_app_parse_options
1246  */
1247 #define AST_APP_OPTION_ARG(option, flagno, argno) \
1248         [option] = { .flag = flagno, .arg_index = argno + 1 }
1249
1250 /*!
1251   \brief Parses a string containing application options and sets flags/arguments.
1252   \param options The array of possible options declared with AST_APP_OPTIONS
1253   \param flags The flag structure to have option flags set
1254   \param args The array of argument pointers to hold arguments found
1255   \param optstr The string containing the options to be parsed
1256   \return zero for success, non-zero if an error occurs
1257   \sa AST_APP_OPTIONS
1258  */
1259 int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr);
1260
1261         /*!
1262   \brief Parses a string containing application options and sets flags/arguments.
1263   \param options The array of possible options declared with AST_APP_OPTIONS
1264   \param flags The 64-bit flag structure to have option flags set
1265   \param args The array of argument pointers to hold arguments found
1266   \param optstr The string containing the options to be parsed
1267   \return zero for success, non-zero if an error occurs
1268   \sa AST_APP_OPTIONS
1269  */
1270 int ast_app_parse_options64(const struct ast_app_option *options, struct ast_flags64 *flags, char **args, char *optstr);
1271
1272 /*! \brief Given a list of options array, return an option string based on passed flags
1273         \param options The array of possible options declared with AST_APP_OPTIONS
1274         \param flags The flags of the options that you wish to populate the buffer with
1275         \param buf The buffer to fill with the string of options
1276         \param len The maximum length of buf
1277 */
1278 void ast_app_options2str64(const struct ast_app_option *options, struct ast_flags64 *flags, char *buf, size_t len);
1279
1280 /*! \brief Present a dialtone and collect a certain length extension.
1281     \return Returns 1 on valid extension entered, -1 on hangup, or 0 on invalid extension.
1282 \note Note that if 'collect' holds digits already, new digits will be appended, so be sure it's initialized properly */
1283 int ast_app_dtget(struct ast_channel *chan, const char *context, char *collect, size_t size, int maxlen, int timeout);
1284
1285 /*! \brief Allow to record message and have a review option */
1286 int ast_record_review(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, const char *path);
1287
1288 /*!
1289  * \brief Decode an encoded control or extended ASCII character
1290  * \param[in] stream String to decode
1291  * \param[out] result Decoded character
1292  * \param[out] consumed Number of characters used in stream to encode the character
1293  * \retval -1 Stream is of zero length
1294  * \retval 0 Success
1295  */
1296 int ast_get_encoded_char(const char *stream, char *result, size_t *consumed);
1297
1298 /*!
1299  * \brief Decode a stream of encoded control or extended ASCII characters
1300  * \param[in] stream Encoded string
1301  * \param[out] result Decoded string
1302  * \param[in] result_len Maximum size of the result buffer
1303  * \return A pointer to the result string
1304  */
1305 char *ast_get_encoded_str(const char *stream, char *result, size_t result_len);
1306
1307 /*! \brief Decode a stream of encoded control or extended ASCII characters */
1308 int ast_str_get_encoded_str(struct ast_str **str, int maxlen, const char *stream);
1309
1310 /*!
1311  * \brief Common routine for child processes, to close all fds prior to exec(2)
1312  * \param[in] n starting file descriptor number for closing all higher file descriptors
1313  * \since 1.6.1
1314  */
1315 void ast_close_fds_above_n(int n);
1316
1317 /*!
1318  * \brief Common routine to safely fork without a chance of a signal handler firing badly in the child
1319  * \param[in] stop_reaper flag to determine if sigchld handler is replaced or not
1320  * \since 1.6.1
1321  */
1322 int ast_safe_fork(int stop_reaper);
1323
1324 /*!
1325  * \brief Common routine to cleanup after fork'ed process is complete (if reaping was stopped)
1326  * \since 1.6.1
1327  */
1328 void ast_safe_fork_cleanup(void);
1329
1330 /*!
1331  * \brief Common routine to parse time lengths, with optional time unit specifier
1332  * \param[in] timestr String to parse
1333  * \param[in] defunit Default unit type
1334  * \param[out] result Resulting value, specified in milliseconds
1335  * \retval 0 Success
1336  * \retval -1 Failure
1337  * \since 1.8
1338  */
1339 int ast_app_parse_timelen(const char *timestr, int *result, enum ast_timelen defunit);
1340
1341 /*!
1342  * \since 12
1343  * \brief Publish a MWI state update via stasis
1344  *
1345  * \param[in] mailbox The mailbox identifier string.
1346  * \param[in] context The context this mailbox resides in (NULL or "" if only using mailbox)
1347  * \param[in] new_msgs The number of new messages in this mailbox
1348  * \param[in] old_msgs The number of old messages in this mailbox
1349  *
1350  * \retval 0 Success
1351  * \retval -1 Failure
1352  */
1353 #define ast_publish_mwi_state(mailbox, context, new_msgs, old_msgs) \
1354         ast_publish_mwi_state_full(mailbox, context, new_msgs, old_msgs, NULL, NULL)
1355
1356 /*!
1357  * \since 12
1358  * \brief Publish a MWI state update associated with some channel
1359  *
1360  * \param[in] mailbox The mailbox identifier string.
1361  * \param[in] context The context this mailbox resides in (NULL or "" if only using mailbox)
1362  * \param[in] new_msgs The number of new messages in this mailbox
1363  * \param[in] old_msgs The number of old messages in this mailbox
1364  * \param[in] channel_id A unique identifier for a channel associated with this
1365  * change in mailbox state
1366  *
1367  * \retval 0 Success
1368  * \retval -1 Failure
1369  */
1370 #define ast_publish_mwi_state_channel(mailbox, context, new_msgs, old_msgs, channel_id) \
1371         ast_publish_mwi_state_full(mailbox, context, new_msgs, old_msgs, channel_id, NULL)
1372
1373 /*!
1374  * \since 12
1375  * \brief Publish a MWI state update via stasis with all parameters
1376  *
1377  * \param[in] mailbox The mailbox identifier string.
1378  * \param[in] context The context this mailbox resides in (NULL or "" if only using mailbox)
1379  * \param[in] new_msgs The number of new messages in this mailbox
1380  * \param[in] old_msgs The number of old messages in this mailbox
1381  * \param[in] channel_id A unique identifier for a channel associated with this
1382  * change in mailbox state
1383  * \param[in] eid The EID of the server that originally published the message
1384  *
1385  * \retval 0 Success
1386  * \retval -1 Failure
1387  */
1388 int ast_publish_mwi_state_full(
1389         const char *mailbox,
1390         const char *context,
1391         int new_msgs,
1392         int old_msgs,
1393         const char *channel_id,
1394         struct ast_eid *eid);
1395
1396 /*!
1397  * \since 12.2.0
1398  * \brief Delete MWI state cached by stasis
1399  *
1400  * \param[in] mailbox The mailbox identifier string.
1401  * \param[in] context The context this mailbox resides in (NULL or "" if only using mailbox)
1402  *
1403  * \retval 0 Success
1404  * \retval -1 Failure
1405  */
1406 #define ast_delete_mwi_state(mailbox, context) \
1407         ast_delete_mwi_state_full(mailbox, context, NULL)
1408
1409 /*!
1410  * \since 12.2.0
1411  * \brief Delete MWI state cached by stasis with all parameters
1412  *
1413  * \param[in] mailbox The mailbox identifier string.
1414  * \param[in] context The context this mailbox resides in (NULL or "" if only using mailbox)
1415  * \param[in] eid The EID of the server that originally published the message
1416  *
1417  * \retval 0 Success
1418  * \retval -1 Failure
1419  */
1420 int ast_delete_mwi_state_full(const char *mailbox, const char *context, struct ast_eid *eid);
1421
1422 /*! \addtogroup StasisTopicsAndMessages
1423  * @{
1424  */
1425
1426 /*!
1427  * \brief The structure that contains MWI state
1428  * \since 12
1429  */
1430 struct ast_mwi_state {
1431         AST_DECLARE_STRING_FIELDS(
1432                 AST_STRING_FIELD(uniqueid);  /*!< Unique identifier for this mailbox */
1433         );
1434         int new_msgs;                    /*!< The current number of new messages for this mailbox */
1435         int old_msgs;                    /*!< The current number of old messages for this mailbox */
1436         /*! If applicable, a snapshot of the channel that caused this MWI change */
1437         struct ast_channel_snapshot *snapshot;
1438         struct ast_eid eid;              /*!< The EID of the server where this message originated */
1439 };
1440
1441 /*!
1442  * \brief Object that represents an MWI update with some additional application
1443  * defined data
1444  */
1445 struct ast_mwi_blob {
1446         struct ast_mwi_state *mwi_state;    /*!< MWI state */
1447         struct ast_json *blob;              /*!< JSON blob of data */
1448 };
1449
1450 /*!
1451  * \since 12
1452  * \brief Create a \ref ast_mwi_state object
1453  *
1454  * \param[in] mailbox The mailbox identifier string.
1455  * \param[in] context The context this mailbox resides in (NULL or "" if only using mailbox)
1456  *
1457  * \retval \ref ast_mwi_state object on success
1458  * \retval NULL on error
1459  */
1460 struct ast_mwi_state *ast_mwi_create(const char *mailbox, const char *context);
1461
1462 /*!
1463  * \since 12
1464  * \brief Creates a \ref ast_mwi_blob message.
1465  *
1466  * The \a blob JSON object requires a \c "type" field describing the blob. It
1467  * should also be treated as immutable and not modified after it is put into the
1468  * message.
1469  *
1470  * \param mwi_state MWI state associated with the update
1471  * \param message_type The type of message to create
1472  * \param blob JSON object representing the data.
1473  * \return \ref ast_mwi_blob message.
1474  * \return \c NULL on error
1475  */
1476 struct stasis_message *ast_mwi_blob_create(struct ast_mwi_state *mwi_state,
1477                                            struct stasis_message_type *message_type,
1478                                            struct ast_json *blob);
1479
1480 /*!
1481  * \brief Get the \ref stasis topic for MWI messages
1482  * \retval The topic structure for MWI messages
1483  * \retval NULL if it has not been allocated
1484  * \since 12
1485  */
1486 struct stasis_topic *ast_mwi_topic_all(void);
1487
1488 /*!
1489  * \brief Get the \ref stasis topic for MWI messages on a unique ID
1490  * \param uniqueid The unique id for which to get the topic
1491  * \retval The topic structure for MWI messages for a given uniqueid
1492  * \retval NULL if it failed to be found or allocated
1493  * \since 12
1494  */
1495 struct stasis_topic *ast_mwi_topic(const char *uniqueid);
1496
1497 /*!
1498  * \brief Get the \ref stasis caching topic for MWI messages
1499  * \retval The caching topic structure for MWI messages
1500  * \retval NULL if it has not been allocated
1501  * \since 12
1502  */
1503 struct stasis_topic *ast_mwi_topic_cached(void);
1504
1505 /*!
1506  * \brief Backend cache for ast_mwi_topic_cached().
1507  * \retval Cache of \ref ast_mwi_state.
1508  */
1509 struct stasis_cache *ast_mwi_state_cache(void);
1510
1511 /*!
1512  * \brief Get the \ref stasis message type for MWI messages
1513  * \retval The message type structure for MWI messages
1514  * \retval NULL on error
1515  * \since 12
1516  */
1517 struct stasis_message_type *ast_mwi_state_type(void);
1518
1519 /*!
1520  * \brief Get the \ref stasis message type for voicemail application specific messages
1521  *
1522  * This message type exists for those messages a voicemail application may wish to send
1523  * that have no logical relationship with other voicemail applications. Voicemail apps
1524  * that use this message type must pass a \ref ast_mwi_blob. Any extraneous information
1525  * in the JSON blob must be packed as key/value pair tuples of strings.
1526  *
1527  * At least one key/value tuple must have a key value of "Event".
1528  *
1529  * \retval The \ref stasis_message_type for voicemail application specific messages
1530  * \retval NULL on error
1531  * \since 12
1532  */
1533 struct stasis_message_type *ast_mwi_vm_app_type(void);
1534
1535 /*!
1536  * \brief Get the \ref stasis topic for queue messages
1537  * \retval The topic structure for queue messages
1538  * \retval NULL if it has not been allocated
1539  * \since 12
1540  */
1541 struct stasis_topic *ast_queue_topic_all(void);
1542
1543 /*!
1544  * \brief Get the \ref stasis topic for queue messages for a particular queue name
1545  * \param queuename The name for which to get the topic
1546  * \retval The topic structure for queue messages for a given name
1547  * \retval NULL if it failed to be found or allocated
1548  * \since 12
1549  */
1550 struct stasis_topic *ast_queue_topic(const char *queuename);
1551 /*! @} */
1552
1553 /*!
1554  * \brief Initialize the application core
1555  * \retval 0 Success
1556  * \retval -1 Failure
1557  * \since 12
1558  */
1559 int app_init(void);
1560
1561 #define AST_MAX_MAILBOX_UNIQUEID (AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2)
1562 #if defined(__cplusplus) || defined(c_plusplus)
1563 }
1564 #endif
1565
1566 #endif /* _ASTERISK_APP_H */