app_sayunixtime.c 5.4 KB

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