Merged revisions 328247 via svnmerge from
[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">
50                                 <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
51                         </parameter>
52                         <parameter name="timezone">
53                                 <para>timezone, see <directory>/usr/share/zoneinfo</directory> for a list. Defaults to machine default.</para>
54                         </parameter>
55                         <parameter name="format">
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                 </syntax>
60                 <description>
61                         <para>Uses some of the sound files stored in <directory>/var/lib/asterisk/sounds</directory> to construct a phrase 
62                         saying the specified date and/or time in the specified format. </para>
63                 </description>
64                 <see-also>
65                         <ref type="function">STRFTIME</ref>
66                         <ref type="function">STRPTIME</ref>
67                         <ref type="function">IFTIME</ref>
68                 </see-also>
69         </application>
70         <application name="DateTime" language="en_US">
71                 <synopsis>
72                         Says a specified time in a custom format.
73                 </synopsis>
74                 <syntax>
75                         <parameter name="unixtime">
76                                 <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
77                         </parameter>
78                         <parameter name="timezone">
79                                 <para>timezone, see <filename>/usr/share/zoneinfo</filename> for a list. Defaults to machine default.</para>
80                         </parameter>
81                         <parameter name="format">
82                                 <para>a format the time is to be said in.  See <filename>voicemail.conf</filename>.
83                                 Defaults to <literal>ABdY "digits/at" IMp</literal></para>
84                         </parameter>
85                 </syntax>
86                 <description>
87                         <para>Say the date and time in a specified format.</para>
88                 </description>
89         </application>
90
91  ***/
92
93 static char *app_sayunixtime = "SayUnixTime";
94 static char *app_datetime = "DateTime";
95
96 static int sayunixtime_exec(struct ast_channel *chan, const char *data)
97 {
98         AST_DECLARE_APP_ARGS(args,
99                 AST_APP_ARG(timeval);
100                 AST_APP_ARG(timezone);
101                 AST_APP_ARG(format);
102         );
103         char *parse;
104         int res = 0;
105         time_t unixtime;
106         
107         if (!data)
108                 return 0;
109
110         parse = ast_strdupa(data);
111
112         AST_STANDARD_APP_ARGS(args, parse);
113
114         ast_get_time_t(args.timeval, &unixtime, time(NULL), NULL);
115
116         if (chan->_state != AST_STATE_UP)
117                 res = ast_answer(chan);
118
119         if (!res)
120                 res = ast_say_date_with_format(chan, unixtime, AST_DIGIT_ANY,
121                                                chan->language, args.format, args.timezone);
122
123         return res;
124 }
125
126 static int unload_module(void)
127 {
128         int res;
129         
130         res = ast_unregister_application(app_sayunixtime);
131         res |= ast_unregister_application(app_datetime);
132         
133         return res;
134 }
135
136 static int load_module(void)
137 {
138         int res;
139         
140         res = ast_register_application_xml(app_sayunixtime, sayunixtime_exec);
141         res |= ast_register_application_xml(app_datetime, sayunixtime_exec);
142         
143         return res;
144 }
145
146 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Say time");