app_sayunixtime.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (c) 2003 Tilghman Lesher. All rights reserved.
  5. *
  6. * Tilghman Lesher <app_sayunixtime__200309@the-tilghman.com>
  7. *
  8. * This code is released by the author with no restrictions on usage.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. */
  17. /*! \file
  18. *
  19. * \brief SayUnixTime application
  20. *
  21. * \ingroup applications
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/file.h"
  30. #include "asterisk/logger.h"
  31. #include "asterisk/options.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/say.h"
  36. static char *tdesc = "Say time";
  37. static char *app_sayunixtime = "SayUnixTime";
  38. static char *app_datetime = "DateTime";
  39. static char *sayunixtime_synopsis = "Says a specified time in a custom format";
  40. static char *sayunixtime_descrip =
  41. "SayUnixTime([unixtime][|[timezone][|format]])\n"
  42. " unixtime: time, in seconds since Jan 1, 1970. May be negative.\n"
  43. " defaults to now.\n"
  44. " timezone: timezone, see /usr/share/zoneinfo for a list.\n"
  45. " defaults to machine default.\n"
  46. " format: a format the time is to be said in. See voicemail.conf.\n"
  47. " defaults to \"ABdY 'digits/at' IMp\"\n";
  48. static char *datetime_descrip =
  49. "DateTime([unixtime][|[timezone][|format]])\n"
  50. " unixtime: time, in seconds since Jan 1, 1970. May be negative.\n"
  51. " defaults to now.\n"
  52. " timezone: timezone, see /usr/share/zoneinfo for a list.\n"
  53. " defaults to machine default.\n"
  54. " format: a format the time is to be said in. See voicemail.conf.\n"
  55. " defaults to \"ABdY 'digits/at' IMp\"\n";
  56. STANDARD_LOCAL_USER;
  57. LOCAL_USER_DECL;
  58. static int sayunixtime_exec(struct ast_channel *chan, void *data)
  59. {
  60. int res=0;
  61. struct localuser *u;
  62. char *s,*zone=NULL,*timec,*format;
  63. time_t unixtime;
  64. struct timeval tv;
  65. LOCAL_USER_ADD(u);
  66. tv = ast_tvnow();
  67. unixtime = (time_t)tv.tv_sec;
  68. if( !strcasecmp(chan->language, "da" ) ) {
  69. format = "A dBY HMS";
  70. } else if ( !strcasecmp(chan->language, "de" ) ) {
  71. format = "A dBY HMS";
  72. } else {
  73. format = "ABdY 'digits/at' IMp";
  74. }
  75. if (data) {
  76. s = data;
  77. s = ast_strdupa(s);
  78. if (s) {
  79. timec = strsep(&s,"|");
  80. if ((timec) && (*timec != '\0')) {
  81. long timein;
  82. if (sscanf(timec,"%ld",&timein) == 1) {
  83. unixtime = (time_t)timein;
  84. }
  85. }
  86. if (s) {
  87. zone = strsep(&s,"|");
  88. if (zone && (*zone == '\0'))
  89. zone = NULL;
  90. if (s) {
  91. format = s;
  92. }
  93. }
  94. } else {
  95. ast_log(LOG_ERROR, "Out of memory error\n");
  96. }
  97. }
  98. if (chan->_state != AST_STATE_UP) {
  99. res = ast_answer(chan);
  100. }
  101. if (!res)
  102. res = ast_say_date_with_format(chan, unixtime, AST_DIGIT_ANY, chan->language, format, zone);
  103. LOCAL_USER_REMOVE(u);
  104. return res;
  105. }
  106. int unload_module(void)
  107. {
  108. int res;
  109. res = ast_unregister_application(app_sayunixtime);
  110. res |= ast_unregister_application(app_datetime);
  111. STANDARD_HANGUP_LOCALUSERS;
  112. return res;
  113. }
  114. int load_module(void)
  115. {
  116. int res;
  117. res = ast_register_application(app_sayunixtime, sayunixtime_exec, sayunixtime_synopsis, sayunixtime_descrip);
  118. res |= ast_register_application(app_datetime, sayunixtime_exec, sayunixtime_synopsis, datetime_descrip);
  119. return res;
  120. }
  121. char *description(void)
  122. {
  123. return tdesc;
  124. }
  125. int usecount(void)
  126. {
  127. int res;
  128. STANDARD_USECOUNT(res);
  129. return res;
  130. }
  131. char *key()
  132. {
  133. return ASTERISK_GPL_KEY;
  134. }