parse_event.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Event header field body parser.
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * ser is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * --------
00026  * 2003-04-26 ZSW (jiri)
00027  */
00028 
00041 #include "parse_event.h"
00042 #include "../mem/mem.h"    /* pkg_malloc, pkg_free */
00043 #include "../dprint.h"
00044 #include <string.h>        /* memset */
00045 #include "../trim.h"       /* trim_leading */
00046 #include <stdio.h>         /* printf */
00047 #include "../ut.h"
00048 
00049 static struct {
00050         str name;
00051         int type;       
00052 } events[] = {
00053         {STR_STATIC_INIT("presence"),        EVENT_PRESENCE},
00054         {STR_STATIC_INIT("presence.winfo"),  EVENT_PRESENCE_WINFO},
00055         {STR_STATIC_INIT("xcap-change"),     EVENT_XCAP_CHANGE},
00056         {STR_STATIC_INIT("sip-profile"),     EVENT_SIP_PROFILE},
00057         {STR_STATIC_INIT("message-summary"), EVENT_MESSAGE_SUMMARY},
00058         {STR_STATIC_INIT("dialog"),          EVENT_DIALOG},
00059         {STR_STATIC_INIT("ua-profile"),      EVENT_UA_PROFILE},
00060         /* The following must be the last element in the array */
00061         {STR_NULL,                           EVENT_OTHER}
00062 };
00063 
00064 
00065 static inline char* skip_token(char* _b, int _l)
00066 {
00067         int i = 0;
00068 
00069         for(i = 0; i < _l; i++) {
00070                 switch(_b[i]) {
00071                 case ' ':
00072                 case '\r':
00073                 case '\n':
00074                 case '\t':
00075                 case ';':
00076                         return _b + i;
00077                 }
00078         }
00079 
00080         return _b + _l;
00081 }
00082 
00083 
00084 int event_parser(char* s, int len, event_t* e)
00085 {
00086         int i;
00087         str tmp;
00088         char* end;
00089         param_hooks_t* phooks = NULL;
00090         enum pclass pclass = CLASS_ANY;
00091 
00092         if (e == NULL) {
00093                 ERR("event_parser: Invalid parameter value\n");
00094                 return -1;
00095         }
00096 
00097         tmp.s = s;
00098         tmp.len = len;
00099         trim_leading(&tmp);
00100 
00101         if (tmp.len == 0) {
00102                 LOG(L_ERR, "event_parser: Empty body\n");
00103                 return -1;
00104         }
00105 
00106         e->name.s = tmp.s;
00107         end = skip_token(tmp.s, tmp.len);
00108         e->name.len = end - tmp.s;
00109 
00110         e->type = EVENT_OTHER;
00111         for(i = 0; events[i].name.len; i++) {
00112                 if (e->name.len == events[i].name.len &&
00113                         !strncasecmp(e->name.s, events[i].name.s, e->name.len)) {
00114                         e->type = events[i].type;
00115                         break;
00116                 }
00117         }
00118 
00119         tmp.len -= end - tmp.s;
00120         tmp.s = end;
00121         trim_leading(&tmp);
00122 
00123         e->params.list = NULL;
00124         
00125         if (tmp.len && (tmp.s[0] == ';')) {
00126                 /* Shift the semicolon and skip any leading whitespace, this is needed
00127                  * for parse_params to work correctly. */
00128                 tmp.s++; tmp.len--;
00129                 trim_leading(&tmp);
00130                 if (!tmp.len) return 0;
00131 
00132                 /* We have parameters to parse */
00133                 if (e->type == EVENT_DIALOG) {
00134                         pclass = CLASS_EVENT_DIALOG;
00135                         phooks = (param_hooks_t*)&e->params.hooks;
00136                 }
00137 
00138                 if (parse_params(&tmp, pclass, phooks, &e->params.list) < 0) {
00139                         ERR("event_parser: Error while parsing parameters parameters\n");
00140                         return -1;
00141                 }
00142         }
00143         return 0;
00144 }
00145 
00146 
00150 int parse_event(struct hdr_field* _h)
00151 {
00152         event_t* e;
00153 
00154         if (_h->parsed != 0) {
00155                 return 0;
00156         }
00157 
00158         e = (event_t*)pkg_malloc(sizeof(event_t));
00159         if (e == 0) {
00160                 LOG(L_ERR, "parse_event(): No memory left\n");
00161                 return -1;
00162         }
00163 
00164         memset(e, 0, sizeof(event_t));
00165 
00166         if (event_parser(_h->body.s, _h->body.len, e) < 0) {
00167                 LOG(L_ERR, "parse_event(): Error in event_parser\n");
00168                 pkg_free(e);
00169                 return -2;
00170         }
00171 
00172         _h->parsed = (void*)e;
00173         return 0;
00174 }
00175 
00176 
00180 void free_event(event_t** _e)
00181 {
00182         if (*_e) {
00183                 if ((*_e)->params.list) free_params((*_e)->params.list);
00184                 pkg_free(*_e);
00185                 *_e = NULL;
00186         }
00187 }
00188 
00189 
00193 void print_event(event_t* e)
00194 {
00195         fprintf(stderr, "===Event===\n");
00196         fprintf(stderr, "name  : \'%.*s\'\n", STR_FMT(&e->name));
00197         fprintf(stderr, "type: %d\n", e->type);
00198         if (e->params.list) {
00199                 print_params(stderr, e->params.list);
00200         }
00201         fprintf(stderr, "===/Event===\n");
00202 }