comparison src/protocols/simple/sipmsg.c @ 11181:e5bbe5070e04

[gaim-migrate @ 13292] first import sip/simple prpl files. still buggy and deactivated by default. committer: Tailor Script <tailor@pidgin.im>
author Thomas Butter <tbutter>
date Tue, 02 Aug 2005 20:24:51 +0000
parents
children 90290f579926
comparison
equal deleted inserted replaced
11180:5d103f550f6a 11181:e5bbe5070e04
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(gchar *msg) {
39 char *tmp = strstr(msg, "\r\n\r\n");
40 struct sipmsg *smsg;
41 tmp[0]=0;
42 smsg = sipmsg_parse_header(msg);
43 tmp[0]='\r';
44 smsg->body = g_strdup(tmp+4);
45 return smsg;
46 }
47
48 struct sipmsg *sipmsg_parse_header(gchar *header) {
49 struct sipmsg *msg = g_new0(struct sipmsg,1);
50 gchar **lines = g_strsplit(header,"\r\n",0);
51 gchar **parts;
52 gchar *dummy;
53 gchar *dummy2;
54 gchar *tmp;
55 int i=1;
56 parts = g_strsplit(lines[0], " ", 3);
57 if(!parts[0] || !parts[1] || !parts[2]) {
58 g_free(msg);
59 return NULL;
60 }
61 if(strstr(parts[0],"SIP")) { // numeric response
62 msg->method = g_strdup(parts[2]);
63 msg->response = strtol(parts[1],NULL,10);
64 } else { // request
65 msg->method = g_strdup(parts[0]);
66 msg->target = g_strdup(parts[1]);
67 msg->response = 0;
68 }
69 g_strfreev(parts);
70 for(i=1; lines[i] && strlen(lines[i])>2; i++) {
71 parts = g_strsplit(lines[i], ":", 2);
72 if(!parts[0] || !parts[1]) {
73 g_free(msg);
74 return NULL;
75 }
76 dummy = parts[1];
77 dummy2 = 0;
78 while(*dummy==' ' || *dummy=='\t') dummy++;
79 dummy2 = g_strdup(dummy);
80 while(lines[i+1] && (lines[i+1][0]==' ' || lines[i+1][0]=='\t')) {
81 i++;
82 dummy = lines[i];
83 while(*dummy==' ' || *dummy=='\t') dummy++;
84 tmp = g_strdup_printf("%s %s",dummy2, dummy);
85 g_free(dummy2);
86 dummy2 = tmp;
87 }
88 sipmsg_add_header(msg, parts[0], dummy2);
89 g_strfreev(parts);
90 }
91 msg->bodylen = strtol(sipmsg_find_header(msg, "Content-Length"),NULL,10);
92 if(msg->response) {
93 tmp = sipmsg_find_header(msg, "CSeq");
94 if(!tmp) {
95 // SHOULD NOT HAPPEN
96 msg->method = 0;
97 } else {
98 parts = g_strsplit(tmp, " ", 2);
99 msg->method = g_strdup(parts[1]);
100 g_strfreev(parts);
101 }
102 }
103 return msg;
104 }
105
106 void sipmsg_print(struct sipmsg *msg) {
107 GSList *cur;
108 struct siphdrelement *elem;
109 gaim_debug(GAIM_DEBUG_MISC, "simple", "SIP MSG\n");
110 gaim_debug(GAIM_DEBUG_MISC, "simple", "response: %d\nmethod: %s\nbodylen: %d\n",msg->response,msg->method,msg->bodylen);
111 if(msg->target) gaim_debug(GAIM_DEBUG_MISC, "simple", "target: %s\n",msg->target);
112 cur = msg->headers;
113 while(cur) {
114 elem = cur->data;
115 gaim_debug(GAIM_DEBUG_MISC, "simple", "name: %s value: %s\n",elem->name, elem->value);
116 cur = g_slist_next(cur);
117 }
118 }
119
120 char *sipmsg_to_string(struct sipmsg *msg) {
121 gchar *out;
122 gchar *old;
123 GSList *cur;
124 struct siphdrelement *elem;
125 if(msg->response) out = g_strdup_printf("SIP/2.0 %d AAA\r\n", msg->response);
126 else out = g_strdup_printf("%s %s SIP/2.0\r\n",msg->method, msg->target);
127 cur = msg->headers;
128 while(cur) {
129 elem = cur->data;
130 old = out;
131 out = g_strdup_printf("%s%s: %s\r\n", out, elem->name, elem->value);
132 g_free(old);
133 cur = g_slist_next(cur);
134 }
135 old = out;
136 out = g_strdup_printf("%s\r\n",out);
137 g_free(old);
138
139 if(msg->bodylen) {
140 old = out;
141 out = g_strdup_printf("%s%s",out, msg->body);
142 g_free(old);
143 }
144 return out;
145 }
146 void sipmsg_add_header(struct sipmsg *msg, gchar *name, gchar *value) {
147 struct siphdrelement *element = g_new0(struct siphdrelement,1);
148 element->name = g_strdup(name);
149 element->value = g_strdup(value);
150 msg->headers = g_slist_append(msg->headers, element);
151 }
152
153 void sipmsg_free(struct sipmsg *msg) {
154 struct siphdrelement *elem;
155 while(msg->headers) {
156 elem = msg->headers->data;
157 msg->headers = g_slist_remove(msg->headers,elem);
158 g_free(elem->name);
159 g_free(elem->value);
160 g_free(elem);
161 }
162 if(msg->method) g_free(msg->method);
163 if(msg->target) g_free(msg->target);
164 if(msg->body) g_free(msg->body);
165 g_free(msg);
166 }
167
168 void sipmsg_remove_header(struct sipmsg *msg, gchar *name) {
169 struct siphdrelement *elem;
170 GSList *tmp = msg->headers;
171 while(tmp) {
172 elem = tmp->data;
173 if(strcmp(elem->name, name)==0) {
174 msg->headers = g_slist_remove(msg->headers, elem);
175 return;
176 }
177 tmp = g_slist_next(tmp);
178 }
179 return;
180 }
181
182 gchar *sipmsg_find_header(struct sipmsg *msg, gchar *name) {
183 GSList *tmp;
184 struct siphdrelement *elem;
185 tmp = msg->headers;
186 while(tmp) {
187 elem = tmp->data;
188 if(strcmp(elem->name,name)==0) {
189 return elem->value;
190 }
191 tmp = g_slist_next(tmp);
192 }
193 return NULL;
194 }
195