2 * $Id: parse_event.c,v 1.7 2004/08/24 09:01:25 janakj Exp $
4 * Event header field body parser.
5 * The parser was written for Presence Agent module only.
6 * it recognize presence package only, no sub-packages, no parameters
7 * It should be replaced by a more generic parser if sub-packages or
8 * parameters should be parsed too.
10 * Copyright (C) 2001-2003 FhG Fokus
12 * This file is part of ser, a free SIP server.
14 * ser is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version
19 * For a license to use the ser software under conditions
20 * other than those described here, or to purchase support for this
21 * software, please contact iptel.org by e-mail at the following addresses:
24 * ser is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 * 2003-04-26 ZSW (jiri)
39 #include "parse_event.h"
40 #include "../mem/mem.h" /* pkg_malloc, pkg_free */
41 #include "../dprint.h"
42 #include <string.h> /* memset */
43 #include "../trim.h" /* trim_leading */
44 #include <stdio.h> /* printf */
48 #define PRES_STR "presence"
49 #define PRES_STR_LEN 8
51 #define PRES_WINFO_STR "presence.winfo"
52 #define PRES_WINFO_STR_LEN 14
54 #define PRES_XCAP_CHANGE_STR "xcap-change"
55 #define PRES_XCAP_CHANGE_STR_LEN 11
57 #define PRES_LOCATION_STR "location"
58 #define PRES_LOCATION_STR_LEN 8
62 static inline char* skip_token(char* _b, int _l)
66 for(i = 0; i < _l; i++) {
81 static inline int event_parser(char* _s, int _l, event_t* _e)
93 LOG(L_ERR, "event_parser(): Empty body\n");
99 end = skip_token(tmp.s, tmp.len);
101 _e->text.len = end - tmp.s;
103 strncpy(buf, tmp.s, tmp.len);
106 if ((_e->text.len == PRES_STR_LEN) &&
107 !strncasecmp(PRES_STR, tmp.s, _e->text.len)) {
108 _e->parsed = EVENT_PRESENCE;
109 } else if ((_e->text.len == PRES_XCAP_CHANGE_STR_LEN) &&
110 !strncasecmp(PRES_XCAP_CHANGE_STR, tmp.s, _e->text.len)) {
111 _e->parsed = EVENT_XCAP_CHANGE;
112 } else if ((_e->text.len == PRES_LOCATION_STR_LEN) &&
113 !strncasecmp(PRES_LOCATION_STR, tmp.s, _e->text.len)) {
114 _e->parsed = EVENT_LOCATION;
115 } else if ((_e->text.len == PRES_WINFO_STR_LEN) &&
116 !strncasecmp(PRES_WINFO_STR, tmp.s, _e->text.len)) {
117 _e->parsed = EVENT_PRESENCE_WINFO;
119 _e->parsed = EVENT_OTHER;
127 * Parse Event header field body
129 int parse_event(struct hdr_field* _h)
133 if (_h->parsed != 0) {
137 e = (event_t*)pkg_malloc(sizeof(event_t));
139 LOG(L_ERR, "parse_event(): No memory left\n");
143 memset(e, 0, sizeof(event_t));
145 if (event_parser(_h->body.s, _h->body.len, e) < 0) {
146 LOG(L_ERR, "parse_event(): Error in event_parser\n");
151 _h->parsed = (void*)e;
159 void free_event(event_t** _e)
161 if (*_e) pkg_free(*_e);
167 * Print structure, for debugging only
169 void print_event(event_t* _e)
171 printf("===Event===\n");
172 printf("text : \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
173 printf("parsed: %s\n",
174 (_e->parsed == EVENT_PRESENCE) ? ("EVENT_PRESENCE") : ("EVENT_OTHER"));
175 printf("===/Event===\n");