2
0

parse_event.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. {STR_STATIC_INIT("ua-profile"), EVENT_UA_PROFILE},
  56. /* The following must be the last element in the array */
  57. {STR_NULL, EVENT_OTHER}
  58. };
  59. static inline char* skip_token(char* _b, int _l)
  60. {
  61. int i = 0;
  62. for(i = 0; i < _l; i++) {
  63. switch(_b[i]) {
  64. case ' ':
  65. case '\r':
  66. case '\n':
  67. case '\t':
  68. case ';':
  69. return _b + i;
  70. }
  71. }
  72. return _b + _l;
  73. }
  74. int event_parser(char* s, int len, event_t* e)
  75. {
  76. int i;
  77. str tmp;
  78. char* end;
  79. param_hooks_t* phooks = NULL;
  80. enum pclass pclass = CLASS_ANY;
  81. if (e == NULL) {
  82. ERR("event_parser: Invalid parameter value\n");
  83. return -1;
  84. }
  85. tmp.s = s;
  86. tmp.len = len;
  87. trim_leading(&tmp);
  88. if (tmp.len == 0) {
  89. LOG(L_ERR, "event_parser: Empty body\n");
  90. return -1;
  91. }
  92. e->name.s = tmp.s;
  93. end = skip_token(tmp.s, tmp.len);
  94. e->name.len = end - tmp.s;
  95. e->type = EVENT_OTHER;
  96. for(i = 0; events[i].name.len; i++) {
  97. if (e->name.len == events[i].name.len &&
  98. !strncasecmp(e->name.s, events[i].name.s, e->name.len)) {
  99. e->type = events[i].type;
  100. break;
  101. }
  102. }
  103. tmp.len -= end - tmp.s;
  104. tmp.s = end;
  105. trim_leading(&tmp);
  106. e->params.list = NULL;
  107. if (tmp.len && (tmp.s[0] == ';')) {
  108. /* Shift the semicolon and skip any leading whitespace, this is needed
  109. * for parse_params to work correctly. */
  110. tmp.s++; tmp.len--;
  111. trim_leading(&tmp);
  112. if (!tmp.len) return 0;
  113. /* We have parameters to parse */
  114. if (e->type == EVENT_DIALOG) {
  115. pclass = CLASS_EVENT_DIALOG;
  116. phooks = (param_hooks_t*)&e->params.hooks;
  117. }
  118. if (parse_params(&tmp, pclass, phooks, &e->params.list) < 0) {
  119. ERR("event_parser: Error while parsing parameters parameters\n");
  120. return -1;
  121. }
  122. }
  123. return 0;
  124. }
  125. /*! \brief
  126. * Parse Event header field body
  127. */
  128. int parse_event(struct hdr_field* _h)
  129. {
  130. event_t* e;
  131. if (_h->parsed != 0) {
  132. return 0;
  133. }
  134. e = (event_t*)pkg_malloc(sizeof(event_t));
  135. if (e == 0) {
  136. LOG(L_ERR, "parse_event(): No memory left\n");
  137. return -1;
  138. }
  139. memset(e, 0, sizeof(event_t));
  140. if (event_parser(_h->body.s, _h->body.len, e) < 0) {
  141. LOG(L_ERR, "parse_event(): Error in event_parser\n");
  142. pkg_free(e);
  143. return -2;
  144. }
  145. _h->parsed = (void*)e;
  146. return 0;
  147. }
  148. /*! \brief
  149. * Free all memory
  150. */
  151. void free_event(event_t** _e)
  152. {
  153. if (*_e) {
  154. if ((*_e)->params.list) free_params((*_e)->params.list);
  155. pkg_free(*_e);
  156. *_e = NULL;
  157. }
  158. }
  159. /*! \brief
  160. * Print structure, for debugging only
  161. */
  162. void print_event(event_t* e)
  163. {
  164. fprintf(stderr, "===Event===\n");
  165. fprintf(stderr, "name : \'%.*s\'\n", STR_FMT(&e->name));
  166. fprintf(stderr, "type: %d\n", e->type);
  167. if (e->params.list) {
  168. print_params(stderr, e->params.list);
  169. }
  170. fprintf(stderr, "===/Event===\n");
  171. }