tmrec_mod.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2012 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "../../sr_module.h"
  29. #include "../../dprint.h"
  30. #include "../../ut.h"
  31. #include "../../pvar.h"
  32. #include "../../mod_fix.h"
  33. #include "../../lib/srutils/tmrec.h"
  34. #include "period.h"
  35. MODULE_VERSION
  36. static int mod_init(void);
  37. static int child_init(int);
  38. static void mod_destroy(void);
  39. static int w_tmrec_match(struct sip_msg* msg, char* rec, char* t);
  40. static int fixup_tmrec_match(void** param, int param_no);
  41. static int w_is_leap_year(struct sip_msg* msg, char* t, char* p2);
  42. static int fixup_is_leap_year(void** param, int param_no);
  43. static int fixup_time_period_match(void** param, int param_no);
  44. static int w_time_period_match(struct sip_msg* msg, char* period, char* t);
  45. int tmrec_wday = 0;
  46. char tmrec_separator = '|';
  47. char *tmrec_separator_param = NULL;
  48. static cmd_export_t cmds[]={
  49. {"tmrec_match", (cmd_function)w_tmrec_match, 1, fixup_tmrec_match,
  50. 0, ANY_ROUTE},
  51. {"tmrec_match", (cmd_function)w_tmrec_match, 2, fixup_tmrec_match,
  52. 0, ANY_ROUTE},
  53. {"is_leap_year", (cmd_function)w_is_leap_year, 0, fixup_is_leap_year,
  54. 0, ANY_ROUTE},
  55. {"is_leap_year", (cmd_function)w_is_leap_year, 1, fixup_is_leap_year,
  56. 0, ANY_ROUTE},
  57. {"time_period_match", (cmd_function)w_time_period_match, 1, fixup_time_period_match,
  58. 0, ANY_ROUTE},
  59. {"time_period_match", (cmd_function)w_time_period_match, 2, fixup_time_period_match,
  60. 0, ANY_ROUTE},
  61. {0, 0, 0, 0, 0, 0}
  62. };
  63. static param_export_t params[]={
  64. {"wday", INT_PARAM, &tmrec_wday},
  65. {"separator", PARAM_STRING, &tmrec_separator_param},
  66. {0, 0, 0}
  67. };
  68. struct module_exports exports = {
  69. "tmrec",
  70. DEFAULT_DLFLAGS, /* dlopen flags */
  71. cmds,
  72. params,
  73. 0,
  74. 0, /* exported MI functions */
  75. 0, /* exported pseudo-variables */
  76. 0, /* extra processes */
  77. mod_init, /* module initialization function */
  78. 0, /* response function */
  79. mod_destroy, /* destroy function */
  80. child_init /* per child init function */
  81. };
  82. /**
  83. * init module function
  84. */
  85. static int mod_init(void)
  86. {
  87. if(tmrec_separator_param!=NULL)
  88. tmrec_separator = tmrec_separator_param[0];
  89. return 0;
  90. }
  91. /**
  92. * @brief Initialize async module children
  93. */
  94. static int child_init(int rank)
  95. {
  96. if (rank!=PROC_MAIN)
  97. return 0;
  98. return 0;
  99. }
  100. /**
  101. * destroy module function
  102. */
  103. static void mod_destroy(void)
  104. {
  105. return;
  106. }
  107. static int w_is_leap_year(struct sip_msg* msg, char* t, char* str2)
  108. {
  109. time_t tv;
  110. struct tm *tb;
  111. int y;
  112. if(msg==NULL)
  113. return -1;
  114. if(t!=NULL)
  115. {
  116. if(fixup_get_ivalue(msg, (gparam_t*)t, &y)!=0)
  117. {
  118. LM_ERR("invalid time parameter value\n");
  119. return -1;
  120. }
  121. } else {
  122. tv = time(NULL);
  123. tb = localtime(&tv);
  124. y = 1900 + tb->tm_year;
  125. }
  126. if(tr_is_leap_year(y))
  127. return 1;
  128. return -1;
  129. }
  130. static int fixup_is_leap_year(void** param, int param_no)
  131. {
  132. if(param_no==1)
  133. return fixup_igp_null(param, param_no);
  134. return 0;
  135. }
  136. static int w_tmrec_match(struct sip_msg* msg, char* rec, char* t)
  137. {
  138. str rv;
  139. time_t tv;
  140. int ti;
  141. ac_tm_t act;
  142. tmrec_t tmr;
  143. if(msg==NULL)
  144. return -1;
  145. if(fixup_get_svalue(msg, (gparam_t*)rec, &rv)!=0)
  146. {
  147. LM_ERR("invalid time recurrence parameter value\n");
  148. return -1;
  149. }
  150. if(t!=NULL)
  151. {
  152. if(fixup_get_ivalue(msg, (gparam_t*)t, &ti)!=0)
  153. {
  154. LM_ERR("invalid time stamp parameter value\n");
  155. return -1;
  156. }
  157. tv = (time_t)ti;
  158. } else {
  159. tv = time(NULL);
  160. }
  161. memset(&act, 0, sizeof(act));
  162. memset(&tmr, 0, sizeof(tmr));
  163. /* parse time recurrence definition */
  164. if(tr_parse_recurrence_string(&tmr, rv.s, tmrec_separator)<0)
  165. return -1;
  166. /* if there is no dstart, timerec is valid */
  167. if (tmr.dtstart==0)
  168. goto done;
  169. /* set current time */
  170. if (ac_tm_set_time(&act, tv)<0)
  171. goto error;
  172. /* match the specified recurence */
  173. if (tr_check_recurrence(&tmr, &act, 0)!=0)
  174. goto error;
  175. done:
  176. tmrec_destroy(&tmr);
  177. ac_tm_destroy(&act);
  178. return 1;
  179. error:
  180. tmrec_destroy(&tmr);
  181. ac_tm_destroy(&act);
  182. return -1;
  183. }
  184. static int fixup_tmrec_match(void** param, int param_no)
  185. {
  186. if(param_no==1)
  187. {
  188. if(fixup_spve_null(param, 1)<0)
  189. return -1;
  190. return 0;
  191. } else if(param_no==2) {
  192. if(fixup_igp_null(param, 1)<0)
  193. return -1;
  194. }
  195. return 0;
  196. }
  197. static int fixup_time_period_match(void** param, int param_no)
  198. {
  199. if(param_no==1)
  200. {
  201. if(fixup_spve_null(param, 1)<0)
  202. return -1;
  203. return 0;
  204. } else if(param_no==2) {
  205. if(fixup_igp_null(param, 1)<0)
  206. return -1;
  207. }
  208. return 0;
  209. }
  210. static int w_time_period_match(struct sip_msg* msg, char* period, char* t)
  211. {
  212. str rv;
  213. time_t tv;
  214. int ti;
  215. if(msg==NULL)
  216. return -2;
  217. if(fixup_get_svalue(msg, (gparam_t*)period, &rv)!=0)
  218. {
  219. LM_ERR("invalid period parameter value\n");
  220. return -3;
  221. }
  222. if(t!=NULL)
  223. {
  224. if(fixup_get_ivalue(msg, (gparam_t*)t, &ti)!=0)
  225. {
  226. LM_ERR("invalid time stamp parameter value\n");
  227. return -4;
  228. }
  229. tv = (time_t)ti;
  230. } else {
  231. tv = time(NULL);
  232. }
  233. if (in_period(tv, rv.s))
  234. return 1;
  235. return -1;
  236. }