This patch allows for a username to have a different suffix from the
[siplcs.git] / src / sipmsg.c
blobfb449a834678171e90924818505674934b8bb3c9
1 /**
2 * @file sipmsg.c
4 * gaim
6 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
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.
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.
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
23 #include "sip-internal.h"
25 #include "gaim-compat.h"
26 #include "accountopt.h"
27 #include "blist.h"
28 #include "conversation.h"
29 #include "debug.h"
30 #include "notify.h"
31 #include "prpl.h"
32 #include "plugin.h"
33 #include "util.h"
34 #include "version.h"
36 #include "sipe.h"
37 #include "sipmsg.h"
39 struct sipmsg *sipmsg_parse_msg(const gchar *msg) {
40 const char *tmp = strstr(msg, "\r\n\r\n");
41 char *line;
42 struct sipmsg *smsg;
44 if(!tmp) return NULL;
46 line = g_strndup(msg, tmp - msg);
48 smsg = sipmsg_parse_header(line);
49 smsg->body = g_strdup(tmp + 4);
51 g_free(line);
52 return smsg;
55 struct sipmsg *sipmsg_parse_header(const gchar *header) {
56 struct sipmsg *msg = g_new0(struct sipmsg,1);
57 gchar **lines = g_strsplit(header,"\r\n",0);
58 gchar **parts;
59 gchar *dummy;
60 gchar *dummy2;
61 gchar *tmp;
62 int i=1;
63 if(!lines[0]) return NULL;
64 parts = g_strsplit(lines[0], " ", 3);
65 if(!parts[0] || !parts[1] || !parts[2]) {
66 g_strfreev(parts);
67 g_strfreev(lines);
68 g_free(msg);
69 return NULL;
71 if(strstr(parts[0],"SIP")) { /* numeric response */
72 msg->method = g_strdup(parts[2]);
73 msg->response = strtol(parts[1],NULL,10);
74 } else { /* request */
75 msg->method = g_strdup(parts[0]);
76 msg->target = g_strdup(parts[1]);
77 msg->response = 0;
79 g_strfreev(parts);
80 for(i=1; lines[i] && strlen(lines[i])>2; i++) {
81 parts = g_strsplit(lines[i], ":", 2);
82 if(!parts[0] || !parts[1]) {
83 g_strfreev(parts);
84 g_strfreev(lines);
85 g_free(msg);
86 return NULL;
88 dummy = parts[1];
89 dummy2 = 0;
90 while(*dummy==' ' || *dummy=='\t') dummy++;
91 dummy2 = g_strdup(dummy);
92 while(lines[i+1] && (lines[i+1][0]==' ' || lines[i+1][0]=='\t')) {
93 i++;
94 dummy = lines[i];
95 while(*dummy==' ' || *dummy=='\t') dummy++;
96 tmp = g_strdup_printf("%s %s",dummy2, dummy);
97 g_free(dummy2);
98 dummy2 = tmp;
100 sipmsg_add_header(msg, parts[0], dummy2);
101 g_strfreev(parts);
103 g_strfreev(lines);
104 msg->bodylen = strtol(sipmsg_find_header(msg, "Content-Length"),NULL,10);
105 if(msg->response) {
106 tmp = sipmsg_find_header(msg, "CSeq");
107 if(!tmp) {
108 /* SHOULD NOT HAPPEN */
109 msg->method = 0;
110 } else {
111 parts = g_strsplit(tmp, " ", 2);
112 msg->method = g_strdup(parts[1]);
113 g_strfreev(parts);
116 return msg;
119 void sipmsg_print(const struct sipmsg *msg) {
120 GSList *cur;
121 struct siphdrelement *elem;
122 gaim_debug(GAIM_DEBUG_MISC, "sipe", "SIP MSG\n");
123 gaim_debug(GAIM_DEBUG_MISC, "sipe", "response: %d\nmethod: %s\nbodylen: %d\n",msg->response,msg->method,msg->bodylen);
124 if(msg->target) gaim_debug(GAIM_DEBUG_MISC, "sipe", "target: %s\n",msg->target);
125 cur = msg->headers;
126 while(cur) {
127 elem = cur->data;
128 gaim_debug(GAIM_DEBUG_MISC, "sipe", "name: %s value: %s\n",elem->name, elem->value);
129 cur = g_slist_next(cur);
133 char *sipmsg_to_string(const struct sipmsg *msg) {
134 GSList *cur;
135 GString *outstr = g_string_new("");
136 struct siphdrelement *elem;
138 if(msg->response)
139 g_string_append_printf(outstr, "SIP/2.0 %d Unknown\r\n",
140 msg->response);
141 else
142 g_string_append_printf(outstr, "%s %s SIP/2.0\r\n",
143 msg->method, msg->target);
145 cur = msg->headers;
146 while(cur) {
147 elem = cur->data;
148 g_string_append_printf(outstr, "%s: %s\r\n", elem->name,
149 elem->value);
150 cur = g_slist_next(cur);
153 g_string_append_printf(outstr, "\r\n%s", msg->bodylen ? msg->body : "");
155 return g_string_free(outstr, FALSE);
157 void sipmsg_add_header(struct sipmsg *msg, const gchar *name, const gchar *value) {
158 struct siphdrelement *element = g_new0(struct siphdrelement,1);
159 element->name = g_strdup(name);
160 element->value = g_strdup(value);
161 msg->headers = g_slist_append(msg->headers, element);
164 void sipmsg_add_header_pos(struct sipmsg *msg, const gchar *name, const gchar *value, int pos) {
165 struct siphdrelement *element = g_new0(struct siphdrelement,1);
166 element->name = g_strdup(name);
167 element->value = g_strdup(value);
168 msg->headers = g_slist_insert(msg->headers, element,pos);
171 void sipmsg_free(struct sipmsg *msg) {
172 struct siphdrelement *elem;
173 while(msg->headers) {
174 elem = msg->headers->data;
175 msg->headers = g_slist_remove(msg->headers,elem);
176 g_free(elem->name);
177 g_free(elem->value);
178 g_free(elem);
180 g_free(msg->method);
181 g_free(msg->target);
182 g_free(msg->body);
183 g_free(msg);
186 void sipmsg_remove_header(struct sipmsg *msg, const gchar *name) {
187 struct siphdrelement *elem;
188 GSList *tmp = msg->headers;
189 while(tmp) {
190 elem = tmp->data;
191 if(strcmp(elem->name, name)==0) {
192 msg->headers = g_slist_remove(msg->headers, elem);
193 g_free(elem->name);
194 g_free(elem->value);
195 g_free(elem);
196 return;
198 tmp = g_slist_next(tmp);
200 return;
203 gchar *sipmsg_find_header(struct sipmsg *msg, const gchar *name) {
204 GSList *tmp;
205 struct siphdrelement *elem;
206 tmp = msg->headers;
207 while(tmp) {
208 elem = tmp->data;
209 if(strcmp(elem->name,name)==0) {
210 return elem->value;
212 tmp = g_slist_next(tmp);
214 return NULL;