msg_rules.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <xcap/msg_rules.h>
  26. #include <xcap/parse_msg_rules.h>
  27. #include <xcap/xcap_result_codes.h>
  28. #include <cds/dstring.h>
  29. #include <cds/memory.h>
  30. #include <cds/logger.h>
  31. #include <cds/list.h>
  32. #include <cds/sstr.h>
  33. #include <string.h>
  34. int get_msg_rules(const str_t *username, const str_t *filename,
  35. xcap_query_params_t *xcap_params, msg_rules_t **dst)
  36. {
  37. char *data = NULL;
  38. int dsize = 0;
  39. char *uri = NULL;
  40. int res = RES_OK;
  41. uri = xcap_uri_for_users_document(xcap_doc_im_rules,
  42. username, filename,
  43. xcap_params);
  44. if (!uri) {
  45. /* can't create XCAP uri */
  46. ERROR_LOG("can't build XCAP uri\n");
  47. return RES_XCAP_QUERY_ERR;
  48. }
  49. res = xcap_query(uri, xcap_params, &data, &dsize);
  50. if (res != RES_OK) {
  51. DEBUG_LOG("XCAP problems for uri \'%s\'\n", uri);
  52. if (data) cds_free(data);
  53. cds_free(uri);
  54. return RES_XCAP_QUERY_ERR;
  55. }
  56. cds_free(uri);
  57. /* parse input data */
  58. res = parse_msg_rules(data, dsize, dst);
  59. if (res != RES_OK) {
  60. ERROR_LOG("Error occured during document parsing!\n");
  61. }
  62. if (data) cds_free(data);
  63. return res;
  64. }
  65. int get_msg_rules_action(cp_ruleset_t *r, const str_t *wuri,
  66. msg_handling_t *dst_action)
  67. {
  68. int res = 1; /* rule not found */
  69. cp_rule_t *rule;
  70. msg_handling_t a = msg_handling_block;
  71. msg_handling_t aa;
  72. if (!r) return -1;
  73. rule = r->rules;
  74. while (rule) {
  75. DEBUG_LOG("TRYING rule %.*s for uri %.*s\n",
  76. FMT_STR(rule->id), FMT_STR(*wuri));
  77. if (is_rule_for_uri(rule, wuri)) {
  78. DEBUG_LOG("rule %.*s matches for uri %.*s\n",
  79. FMT_STR(rule->id), FMT_STR(*wuri));
  80. if (!rule->actions) continue;
  81. if (!rule->actions->unknown) continue;
  82. aa = *(msg_handling_t*)(rule->actions->unknown->data);
  83. if (aa > a) a = aa;
  84. res = 0;
  85. }
  86. rule = rule->next;
  87. }
  88. if (dst_action && (res == 0)) *dst_action = a;
  89. return res;
  90. }
  91. void free_msg_actions(cp_actions_t *a)
  92. {
  93. cp_unknown_t *u, *nu;
  94. if (!a) return;
  95. u = a->unknown;
  96. while (u) {
  97. nu = u->next;
  98. cds_free(u);
  99. u = nu;
  100. }
  101. cds_free(a);
  102. }
  103. void free_msg_rules(cp_ruleset_t *r)
  104. {
  105. free_common_rules(r, free_msg_actions);
  106. }