Add explanation of strange flag setup in app_meetme (stolen from Mark's message to...
[asterisk/asterisk.git] / apps / app_waituntil.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Redfish Solutions
5  *
6  * Philip Prindeville <philipp@redfish-solutions.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 /*! \file
20  *
21  * \brief Sleep until the given epoch
22  *
23  * \author Philip Prindeville <philipp@redfish-solutions.com>
24  *
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/logger.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/module.h"
36
37 /*** DOCUMENTATION
38         <application name="WaitUntil" language="en_US">
39                 <synopsis>
40                         Wait (sleep) until the current time is the given epoch.
41                 </synopsis>
42                 <syntax>
43                         <parameter name="epoch" required="true" />
44                 </syntax>
45                 <description>
46                         <para>Waits until the given <replaceable>epoch</replaceable>.</para>
47                         <para>Sets <variable>WAITUNTILSTATUS</variable> to one of the following values:</para>
48                         <variablelist>
49                                 <variable name="WAITUNTILSTATUS">
50                                         <value name="OK">
51                                                 Wait succeeded.
52                                         </value>
53                                         <value name="FAILURE">
54                                                 Invalid argument.
55                                         </value>
56                                         <value name="HANGUP">
57                                                 Channel hungup before time elapsed.
58                                         </value>
59                                         <value name="PAST">
60                                                 Time specified had already past.
61                                         </value>
62                                 </variable>
63                         </variablelist>
64                 </description>
65         </application>
66  ***/
67
68 static char *app = "WaitUntil";
69
70 static int waituntil_exec(struct ast_channel *chan, const char *data)
71 {
72         int res;
73         double fraction;
74         long seconds;
75         struct timeval future = { 0, };
76         struct timeval now = ast_tvnow();
77         int msec;
78
79         if (ast_strlen_zero(data)) {
80                 ast_log(LOG_WARNING, "WaitUntil requires an argument(epoch)\n");
81                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
82                 return 0;
83         }
84
85         if (sscanf(data, "%30ld%30lf", &seconds, &fraction) == 0) {
86                 ast_log(LOG_WARNING, "WaitUntil called with non-numeric argument\n");
87                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "FAILURE");
88                 return 0;
89         }
90
91         future.tv_sec = seconds;
92         future.tv_usec = fraction * 1000000;
93
94         if ((msec = ast_tvdiff_ms(future, now)) < 0) {
95                 ast_log(LOG_NOTICE, "WaitUntil called in the past (now %ld, arg %ld)\n", (long)now.tv_sec, (long)future.tv_sec);
96                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "PAST");
97                 return 0;
98         }
99
100         if ((res = ast_safe_sleep(chan, msec)))
101                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "HANGUP");
102         else
103                 pbx_builtin_setvar_helper(chan, "WAITUNTILSTATUS", "OK");
104
105         return res;
106 }
107
108 static int unload_module(void)
109 {
110         return ast_unregister_application(app);
111 }
112
113 static int load_module(void)
114 {
115         return ast_register_application_xml(app, waituntil_exec);
116 }
117
118 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait until specified time");