Asterisk - The Open Source Telephony Project  18.5.0
app_sayunixtime.c
Go to the documentation of this file.
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 <[email protected]>
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 <[email protected]>
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 {
107  /* note: this entry _MUST_ be the last one in the enum */
109 };
110 
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 {
121  AST_APP_ARG(timeval);
122  AST_APP_ARG(timezone);
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 
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
static char * app_sayunixtime
#define AST_DIGIT_ANY
Definition: file.h:48
static const struct ast_app_option sayunixtime_exec_options[128]
#define BEGIN_OPTIONS
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
static int sayunixtime_exec(struct ast_channel *chan, const char *data)
ast_channel_state
ast_channel states
Definition: channelstate.h:35
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
const char * args
#define NULL
Definition: resample.c:96
Structure used to handle a large number of boolean flags == used only in app_dial?
Definition: utils.h:204
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static char * app_datetime
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
get values from config variables.
Definition: main/utils.c:2198
General Asterisk PBX channel definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
Core PBX routines and definitions.
int ast_app_parse_options64(const struct ast_app_option *options, struct ast_flags64 *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:2911
SAY_EXTERN int(* ast_say_date_with_format)(struct ast_channel *chan, time_t t, const char *ints, const char *lang, const char *format, const char *timezone) SAY_INIT(ast_say_date_with_format)
Definition: say.h:189
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: main/utils.c:1951
#define AST_APP_OPTION_ARG(option, flagno, argno)
Declares an application option that accepts an argument.
#define AST_DIGIT_NONE
Definition: file.h:47
#define ast_channel_unlock(chan)
Definition: channel.h:2946
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define END_OPTIONS
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2814
const char * ast_channel_language(const struct ast_channel *chan)
static struct test_options options
Say numbers and dates (maybe words one day too)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
static snd_pcm_format_t format
Definition: chan_alsa.c:102
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
static int unload_module(void)
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626
#define AST_APP_ARG(name)
Define an application argument.
static int load_module(void)
#define ast_test_flag64(p, flag)
Definition: utils.h:120