app_sayunixtime.c 3.2 KB

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