Don't do SIP contact/route DNS if we're not using the result.
[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_FILE_VERSION(__FILE__, "$Revision$")
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.</para>
63                                         </option>
64                                 </optionlist>
65                         </parameter>
66                 </syntax>
67                 <description>
68                         <para>Uses some of the sound files stored in <directory>/var/lib/asterisk/sounds</directory> to construct a phrase 
69                         saying the specified date and/or time in the specified format. </para>
70                 </description>
71                 <see-also>
72                         <ref type="function">STRFTIME</ref>
73                         <ref type="function">STRPTIME</ref>
74                         <ref type="function">IFTIME</ref>
75                 </see-also>
76         </application>
77         <application name="DateTime" language="en_US">
78                 <synopsis>
79                         Says a specified time in a custom format.
80                 </synopsis>
81                 <syntax>
82                         <parameter name="unixtime">
83                                 <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
84                         </parameter>
85                         <parameter name="timezone">
86                                 <para>timezone, see <filename>/usr/share/zoneinfo</filename> for a list. Defaults to machine default.</para>
87                         </parameter>
88                         <parameter name="format">
89                                 <para>a format the time is to be said in.  See <filename>voicemail.conf</filename>.
90                                 Defaults to <literal>ABdY "digits/at" IMp</literal></para>
91                         </parameter>
92                 </syntax>
93                 <description>
94                         <para>Say the date and time in a specified format.</para>
95                 </description>
96         </application>
97
98  ***/
99
100 enum {
101         OPT_JUMP =          (1 << 0),
102 };
103
104 enum {
105         OPT_ARG_JUMP = 0,
106         /* note: this entry _MUST_ be the last one in the enum */
107         OPT_ARG_ARRAY_SIZE,
108 };
109
110 AST_APP_OPTIONS(sayunixtime_exec_options, BEGIN_OPTIONS
111         AST_APP_OPTION_ARG('j', OPT_JUMP, OPT_ARG_JUMP),
112 END_OPTIONS );
113
114 static char *app_sayunixtime = "SayUnixTime";
115 static char *app_datetime = "DateTime";
116
117 static int sayunixtime_exec(struct ast_channel *chan, const char *data)
118 {
119         AST_DECLARE_APP_ARGS(args,
120                 AST_APP_ARG(timeval);
121                 AST_APP_ARG(timezone);
122                 AST_APP_ARG(format);
123                 AST_APP_ARG(options);
124         );
125         char *parse;
126         int res = 0;
127         time_t unixtime;
128         /* New default behavior is do not jump on key pressed */
129         const char * haltondigits = AST_DIGIT_NONE;
130         struct ast_flags64 opts = { 0, };
131         char *opt_args[OPT_ARG_ARRAY_SIZE];
132
133         if (!data) {
134                 return 0;
135         }
136
137         parse = ast_strdupa(data);
138
139         AST_STANDARD_APP_ARGS(args, parse);
140
141         /* check if we had the 'j' jump flag in option list */
142         if (!ast_strlen_zero(args.options))     {
143                 ast_app_parse_options64(sayunixtime_exec_options, &opts, opt_args, args.options);
144                 if (ast_test_flag64(&opts, OPT_JUMP)){
145                         haltondigits = AST_DIGIT_ANY;
146                 }
147         }
148
149         ast_get_time_t(ast_strlen_zero(args.timeval) ? NULL : args.timeval, &unixtime, time(NULL), NULL);
150
151         if (ast_channel_state(chan) != AST_STATE_UP) {
152                 res = ast_answer(chan);
153         }
154
155         if (!res) {
156                 res = ast_say_date_with_format(chan, unixtime, haltondigits,
157                                                ast_channel_language(chan), ast_strlen_zero(args.format) ? NULL : args.format, ast_strlen_zero(args.timezone) ? NULL : args.timezone);
158         }
159
160         return res;
161 }
162
163 static int unload_module(void)
164 {
165         int res;
166         
167         res = ast_unregister_application(app_sayunixtime);
168         res |= ast_unregister_application(app_datetime);
169         
170         return res;
171 }
172
173 static int load_module(void)
174 {
175         int res;
176         
177         res = ast_register_application_xml(app_sayunixtime, sayunixtime_exec);
178         res |= ast_register_application_xml(app_datetime, sayunixtime_exec);
179         
180         return res;
181 }
182
183 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Say time");