git migration: Refactor the ASTERISK_FILE_VERSION macro
[asterisk/asterisk.git] / apps / app_sayunixtime.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2003, 2006 Tilghman Lesher.  All rights reserved.
5  * Copyright (c) 2006 Digium, Inc.
6  *
7  * Tilghman Lesher <app_sayunixtime__200309@the-tilghman.com>
8  *
9  * This code is released by the author with no restrictions on usage.
10  *
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  */
18
19 /*! \file
20  *
21  * \brief SayUnixTime application
22  *
23  * \author Tilghman Lesher <app_sayunixtime__200309@the-tilghman.com>
24  * 
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_REGISTER_FILE()
35
36 #include "asterisk/file.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/say.h"
41 #include "asterisk/app.h"
42
43 /*** DOCUMENTATION
44         <application name="SayUnixTime" language="en_US">
45                 <synopsis>
46                         Says a specified time in a custom format.
47                 </synopsis>
48                 <syntax>
49                         <parameter name="unixtime" required="false">
50                                 <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
51                         </parameter>
52                         <parameter name="timezone" required="false" >
53                                 <para>timezone, see <directory>/usr/share/zoneinfo</directory> for a list. Defaults to machine default.</para>
54                         </parameter>
55                         <parameter name="format" required="false" >
56                                 <para>a format the time is to be said in.  See <filename>voicemail.conf</filename>.
57                                 Defaults to <literal>ABdY "digits/at" IMp</literal></para>
58                         </parameter>
59                         <parameter name="options" required="false">
60                                  <optionlist>
61                                         <option name="j">
62                                                 <para>Allow the calling user to dial digits to jump to that extension.
63                                                 This option is automatically enabled if
64                                                 <variable>SAY_DTMF_INTERRUPT</variable> is present on the channel and
65                                                 set to 'true' (case insensitive)</para>
66                                         </option>
67                                 </optionlist>
68                         </parameter>
69                 </syntax>
70                 <description>
71                         <para>Uses some of the sound files stored in <directory>/var/lib/asterisk/sounds</directory> to construct a phrase 
72                         saying the specified date and/or time in the specified format. </para>
73                 </description>
74                 <see-also>
75                         <ref type="function">STRFTIME</ref>
76                         <ref type="function">STRPTIME</ref>
77                         <ref type="function">IFTIME</ref>
78                 </see-also>
79         </application>
80         <application name="DateTime" language="en_US">
81                 <synopsis>
82                         Says a specified time in a custom format.
83                 </synopsis>
84                 <syntax>
85                         <parameter name="unixtime">
86                                 <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
87                         </parameter>
88                         <parameter name="timezone">
89                                 <para>timezone, see <filename>/usr/share/zoneinfo</filename> for a list. Defaults to machine default.</para>
90                         </parameter>
91                         <parameter name="format">
92                                 <para>a format the time is to be said in.  See <filename>voicemail.conf</filename>.
93                                 Defaults to <literal>ABdY "digits/at" IMp</literal></para>
94                         </parameter>
95                 </syntax>
96                 <description>
97                         <para>Say the date and time in a specified format.</para>
98                 </description>
99         </application>
100
101  ***/
102
103 enum {
104         OPT_JUMP =          (1 << 0),
105 };
106
107 enum {
108         OPT_ARG_JUMP = 0,
109         /* note: this entry _MUST_ be the last one in the enum */
110         OPT_ARG_ARRAY_SIZE,
111 };
112
113 AST_APP_OPTIONS(sayunixtime_exec_options, BEGIN_OPTIONS
114         AST_APP_OPTION_ARG('j', OPT_JUMP, OPT_ARG_JUMP),
115 END_OPTIONS );
116
117 static char *app_sayunixtime = "SayUnixTime";
118 static char *app_datetime = "DateTime";
119
120 static int sayunixtime_exec(struct ast_channel *chan, const char *data)
121 {
122         AST_DECLARE_APP_ARGS(args,
123                 AST_APP_ARG(timeval);
124                 AST_APP_ARG(timezone);
125                 AST_APP_ARG(format);
126                 AST_APP_ARG(options);
127         );
128         char *parse;
129         int res = 0;
130         time_t unixtime;
131         /* New default behavior is do not jump on key pressed */
132         const char * haltondigits = AST_DIGIT_NONE;
133         struct ast_flags64 opts = { 0, };
134         char *opt_args[OPT_ARG_ARRAY_SIZE];
135         const char *interrupt_string;
136
137         if (!data) {
138                 return 0;
139         }
140
141         parse = ast_strdupa(data);
142
143         AST_STANDARD_APP_ARGS(args, parse);
144
145         /* check if we had the 'j' jump flag in option list */
146         if (!ast_strlen_zero(args.options))     {
147                 ast_app_parse_options64(sayunixtime_exec_options, &opts, opt_args, args.options);
148                 if (ast_test_flag64(&opts, OPT_JUMP)){
149                         haltondigits = AST_DIGIT_ANY;
150                 }
151         }
152
153         /* Check if 'SAY_DTMF_INTERRUPT' is true and apply the same behavior as the j flag. */
154         ast_channel_lock(chan);
155         interrupt_string = pbx_builtin_getvar_helper(chan, "SAY_DTMF_INTERRUPT");
156         if (ast_true(interrupt_string)) {
157                 haltondigits = AST_DIGIT_ANY;
158         }
159         ast_channel_unlock(chan);
160
161         ast_get_time_t(ast_strlen_zero(args.timeval) ? NULL : args.timeval, &unixtime, time(NULL), NULL);
162
163         if (ast_channel_state(chan) != AST_STATE_UP) {
164                 res = ast_answer(chan);
165         }
166
167         if (!res) {
168                 res = ast_say_date_with_format(chan, unixtime, haltondigits,
169                                                ast_channel_language(chan), ast_strlen_zero(args.format) ? NULL : args.format, ast_strlen_zero(args.timezone) ? NULL : args.timezone);
170         }
171
172         return res;
173 }
174
175 static int unload_module(void)
176 {
177         int res;
178         
179         res = ast_unregister_application(app_sayunixtime);
180         res |= ast_unregister_application(app_datetime);
181         
182         return res;
183 }
184
185 static int load_module(void)
186 {
187         int res;
188         
189         res = ast_register_application_xml(app_sayunixtime, sayunixtime_exec);
190         res |= ast_register_application_xml(app_datetime, sayunixtime_exec);
191         
192         return res;
193 }
194
195 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Say time");