parse_event.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * $Id$
  3. *
  4. * Event header field body parser.
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * ser is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * History:
  25. * --------
  26. * 2003-04-26 ZSW (jiri)
  27. */
  28. /*! \file
  29. * \brief Parser :: Event header field body parser.
  30. *
  31. * The parser was written for Presence Agent module only.
  32. * it recognize presence package only, no sub-packages, no parameters
  33. * It should be replaced by a more generic parser if sub-packages or
  34. * parameters should be parsed too.
  35. *
  36. * \ingroup parser
  37. */
  38. #include "parse_event.h"
  39. #include "../mem/mem.h" /* pkg_malloc, pkg_free */
  40. #include "../dprint.h"
  41. #include <string.h> /* memset */
  42. #include "../trim.h" /* trim_leading */
  43. #include <stdio.h> /* printf */
  44. #include "../ut.h"
  45. static struct {
  46. str name;
  47. int type;
  48. } events[] = {
  49. {STR_STATIC_INIT("presence"), EVENT_PRESENCE},
  50. {STR_STATIC_INIT("presence.winfo"), EVENT_PRESENCE_WINFO},
  51. {STR_STATIC_INIT("xcap-change"), EVENT_XCAP_CHANGE},
  52. {STR_STATIC_INIT("sip-profile"), EVENT_SIP_PROFILE},
  53. {STR_STATIC_INIT("message-summary"), EVENT_MESSAGE_SUMMARY},
  54. {STR_STATIC_INIT("dialog"), EVENT_DIALOG},
  55. /* The following must be the last element in the array */
  56. {STR_NULL, EVENT_OTHER}
  57. };
  58. static inline char* skip_token(char* _b, int _l)
  59. {
  60. int i = 0;
  61. for(i = 0; i < _l; i++) {
  62. switch(_b[i]) {
  63. case ' ':
  64. case '\r':
  65. case '\n':
  66. case '\t':
  67. case ';':
  68. return _b + i;
  69. }
  70. }
  71. return _b + _l;
  72. }
  73. int event_parser(char* s, int len, event_t* e)
  74. {
  75. int i;
  76. str tmp;
  77. char* end;
  78. param_hooks_t* phooks = NULL;
  79. enum pclass pclass = CLASS_ANY;
  80. if (e == NULL) {
  81. ERR("event_parser: Invalid parameter value\n");
  82. return -1;
  83. }
  84. tmp.s = s;
  85. tmp.len = len;
  86. trim_leading(&tmp);
  87. if (tmp.len == 0) {
  88. LOG(L_ERR, "event_parser: Empty body\n");
  89. return -1;
  90. }
  91. e->name.s = tmp.s;
  92. end = skip_token(tmp.s, tmp.len);
  93. e->name.len = end - tmp.s;
  94. e->type = EVENT_OTHER;
  95. for(i = 0; events[i].name.len; i++) {
  96. if (e->name.len == events[i].name.len &&
  97. !strncasecmp(e->name.s, events[i].name.s, e->name.len)) {
  98. e->type = events[i].type;
  99. break;
  100. }
  101. }
  102. tmp.len -= end - tmp.s;
  103. tmp.s = end;
  104. trim_leading(&tmp);
  105. e->params.list = NULL;
  106. if (tmp.len && (tmp.s[0] == ';')) {
  107. /* Shift the semicolon and skip any leading whitespace, this is needed
  108. * for parse_params to work correctly. */
  109. tmp.s++; tmp.len--;
  110. trim_leading(&tmp);
  111. if (!tmp.len) return 0;
  112. /* We have parameters to parse */
  113. if (e->type == EVENT_DIALOG) {
  114. pclass = CLASS_EVENT_DIALOG;
  115. phooks = (param_hooks_t*)&e->params.dialog;
  116. }
  117. if (parse_params(&tmp, pclass, phooks, &e->params.list) < 0) {
  118. ERR("event_parser: Error while parsing parameters parameters\n");
  119. return -1;
  120. }
  121. }
  122. return 0;
  123. }
  124. /*! \brief
  125. * Parse Event header field body
  126. */
  127. int parse_event(struct hdr_field* _h)
  128. {
  129. event_t* e;
  130. if (_h->parsed != 0) {
  131. return 0;
  132. }
  133. e = (event_t*)pkg_malloc(sizeof(event_t));
  134. if (e == 0) {
  135. LOG(L_ERR, "parse_event(): No memory left\n");
  136. return -1;
  137. }
  138. memset(e, 0, sizeof(event_t));
  139. if (event_parser(_h->body.s, _h->body.len, e) < 0) {
  140. LOG(L_ERR, "parse_event(): Error in event_parser\n");
  141. pkg_free(e);
  142. return -2;
  143. }
  144. _h->parsed = (void*)e;
  145. return 0;
  146. }
  147. /*! \brief
  148. * Free all memory
  149. */
  150. void free_event(event_t** _e)
  151. {
  152. if (*_e) {
  153. if ((*_e)->params.list) free_params((*_e)->params.list);
  154. pkg_free(*_e);
  155. *_e = NULL;
  156. }
  157. }
  158. /*! \brief
  159. * Print structure, for debugging only
  160. */
  161. void print_event(event_t* e)
  162. {
  163. fprintf(stderr, "===Event===\n");
  164. fprintf(stderr, "name : \'%.*s\'\n", STR_FMT(&e->name));
  165. fprintf(stderr, "type: %d\n", e->type);
  166. if (e->params.list) {
  167. print_params(stderr, e->params.list);
  168. }
  169. fprintf(stderr, "===/Event===\n");
  170. }