app_sayunixtime.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.</para>
  57. </option>
  58. </optionlist>
  59. </parameter>
  60. </syntax>
  61. <description>
  62. <para>Uses some of the sound files stored in <directory>/var/lib/asterisk/sounds</directory> to construct a phrase
  63. saying the specified date and/or time in the specified format. </para>
  64. </description>
  65. <see-also>
  66. <ref type="function">STRFTIME</ref>
  67. <ref type="function">STRPTIME</ref>
  68. <ref type="function">IFTIME</ref>
  69. </see-also>
  70. </application>
  71. <application name="DateTime" language="en_US">
  72. <synopsis>
  73. Says a specified time in a custom format.
  74. </synopsis>
  75. <syntax>
  76. <parameter name="unixtime">
  77. <para>time, in seconds since Jan 1, 1970. May be negative. Defaults to now.</para>
  78. </parameter>
  79. <parameter name="timezone">
  80. <para>timezone, see <filename>/usr/share/zoneinfo</filename> for a list. Defaults to machine default.</para>
  81. </parameter>
  82. <parameter name="format">
  83. <para>a format the time is to be said in. See <filename>voicemail.conf</filename>.
  84. Defaults to <literal>ABdY "digits/at" IMp</literal></para>
  85. </parameter>
  86. </syntax>
  87. <description>
  88. <para>Say the date and time in a specified format.</para>
  89. </description>
  90. </application>
  91. ***/
  92. enum {
  93. OPT_JUMP = (1 << 0),
  94. };
  95. enum {
  96. OPT_ARG_JUMP = 0,
  97. /* note: this entry _MUST_ be the last one in the enum */
  98. OPT_ARG_ARRAY_SIZE,
  99. };
  100. AST_APP_OPTIONS(sayunixtime_exec_options, BEGIN_OPTIONS
  101. AST_APP_OPTION_ARG('j', OPT_JUMP, OPT_ARG_JUMP),
  102. END_OPTIONS );
  103. static char *app_sayunixtime = "SayUnixTime";
  104. static char *app_datetime = "DateTime";
  105. static int sayunixtime_exec(struct ast_channel *chan, const char *data)
  106. {
  107. AST_DECLARE_APP_ARGS(args,
  108. AST_APP_ARG(timeval);
  109. AST_APP_ARG(timezone);
  110. AST_APP_ARG(format);
  111. AST_APP_ARG(options);
  112. );
  113. char *parse;
  114. int res = 0;
  115. time_t unixtime;
  116. /* New default behavior is do not jump on key pressed */
  117. const char * haltondigits = AST_DIGIT_NONE;
  118. struct ast_flags64 opts = { 0, };
  119. char *opt_args[OPT_ARG_ARRAY_SIZE];
  120. if (!data) {
  121. return 0;
  122. }
  123. parse = ast_strdupa(data);
  124. AST_STANDARD_APP_ARGS(args, parse);
  125. /* check if we had the 'j' jump flag in option list */
  126. if (!ast_strlen_zero(args.options)) {
  127. ast_app_parse_options64(sayunixtime_exec_options, &opts, opt_args, args.options);
  128. if (ast_test_flag64(&opts, OPT_JUMP)){
  129. haltondigits = AST_DIGIT_ANY;
  130. }
  131. }
  132. ast_get_time_t(ast_strlen_zero(args.timeval) ? NULL : args.timeval, &unixtime, time(NULL), NULL);
  133. if (ast_channel_state(chan) != AST_STATE_UP) {
  134. res = ast_answer(chan);
  135. }
  136. if (!res) {
  137. res = ast_say_date_with_format(chan, unixtime, haltondigits,
  138. ast_channel_language(chan), ast_strlen_zero(args.format) ? NULL : args.format, ast_strlen_zero(args.timezone) ? NULL : args.timezone);
  139. }
  140. return res;
  141. }
  142. static int unload_module(void)
  143. {
  144. int res;
  145. res = ast_unregister_application(app_sayunixtime);
  146. res |= ast_unregister_application(app_datetime);
  147. return res;
  148. }
  149. static int load_module(void)
  150. {
  151. int res;
  152. res = ast_register_application_xml(app_sayunixtime, sayunixtime_exec);
  153. res |= ast_register_application_xml(app_datetime, sayunixtime_exec);
  154. return res;
  155. }
  156. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Say time");