| |
1 /** |
| |
2 * @file sipmsg.c |
| |
3 * |
| |
4 * gaim |
| |
5 * |
| |
6 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de> |
| |
7 * |
| |
8 * This program is free software; you can redistribute it and/or modify |
| |
9 * it under the terms of the GNU General Public License as published by |
| |
10 * the Free Software Foundation; either version 2 of the License, or |
| |
11 * (at your option) any later version. |
| |
12 * |
| |
13 * This program is distributed in the hope that it will be useful, |
| |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
16 * GNU General Public License for more details. |
| |
17 * |
| |
18 * You should have received a copy of the GNU General Public License |
| |
19 * along with this program; if not, write to the Free Software |
| |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| |
21 */ |
| |
22 |
| |
23 #include "internal.h" |
| |
24 |
| |
25 #include "accountopt.h" |
| |
26 #include "blist.h" |
| |
27 #include "conversation.h" |
| |
28 #include "debug.h" |
| |
29 #include "notify.h" |
| |
30 #include "prpl.h" |
| |
31 #include "plugin.h" |
| |
32 #include "util.h" |
| |
33 #include "version.h" |
| |
34 |
| |
35 #include "simple.h" |
| |
36 #include "sipmsg.h" |
| |
37 |
| |
38 struct sipmsg *sipmsg_parse_msg(const gchar *msg) { |
| |
39 const char *tmp = strstr(msg, "\r\n\r\n"); |
| |
40 char *line; |
| |
41 struct sipmsg *smsg; |
| |
42 |
| |
43 if(!tmp) return NULL; |
| |
44 |
| |
45 line = g_strndup(msg, tmp - msg); |
| |
46 |
| |
47 smsg = sipmsg_parse_header(line); |
| |
48 smsg->body = g_strdup(tmp + 4); |
| |
49 |
| |
50 g_free(line); |
| |
51 return smsg; |
| |
52 } |
| |
53 |
| |
54 struct sipmsg *sipmsg_parse_header(const gchar *header) { |
| |
55 struct sipmsg *msg = g_new0(struct sipmsg,1); |
| |
56 gchar **lines = g_strsplit(header,"\r\n",0); |
| |
57 gchar **parts; |
| |
58 gchar *dummy; |
| |
59 gchar *dummy2; |
| |
60 gchar *tmp; |
| |
61 int i=1; |
| |
62 if(!lines[0]) return NULL; |
| |
63 parts = g_strsplit(lines[0], " ", 3); |
| |
64 if(!parts[0] || !parts[1] || !parts[2]) { |
| |
65 g_strfreev(parts); |
| |
66 g_strfreev(lines); |
| |
67 g_free(msg); |
| |
68 return NULL; |
| |
69 } |
| |
70 if(strstr(parts[0],"SIP")) { /* numeric response */ |
| |
71 msg->method = g_strdup(parts[2]); |
| |
72 msg->response = strtol(parts[1],NULL,10); |
| |
73 } else { /* request */ |
| |
74 msg->method = g_strdup(parts[0]); |
| |
75 msg->target = g_strdup(parts[1]); |
| |
76 msg->response = 0; |
| |
77 } |
| |
78 g_strfreev(parts); |
| |
79 for(i=1; lines[i] && strlen(lines[i])>2; i++) { |
| |
80 parts = g_strsplit(lines[i], ":", 2); |
| |
81 if(!parts[0] || !parts[1]) { |
| |
82 g_strfreev(parts); |
| |
83 g_strfreev(lines); |
| |
84 g_free(msg); |
| |
85 return NULL; |
| |
86 } |
| |
87 dummy = parts[1]; |
| |
88 dummy2 = 0; |
| |
89 while(*dummy==' ' || *dummy=='\t') dummy++; |
| |
90 dummy2 = g_strdup(dummy); |
| |
91 while(lines[i+1] && (lines[i+1][0]==' ' || lines[i+1][0]=='\t')) { |
| |
92 i++; |
| |
93 dummy = lines[i]; |
| |
94 while(*dummy==' ' || *dummy=='\t') dummy++; |
| |
95 tmp = g_strdup_printf("%s %s",dummy2, dummy); |
| |
96 g_free(dummy2); |
| |
97 dummy2 = tmp; |
| |
98 } |
| |
99 sipmsg_add_header(msg, parts[0], dummy2); |
| |
100 g_strfreev(parts); |
| |
101 } |
| |
102 g_strfreev(lines); |
| |
103 msg->bodylen = strtol(sipmsg_find_header(msg, "Content-Length"),NULL,10); |
| |
104 if(msg->response) { |
| |
105 tmp = sipmsg_find_header(msg, "CSeq"); |
| |
106 if(!tmp) { |
| |
107 /* SHOULD NOT HAPPEN */ |
| |
108 msg->method = 0; |
| |
109 } else { |
| |
110 parts = g_strsplit(tmp, " ", 2); |
| |
111 msg->method = g_strdup(parts[1]); |
| |
112 g_strfreev(parts); |
| |
113 } |
| |
114 } |
| |
115 return msg; |
| |
116 } |
| |
117 |
| |
118 void sipmsg_print(const struct sipmsg *msg) { |
| |
119 GSList *cur; |
| |
120 struct siphdrelement *elem; |
| |
121 gaim_debug(GAIM_DEBUG_MISC, "simple", "SIP MSG\n"); |
| |
122 gaim_debug(GAIM_DEBUG_MISC, "simple", "response: %d\nmethod: %s\nbodylen: %d\n",msg->response,msg->method,msg->bodylen); |
| |
123 if(msg->target) gaim_debug(GAIM_DEBUG_MISC, "simple", "target: %s\n",msg->target); |
| |
124 cur = msg->headers; |
| |
125 while(cur) { |
| |
126 elem = cur->data; |
| |
127 gaim_debug(GAIM_DEBUG_MISC, "simple", "name: %s value: %s\n",elem->name, elem->value); |
| |
128 cur = g_slist_next(cur); |
| |
129 } |
| |
130 } |
| |
131 |
| |
132 char *sipmsg_to_string(const struct sipmsg *msg) { |
| |
133 GSList *cur; |
| |
134 GString *outstr = g_string_new(""); |
| |
135 struct siphdrelement *elem; |
| |
136 |
| |
137 if(msg->response) |
| |
138 g_string_append_printf(outstr, "SIP/2.0 %d Unknown\r\n", |
| |
139 msg->response); |
| |
140 else |
| |
141 g_string_append_printf(outstr, "%s %s SIP/2.0\r\n", |
| |
142 msg->method, msg->target); |
| |
143 |
| |
144 cur = msg->headers; |
| |
145 while(cur) { |
| |
146 elem = cur->data; |
| |
147 g_string_append_printf(outstr, "%s: %s\r\n", elem->name, |
| |
148 elem->value); |
| |
149 cur = g_slist_next(cur); |
| |
150 } |
| |
151 |
| |
152 g_string_append_printf(outstr, "\r\n%s", msg->bodylen ? msg->body : ""); |
| |
153 |
| |
154 return g_string_free(outstr, FALSE); |
| |
155 } |
| |
156 void sipmsg_add_header(struct sipmsg *msg, const gchar *name, const gchar *value) { |
| |
157 struct siphdrelement *element = g_new0(struct siphdrelement,1); |
| |
158 element->name = g_strdup(name); |
| |
159 element->value = g_strdup(value); |
| |
160 msg->headers = g_slist_append(msg->headers, element); |
| |
161 } |
| |
162 |
| |
163 void sipmsg_free(struct sipmsg *msg) { |
| |
164 struct siphdrelement *elem; |
| |
165 while(msg->headers) { |
| |
166 elem = msg->headers->data; |
| |
167 msg->headers = g_slist_remove(msg->headers,elem); |
| |
168 g_free(elem->name); |
| |
169 g_free(elem->value); |
| |
170 g_free(elem); |
| |
171 } |
| |
172 g_free(msg->method); |
| |
173 g_free(msg->target); |
| |
174 g_free(msg->body); |
| |
175 g_free(msg); |
| |
176 } |
| |
177 |
| |
178 void sipmsg_remove_header(struct sipmsg *msg, const gchar *name) { |
| |
179 struct siphdrelement *elem; |
| |
180 GSList *tmp = msg->headers; |
| |
181 while(tmp) { |
| |
182 elem = tmp->data; |
| |
183 if(strcmp(elem->name, name)==0) { |
| |
184 msg->headers = g_slist_remove(msg->headers, elem); |
| |
185 g_free(elem->name); |
| |
186 g_free(elem->value); |
| |
187 g_free(elem); |
| |
188 return; |
| |
189 } |
| |
190 tmp = g_slist_next(tmp); |
| |
191 } |
| |
192 return; |
| |
193 } |
| |
194 |
| |
195 gchar *sipmsg_find_header(struct sipmsg *msg, const gchar *name) { |
| |
196 GSList *tmp; |
| |
197 struct siphdrelement *elem; |
| |
198 tmp = msg->headers; |
| |
199 while(tmp) { |
| |
200 elem = tmp->data; |
| |
201 if(strcmp(elem->name,name)==0) { |
| |
202 return elem->value; |
| |
203 } |
| |
204 tmp = g_slist_next(tmp); |
| |
205 } |
| |
206 return NULL; |
| |
207 } |
| |
208 |