parse_event.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "parse_event.h"
00035 #include "../mem/mem.h"
00036 #include "../dprint.h"
00037 #include <string.h>
00038 #include "../trim.h"
00039 #include <stdio.h>
00040 #include "../ut.h"
00041
00042
00043 #define PRES_STR "presence"
00044 #define PRES_STR_LEN 8
00045
00046 #define PRES_WINFO_STR "presence.winfo"
00047 #define PRES_WINFO_STR_LEN 14
00048
00049 #define PRES_XCAP_CHANGE_STR "xcap-change"
00050 #define PRES_XCAP_CHANGE_STR_LEN 11
00051
00052 #define PRES_SIP_PROFILE_STR "sip-profile"
00053 #define PRES_SIP_PROFILE_STR_LEN 11
00054
00055 #define MWI_STR "message-summary"
00056 #define MWI_STR_LEN 15
00057
00058 #define DIALOG_STR "dialog"
00059 #define DIALOG_STR_LEN 6
00060
00061 #define DIALOG_SLA_STR "dialog;sla"
00062 #define DIALOG_SLA_STR_LEN 10
00063
00064 static inline char* skip_token(char* _b, int _l)
00065 {
00066 int i = 0;
00067
00068 for(i = 0; i < _l; i++) {
00069 switch(_b[i]) {
00070 case ' ':
00071 case '\r':
00072 case '\n':
00073 case '\t':
00074 case ';':
00075 return _b + i;
00076 }
00077 }
00078
00079 return _b + _l;
00080 }
00081
00082
00083 int event_parser(char* _s, int _l, event_t* _e)
00084 {
00085 str tmp;
00086 char* end;
00087 char buf[128];
00088 param_hooks_t phooks;
00089
00090 tmp.s = _s;
00091 tmp.len = _l;
00092
00093 trim_leading(&tmp);
00094
00095 if (tmp.len == 0) {
00096 LM_ERR("empty body\n");
00097 return -1;
00098 }
00099
00100 _e->text.s = tmp.s;
00101
00102 end = skip_token(tmp.s, tmp.len);
00103
00104 _e->text.len = end - tmp.s;
00105
00106 strncpy(buf, tmp.s, tmp.len);
00107 buf[tmp.len] = 0;
00108
00109 if ((_e->text.len == PRES_STR_LEN) &&
00110 !strncasecmp(PRES_STR, tmp.s, _e->text.len)) {
00111 _e->parsed = EVENT_PRESENCE;
00112 } else if ((_e->text.len == PRES_XCAP_CHANGE_STR_LEN) &&
00113 !strncasecmp(PRES_XCAP_CHANGE_STR, tmp.s, _e->text.len)) {
00114 _e->parsed = EVENT_XCAP_CHANGE;
00115 } else if ((_e->text.len == PRES_WINFO_STR_LEN) &&
00116 !strncasecmp(PRES_WINFO_STR, tmp.s, _e->text.len)) {
00117 _e->parsed = EVENT_PRESENCE_WINFO;
00118 } else if ((_e->text.len == PRES_SIP_PROFILE_STR_LEN) &&
00119 !strncasecmp(PRES_SIP_PROFILE_STR, tmp.s, _e->text.len)) {
00120 _e->parsed = EVENT_SIP_PROFILE;
00121 } else if ((_e->text.len == DIALOG_STR_LEN) &&
00122 !strncasecmp(DIALOG_STR, tmp.s, _e->text.len)) {
00123 _e->parsed = EVENT_DIALOG;
00124 } else if ((_e->text.len == MWI_STR_LEN) &&
00125 !strncasecmp(MWI_STR, tmp.s, _e->text.len)) {
00126 _e->parsed = EVENT_MWI;
00127 } else {
00128 _e->parsed = EVENT_OTHER;
00129 }
00130
00131
00132 if( (*end)== ';')
00133 {
00134 str params_str;
00135 params_str.s= end+1;
00136 end= skip_token(params_str.s, tmp.len- _e->text.len- 1);
00137
00138 params_str.len= end- params_str.s;
00139
00140 if (parse_params(¶ms_str, CLASS_ANY, &phooks, &_e->params)<0)
00141 return -1;
00142
00143 if(_e->parsed == EVENT_DIALOG && _e->params!= NULL && _e->params->next== NULL&&
00144 _e->params->name.len== 3 && strncmp(_e->params->name.s, "sla", 3)== 0 )
00145 {
00146 _e->parsed = EVENT_DIALOG_SLA;
00147 }
00148
00149 }
00150 else
00151 _e->params= NULL;
00152
00153 return 0;
00154 }
00155
00156
00157
00158
00159
00160 int parse_event(struct hdr_field* _h)
00161 {
00162 event_t* e;
00163
00164 if (_h->parsed != 0) {
00165 return 0;
00166 }
00167
00168 e = (event_t*)pkg_malloc(sizeof(event_t));
00169 if (e == 0) {
00170 LM_ERR("no pkg memory left\n");
00171 return -1;
00172 }
00173
00174 memset(e, 0, sizeof(event_t));
00175
00176 if (event_parser(_h->body.s, _h->body.len, e) < 0) {
00177 LM_ERR("event_parser failed\n");
00178 pkg_free(e);
00179 return -2;
00180 }
00181
00182 _h->parsed = (void*)e;
00183 return 0;
00184 }
00185
00186
00187
00188
00189
00190 void free_event(event_t** _e)
00191 {
00192 if (*_e)
00193 {
00194 if((*_e)->params)
00195 free_params((*_e)->params);
00196 pkg_free(*_e);
00197 }
00198 *_e = 0;
00199 }
00200
00201
00202
00203
00204
00205 void print_event(event_t* _e)
00206 {
00207 printf("===Event===\n");
00208 printf("text : \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
00209 printf("parsed: %s\n",
00210 (_e->parsed == EVENT_PRESENCE) ? ("EVENT_PRESENCE") : ("EVENT_OTHER"));
00211 printf("===/Event===\n");
00212 }