Merge "astobj2: Create function to copy weak proxied objects from container."
[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 #include "asterisk/file.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/say.h"
39 #include "asterisk/app.h"
40
41 /*** DOCUMENTATION
42         <application name="SayUnixTime" language="en_US">
43                 <synopsis>
44                         Says a specified time in a custom format.
45                 </synopsis>
46                 <syntax>
47                         <parameter name="unixtime" required="false">
48                                 <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
49                         </parameter>
50                         <parameter name="timezone" required="false" >
51                                 <para>timezone, see <directory>/usr/share/zoneinfo</directory> for a list. Defaults to machine default.</para>
52                         </parameter>
53                         <parameter name="format" required="false" >
54                                 <para>a format the time is to be said in.  See <filename>voicemail.conf</filename>.
55                                 Defaults to <literal>ABdY "digits/at" IMp</literal></para>
56                         </parameter>
57                         <parameter name="options" required="false">
58                                  <optionlist>
59                                         <option name="j">
60                                                 <para>Allow the calling user to dial digits to jump to that extension.
61                                                 This option is automatically enabled if
62                                                 <variable>SAY_DTMF_INTERRUPT</variable> is present on the channel and
63                                                 set to 'true' (case insensitive)</para>
64                                         </option>
65                                 </optionlist>
66                         </parameter>
67                 </syntax>
68                 <description>
69                         <para>Uses some of the sound files stored in <directory>/var/lib/asterisk/sounds</directory> to construct a phrase
70                         saying the specified date and/or time in the specified format. </para>
71                 </description>
72                 <see-also>
73                         <ref type="function">STRFTIME</ref>
74                         <ref type="function">STRPTIME</ref>
75                         <ref type="function">IFTIME</ref>
76                 </see-also>
77         </application>
78         <application name="DateTime" language="en_US">
79                 <synopsis>
80                         Says a specified time in a custom format.
81                 </synopsis>
82                 <syntax>
83                         <parameter name="unixtime">
84                                 <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
85                         </parameter>
86                         <parameter name="timezone">
87                                 <para>timezone, see <filename>/usr/share/zoneinfo</filename> for a list. Defaults to machine default.</para>
88                         </parameter>
89                         <parameter name="format">
90                                 <para>a format the time is to be said in.  See <filename>voicemail.conf</filename>.
91                                 Defaults to <literal>ABdY "digits/at" IMp</literal></para>
92                         </parameter>
93                 </syntax>
94                 <description>
95                         <para>Say the date and time in a specified format.</para>
96                 </description>
97         </application>
98
99  ***/
100
101 enum {
102         OPT_JUMP =          (1 << 0),
103 };
104
105 enum {
106         OPT_ARG_JUMP = 0,
107         /* note: this entry _MUST_ be the last one in the enum */
108         OPT_ARG_ARRAY_SIZE,
109 };
110
111 AST_APP_OPTIONS(sayunixtime_exec_options, BEGIN_OPTIONS
112         AST_APP_OPTION_ARG('j', OPT_JUMP, OPT_ARG_JUMP),
113 END_OPTIONS );
114
115 static char *app_sayunixtime = "SayUnixTime";
116 static char *app_datetime = "DateTime";
117
118 static int sayunixtime_exec(struct ast_channel *chan, const char *data)
119 {
120         AST_DECLARE_APP_ARGS(args,
121                 AST_APP_ARG(timeval);
122                 AST_APP_ARG(timezone);
123                 AST_APP_ARG(format);
124                 AST_APP_ARG(options);
125         );
126         char *parse;
127         int res = 0;
128         time_t unixtime;
129         /* New default behavior is do not jump on key pressed */
130         const char * haltondigits = AST_DIGIT_NONE;
131         struct ast_flags64 opts = { 0, };
132         char *opt_args[OPT_ARG_ARRAY_SIZE];
133         const char *interrupt_string;
134
135         if (!data) {
136                 return 0;
137         }
138
139         parse = ast_strdupa(data);
140
141         AST_STANDARD_APP_ARGS(args, parse);
142
143         /* check if we had the 'j' jump flag in option list */
144         if (!ast_strlen_zero(args.options))     {
145                 ast_app_parse_options64(sayunixtime_exec_options, &opts, opt_args, args.options);
146                 if (ast_test_flag64(&opts, OPT_JUMP)){
147                         haltondigits = AST_DIGIT_ANY;
148                 }
149         }
150
151         /* Check if 'SAY_DTMF_INTERRUPT' is true and apply the same behavior as the j flag. */
152         ast_channel_lock(chan);
153         interrupt_string = pbx_builtin_getvar_helper(chan, "SAY_DTMF_INTERRUPT");
154         if (ast_true(interrupt_string)) {
155                 haltondigits = AST_DIGIT_ANY;
156         }
157         ast_channel_unlock(chan);
158
159         ast_get_time_t(ast_strlen_zero(args.timeval) ? NULL : args.timeval, &unixtime, time(NULL), NULL);
160
161         if (ast_channel_state(chan) != AST_STATE_UP) {
162                 res = ast_answer(chan);
163         }
164
165         if (!res) {
166                 res = ast_say_date_with_format(chan, unixtime, haltondigits,
167                                                ast_channel_language(chan), ast_strlen_zero(args.format) ? NULL : args.format, ast_strlen_zero(args.timezone) ? NULL : args.timezone);
168         }
169
170         return res;
171 }
172
173 static int unload_module(void)
174 {
175         int res;
176
177         res = ast_unregister_application(app_sayunixtime);
178         res |= ast_unregister_application(app_datetime);
179
180         return res;
181 }
182
183 static int load_module(void)
184 {
185         int res;
186
187         res = ast_register_application_xml(app_sayunixtime, sayunixtime_exec);
188         res |= ast_register_application_xml(app_datetime, sayunixtime_exec);
189
190         return res;
191 }
192
193 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Say time");