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