core: fix warnings/eliminate unused variables
[sip-router] / parser / parse_subscription_state.c
1 /*! \file
2  * \brief Parser :: Parse subscription-state in NOTIFY
3  *
4  * \ingroup parser
5  */
6
7 #include "parse_subscription_state.h"
8 #include "../dprint.h"
9 #include "../trim.h"
10 #include "../mem/mem.h"
11 #include "../ut.h"
12 #include "parser_f.h"
13 #include "parse_param.h"
14 #include <string.h>
15
16 void free_subscription_state(subscription_state_t**ss)
17 {
18         if (ss) {
19                 if (*ss) pkg_free(*ss);
20                 *ss = 0;
21         }
22 }
23
24 static inline int str_cmp(const str *a, const str *b)
25 {
26         int i;
27         
28         if (a->len != b->len) return 1;
29         
30         for (i = 0; i < a->len; i++) 
31                 if (a->s[i] != b->s[i]) return 1;
32         return 0;
33 }
34
35 static int ss_parse(str *src, subscription_state_t *ss)
36 {
37         static str active = STR_STATIC_INIT("active");
38         static str pending = STR_STATIC_INIT("pending");
39         static str terminated = STR_STATIC_INIT("terminated");
40         
41         int res = 0;
42         param_hooks_t ph;
43         param_t *params;
44         str s = *src;
45         str state;
46         char *c;
47         
48         /* initialization */
49         ss->expires_set = 0;
50         ss->expires = 0;
51         
52         trim_leading(&s);
53                 
54         state = s;
55         
56         c = find_not_quoted(&s, ';');
57         if (c) {
58                 /* first parameter starts after c */
59                 state.len = c - state.s;
60                 s.len = s.len - (c - s.s) - 1;
61                 s.s = c + 1;
62         }
63         else {
64                 s.len = 0;
65         }
66
67         /* set state value */
68         trim(&state);
69         if (str_cmp(&state, &active) == 0) {
70                 ss->value = ss_active;
71         }
72         else if (str_cmp(&state, &pending) == 0) {
73                 ss->value = ss_pending;
74         }
75         else if (str_cmp(&state, &terminated) == 0) {
76                 ss->value = ss_terminated;
77         }
78         else { 
79                 /* INFO("unknown subscription-State value :%.*s\n",
80                                         state.len, state.s); */
81                 ss->value = ss_extension;
82         }
83
84         /* explore parameters */
85         
86         trim_leading(&s);
87         if (s.len > 0) {
88                 params = NULL;
89                 if (parse_params(&s, CLASS_CONTACT, &ph, &params) < 0) {
90                         ERR("can't parse params\n");
91                         res = -1;
92                 }
93                 else {
94                         if (ph.contact.expires) {
95                                 ss->expires_set = 1;
96                                 res = str2int(&ph.contact.expires->body, &ss->expires);
97                                 if (res != 0) 
98                                         ERR("invalid expires value: \'%.*s\'\n", 
99                                                 ph.contact.expires->body.len,
100                                                 ph.contact.expires->body.s);
101                         }
102                         if (params) free_params(params);
103                 }
104         }
105         /*
106         ss->value = ss_active;
107         ss->expires = 0;*/
108
109
110         return res;
111 }
112
113 int parse_subscription_state(struct hdr_field *h)
114 {
115         subscription_state_t *ss;
116         if (h->parsed) return 0;
117
118         ss = (subscription_state_t*)pkg_malloc(sizeof(*ss));
119         if (!ss) {
120                 ERR("No memory left\n");
121                 return -1;
122         }
123
124         memset(ss, 0, sizeof(*ss));
125
126         if (ss_parse(&h->body, ss) < 0) {
127                 ERR("Can't parse Subscription-State\n");
128                 pkg_free(ss);
129                 return -2;
130         }
131
132         h->parsed = (void*)ss;
133         
134         return 0;
135 }