OSDN Git Service

Add MS7619SE
[uclinux-h8/uClinux-dist.git] / user / ser / parser / parse_event.c
1 /*
2  * $Id: parse_event.c,v 1.7 2004/08/24 09:01:25 janakj Exp $
3  *
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.
9  *
10  * Copyright (C) 2001-2003 FhG Fokus
11  *
12  * This file is part of ser, a free SIP server.
13  *
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
18  *
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:
22  *    info@iptel.org
23  *
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.
28  *
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
32  *
33  * History:
34  * --------
35  * 2003-04-26 ZSW (jiri)
36  */
37
38
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 */
45 #include "../ut.h"
46
47
48 #define PRES_STR "presence"
49 #define PRES_STR_LEN 8
50
51 #define PRES_WINFO_STR "presence.winfo"
52 #define PRES_WINFO_STR_LEN 14
53
54 #define PRES_XCAP_CHANGE_STR "xcap-change"
55 #define PRES_XCAP_CHANGE_STR_LEN 11
56
57 #define PRES_LOCATION_STR "location"
58 #define PRES_LOCATION_STR_LEN 8
59
60
61
62 static inline char* skip_token(char* _b, int _l)
63 {
64         int i = 0;
65
66         for(i = 0; i < _l; i++) {
67                 switch(_b[i]) {
68                 case ' ':
69                 case '\r':
70                 case '\n':
71                 case '\t':
72                 case ';':
73                         return _b + i;
74                 }
75         }
76
77         return _b + _l;
78 }
79
80
81 static inline int event_parser(char* _s, int _l, event_t* _e)
82 {
83         str tmp;
84         char* end;
85         char buf[128];
86
87         tmp.s = _s;
88         tmp.len = _l;
89
90         trim_leading(&tmp);
91
92         if (tmp.len == 0) {
93                 LOG(L_ERR, "event_parser(): Empty body\n");
94                 return -1;
95         }
96
97         _e->text.s = tmp.s;
98
99         end = skip_token(tmp.s, tmp.len);
100
101         _e->text.len = end - tmp.s;
102
103         strncpy(buf, tmp.s, tmp.len);
104         buf[tmp.len] = 0;
105
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;
118         } else {
119                 _e->parsed = EVENT_OTHER;
120         }
121
122         return 0;
123 }
124
125
126 /*
127  * Parse Event header field body
128  */
129 int parse_event(struct hdr_field* _h)
130 {
131         event_t* e;
132
133         if (_h->parsed != 0) {
134                 return 0;
135         }
136
137         e = (event_t*)pkg_malloc(sizeof(event_t));
138         if (e == 0) {
139                 LOG(L_ERR, "parse_event(): No memory left\n");
140                 return -1;
141         }
142
143         memset(e, 0, sizeof(event_t));
144
145         if (event_parser(_h->body.s, _h->body.len, e) < 0) {
146                 LOG(L_ERR, "parse_event(): Error in event_parser\n");
147                 pkg_free(e);
148                 return -2;
149         }
150
151         _h->parsed = (void*)e;
152         return 0;
153 }
154
155
156 /*
157  * Free all memory
158  */
159 void free_event(event_t** _e)
160 {
161         if (*_e) pkg_free(*_e);
162         *_e = 0;
163 }
164
165
166 /*
167  * Print structure, for debugging only
168  */
169 void print_event(event_t* _e)
170 {
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");
176 }