comparison libpurple/protocols/msn/msg.c @ 19843:60bc06498746

Committing khc's msnp14 changes from Trac Ticket #148. --rlaager committer: Richard Laager <rlaager@wiktel.com>
author Ka-Hing Cheung <khc@hxbc.us>
date Sun, 15 Apr 2007 04:12:27 +0000
parents 32c366eeeb99
children 6f986caeab59
comparison
equal deleted inserted replaced
16121:6167ee79ecd0 19843:60bc06498746
21 * along with this program; if not, write to the Free Software 21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */ 23 */
24 #include "msn.h" 24 #include "msn.h"
25 #include "msg.h" 25 #include "msg.h"
26 #define MSN_DEBUG_MSG
26 27
27 MsnMessage * 28 MsnMessage *
28 msn_message_new(MsnMsgType type) 29 msn_message_new(MsnMsgType type)
29 { 30 {
30 MsnMessage *msg; 31 MsnMessage *msg;
102 103
103 #ifdef MSN_DEBUG_MSG 104 #ifdef MSN_DEBUG_MSG
104 purple_debug_info("msn", "message unref (%p)[%d]\n", msg, msg->ref_count); 105 purple_debug_info("msn", "message unref (%p)[%d]\n", msg, msg->ref_count);
105 #endif 106 #endif
106 107
107 if (msg->ref_count == 0) 108 if (msg->ref_count == 0){
108 {
109 msn_message_destroy(msg); 109 msn_message_destroy(msg);
110 110
111 return NULL; 111 return NULL;
112 } 112 }
113 113
124 msn_message_set_attr(msg, "User-Agent", PACKAGE_NAME "/" VERSION); 124 msn_message_set_attr(msg, "User-Agent", PACKAGE_NAME "/" VERSION);
125 msn_message_set_content_type(msg, "text/plain"); 125 msn_message_set_content_type(msg, "text/plain");
126 msn_message_set_charset(msg, "UTF-8"); 126 msn_message_set_charset(msg, "UTF-8");
127 msn_message_set_flag(msg, 'A'); 127 msn_message_set_flag(msg, 'A');
128 msn_message_set_attr(msg, "X-MMS-IM-Format", 128 msn_message_set_attr(msg, "X-MMS-IM-Format",
129 "FN=MS%20Sans%20Serif; EF=; CO=0; PF=0"); 129 "FN=MS%20Sans%20Serif; EF=; CO=0; CS=86;PF=0");
130 130
131 message_cr = purple_str_add_cr(message); 131 message_cr = purple_str_add_cr(message);
132 msn_message_set_bin_data(msg, message_cr, strlen(message_cr)); 132 msn_message_set_bin_data(msg, message_cr, strlen(message_cr));
133 g_free(message_cr); 133 g_free(message_cr);
134 134
204 } 204 }
205 } 205 }
206 206
207 void 207 void
208 msn_message_parse_payload(MsnMessage *msg, 208 msn_message_parse_payload(MsnMessage *msg,
209 const char *payload, size_t payload_len) 209 const char *payload, size_t payload_len,
210 const char *line_dem,const char *body_dem)
210 { 211 {
211 char *tmp_base, *tmp; 212 char *tmp_base, *tmp;
212 const char *content_type; 213 const char *content_type;
213 char *end; 214 char *end;
214 char **elems, **cur, **tokens; 215 char **elems, **cur, **tokens;
215 216
216 g_return_if_fail(payload != NULL); 217 g_return_if_fail(payload != NULL);
217 218 // purple_debug_info("MaYuan","payload:{%s}\n",payload);
218 tmp_base = tmp = g_malloc0(payload_len + 1); 219 tmp_base = tmp = g_malloc0(payload_len + 1);
219 memcpy(tmp_base, payload, payload_len); 220 memcpy(tmp_base, payload, payload_len);
220 221
221 /* Parse the attributes. */ 222 /* Parse the attributes. */
222 end = strstr(tmp, "\r\n\r\n"); 223 end = strstr(tmp, body_dem);
223 /* TODO? some clients use \r delimiters instead of \r\n, the official client 224 /* TODO? some clients use \r delimiters instead of \r\n, the official client
224 * doesn't send such messages, but does handle receiving them. We'll just 225 * doesn't send such messages, but does handle receiving them. We'll just
225 * avoid crashing for now */ 226 * avoid crashing for now */
226 if (end == NULL) { 227 if (end == NULL) {
227 g_free(tmp_base); 228 g_free(tmp_base);
228 g_return_if_reached(); 229 g_return_if_reached();
229 } 230 }
230 *end = '\0'; 231 *end = '\0';
231 232
232 elems = g_strsplit(tmp, "\r\n", 0); 233 elems = g_strsplit(tmp, line_dem, 0);
233 234
234 for (cur = elems; *cur != NULL; cur++) 235 for (cur = elems; *cur != NULL; cur++){
235 {
236 const char *key, *value; 236 const char *key, *value;
237 237
238 tokens = g_strsplit(*cur, ": ", 2); 238 tokens = g_strsplit(*cur, ": ", 2);
239 239
240 key = tokens[0]; 240 key = tokens[0];
241 value = tokens[1]; 241 value = tokens[1];
242 242
243 if (!strcmp(key, "MIME-Version")) 243 /*if not MIME content ,then return*/
244 { 244 if (!strcmp(key, "MIME-Version")){
245 g_strfreev(tokens); 245 g_strfreev(tokens);
246 continue; 246 continue;
247 } 247 }
248 248
249 if (!strcmp(key, "Content-Type")) 249 if (!strcmp(key, "Content-Type")){
250 {
251 char *charset, *c; 250 char *charset, *c;
252 251
253 if ((c = strchr(value, ';')) != NULL) 252 if ((c = strchr(value, ';')) != NULL){
254 { 253 if ((charset = strchr(c, '=')) != NULL) {
255 if ((charset = strchr(c, '=')) != NULL)
256 {
257 charset++; 254 charset++;
258 msn_message_set_charset(msg, charset); 255 msn_message_set_charset(msg, charset);
259 } 256 }
260 257
261 *c = '\0'; 258 *c = '\0';
262 } 259 }
263 260
264 msn_message_set_content_type(msg, value); 261 msn_message_set_content_type(msg, value);
265 } 262 }else{
266 else
267 {
268 msn_message_set_attr(msg, key, value); 263 msn_message_set_attr(msg, key, value);
269 } 264 }
270 265
271 g_strfreev(tokens); 266 g_strfreev(tokens);
272 } 267 }
273 268
274 g_strfreev(elems); 269 g_strfreev(elems);
275 270
276 /* Proceed to the end of the "\r\n\r\n" */ 271 /* Proceed to the end of the "\r\n\r\n" */
277 tmp = end + 4; 272 tmp = end + strlen(body_dem);
278 273
279 /* Now we *should* be at the body. */ 274 /* Now we *should* be at the body. */
280 content_type = msn_message_get_content_type(msg); 275 content_type = msn_message_get_content_type(msg);
281 276
282 if (content_type != NULL && 277 if (content_type != NULL &&
283 !strcmp(content_type, "application/x-msnmsgrp2p")) 278 !strcmp(content_type, "application/x-msnmsgrp2p")){
284 {
285 MsnSlpHeader header; 279 MsnSlpHeader header;
286 MsnSlpFooter footer; 280 MsnSlpFooter footer;
287 int body_len; 281 int body_len;
288 282
289 if (payload_len - (tmp - tmp_base) < sizeof(header)) { 283 if (payload_len - (tmp - tmp_base) < sizeof(header)) {
321 if (body_len >= 0) { 315 if (body_len >= 0) {
322 memcpy(&footer, tmp, sizeof(footer)); 316 memcpy(&footer, tmp, sizeof(footer));
323 tmp += sizeof(footer); 317 tmp += sizeof(footer);
324 msg->msnslp_footer.value = GUINT32_FROM_BE(footer.value); 318 msg->msnslp_footer.value = GUINT32_FROM_BE(footer.value);
325 } 319 }
326 } 320 }else{
327 else
328 {
329 if (payload_len - (tmp - tmp_base) > 0) { 321 if (payload_len - (tmp - tmp_base) > 0) {
330 msg->body_len = payload_len - (tmp - tmp_base); 322 msg->body_len = payload_len - (tmp - tmp_base);
331 msg->body = g_malloc0(msg->body_len + 1); 323 msg->body = g_malloc0(msg->body_len + 1);
332 memcpy(msg->body, tmp, msg->body_len); 324 memcpy(msg->body, tmp, msg->body_len);
333 } 325 }
474 memcpy(n, &footer, 4); 466 memcpy(n, &footer, 4);
475 n += 4; 467 n += 4;
476 } 468 }
477 else 469 else
478 { 470 {
479 if (body != NULL) 471 if (body != NULL){
480 {
481 memcpy(n, body, body_len); 472 memcpy(n, body, body_len);
482 n += body_len; 473 n += body_len;
483 } 474 *n = '\0';
484 } 475 }
485 476 }
486 if (ret_size != NULL) 477
487 { 478 if (ret_size != NULL){
488 *ret_size = n - base; 479 *ret_size = n - base;
489 480
490 if (*ret_size > 1664) 481 if (*ret_size > 1664)
491 *ret_size = 1664; 482 *ret_size = 1664;
492 } 483 }
521 len = 1664; 512 len = 1664;
522 513
523 if (msg->body != NULL) 514 if (msg->body != NULL)
524 g_free(msg->body); 515 g_free(msg->body);
525 516
526 if (data != NULL && len > 0) 517 if (data != NULL && len > 0){
527 {
528 msg->body = g_malloc0(len + 1); 518 msg->body = g_malloc0(len + 1);
529 memcpy(msg->body, data, len); 519 memcpy(msg->body, data, len);
530 msg->body_len = len; 520 msg->body_len = len;
531 } 521 }else{
532 else
533 {
534 msg->body = NULL; 522 msg->body = NULL;
535 msg->body_len = 0; 523 msg->body_len = 0;
536 } 524 }
537 } 525 }
538 526
783 771
784 purple_debug_info("msn", "Message %s:\n{%s}\n", info, str->str); 772 purple_debug_info("msn", "Message %s:\n{%s}\n", info, str->str);
785 773
786 g_string_free(str, TRUE); 774 g_string_free(str, TRUE);
787 } 775 }
776