app_sayunixtime.c 5.4 KB

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