annotate src/util.c @ 13238:1e855032d7bc

[gaim-migrate @ 15604] Someone in #wingaim said this was necessary committer: Tailor Script <tailor@pidgin.im>
author Stu Tomlinson <stu@nosnilmot.com>
date Sun, 12 Feb 2006 15:27:11 +0000
parents 15b3926e2147
children 2990bc8c89fa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
1 /*
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
2 * @file util.h Utility Functions
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3 * @ingroup core
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
4 *
8046
fa6395637e2c [gaim-migrate @ 8730]
Sean Egan <seanegan@gmail.com>
parents: 8026
diff changeset
5 * Gaim is the legal property of its developers, whose names are too numerous
fa6395637e2c [gaim-migrate @ 8730]
Sean Egan <seanegan@gmail.com>
parents: 8026
diff changeset
6 * to list here. Please refer to the COPYRIGHT file distributed with this
fa6395637e2c [gaim-migrate @ 8730]
Sean Egan <seanegan@gmail.com>
parents: 8026
diff changeset
7 * source distribution.
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
8 *
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
9 * This program is free software; you can redistribute it and/or modify
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
10 * it under the terms of the GNU General Public License as published by
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
11 * the Free Software Foundation; either version 2 of the License, or
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
12 * (at your option) any later version.
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
13 *
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful,
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
17 * GNU General Public License for more details.
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
18 *
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
19 * You should have received a copy of the GNU General Public License
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
20 * along with this program; if not, write to the Free Software
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
22 */
5872
059d95c67cda [gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents: 5826
diff changeset
23 #include "internal.h"
3630
9682c0e022c6 [gaim-migrate @ 3753]
Rob Flynn <gaim@robflynn.com>
parents: 3572
diff changeset
24
5872
059d95c67cda [gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents: 5826
diff changeset
25 #include "conversation.h"
059d95c67cda [gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents: 5826
diff changeset
26 #include "debug.h"
10425
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
27 #include "notify.h"
1575
427e1409917c [gaim-migrate @ 1585]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1560
diff changeset
28 #include "prpl.h"
5545
7a64114641c3 [gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents: 5532
diff changeset
29 #include "prefs.h"
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
30 #include "util.h"
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
31
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
32 typedef struct
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
33 {
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
34 void (*callback)(void *, const char *, size_t);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
35 void *user_data;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
36
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
37 struct
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
38 {
9227
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
39 char *user;
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
40 char *passwd;
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
41 char *address;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
42 int port;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
43 char *page;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
44
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
45 } website;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
46
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
47 char *url;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
48 gboolean full;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
49 char *user_agent;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
50 gboolean http11;
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
51 char *request;
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
52 gsize request_written;
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
53 gboolean include_headers;
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
54
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
55 int inpa;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
56
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
57 gboolean got_headers;
9240
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
58 gboolean has_explicit_data_len;
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
59 char *webdata;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
60 unsigned long len;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
61 unsigned long data_len;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
62
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
63 } GaimFetchUrlData;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
64
8596
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
65 static char custom_home_dir[MAXPATHLEN];
3630
9682c0e022c6 [gaim-migrate @ 3753]
Rob Flynn <gaim@robflynn.com>
parents: 3572
diff changeset
66 static char home_dir[MAXPATHLEN];
9682c0e022c6 [gaim-migrate @ 3753]
Rob Flynn <gaim@robflynn.com>
parents: 3572
diff changeset
67
12919
248b8b39c671 [gaim-migrate @ 15272]
Etan Reisner <pidgin@unreliablesource.net>
parents: 12907
diff changeset
68 GaimMenuAction *
13102
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
69 gaim_menu_action_new(const char *label, GaimCallback callback, gpointer data,
12919
248b8b39c671 [gaim-migrate @ 15272]
Etan Reisner <pidgin@unreliablesource.net>
parents: 12907
diff changeset
70 GList *children)
248b8b39c671 [gaim-migrate @ 15272]
Etan Reisner <pidgin@unreliablesource.net>
parents: 12907
diff changeset
71 {
248b8b39c671 [gaim-migrate @ 15272]
Etan Reisner <pidgin@unreliablesource.net>
parents: 12907
diff changeset
72 GaimMenuAction *act = g_new0(GaimMenuAction, 1);
13102
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
73 act->label = g_strdup(label);
12919
248b8b39c671 [gaim-migrate @ 15272]
Etan Reisner <pidgin@unreliablesource.net>
parents: 12907
diff changeset
74 act->callback = callback;
248b8b39c671 [gaim-migrate @ 15272]
Etan Reisner <pidgin@unreliablesource.net>
parents: 12907
diff changeset
75 act->data = data;
248b8b39c671 [gaim-migrate @ 15272]
Etan Reisner <pidgin@unreliablesource.net>
parents: 12907
diff changeset
76 act->children = children;
248b8b39c671 [gaim-migrate @ 15272]
Etan Reisner <pidgin@unreliablesource.net>
parents: 12907
diff changeset
77 return act;
248b8b39c671 [gaim-migrate @ 15272]
Etan Reisner <pidgin@unreliablesource.net>
parents: 12907
diff changeset
78 }
248b8b39c671 [gaim-migrate @ 15272]
Etan Reisner <pidgin@unreliablesource.net>
parents: 12907
diff changeset
79
13102
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
80 void
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
81 gaim_menu_action_free(GaimMenuAction *act)
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
82 {
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
83 g_return_if_fail(act != NULL);
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
84
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
85 g_free(act->label);
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
86 g_free(act);
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
87 }
5828d42e8684 [gaim-migrate @ 15464]
Richard Laager <rlaager@wiktel.com>
parents: 13097
diff changeset
88
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
89 /**************************************************************************
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
90 * Base16 Functions
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
91 **************************************************************************/
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
92 gchar *
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
93 gaim_base16_encode(const guchar *data, gsize len)
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
94 {
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
95 int i;
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
96 gchar *ascii = NULL;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
97
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
98 g_return_val_if_fail(data != NULL, NULL);
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
99 g_return_val_if_fail(len > 0, NULL);
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
100
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
101 ascii = g_malloc(len * 2 + 1);
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
102
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
103 for (i = 0; i < len; i++)
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
104 snprintf(&ascii[i * 2], 3, "%02hhx", data[i]);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
105
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
106 return ascii;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
107 }
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
108
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
109 guchar *
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
110 gaim_base16_decode(const char *str, gsize *ret_len)
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
111 {
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
112 int len, i, accumulator = 0;
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
113 guchar *data;
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
114
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
115 g_return_val_if_fail(str != NULL, NULL);
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
116
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
117 len = strlen(str);
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
118
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
119 g_return_val_if_fail(strlen(str) > 0, 0);
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
120 g_return_val_if_fail(len % 2 > 0, 0);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
121
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
122 data = g_malloc(len / 2);
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
123
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
124 for (i = 0; i < len; i++)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
125 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
126 if ((i % 2) == 0)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
127 accumulator = 0;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
128 else
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
129 accumulator <<= 4;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
130
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
131 if (isdigit(str[i]))
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
132 accumulator |= str[i] - 48;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
133 else
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
134 {
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
135 switch(str[i])
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
136 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
137 case 'a': case 'A': accumulator |= 10; break;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
138 case 'b': case 'B': accumulator |= 11; break;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
139 case 'c': case 'C': accumulator |= 12; break;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
140 case 'd': case 'D': accumulator |= 13; break;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
141 case 'e': case 'E': accumulator |= 14; break;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
142 case 'f': case 'F': accumulator |= 15; break;
1826
7f889cdfa03e [gaim-migrate @ 1836]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1815
diff changeset
143 }
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
144 }
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
145
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
146 if (i % 2)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
147 data[(i - 1) / 2] = accumulator;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
148 }
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
149
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
150 if (ret_len != NULL)
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
151 *ret_len = len / 2;
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
152
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
153 return data;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
154 }
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
155
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
156 /**************************************************************************
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
157 * Base64 Functions
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
158 **************************************************************************/
4888
912294585edf [gaim-migrate @ 5218]
Christian Hammond <chipx86@chipx86.com>
parents: 4853
diff changeset
159 static const char alphabet[] =
912294585edf [gaim-migrate @ 5218]
Christian Hammond <chipx86@chipx86.com>
parents: 4853
diff changeset
160 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
912294585edf [gaim-migrate @ 5218]
Christian Hammond <chipx86@chipx86.com>
parents: 4853
diff changeset
161 "0123456789+/";
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
162
8929
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
163 static const char xdigits[] =
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
164 "0123456789abcdef";
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
165
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
166 gchar *
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
167 gaim_base64_encode(const guchar *data, gsize len)
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
168 {
6872
dd0eecfbe413 [gaim-migrate @ 7418]
Nathan Walp <nwalp@pidgin.im>
parents: 6861
diff changeset
169 char *out, *rv;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
170
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
171 g_return_val_if_fail(data != NULL, NULL);
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
172 g_return_val_if_fail(len > 0, NULL);
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
173
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
174 rv = out = g_malloc(((len/3)+1)*4 + 1);
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
175
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
176 for (; len >= 3; len -= 3)
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
177 {
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
178 *out++ = alphabet[data[0] >> 2];
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
179 *out++ = alphabet[((data[0] << 4) & 0x30) | (data[1] >> 4)];
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
180 *out++ = alphabet[((data[1] << 2) & 0x3c) | (data[2] >> 6)];
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
181 *out++ = alphabet[data[2] & 0x3f];
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
182 data += 3;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
183 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
184
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
185 if (len > 0)
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
186 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
187 unsigned char fragment;
6872
dd0eecfbe413 [gaim-migrate @ 7418]
Nathan Walp <nwalp@pidgin.im>
parents: 6861
diff changeset
188
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
189 *out++ = alphabet[data[0] >> 2];
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
190 fragment = (data[0] << 4) & 0x30;
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
191
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
192 if (len > 1)
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
193 fragment |= data[1] >> 4;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
194
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
195 *out++ = alphabet[fragment];
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
196 *out++ = (len < 2) ? '=' : alphabet[(data[1] << 2) & 0x3c];
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
197 *out++ = '=';
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
198 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
199
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
200 *out = '\0';
5426
8d8bf0d31a23 [gaim-migrate @ 5805]
Nathan Walp <nwalp@pidgin.im>
parents: 5341
diff changeset
201
8d8bf0d31a23 [gaim-migrate @ 5805]
Nathan Walp <nwalp@pidgin.im>
parents: 5341
diff changeset
202 return rv;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
203 }
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
204
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
205 guchar *
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
206 gaim_base64_decode(const char *str, gsize *ret_len)
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
207 {
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
208 guchar *out = NULL;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
209 char tmp = 0;
1920
5bed3bc833b5 [gaim-migrate @ 1930]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1878
diff changeset
210 const char *c;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
211 gint32 tmp2 = 0;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
212 int len = 0, n = 0;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
213
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
214 g_return_val_if_fail(str != NULL, NULL);
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
215
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
216 c = str;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
217
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
218 while (*c) {
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
219 if (*c >= 'A' && *c <= 'Z') {
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
220 tmp = *c - 'A';
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
221 } else if (*c >= 'a' && *c <= 'z') {
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
222 tmp = 26 + (*c - 'a');
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
223 } else if (*c >= '0' && *c <= 57) {
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
224 tmp = 52 + (*c - '0');
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
225 } else if (*c == '+') {
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
226 tmp = 62;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
227 } else if (*c == '/') {
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
228 tmp = 63;
5289
cfbd0cb3f18b [gaim-migrate @ 5661]
Nathan Walp <nwalp@pidgin.im>
parents: 5247
diff changeset
229 } else if (*c == '\r' || *c == '\n') {
cfbd0cb3f18b [gaim-migrate @ 5661]
Nathan Walp <nwalp@pidgin.im>
parents: 5247
diff changeset
230 c++;
cfbd0cb3f18b [gaim-migrate @ 5661]
Nathan Walp <nwalp@pidgin.im>
parents: 5247
diff changeset
231 continue;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
232 } else if (*c == '=') {
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
233 if (n == 3) {
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
234 out = g_realloc(out, len + 2);
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
235 out[len] = (guchar)(tmp2 >> 10) & 0xff;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
236 len++;
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
237 out[len] = (guchar)(tmp2 >> 2) & 0xff;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
238 len++;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
239 } else if (n == 2) {
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
240 out = g_realloc(out, len + 1);
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
241 out[len] = (guchar)(tmp2 >> 4) & 0xff;
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
242 len++;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
243 }
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
244 break;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
245 }
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
246 tmp2 = ((tmp2 << 6) | (tmp & 0xff));
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
247 n++;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
248 if (n == 4) {
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
249 out = g_realloc(out, len + 3);
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
250 out[len] = (guchar)((tmp2 >> 16) & 0xff);
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
251 len++;
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
252 out[len] = (guchar)((tmp2 >> 8) & 0xff);
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
253 len++;
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
254 out[len] = (guchar)(tmp2 & 0xff);
1252
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
255 len++;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
256 tmp2 = 0;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
257 n = 0;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
258 }
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
259 c++;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
260 }
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
261
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
262 out = g_realloc(out, len + 1);
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
263 out[len] = 0;
46c09828e929 [gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1250
diff changeset
264
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
265 if (ret_len != NULL)
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
266 *ret_len = len;
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
267
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
268 return out;
1
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
269 }
2846a03bda67 [gaim-migrate @ 10]
Rob Flynn <gaim@robflynn.com>
parents:
diff changeset
270
7679
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
271 /**************************************************************************
11132
a4611130e3eb [gaim-migrate @ 13189]
Mark Doliner <mark@kingant.net>
parents: 11127
diff changeset
272 * Quoted Printable Functions (see RFC 2045).
7679
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
273 **************************************************************************/
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
274 guchar *
11132
a4611130e3eb [gaim-migrate @ 13189]
Mark Doliner <mark@kingant.net>
parents: 11127
diff changeset
275 gaim_quotedp_decode(const char *str, gsize *ret_len)
7679
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
276 {
8125
abbfed0b0050 [gaim-migrate @ 8829]
Nathan Walp <nwalp@pidgin.im>
parents: 8123
diff changeset
277 char *n, *new;
abbfed0b0050 [gaim-migrate @ 8829]
Nathan Walp <nwalp@pidgin.im>
parents: 8123
diff changeset
278 const char *end, *p;
7722
db1542e84ee6 [gaim-migrate @ 8367]
Nathan Walp <nwalp@pidgin.im>
parents: 7679
diff changeset
279
8005
3bdfb4308d10 [gaim-migrate @ 8682]
Christian Hammond <chipx86@chipx86.com>
parents: 7956
diff changeset
280 n = new = g_malloc(strlen (str) + 1);
8125
abbfed0b0050 [gaim-migrate @ 8829]
Nathan Walp <nwalp@pidgin.im>
parents: 8123
diff changeset
281 end = str + strlen(str);
7679
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
282
8125
abbfed0b0050 [gaim-migrate @ 8829]
Nathan Walp <nwalp@pidgin.im>
parents: 8123
diff changeset
283 for (p = str; p < end; p++, n++) {
7679
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
284 if (*p == '=') {
8929
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
285 if (p[1] == '\r' && p[2] == '\n') { /* 5.1 #5 */
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
286 n -= 1;
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
287 p += 2;
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
288 } else if (p[1] == '\n') { /* fuzzy case for 5.1 #5 */
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
289 n -= 1;
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
290 p += 1;
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
291 } else if (p[1] && p[2]) {
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
292 char *nibble1 = strchr(xdigits, tolower(p[1]));
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
293 char *nibble2 = strchr(xdigits, tolower(p[2]));
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
294 if (nibble1 && nibble2) { /* 5.1 #1 */
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
295 *n = ((nibble1 - xdigits) << 4) | (nibble2 - xdigits);
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
296 p += 2;
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
297 } else { /* This should never happen */
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
298 *n = *p;
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
299 }
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
300 } else { /* This should never happen */
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
301 *n = *p;
54eba3833e34 [gaim-migrate @ 9699]
Ethan Blanton <elb@pidgin.im>
parents: 8918
diff changeset
302 }
7679
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
303 }
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
304 else if (*p == '_')
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
305 *n = ' ';
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
306 else
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
307 *n = *p;
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
308 }
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
309
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
310 *n = '\0';
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
311
11132
a4611130e3eb [gaim-migrate @ 13189]
Mark Doliner <mark@kingant.net>
parents: 11127
diff changeset
312 if (ret_len != NULL)
7679
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
313 *ret_len = n - new;
7722
db1542e84ee6 [gaim-migrate @ 8367]
Nathan Walp <nwalp@pidgin.im>
parents: 7679
diff changeset
314
7679
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
315 /* Resize to take less space */
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
316 /* new = realloc(new, n - new); */
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
317
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
318 return (guchar *)new;
7679
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
319 }
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
320
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
321 /**************************************************************************
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
322 * MIME Functions
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
323 **************************************************************************/
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
324 char *
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
325 gaim_mime_decode_field(const char *str)
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
326 {
7858
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
327 /*
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
328 * This is wing's version, partially based on revo/shx's version
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
329 * See RFC2047 [which apparently obsoletes RFC1342]
7858
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
330 */
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
331 typedef enum {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
332 state_start, state_equal1, state_question1,
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
333 state_charset, state_question2,
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
334 state_encoding, state_question3,
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
335 state_encoded_text, state_question4, state_equal2 = state_start
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
336 } encoded_word_state_t;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
337 encoded_word_state_t state = state_start;
7858
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
338 const char *cur, *mark;
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
339 const char *charset0 = NULL, *encoding0 = NULL, *encoded_text0 = NULL;
7858
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
340 char *n, *new;
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
341
8976
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
342 /* token can be any CHAR (supposedly ISO8859-1/ISO2022), not just ASCII */
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
343 #define token_char_p(c) \
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
344 (c != ' ' && !iscntrl(c) && !strchr("()<>@,;:\"/[]?.=", c))
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
345
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
346 /* But encoded-text must be ASCII; alas, isascii() may not exist */
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
347 #define encoded_text_char_p(c) \
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
348 ((c & 0x80) == 0 && c != '?' && c != ' ' && isgraph(c))
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
349
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
350 #define RECOVER_MARKED_TEXT strncpy(n, mark, cur - mark + 1); \
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
351 n += cur - mark + 1
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
352
8976
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
353 g_return_val_if_fail(str != NULL, NULL);
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
354
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
355 /* NOTE: Assuming that we need just strlen(str)+1 *may* be wrong.
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
356 * It would be wrong if one byte (in some unknown encoding) could
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
357 * expand to >=4 bytes of UTF-8; I don't know if there are such things.
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
358 */
7858
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
359 n = new = g_malloc(strlen(str) + 1);
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
360
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
361 /* Here we will be looking for encoded words and if they seem to be
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
362 * valid then decode them.
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
363 * They are of this form: =?charset?encoding?text?=
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
364 */
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
365
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
366 for (cur = str, mark = NULL; *cur; cur += 1) {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
367 switch (state) {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
368 case state_equal1:
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
369 if (*cur == '?') {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
370 state = state_question1;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
371 } else {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
372 RECOVER_MARKED_TEXT;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
373 state = state_start;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
374 }
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
375 break;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
376 case state_question1:
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
377 if (token_char_p(*cur)) {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
378 charset0 = cur;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
379 state = state_charset;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
380 } else { /* This should never happen */
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
381 RECOVER_MARKED_TEXT;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
382 state = state_start;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
383 }
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
384 break;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
385 case state_charset:
7858
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
386 if (*cur == '?') {
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
387 state = state_question2;
8976
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
388 } else if (!token_char_p(*cur)) { /* This should never happen */
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
389 RECOVER_MARKED_TEXT;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
390 state = state_start;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
391 }
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
392 break;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
393 case state_question2:
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
394 if (token_char_p(*cur)) {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
395 encoding0 = cur;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
396 state = state_encoding;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
397 } else { /* This should never happen */
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
398 RECOVER_MARKED_TEXT;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
399 state = state_start;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
400 }
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
401 break;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
402 case state_encoding:
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
403 if (*cur == '?') {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
404 state = state_question3;
8976
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
405 } else if (!token_char_p(*cur)) { /* This should never happen */
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
406 RECOVER_MARKED_TEXT;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
407 state = state_start;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
408 }
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
409 break;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
410 case state_question3:
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
411 if (encoded_text_char_p(*cur)) {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
412 encoded_text0 = cur;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
413 state = state_encoded_text;
8976
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
414 } else if (*cur == '?') { /* empty string */
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
415 encoded_text0 = cur;
6f21aa413b18 [gaim-migrate @ 9750]
Mark Doliner <mark@kingant.net>
parents: 8958
diff changeset
416 state = state_question4;
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
417 } else { /* This should never happen */
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
418 RECOVER_MARKED_TEXT;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
419 state = state_start;
7858
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
420 }
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
421 break;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
422 case state_encoded_text:
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
423 if (*cur == '?') {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
424 state = state_question4;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
425 } else if (!encoded_text_char_p(*cur)) {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
426 RECOVER_MARKED_TEXT;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
427 state = state_start;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
428 }
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
429 break;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
430 case state_question4:
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
431 if (*cur == '=') { /* Got the whole encoded-word */
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
432 char *charset = g_strndup(charset0, encoding0 - charset0 - 1);
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
433 char *encoding = g_strndup(encoding0, encoded_text0 - encoding0 - 1);
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
434 char *encoded_text = g_strndup(encoded_text0, cur - encoded_text0 - 1);
11137
096020ae09a9 [gaim-migrate @ 13201]
Mark Doliner <mark@kingant.net>
parents: 11135
diff changeset
435 guchar *decoded = NULL;
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
436 gsize dec_len;
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
437 if (g_ascii_strcasecmp(encoding, "Q") == 0)
11132
a4611130e3eb [gaim-migrate @ 13189]
Mark Doliner <mark@kingant.net>
parents: 11127
diff changeset
438 decoded = gaim_quotedp_decode(encoded_text, &dec_len);
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
439 else if (g_ascii_strcasecmp(encoding, "B") == 0)
11127
719779387f96 [gaim-migrate @ 13183]
Mark Doliner <mark@kingant.net>
parents: 10987
diff changeset
440 decoded = gaim_base64_decode(encoded_text, &dec_len);
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
441 else
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
442 decoded = NULL;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
443 if (decoded) {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
444 gsize len;
11132
a4611130e3eb [gaim-migrate @ 13189]
Mark Doliner <mark@kingant.net>
parents: 11127
diff changeset
445 char *converted = g_convert((const gchar *)decoded, dec_len, "utf-8", charset, NULL, &len, NULL);
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
446
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
447 if (converted) {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
448 n = strncpy(n, converted, len) + len;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
449 g_free(converted);
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
450 }
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
451 g_free(decoded);
7858
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
452 }
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
453 g_free(charset);
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
454 g_free(encoding);
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
455 g_free(encoded_text);
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
456 state = state_equal2; /* Restart the FSM */
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
457 } else { /* This should never happen */
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
458 RECOVER_MARKED_TEXT;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
459 state = state_start;
7858
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
460 }
8958
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
461 break;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
462 default:
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
463 if (*cur == '=') {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
464 mark = cur;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
465 state = state_equal1;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
466 } else {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
467 /* Some unencoded text. */
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
468 *n = *cur;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
469 n += 1;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
470 }
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
471 break;
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
472 } /* switch */
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
473 } /* for */
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
474
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
475 if (state != state_start) {
60a47725df97 [gaim-migrate @ 9732]
Luke Schierer <lschiere@pidgin.im>
parents: 8929
diff changeset
476 RECOVER_MARKED_TEXT;
7858
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
477 }
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
478 *n = '\0';
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
479
1ce040412bd5 [gaim-migrate @ 8512]
Mark Doliner <mark@kingant.net>
parents: 7840
diff changeset
480 return new;
7840
13334c29799b [gaim-migrate @ 8493]
Mark Doliner <mark@kingant.net>
parents: 7824
diff changeset
481 }
7824
1663c076a744 [gaim-migrate @ 8476]
Mark Doliner <mark@kingant.net>
parents: 7820
diff changeset
482
7679
1363f91da9cc [gaim-migrate @ 8323]
Mark Doliner <mark@kingant.net>
parents: 7675
diff changeset
483
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
484 /**************************************************************************
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
485 * Date/Time Functions
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
486 **************************************************************************/
13152
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
487
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
488 #ifndef HAVE_STRFTIME_Z_FORMAT
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
489 static const char *get_tmoff(const struct tm *tm)
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
490 {
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
491 static char buf[6];
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
492 long off;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
493 gint8 min;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
494 gint8 hrs;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
495 struct tm new_tm = *tm;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
496
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
497 mktime(&new_tm);
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
498
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
499 if (new_tm.tm_isdst < 0)
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
500 g_return_val_if_reached("");
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
501
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
502 #ifdef _WIN32
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
503 TIME_ZONE_INFORMATION tzi;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
504 DWORD ret;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
505 if ((ret = GetTimeZoneInformation(&tzi)) != TIME_ZONE_ID_INVALID)
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
506 {
13163
b230ed49c5d1 [gaim-migrate @ 15526]
Richard Laager <rlaager@wiktel.com>
parents: 13152
diff changeset
507 off = -(tzi.Bias * 60);
13152
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
508 if (ret == TIME_ZONE_ID_DAYLIGHT)
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
509 off -= tzi.DaylightBias * 60;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
510 }
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
511 else
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
512 return "";
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
513 #else
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
514 # ifdef HAVE_TM_GMTOFF
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
515 off = new_tm.tm_gmtoff;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
516 # else
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
517 # ifdef HAVE_TIMEZONE
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
518 tzset();
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
519 off = -timezone;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
520 # endif /* HAVE_TIMEZONE */
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
521 # endif /* !HAVE_TM_GMTOFF */
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
522 #endif /* _WIN32 */
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
523
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
524 min = (off / 60) % 60;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
525 hrs = ((off / 60) - min) / 60;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
526
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
527 if (g_snprintf(buf, sizeof(buf), "%+03d%02d", hrs, ABS(min)) > 5)
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
528 g_return_val_if_reached("");
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
529
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
530 return buf;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
531 }
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
532
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
533 static size_t gaim_internal_strftime(char *s, size_t max, const char *format, const struct tm *tm)
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
534 {
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
535 const char *start;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
536 const char *c;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
537 char *fmt = NULL;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
538
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
539 /* Yes, this is checked in gaim_utf8_strftime(),
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
540 * but better safe than sorry. -- rlaager */
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
541 g_return_val_if_fail(format != NULL, 0);
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
542
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
543 /* This is fairly efficient, and it only gets
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
544 * executed if the underlying system doesn't
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
545 * support the %z format string for strftime(),
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
546 * so I think it's good enough. -- rlaager */
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
547 for (c = start = format; *c ; c++)
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
548 {
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
549 if (*c != '%')
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
550 continue;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
551
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
552 c++;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
553
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
554 if (*c == 'z')
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
555 {
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
556 char *tmp = g_strdup_printf("%s%.*s%s",
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
557 fmt ? fmt : "",
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
558 c - start - 1,
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
559 start,
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
560 get_tmoff(tm));
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
561 g_free(fmt);
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
562 fmt = tmp;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
563 start = c + 1;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
564 }
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
565 }
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
566
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
567 if (fmt != NULL)
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
568 {
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
569 size_t ret;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
570
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
571 if (*start)
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
572 {
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
573 char *tmp = g_strconcat(fmt, start, NULL);
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
574 g_free(fmt);
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
575 fmt = tmp;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
576 }
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
577
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
578 ret = strftime(s, max, fmt, tm);
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
579 g_free(fmt);
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
580
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
581 return ret;
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
582 }
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
583
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
584 return strftime(s, max, format, tm);
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
585 }
13215
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
586 #else /* HAVE_STRFTIME_Z_FORMAT */
13152
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
587 #define gaim_internal_strftime strftime
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
588 #endif
4bb701a8736f [gaim-migrate @ 15515]
Richard Laager <rlaager@wiktel.com>
parents: 13120
diff changeset
589
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
590 const char *
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
591 gaim_utf8_strftime(const char *format, const struct tm *tm)
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
592 {
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
593 static char buf[128];
13215
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
594 char *locale;
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
595 GError *err = NULL;
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
596 int len;
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
597 char *utf8;
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
598
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
599 g_return_val_if_fail(format != NULL, NULL);
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
600
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
601 if (tm == NULL)
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
602 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
603 time_t now = time(NULL);
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
604 tm = localtime(&now);
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
605 }
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
606
13215
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
607 locale = g_locale_from_utf8(format, -1, NULL, NULL, &err);
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
608 if (err != NULL)
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
609 {
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
610 gaim_debug_error("util", "Format conversion failed in gaim_utf8_strftime(): %s", err->message);
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
611 g_error_free(err);
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
612 locale = g_strdup(format);
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
613 }
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
614
13115
e42983a1038c [gaim-migrate @ 15477]
Richard Laager <rlaager@wiktel.com>
parents: 13104
diff changeset
615 /* A return value of 0 is either an error (in
e42983a1038c [gaim-migrate @ 15477]
Richard Laager <rlaager@wiktel.com>
parents: 13104
diff changeset
616 * which case, the contents of the buffer are
e42983a1038c [gaim-migrate @ 15477]
Richard Laager <rlaager@wiktel.com>
parents: 13104
diff changeset
617 * undefined) or the empty string (in which
e42983a1038c [gaim-migrate @ 15477]
Richard Laager <rlaager@wiktel.com>
parents: 13104
diff changeset
618 * case, no harm is done here). */
13215
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
619 if ((len = gaim_internal_strftime(buf, sizeof(buf), locale, tm)) == 0)
13115
e42983a1038c [gaim-migrate @ 15477]
Richard Laager <rlaager@wiktel.com>
parents: 13104
diff changeset
620 {
13215
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
621 g_free(locale);
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
622 return "";
13115
e42983a1038c [gaim-migrate @ 15477]
Richard Laager <rlaager@wiktel.com>
parents: 13104
diff changeset
623 }
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
624
13215
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
625 utf8 = g_locale_to_utf8(buf, len, NULL, NULL, &err);
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
626 if (err != NULL)
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
627 {
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
628 gaim_debug_error("util", "Result conversion failed in gaim_utf8_strftime(): %s", err->message);
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
629 g_error_free(err);
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
630 }
15b3926e2147 [gaim-migrate @ 15579]
Richard Laager <rlaager@wiktel.com>
parents: 13200
diff changeset
631 else
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
632 {
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
633 gaim_strlcpy(buf, utf8);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
634 g_free(utf8);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
635 }
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
636
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
637 return buf;
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
638 }
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
639
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
640 const char *
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
641 gaim_date_format_short(const struct tm *tm)
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
642 {
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
643 return gaim_utf8_strftime("%x", tm);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
644 }
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
645
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
646 const char *
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
647 gaim_date_format_long(const struct tm *tm)
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
648 {
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
649 return gaim_utf8_strftime(_("%x %X"), tm);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
650 }
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
651
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
652 const char *
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
653 gaim_date_format_full(const struct tm *tm)
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
654 {
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
655 return gaim_utf8_strftime("%c", tm);
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
656 }
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
657
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
658 const char *
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
659 gaim_time_format(const struct tm *tm)
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
660 {
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
661 return gaim_utf8_strftime("%X", tm);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
662 }
3630
9682c0e022c6 [gaim-migrate @ 3753]
Rob Flynn <gaim@robflynn.com>
parents: 3572
diff changeset
663
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
664 time_t
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
665 gaim_time_build(int year, int month, int day, int hour, int min, int sec)
1805
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
666 {
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
667 struct tm tm;
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
668
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
669 tm.tm_year = year - 1900;
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
670 tm.tm_mon = month - 1;
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
671 tm.tm_mday = day;
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
672 tm.tm_hour = hour;
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
673 tm.tm_min = min;
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
674 tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
675
1805
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
676 return mktime(&tm);
caa605e70917 [gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 1753
diff changeset
677 }
3230
d05dc05bde8a [gaim-migrate @ 3247]
Jim Seymour <jseymour>
parents: 3153
diff changeset
678
8577
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
679 time_t
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
680 gaim_str_to_time(const char *timestamp, gboolean utc,
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
681 struct tm *tm, long *tz_off, const char **rest)
8577
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
682 {
9722
25866d09063d [gaim-migrate @ 10583]
Mark Doliner <mark@kingant.net>
parents: 9716
diff changeset
683 time_t retval = 0;
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
684 struct tm *t;
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
685 const char *c = timestamp;
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
686 int year = 0;
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
687 long tzoff = GAIM_NO_TZ_OFF;
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
688
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
689 time(&retval);
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
690 t = localtime(&retval);
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
691
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
692 /* 4 digit year */
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
693 if (sscanf(c, "%04d", &year) && year > 1900)
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
694 {
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
695 c += 4;
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
696 if (*c == '-')
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
697 c++;
13117
d9d15ec284ca [gaim-migrate @ 15479]
Richard Laager <rlaager@wiktel.com>
parents: 13116
diff changeset
698 t->tm_year = year - 1900;
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
699 }
8577
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
700
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
701 /* 2 digit month */
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
702 if (!sscanf(c, "%02d", &t->tm_mon))
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
703 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
704 if (rest != NULL && *c != '\0')
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
705 *rest = c;
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
706 return 0;
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
707 }
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
708 c += 2;
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
709 if (*c == '-' || *c == '/')
8577
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
710 c++;
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
711 t->tm_mon -= 1;
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
712
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
713 /* 2 digit day */
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
714 if (!sscanf(c, "%02d", &t->tm_mday))
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
715 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
716 if (rest != NULL && *c != '\0')
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
717 *rest = c;
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
718 return 0;
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
719 }
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
720 c += 2;
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
721 if (*c == '/')
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
722 {
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
723 c++;
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
724
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
725 if (!sscanf(c, "%04d", &t->tm_year))
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
726 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
727 if (rest != NULL && *c != '\0')
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
728 *rest = c;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
729 return 0;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
730 }
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
731 t->tm_year -= 1900;
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
732 }
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
733 else if (*c == 'T' || *c == '.')
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
734 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
735 c++;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
736 /* we have more than a date, keep going */
9725
242a5a97c952 [gaim-migrate @ 10586]
Mark Doliner <mark@kingant.net>
parents: 9722
diff changeset
737
242a5a97c952 [gaim-migrate @ 10586]
Mark Doliner <mark@kingant.net>
parents: 9722
diff changeset
738 /* 2 digit hour */
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
739 if ((sscanf(c, "%02d:%02d:%02d", &t->tm_hour, &t->tm_min, &t->tm_sec) == 3 && (c = c + 8)) ||
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
740 (sscanf(c, "%02d%02d%02d", &t->tm_hour, &t->tm_min, &t->tm_sec) == 3 && (c = c + 6)))
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
741 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
742 gboolean offset_positive = FALSE;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
743 int tzhrs;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
744 int tzmins;
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
745
10876
4fdc8a1102c0 [gaim-migrate @ 12567]
Nathan Walp <nwalp@pidgin.im>
parents: 10871
diff changeset
746 t->tm_isdst = -1;
4fdc8a1102c0 [gaim-migrate @ 12567]
Nathan Walp <nwalp@pidgin.im>
parents: 10871
diff changeset
747
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
748 if (*c == '.' && *(c+1) >= '0' && *(c+1) <= '9') /* dealing with precision we don't care about */
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
749 c += 4;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
750 if (*c == '+')
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
751 offset_positive = TRUE;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
752 if (((*c == '+' || *c == '-') && (c = c + 1)) &&
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
753 ((sscanf(c, "%02d:%02d", &tzhrs, &tzmins) == 2 && (c = c + 5)) ||
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
754 (sscanf(c, "%02d%02d", &tzhrs, &tzmins) == 2 && (c = c + 4))))
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
755 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
756 tzoff = tzhrs*60*60 + tzmins*60;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
757 if (offset_positive)
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
758 tzoff *= -1;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
759 /* We don't want the C library doing DST calculations
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
760 * if we know the UTC offset already. */
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
761 t->tm_isdst = 0;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
762 }
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
763
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
764 if (rest != NULL && *c != '\0')
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
765 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
766 if (*c == ' ')
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
767 c++;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
768 if (*c != '\0')
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
769 *rest = c;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
770 }
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
771
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
772 if (tzoff != GAIM_NO_TZ_OFF || utc)
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
773 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
774 #if defined(_WIN32) || defined(HAVE_TM_GMTOFF) || defined (HAVE_TIMEZONE)
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
775 if (tzoff == GAIM_NO_TZ_OFF)
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
776 tzoff = 0;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
777 #endif
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
778
10987
a8a7730db73c [gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10948
diff changeset
779 #ifdef _WIN32
a8a7730db73c [gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10948
diff changeset
780 TIME_ZONE_INFORMATION tzi;
a8a7730db73c [gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10948
diff changeset
781 DWORD ret;
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
782 if ((ret = GetTimeZoneInformation(&tzi)) != TIME_ZONE_ID_INVALID)
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
783 {
10987
a8a7730db73c [gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10948
diff changeset
784 tzoff -= tzi.Bias * 60;
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
785 if (ret == TIME_ZONE_ID_DAYLIGHT)
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
786 {
10987
a8a7730db73c [gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10948
diff changeset
787 tzoff -= tzi.DaylightBias * 60;
a8a7730db73c [gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10948
diff changeset
788 }
a8a7730db73c [gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10948
diff changeset
789 }
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
790 else
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
791 tzoff = GAIM_NO_TZ_OFF;
10987
a8a7730db73c [gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10948
diff changeset
792 #else
8577
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
793 #ifdef HAVE_TM_GMTOFF
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
794 tzoff += t->tm_gmtoff;
8577
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
795 #else
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
796 # ifdef HAVE_TIMEZONE
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
797 tzset(); /* making sure */
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
798 tzoff -= timezone;
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
799 # endif
8577
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
800 #endif
10987
a8a7730db73c [gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10948
diff changeset
801 #endif /* _WIN32 */
8577
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
802 }
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
803 }
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
804 else
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
805 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
806 if (*rest != NULL && *c != '\0')
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
807 *rest = c;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
808 }
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
809 }
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
810
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
811 if (tm != NULL)
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
812 {
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
813 *tm = *t;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
814 tm->tm_isdst = -1;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
815 mktime(tm);
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
816 }
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
817
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
818 retval = mktime(t);
13119
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
819 if (tzoff != GAIM_NO_TZ_OFF)
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
820 retval += tzoff;
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
821
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
822 if (tz_off != NULL)
fcde3faa1f57 [gaim-migrate @ 15481]
Richard Laager <rlaager@wiktel.com>
parents: 13117
diff changeset
823 *tz_off = tzoff;
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
824
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
825 return retval;
8577
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
826 }
599d6ac9bbfe [gaim-migrate @ 9326]
Nathan Walp <nwalp@pidgin.im>
parents: 8561
diff changeset
827
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
828 /**************************************************************************
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
829 * Markup Functions
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
830 **************************************************************************/
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
831 gboolean
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
832 gaim_markup_find_tag(const char *needle, const char *haystack,
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
833 const char **start, const char **end, GData **attributes)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
834 {
6982
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
835 GData *attribs;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
836 const char *cur = haystack;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
837 char *name = NULL;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
838 gboolean found = FALSE;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
839 gboolean in_tag = FALSE;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
840 gboolean in_attr = FALSE;
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
841 const char *in_quotes = NULL;
7804
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
842 size_t needlelen;
6982
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
843
7804
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
844 g_return_val_if_fail( needle != NULL, FALSE);
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
845 g_return_val_if_fail( *needle != '\0', FALSE);
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
846 g_return_val_if_fail( haystack != NULL, FALSE);
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
847 g_return_val_if_fail( *haystack != '\0', FALSE);
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
848 g_return_val_if_fail( start != NULL, FALSE);
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
849 g_return_val_if_fail( end != NULL, FALSE);
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
850 g_return_val_if_fail(attributes != NULL, FALSE);
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
851
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
852 needlelen = strlen(needle);
6982
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
853 g_datalist_init(&attribs);
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
854
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
855 while (*cur && !found) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
856 if (in_tag) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
857 if (in_quotes) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
858 const char *close = cur;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
859
7078
acd2a66e59ed [gaim-migrate @ 7643]
Nathan Walp <nwalp@pidgin.im>
parents: 7000
diff changeset
860 while (*close && *close != *in_quotes)
6982
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
861 close++;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
862
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
863 /* if we got the close quote, store the value and carry on from *
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
864 * after it. if we ran to the end of the string, point to the NULL *
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
865 * and we're outta here */
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
866 if (*close) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
867 /* only store a value if we have an attribute name */
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
868 if (name) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
869 size_t len = close - cur;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
870 char *val = g_strndup(cur, len);
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
871
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
872 g_datalist_set_data_full(&attribs, name, val, g_free);
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
873 g_free(name);
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
874 name = NULL;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
875 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
876
7078
acd2a66e59ed [gaim-migrate @ 7643]
Nathan Walp <nwalp@pidgin.im>
parents: 7000
diff changeset
877 in_quotes = NULL;
6982
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
878 cur = close + 1;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
879 } else {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
880 cur = close;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
881 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
882 } else if (in_attr) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
883 const char *close = cur;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
884
7078
acd2a66e59ed [gaim-migrate @ 7643]
Nathan Walp <nwalp@pidgin.im>
parents: 7000
diff changeset
885 while (*close && *close != '>' && *close != '"' &&
acd2a66e59ed [gaim-migrate @ 7643]
Nathan Walp <nwalp@pidgin.im>
parents: 7000
diff changeset
886 *close != '\'' && *close != ' ' && *close != '=')
6982
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
887 close++;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
888
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
889 /* if we got the equals, store the name of the attribute. if we got
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
890 * the quote, save the attribute and go straight to quote mode.
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
891 * otherwise the tag closed or we reached the end of the string,
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
892 * so we can get outta here */
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
893 switch (*close) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
894 case '"':
7078
acd2a66e59ed [gaim-migrate @ 7643]
Nathan Walp <nwalp@pidgin.im>
parents: 7000
diff changeset
895 case '\'':
acd2a66e59ed [gaim-migrate @ 7643]
Nathan Walp <nwalp@pidgin.im>
parents: 7000
diff changeset
896 in_quotes = close;
6982
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
897 case '=':
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
898 {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
899 size_t len = close - cur;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
900
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
901 /* don't store a blank attribute name */
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
902 if (len) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
903 if (name)
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
904 g_free(name);
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
905 name = g_ascii_strdown(cur, len);
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
906 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
907
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
908 in_attr = FALSE;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
909 cur = close + 1;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
910 break;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
911 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
912 case ' ':
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
913 case '>':
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
914 in_attr = FALSE;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
915 default:
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
916 cur = close;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
917 break;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
918 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
919 } else {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
920 switch (*cur) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
921 case ' ':
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
922 /* swallow extra spaces inside tag */
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
923 while (*cur && *cur == ' ') cur++;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
924 in_attr = TRUE;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
925 break;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
926 case '>':
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
927 found = TRUE;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
928 *end = cur;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
929 break;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
930 case '"':
7078
acd2a66e59ed [gaim-migrate @ 7643]
Nathan Walp <nwalp@pidgin.im>
parents: 7000
diff changeset
931 case '\'':
acd2a66e59ed [gaim-migrate @ 7643]
Nathan Walp <nwalp@pidgin.im>
parents: 7000
diff changeset
932 in_quotes = cur;
6982
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
933 default:
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
934 cur++;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
935 break;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
936 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
937 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
938 } else {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
939 /* if we hit a < followed by the name of our tag... */
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
940 if (*cur == '<' && !g_ascii_strncasecmp(cur + 1, needle, needlelen)) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
941 *start = cur;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
942 cur = cur + needlelen + 1;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
943
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
944 /* if we're pointing at a space or a >, we found the right tag. if *
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
945 * we're not, we've found a longer tag, so we need to skip to the *
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
946 * >, but not being distracted by >s inside quotes. */
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
947 if (*cur == ' ' || *cur == '>') {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
948 in_tag = TRUE;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
949 } else {
7804
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
950 while (*cur && *cur != '"' && *cur != '\'' && *cur != '>') {
6982
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
951 if (*cur == '"') {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
952 cur++;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
953 while (*cur && *cur != '"')
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
954 cur++;
7804
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
955 } else if (*cur == '\'') {
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
956 cur++;
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
957 while (*cur && *cur != '\'')
622c9149609c [gaim-migrate @ 8451]
Nathan Walp <nwalp@pidgin.im>
parents: 7722
diff changeset
958 cur++;
6982
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
959 } else {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
960 cur++;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
961 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
962 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
963 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
964 } else {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
965 cur++;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
966 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
967 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
968 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
969
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
970 /* clean up any attribute name from a premature termination */
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
971 if (name)
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
972 g_free(name);
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
973
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
974 if (found) {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
975 *attributes = attribs;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
976 } else {
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
977 *start = NULL;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
978 *end = NULL;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
979 *attributes = NULL;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
980 }
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
981
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
982 return found;
083d1e4a9c78 [gaim-migrate @ 7538]
Mark Doliner <mark@kingant.net>
parents: 6872
diff changeset
983 }
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
984
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
985 gboolean
7675
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
986 gaim_markup_extract_info_field(const char *str, int len, GString *dest,
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
987 const char *start_token, int skip,
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
988 const char *end_token, char check_value,
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
989 const char *no_value_token,
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
990 const char *display_name, gboolean is_link,
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
991 const char *link_prefix,
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
992 GaimInfoFieldFormatCallback format_cb)
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
993 {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
994 const char *p, *q;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
995
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
996 g_return_val_if_fail(str != NULL, FALSE);
7675
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
997 g_return_val_if_fail(dest != NULL, FALSE);
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
998 g_return_val_if_fail(start_token != NULL, FALSE);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
999 g_return_val_if_fail(end_token != NULL, FALSE);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1000 g_return_val_if_fail(display_name != NULL, FALSE);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1001
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1002 p = strstr(str, start_token);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1003
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1004 if (p == NULL)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1005 return FALSE;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1006
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1007 p += strlen(start_token) + skip;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1008
7675
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
1009 if (p >= str + len)
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
1010 return FALSE;
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
1011
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1012 if (check_value != '\0' && *p == check_value)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1013 return FALSE;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1014
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1015 q = strstr(p, end_token);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1016
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1017 /* Trim leading blanks */
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1018 while (*p != '\n' && g_ascii_isspace(*p)) {
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1019 p += 1;
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1020 }
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1021
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1022 /* Trim trailing blanks */
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1023 while (q > p && g_ascii_isspace(*(q - 1))) {
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1024 q -= 1;
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1025 }
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1026
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1027 /* Don't bother with null strings */
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1028 if (p == q)
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1029 return FALSE;
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1030
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1031 if (q != NULL && (!no_value_token ||
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1032 (no_value_token && strncmp(p, no_value_token,
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1033 strlen(no_value_token)))))
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1034 {
9220
f0488214826f [gaim-migrate @ 10016]
Tim Ringenbach <marv@pidgin.im>
parents: 9175
diff changeset
1035 g_string_append_printf(dest, _("<b>%s:</b> "), display_name);
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1036
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1037 if (is_link)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1038 {
7675
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
1039 g_string_append(dest, "<br><a href=\"");
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1040
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1041 if (link_prefix)
7675
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
1042 g_string_append(dest, link_prefix);
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1043
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1044 if (format_cb != NULL)
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1045 {
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1046 char *reformatted = format_cb(p, q - p);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1047 g_string_append(dest, reformatted);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1048 g_free(reformatted);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1049 }
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1050 else
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1051 g_string_append_len(dest, p, q - p);
7675
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
1052 g_string_append(dest, "\">");
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1053
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1054 if (link_prefix)
7675
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
1055 g_string_append(dest, link_prefix);
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1056
7675
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
1057 g_string_append_len(dest, p, q - p);
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
1058 g_string_append(dest, "</a>");
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1059 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1060 else
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1061 {
13104
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1062 if (format_cb != NULL)
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1063 {
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1064 char *reformatted = format_cb(p, q - p);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1065 g_string_append(dest, reformatted);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1066 g_free(reformatted);
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1067 }
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1068 else
e1e5462b7d81 [gaim-migrate @ 15466]
Richard Laager <rlaager@wiktel.com>
parents: 13102
diff changeset
1069 g_string_append_len(dest, p, q - p);
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1070 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1071
7675
66d9440db6ec [gaim-migrate @ 8319]
Luke Schierer <lschiere@pidgin.im>
parents: 7673
diff changeset
1072 g_string_append(dest, "<br>\n");
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1073
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1074 return TRUE;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1075 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1076
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1077 return FALSE;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1078 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1079
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1080 struct gaim_parse_tag {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1081 char *src_tag;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1082 char *dest_tag;
8064
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1083 gboolean ignore;
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1084 };
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1085
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1086 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1087 const char *o = c + strlen("<" x); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1088 const char *p = NULL, *q = NULL, *r = NULL; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1089 GString *innards = g_string_new(""); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1090 while(o && *o) { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1091 if(!q && (*o == '\"' || *o == '\'') ) { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1092 q = o; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1093 } else if(q) { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1094 if(*o == *q) { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1095 char *unescaped = g_strndup(q+1, o-q-1); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1096 char *escaped = g_markup_escape_text(unescaped, -1); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1097 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1098 g_free(unescaped); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1099 g_free(escaped); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1100 q = NULL; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1101 } else if(*c == '\\') { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1102 o++; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1103 } \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1104 } else if(*o == '<') { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1105 r = o; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1106 } else if(*o == '>') { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1107 p = o; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1108 break; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1109 } else { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1110 innards = g_string_append_c(innards, *o); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1111 } \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1112 o++; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1113 } \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1114 if(p && !r) { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1115 if(*(p-1) != '/') { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1116 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1117 pt->src_tag = x; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1118 pt->dest_tag = y; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1119 tags = g_list_prepend(tags, pt); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1120 } \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1121 xhtml = g_string_append(xhtml, "<" y); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1122 c += strlen("<" x ); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1123 xhtml = g_string_append(xhtml, innards->str); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1124 xhtml = g_string_append_c(xhtml, '>'); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1125 c = p + 1; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1126 } else { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1127 xhtml = g_string_append(xhtml, "&lt;"); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1128 plain = g_string_append_c(plain, '<'); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1129 c++; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1130 } \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1131 g_string_free(innards, TRUE); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1132 continue; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1133 } \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1134 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1135 (*(c+strlen("<" x)) == '>' || \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1136 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1137 xhtml = g_string_append(xhtml, "<" y); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1138 c += strlen("<" x); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1139 if(*c != '/') { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1140 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1141 pt->src_tag = x; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1142 pt->dest_tag = y; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1143 tags = g_list_prepend(tags, pt); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1144 xhtml = g_string_append_c(xhtml, '>'); \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1145 } else { \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1146 xhtml = g_string_append(xhtml, "/>");\
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1147 } \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1148 c = strchr(c, '>') + 1; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1149 continue; \
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1150 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1151 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1152 void
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1153 gaim_markup_html_to_xhtml(const char *html, char **xhtml_out,
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1154 char **plain_out)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1155 {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1156 GString *xhtml = g_string_new("");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1157 GString *plain = g_string_new("");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1158 GList *tags = NULL, *tag;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1159 const char *c = html;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1160
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1161 while(c && *c) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1162 if(*c == '<') {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1163 if(*(c+1) == '/') { /* closing tag */
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1164 tag = tags;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1165 while(tag) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1166 struct gaim_parse_tag *pt = tag->data;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1167 if(!g_ascii_strncasecmp((c+2), pt->src_tag, strlen(pt->src_tag)) && *(c+strlen(pt->src_tag)+2) == '>') {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1168 c += strlen(pt->src_tag) + 3;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1169 break;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1170 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1171 tag = tag->next;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1172 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1173 if(tag) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1174 while(tags) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1175 struct gaim_parse_tag *pt = tags->data;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1176 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1177 if(tags == tag)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1178 break;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1179 tags = g_list_remove(tags, pt);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1180 g_free(pt);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1181 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1182 g_free(tag->data);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1183 tags = g_list_remove(tags, tag->data);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1184 } else {
8534
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1185 /* a closing tag we weren't expecting...
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1186 * we'll let it slide, if it's really a tag...if it's
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1187 * just a </ we'll escape it properly */
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1188 const char *end = c+2;
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1189 while(*end && g_ascii_isalpha(*end))
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1190 end++;
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1191 if(*end == '>') {
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1192 c = end+1;
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1193 } else {
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1194 xhtml = g_string_append(xhtml, "&lt;");
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1195 plain = g_string_append_c(plain, '<');
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1196 c++;
389e2b9dae6a [gaim-migrate @ 9273]
Nathan Walp <nwalp@pidgin.im>
parents: 8517
diff changeset
1197 }
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1198 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1199 } else { /* opening tag */
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1200 ALLOW_TAG("a");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1201 ALLOW_TAG_ALT("b", "strong");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1202 ALLOW_TAG("blockquote");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1203 ALLOW_TAG_ALT("bold", "strong");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1204 ALLOW_TAG("cite");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1205 ALLOW_TAG("div");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1206 ALLOW_TAG("em");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1207 ALLOW_TAG("h1");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1208 ALLOW_TAG("h2");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1209 ALLOW_TAG("h3");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1210 ALLOW_TAG("h4");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1211 ALLOW_TAG("h5");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1212 ALLOW_TAG("h6");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1213 /* we only allow html to start the message */
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1214 if(c == html)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1215 ALLOW_TAG("html");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1216 ALLOW_TAG_ALT("i", "em");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1217 ALLOW_TAG_ALT("italic", "em");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1218 ALLOW_TAG("li");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1219 ALLOW_TAG("ol");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1220 ALLOW_TAG("p");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1221 ALLOW_TAG("pre");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1222 ALLOW_TAG("q");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1223 ALLOW_TAG("span");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1224 ALLOW_TAG("strong");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1225 ALLOW_TAG("ul");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1226
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1227 /* we skip <HR> because it's not legal in XHTML-IM. However,
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1228 * we still want to send something sensible, so we put a
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1229 * linebreak in its place. <BR> also needs special handling
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1230 * because putting a </BR> to close it would just be dumb. */
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1231 if((!g_ascii_strncasecmp(c, "<br", 3)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1232 || !g_ascii_strncasecmp(c, "<hr", 3))
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1233 && (*(c+3) == '>' ||
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1234 !g_ascii_strncasecmp(c+3, "/>", 2) ||
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1235 !g_ascii_strncasecmp(c+3, " />", 3))) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1236 c = strchr(c, '>') + 1;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1237 xhtml = g_string_append(xhtml, "<br/>");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1238 if(*c != '\n')
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1239 plain = g_string_append_c(plain, '\n');
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1240 continue;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1241 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1242 if(!g_ascii_strncasecmp(c, "<u>", 3) || !g_ascii_strncasecmp(c, "<underline>", strlen("<underline>"))) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1243 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1244 pt->src_tag = *(c+2) == '>' ? "u" : "underline";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1245 pt->dest_tag = "span";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1246 tags = g_list_prepend(tags, pt);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1247 c = strchr(c, '>') + 1;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1248 xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1249 continue;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1250 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1251 if(!g_ascii_strncasecmp(c, "<s>", 3) || !g_ascii_strncasecmp(c, "<strike>", strlen("<strike>"))) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1252 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1253 pt->src_tag = *(c+2) == '>' ? "s" : "strike";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1254 pt->dest_tag = "span";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1255 tags = g_list_prepend(tags, pt);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1256 c = strchr(c, '>') + 1;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1257 xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1258 continue;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1259 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1260 if(!g_ascii_strncasecmp(c, "<sub>", 5)) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1261 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1262 pt->src_tag = "sub";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1263 pt->dest_tag = "span";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1264 tags = g_list_prepend(tags, pt);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1265 c = strchr(c, '>') + 1;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1266 xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1267 continue;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1268 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1269 if(!g_ascii_strncasecmp(c, "<sup>", 5)) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1270 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1271 pt->src_tag = "sup";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1272 pt->dest_tag = "span";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1273 tags = g_list_prepend(tags, pt);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1274 c = strchr(c, '>') + 1;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1275 xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1276 continue;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1277 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1278 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1279 const char *p = c;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1280 GString *style = g_string_new("");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1281 struct gaim_parse_tag *pt;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1282 while(*p && *p != '>') {
8064
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1283 if(!g_ascii_strncasecmp(p, "back=", strlen("back="))) {
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1284 const char *q = p + strlen("back=");
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1285 GString *color = g_string_new("");
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1286 if(*q == '\'' || *q == '\"')
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1287 q++;
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1288 while(*q && *q != '\"' && *q != '\'' && *q != ' ') {
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1289 color = g_string_append_c(color, *q);
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1290 q++;
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1291 }
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1292 g_string_append_printf(style, "background: %s; ", color->str);
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1293 g_string_free(color, TRUE);
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1294 p = q;
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1295 } else if(!g_ascii_strncasecmp(p, "color=", strlen("color="))) {
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1296 const char *q = p + strlen("color=");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1297 GString *color = g_string_new("");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1298 if(*q == '\'' || *q == '\"')
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1299 q++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1300 while(*q && *q != '\"' && *q != '\'' && *q != ' ') {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1301 color = g_string_append_c(color, *q);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1302 q++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1303 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1304 g_string_append_printf(style, "color: %s; ", color->str);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1305 g_string_free(color, TRUE);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1306 p = q;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1307 } else if(!g_ascii_strncasecmp(p, "face=", strlen("face="))) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1308 const char *q = p + strlen("face=");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1309 gboolean space_allowed = FALSE;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1310 GString *face = g_string_new("");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1311 if(*q == '\'' || *q == '\"') {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1312 space_allowed = TRUE;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1313 q++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1314 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1315 while(*q && *q != '\"' && *q != '\'' && (space_allowed || *q != ' ')) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1316 face = g_string_append_c(face, *q);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1317 q++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1318 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1319 g_string_append_printf(style, "font-family: %s; ", face->str);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1320 g_string_free(face, TRUE);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1321 p = q;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1322 } else if(!g_ascii_strncasecmp(p, "size=", strlen("size="))) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1323 const char *q = p + strlen("size=");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1324 int sz;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1325 const char *size = "medium";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1326 if(*q == '\'' || *q == '\"')
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1327 q++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1328 sz = atoi(q);
8686
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1329 switch (sz)
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1330 {
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1331 case 1:
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1332 size = "xx-small";
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1333 break;
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1334 case 2:
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1335 size = "x-small";
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1336 break;
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1337 case 3:
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1338 size = "small";
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1339 break;
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1340 case 4:
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1341 size = "medium";
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1342 break;
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1343 case 5:
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1344 size = "large";
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1345 break;
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1346 case 6:
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1347 size = "x-large";
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1348 break;
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1349 case 7:
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1350 size = "xx-large";
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1351 break;
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1352 default:
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1353 break;
140b0d020c43 [gaim-migrate @ 9439]
Luke Schierer <lschiere@pidgin.im>
parents: 8625
diff changeset
1354 }
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1355 g_string_append_printf(style, "font-size: %s; ", size);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1356 p = q;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1357 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1358 p++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1359 }
10593
ffabeb0fdc64 [gaim-migrate @ 12005]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10589
diff changeset
1360 if ((c = strchr(c, '>')) != NULL)
ffabeb0fdc64 [gaim-migrate @ 12005]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10589
diff changeset
1361 c++;
ffabeb0fdc64 [gaim-migrate @ 12005]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10589
diff changeset
1362 else
ffabeb0fdc64 [gaim-migrate @ 12005]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10589
diff changeset
1363 c = p;
8064
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1364 pt = g_new0(struct gaim_parse_tag, 1);
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1365 pt->src_tag = "font";
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1366 pt->dest_tag = "span";
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1367 tags = g_list_prepend(tags, pt);
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1368 if(style->len)
8026
17b90334f3cc [gaim-migrate @ 8706]
Nathan Walp <nwalp@pidgin.im>
parents: 8005
diff changeset
1369 g_string_append_printf(xhtml, "<span style='%s'>", style->str);
8064
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1370 else
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1371 pt->ignore = TRUE;
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1372 g_string_free(style, TRUE);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1373 continue;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1374 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1375 if(!g_ascii_strncasecmp(c, "<body ", 6)) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1376 const char *p = c;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1377 gboolean did_something = FALSE;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1378 while(*p && *p != '>') {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1379 if(!g_ascii_strncasecmp(p, "bgcolor=", strlen("bgcolor="))) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1380 const char *q = p + strlen("bgcolor=");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1381 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1382 GString *color = g_string_new("");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1383 if(*q == '\'' || *q == '\"')
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1384 q++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1385 while(*q && *q != '\"' && *q != '\'' && *q != ' ') {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1386 color = g_string_append_c(color, *q);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1387 q++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1388 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1389 g_string_append_printf(xhtml, "<span style='background: %s;'>", color->str);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1390 g_string_free(color, TRUE);
10623
14c32196fcfd [gaim-migrate @ 12094]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10614
diff changeset
1391 if ((c = strchr(c, '>')) != NULL)
14c32196fcfd [gaim-migrate @ 12094]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10614
diff changeset
1392 c++;
14c32196fcfd [gaim-migrate @ 12094]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10614
diff changeset
1393 else
14c32196fcfd [gaim-migrate @ 12094]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10614
diff changeset
1394 c = p;
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1395 pt->src_tag = "body";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1396 pt->dest_tag = "span";
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1397 tags = g_list_prepend(tags, pt);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1398 did_something = TRUE;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1399 break;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1400 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1401 p++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1402 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1403 if(did_something) continue;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1404 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1405 /* this has to come after the special case for bgcolor */
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1406 ALLOW_TAG("body");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1407 if(!g_ascii_strncasecmp(c, "<!--", strlen("<!--"))) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1408 char *p = strstr(c + strlen("<!--"), "-->");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1409 if(p) {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1410 xhtml = g_string_append(xhtml, "<!--");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1411 c += strlen("<!--");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1412 continue;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1413 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1414 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1415
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1416 xhtml = g_string_append(xhtml, "&lt;");
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1417 plain = g_string_append_c(plain, '<');
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1418 c++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1419 }
7287
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1420 } else if(*c == '&') {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1421 char buf[7];
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1422 char *pln;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1423 int len = 1;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1424 guint pound;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1425 if(!g_ascii_strncasecmp(c, "&amp;", 5)) {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1426 pln = "&";
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1427 len = 5;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1428 } else if(!g_ascii_strncasecmp(c, "&lt;", 4)) {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1429 pln = "<";
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1430 len = 4;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1431 } else if(!g_ascii_strncasecmp(c, "&gt;", 4)) {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1432 pln = ">";
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1433 len = 4;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1434 } else if(!g_ascii_strncasecmp(c, "&nbsp;", 6)) {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1435 pln = " ";
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1436 len = 6;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1437 } else if(!g_ascii_strncasecmp(c, "&copy;", 6)) {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1438 pln = "©";
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1439 len = 6;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1440 } else if(!g_ascii_strncasecmp(c, "&quot;", 6)) {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1441 pln = "\"";
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1442 len = 6;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1443 } else if(!g_ascii_strncasecmp(c, "&reg;", 5)) {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1444 pln = "®";
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1445 len = 5;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1446 } else if(!g_ascii_strncasecmp(c, "&apos;", 6)) {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1447 pln = "\'";
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1448 len = 6;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1449 } else if(*(c+1) == '#' && (sscanf(c, "&#%u;", &pound) == 1) &&
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1450 pound != 0 && *(c+3+(gint)log10(pound)) == ';') {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1451 int buflen = g_unichar_to_utf8((gunichar)pound, buf);
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1452 buf[buflen] = '\0';
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1453 pln = buf;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1454
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1455
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1456 len = 2;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1457 while(isdigit((gint) c [len])) len++;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1458 if(c [len] == ';') len++;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1459 } else {
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1460 len = 1;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1461 g_snprintf(buf, sizeof(buf), "%c", *c);
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1462 pln = buf;
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1463 }
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1464 xhtml = g_string_append_len(xhtml, c, len);
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1465 plain = g_string_append(plain, pln);
3a41c3f80228 [gaim-migrate @ 7868]
Nathan Walp <nwalp@pidgin.im>
parents: 7265
diff changeset
1466 c += len;
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1467 } else {
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1468 xhtml = g_string_append_c(xhtml, *c);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1469 plain = g_string_append_c(plain, *c);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1470 c++;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1471 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1472 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1473 tag = tags;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1474 while(tag) {
8064
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1475 struct gaim_parse_tag *pt = tag->data;
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1476 if(!pt->ignore)
9b6bc1010054 [gaim-migrate @ 8757]
Nathan Walp <nwalp@pidgin.im>
parents: 8063
diff changeset
1477 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1478 tag = tag->next;
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1479 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1480 g_list_free(tags);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1481 if(xhtml_out)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1482 *xhtml_out = g_strdup(xhtml->str);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1483 if(plain_out)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1484 *plain_out = g_strdup(plain->str);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1485 g_string_free(xhtml, TRUE);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1486 g_string_free(plain, TRUE);
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1487 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1488
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1489 /* The following are probably reasonable changes:
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1490 * - \n should be converted to a normal space
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1491 * - in addition to <br>, <p> and <div> etc. should also be converted into \n
9241
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1492 * - We want to turn </td>#whitespace<td> sequences into a single tab
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1493 * - We want to turn <td> into a single tab (for msn profile "parsing")
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1494 * - We want to turn </tr>#whitespace<tr> sequences into a single \n
9241
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1495 * - <script>...</script> and <style>...</style> should be completely removed
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1496 */
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1497
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1498 char *
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1499 gaim_markup_strip_html(const char *str)
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1500 {
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1501 int i, j, k;
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1502 gboolean visible = TRUE;
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1503 gboolean closing_td_p = FALSE;
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1504 gchar *str2;
9241
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1505 const gchar *cdata_close_tag = NULL;
12620
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1506 gchar *href = NULL;
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1507 int href_st = 0;
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1508
7110
b003397b16fe [gaim-migrate @ 7676]
Nathan Walp <nwalp@pidgin.im>
parents: 7108
diff changeset
1509 if(!str)
b003397b16fe [gaim-migrate @ 7676]
Nathan Walp <nwalp@pidgin.im>
parents: 7108
diff changeset
1510 return NULL;
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1511
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1512 str2 = g_strdup(str);
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1513
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1514 for (i = 0, j = 0; str2[i]; i++)
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1515 {
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1516 if (str2[i] == '<')
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1517 {
9241
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1518 if (cdata_close_tag)
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1519 {
9241
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1520 /* Note: Don't even assume any other tag is a tag in CDATA */
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1521 if (strncasecmp(str2 + i, cdata_close_tag,
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1522 strlen(cdata_close_tag)) == 0)
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1523 {
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1524 i += strlen(cdata_close_tag) - 1;
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1525 cdata_close_tag = NULL;
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1526 }
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1527 continue;
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1528 }
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1529 else if (strncasecmp(str2 + i, "<td", 3) == 0 && closing_td_p)
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1530 {
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1531 str2[j++] = '\t';
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1532 visible = TRUE;
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1533 }
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1534 else if (strncasecmp(str2 + i, "</td>", 5) == 0)
8517
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1535 {
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1536 closing_td_p = TRUE;
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1537 visible = FALSE;
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1538 }
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1539 else
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1540 {
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1541 closing_td_p = FALSE;
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1542 visible = TRUE;
8517
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1543 }
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1544
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1545 k = i + 1;
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1546
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1547 if(g_ascii_isspace(str2[k]))
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1548 visible = TRUE;
9170
456ef1f4ba19 [gaim-migrate @ 9955]
Tim Ringenbach <marv@pidgin.im>
parents: 9161
diff changeset
1549 else if (str2[k])
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1550 {
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1551 /* Scan until we end the tag either implicitly (closed start
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1552 * tag) or explicitly, using a sloppy method (i.e., < or >
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1553 * inside quoted attributes will screw us up)
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1554 */
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1555 while (str2[k] && str2[k] != '<' && str2[k] != '>')
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1556 {
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1557 k++;
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1558 }
9241
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1559
12620
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1560 /* If we've got an <a> tag with an href, save the address
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1561 * to print later. */
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1562 if (strncasecmp(str2 + i, "<a", 2) == 0 &&
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1563 g_ascii_isspace(str2[i+2]))
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1564 {
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1565 int st; /* start of href, inclusive [ */
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1566 int end; /* end of href, exclusive ) */
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1567 char delim = ' ';
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1568 /* Find start of href */
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1569 for (st = i + 3; st < k; st++)
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1570 {
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1571 if (strncasecmp(str2+st, "href=", 5) == 0)
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1572 {
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1573 st += 5;
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1574 if (str2[st] == '"')
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1575 {
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1576 delim = '"';
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1577 st++;
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1578 }
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1579 break;
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1580 }
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1581 }
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1582 /* find end of address */
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1583 for (end = st; end < k && str2[end] != delim; end++)
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1584 {
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1585 /* All the work is done in the loop construct above. */
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1586 }
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1587
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1588 /* If there's an address, save it. If there was
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1589 * already one saved, kill it. */
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1590 if (st < k)
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1591 {
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1592 char *tmp;
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1593 g_free(href);
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1594 tmp = g_strndup(str2 + st, end - st);
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1595 href = gaim_unescape_html(tmp);
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1596 g_free(tmp);
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1597 href_st = j;
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1598 }
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1599 }
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1600
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1601 /* Replace </a> with an ascii representation of the
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1602 * address the link was pointing to. */
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1603 else if (href != NULL && strncasecmp(str2 + i, "</a>", 4) == 0)
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1604 {
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1605
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1606 size_t hrlen = strlen(href);
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1607
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1608 /* Only insert the href if it's different from the CDATA. */
12640
de306144dc8a [gaim-migrate @ 14976]
Richard Laager <rlaager@wiktel.com>
parents: 12628
diff changeset
1609 if ((hrlen != j - href_st ||
de306144dc8a [gaim-migrate @ 14976]
Richard Laager <rlaager@wiktel.com>
parents: 12628
diff changeset
1610 strncmp(str2 + href_st, href, hrlen)) &&
de306144dc8a [gaim-migrate @ 14976]
Richard Laager <rlaager@wiktel.com>
parents: 12628
diff changeset
1611 (hrlen != j - href_st + 7 || /* 7 == strlen("http://") */
de306144dc8a [gaim-migrate @ 14976]
Richard Laager <rlaager@wiktel.com>
parents: 12628
diff changeset
1612 strncmp(str2 + href_st, href + 7, hrlen - 7)))
12620
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1613 {
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1614 str2[j++] = ' ';
12628
26647ddcc9c5 [gaim-migrate @ 14964]
Richard Laager <rlaager@wiktel.com>
parents: 12622
diff changeset
1615 str2[j++] = '(';
12620
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1616 g_memmove(str2 + j, href, hrlen);
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1617 j += hrlen;
12628
26647ddcc9c5 [gaim-migrate @ 14964]
Richard Laager <rlaager@wiktel.com>
parents: 12622
diff changeset
1618 str2[j++] = ')';
12620
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1619 g_free(href);
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1620 href = NULL;
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1621 }
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1622 }
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1623
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1624 /* Check for tags which should be mapped to newline */
12620
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1625 else if (strncasecmp(str2 + i, "<p>", 3) == 0
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1626 || strncasecmp(str2 + i, "<tr", 3) == 0
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1627 || strncasecmp(str2 + i, "<br", 3) == 0
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1628 || strncasecmp(str2 + i, "<li", 3) == 0
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1629 || strncasecmp(str2 + i, "<div", 4) == 0
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1630 || strncasecmp(str2 + i, "</table>", 8) == 0)
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1631 {
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1632 str2[j++] = '\n';
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1633 }
9241
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1634 /* Check for tags which begin CDATA and need to be closed */
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1635 #if 0 /* FIXME.. option is end tag optional, we can't handle this right now */
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1636 else if (strncasecmp(str2 + i, "<option", 7) == 0)
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1637 {
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1638 /* FIXME: We should not do this if the OPTION is SELECT'd */
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1639 cdata_close_tag = "</option>";
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1640 }
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1641 #endif
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1642 else if (strncasecmp(str2 + i, "<script", 7) == 0)
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1643 {
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1644 cdata_close_tag = "</script>";
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1645 }
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1646 else if (strncasecmp(str2 + i, "<style", 6) == 0)
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1647 {
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1648 cdata_close_tag = "</style>";
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1649 }
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1650 /* Update the index and continue checking after the tag */
10731
783ca1f1ebdb [gaim-migrate @ 12333]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10700
diff changeset
1651 i = (str2[k] == '<' || str2[k] == '\0')? k - 1: k;
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1652 continue;
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1653 }
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1654 }
9241
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1655 else if (cdata_close_tag)
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1656 {
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1657 continue;
5e9018c81bd9 [gaim-migrate @ 10040]
Tim Ringenbach <marv@pidgin.im>
parents: 9240
diff changeset
1658 }
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1659 else if (!g_ascii_isspace(str2[i]))
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1660 {
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1661 visible = TRUE;
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1662 }
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1663
9273
227d3d6c6565 [gaim-migrate @ 10076]
Nathan Walp <nwalp@pidgin.im>
parents: 9257
diff changeset
1664 /* XXX: This sucks. We need to be un-escaping all entities, which
227d3d6c6565 [gaim-migrate @ 10076]
Nathan Walp <nwalp@pidgin.im>
parents: 9257
diff changeset
1665 * includes these, as well as the &#num; ones */
227d3d6c6565 [gaim-migrate @ 10076]
Nathan Walp <nwalp@pidgin.im>
parents: 9257
diff changeset
1666
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1667 if (str2[i] == '&' && strncasecmp(str2 + i, "&quot;", 6) == 0)
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1668 {
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1669 str2[j++] = '\"';
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1670 i = i + 5;
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1671 continue;
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1672 }
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1673
8517
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1674 if (str2[i] == '&' && strncasecmp(str2 + i, "&amp;", 5) == 0)
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1675 {
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1676 str2[j++] = '&';
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1677 i = i + 4;
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1678 continue;
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1679 }
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1680
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1681 if (str2[i] == '&' && strncasecmp(str2 + i, "&lt;", 4) == 0)
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1682 {
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1683 str2[j++] = '<';
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1684 i = i + 3;
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1685 continue;
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1686 }
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1687
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1688 if (str2[i] == '&' && strncasecmp(str2 + i, "&gt;", 4) == 0)
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1689 {
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1690 str2[j++] = '>';
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1691 i = i + 3;
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1692 continue;
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1693 }
5cb93726e4d5 [gaim-migrate @ 9256]
Luke Schierer <lschiere@pidgin.im>
parents: 8511
diff changeset
1694
9273
227d3d6c6565 [gaim-migrate @ 10076]
Nathan Walp <nwalp@pidgin.im>
parents: 9257
diff changeset
1695 if (str2[i] == '&' && strncasecmp(str2 + i, "&apos;", 6) == 0)
227d3d6c6565 [gaim-migrate @ 10076]
Nathan Walp <nwalp@pidgin.im>
parents: 9257
diff changeset
1696 {
227d3d6c6565 [gaim-migrate @ 10076]
Nathan Walp <nwalp@pidgin.im>
parents: 9257
diff changeset
1697 str2[j++] = '\'';
227d3d6c6565 [gaim-migrate @ 10076]
Nathan Walp <nwalp@pidgin.im>
parents: 9257
diff changeset
1698 i = i + 5;
227d3d6c6565 [gaim-migrate @ 10076]
Nathan Walp <nwalp@pidgin.im>
parents: 9257
diff changeset
1699 continue;
227d3d6c6565 [gaim-migrate @ 10076]
Nathan Walp <nwalp@pidgin.im>
parents: 9257
diff changeset
1700 }
227d3d6c6565 [gaim-migrate @ 10076]
Nathan Walp <nwalp@pidgin.im>
parents: 9257
diff changeset
1701
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1702 if (visible)
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
1703 str2[j++] = g_ascii_isspace(str2[i])? ' ': str2[i];
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1704 }
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1705
12620
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1706 g_free(href);
13599d978a31 [gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents: 12483
diff changeset
1707
7103
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1708 str2[j] = '\0';
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1709
5dc4ed905a1a [gaim-migrate @ 7668]
Nathan Walp <nwalp@pidgin.im>
parents: 7099
diff changeset
1710 return str2;
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1711 }
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
1712
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1713 static gboolean
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1714 badchar(char c)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1715 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1716 switch (c) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1717 case ' ':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1718 case ',':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1719 case '\0':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1720 case '\n':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1721 case '<':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1722 case '>':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1723 case '"':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1724 case '\'':
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1725 return TRUE;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1726 default:
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1727 return FALSE;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1728 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1729 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
1730
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1731 static gboolean
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1732 badentity(const char *c)
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1733 {
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1734 if (!g_ascii_strncasecmp(c, "&lt;", 4) ||
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1735 !g_ascii_strncasecmp(c, "&gt;", 4) ||
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1736 !g_ascii_strncasecmp(c, "&quot;", 6)) {
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1737 return TRUE;
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1738 }
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1739 return FALSE;
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1740 }
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1741
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1742 char *
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1743 gaim_markup_linkify(const char *text)
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1744 {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1745 const char *c, *t, *q = NULL;
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1746 char *tmp, *tmpurlbuf, *url_buf;
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1747 gunichar g;
8538
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1748 gboolean inside_html = FALSE;
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1749 int inside_paren = 0;
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1750 GString *ret = g_string_new("");
10372
618a330c0260 [gaim-migrate @ 11595]
Luke Schierer <lschiere@pidgin.im>
parents: 10332
diff changeset
1751 /* Assumes you have a buffer able to carry at least BUF_LEN * 2 bytes */
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1752
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1753 c = text;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1754 while (*c) {
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1755
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1756 if(*c == '(' && !inside_html) {
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1757 inside_paren++;
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1758 ret = g_string_append_c(ret, *c);
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1759 c++;
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1760 }
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1761
8538
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1762 if(inside_html) {
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1763 if(*c == '>') {
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1764 inside_html = FALSE;
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1765 } else if(!q && (*c == '\"' || *c == '\'')) {
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1766 q = c;
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1767 } else if(q) {
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1768 if(*c == *q)
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1769 q = NULL;
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1770 }
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1771 } else if(*c == '<') {
848dfa9fe9d4 [gaim-migrate @ 9279]
Nathan Walp <nwalp@pidgin.im>
parents: 8534
diff changeset
1772 inside_html = TRUE;
9077
13e809489beb [gaim-migrate @ 9853]
Tim Ringenbach <marv@pidgin.im>
parents: 9054
diff changeset
1773 if (!g_ascii_strncasecmp(c, "<A", 2)) {
13e809489beb [gaim-migrate @ 9853]
Tim Ringenbach <marv@pidgin.im>
parents: 9054
diff changeset
1774 while (1) {
13e809489beb [gaim-migrate @ 9853]
Tim Ringenbach <marv@pidgin.im>
parents: 9054
diff changeset
1775 if (!g_ascii_strncasecmp(c, "/A>", 3)) {
13e809489beb [gaim-migrate @ 9853]
Tim Ringenbach <marv@pidgin.im>
parents: 9054
diff changeset
1776 inside_html = FALSE;
13e809489beb [gaim-migrate @ 9853]
Tim Ringenbach <marv@pidgin.im>
parents: 9054
diff changeset
1777 break;
13e809489beb [gaim-migrate @ 9853]
Tim Ringenbach <marv@pidgin.im>
parents: 9054
diff changeset
1778 }
13e809489beb [gaim-migrate @ 9853]
Tim Ringenbach <marv@pidgin.im>
parents: 9054
diff changeset
1779 ret = g_string_append_c(ret, *c);
13e809489beb [gaim-migrate @ 9853]
Tim Ringenbach <marv@pidgin.im>
parents: 9054
diff changeset
1780 c++;
13e809489beb [gaim-migrate @ 9853]
Tim Ringenbach <marv@pidgin.im>
parents: 9054
diff changeset
1781 if (!(*c))
13e809489beb [gaim-migrate @ 9853]
Tim Ringenbach <marv@pidgin.im>
parents: 9054
diff changeset
1782 break;
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1783 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1784 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1785 } else if ((*c=='h') && (!g_ascii_strncasecmp(c, "http://", 7) ||
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1786 (!g_ascii_strncasecmp(c, "https://", 8)))) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1787 t = c;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1788 while (1) {
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1789 if (badchar(*t) || badentity(t)) {
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1790
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1791 if (*(t) == ',' && (*(t + 1) != ' ')) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1792 t++;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1793 continue;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1794 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1795
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1796 if (*(t - 1) == '.')
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1797 t--;
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1798 if ((*(t - 1) == ')' && (inside_paren > 0))) {
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1799 t--;
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1800 }
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1801
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1802 url_buf = g_strndup(c, t - c);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1803 tmpurlbuf = gaim_unescape_html(url_buf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1804 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>",
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1805 tmpurlbuf, url_buf);
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1806 g_free(url_buf);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1807 g_free(tmpurlbuf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1808 c = t;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1809 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1810 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1811 if (!t)
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1812 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1813 t++;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1814
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1815 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1816 } else if (!g_ascii_strncasecmp(c, "www.", 4)) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1817 if (c[4] != '.') {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1818 t = c;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1819 while (1) {
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1820 if (badchar(*t) || badentity(t)) {
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1821 if (t - c == 4) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1822 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1823 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1824
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1825 if (*(t) == ',' && (*(t + 1) != ' ')) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1826 t++;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1827 continue;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1828 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1829
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1830 if (*(t - 1) == '.')
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1831 t--;
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1832 if ((*(t - 1) == ')' && (inside_paren > 0))) {
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1833 t--;
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1834 }
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1835 url_buf = g_strndup(c, t - c);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1836 tmpurlbuf = gaim_unescape_html(url_buf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1837 g_string_append_printf(ret,
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1838 "<A HREF=\"http://%s\">%s</A>", tmpurlbuf,
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1839 url_buf);
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1840 g_free(url_buf);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1841 g_free(tmpurlbuf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1842 c = t;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1843 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1844 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1845 if (!t)
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1846 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1847 t++;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1848 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1849 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1850 } else if (!g_ascii_strncasecmp(c, "ftp://", 6)) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1851 t = c;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1852 while (1) {
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1853 if (badchar(*t) || badentity(t)) {
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1854 if (*(t - 1) == '.')
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1855 t--;
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1856 if ((*(t - 1) == ')' && (inside_paren > 0))) {
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1857 t--;
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1858 }
10905
d41e285af79e [gaim-migrate @ 12634]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10902
diff changeset
1859 url_buf = g_strndup(c, t - c);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1860 tmpurlbuf = gaim_unescape_html(url_buf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1861 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>",
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1862 tmpurlbuf, url_buf);
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1863 g_free(url_buf);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1864 g_free(tmpurlbuf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1865 c = t;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1866 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1867 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1868 if (!t)
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1869 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1870 t++;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1871
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1872 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1873 } else if (!g_ascii_strncasecmp(c, "ftp.", 4)) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1874 if (c[4] != '.') {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1875 t = c;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1876 while (1) {
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1877 if (badchar(*t) || badentity(t)) {
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1878 if (t - c == 4) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1879 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1880 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1881 if (*(t - 1) == '.')
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1882 t--;
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1883 if ((*(t - 1) == ')' && (inside_paren > 0))) {
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1884 t--;
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1885 }
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1886 url_buf = g_strndup(c, t - c);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1887 tmpurlbuf = gaim_unescape_html(url_buf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1888 g_string_append_printf(ret,
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1889 "<A HREF=\"ftp://%s\">%s</A>", tmpurlbuf,
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1890 url_buf);
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1891 g_free(url_buf);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1892 g_free(tmpurlbuf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1893 c = t;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1894 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1895 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1896 if (!t)
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1897 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1898 t++;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1899 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1900 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1901 } else if (!g_ascii_strncasecmp(c, "mailto:", 7)) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1902 t = c;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1903 while (1) {
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1904 if (badchar(*t) || badentity(t)) {
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1905 if (*(t - 1) == '.')
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1906 t--;
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1907 url_buf = g_strndup(c, t - c);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1908 tmpurlbuf = gaim_unescape_html(url_buf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1909 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>",
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1910 tmpurlbuf, url_buf);
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1911 g_free(url_buf);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1912 g_free(tmpurlbuf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1913 c = t;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1914 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1915 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1916 if (!t)
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1917 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1918 t++;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1919
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1920 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1921 } else if (c != text && (*c == '@')) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1922 int flag;
10905
d41e285af79e [gaim-migrate @ 12634]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10902
diff changeset
1923 GString *gurl_buf = NULL;
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1924 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0";
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1925
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1926 if (strchr(illegal_chars,*(c - 1)) || strchr(illegal_chars, *(c + 1)))
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1927 flag = 0;
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1928 else {
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1929 flag = 1;
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1930 gurl_buf = g_string_new("");
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1931 }
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1932
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1933 t = c;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1934 while (flag) {
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1935 /* iterate backwards grabbing the local part of an email address */
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1936 g = g_utf8_get_char(t);
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1937 if (badchar(*t) || (g >= 127) || (*t == '(') ||
11176
6932df31225f [gaim-migrate @ 13283]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11153
diff changeset
1938 ((*t == ';') && ((t > (text+2) && (!g_ascii_strncasecmp(t - 3, "&lt;", 4) ||
6932df31225f [gaim-migrate @ 13283]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11153
diff changeset
1939 !g_ascii_strncasecmp(t - 3, "&gt;", 4))) ||
6932df31225f [gaim-migrate @ 13283]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11153
diff changeset
1940 (t > (text+4) && (!g_ascii_strncasecmp(t - 5, "&quot;", 6)))))) {
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1941 /* local part will already be part of ret, strip it out */
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1942 ret = g_string_truncate(ret, ret->len - (c - t));
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1943 ret = g_string_append_unichar(ret, g);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1944 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1945 } else {
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1946 g_string_prepend_unichar(gurl_buf, g);
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1947 t = g_utf8_find_prev_char(text, t);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1948 if (t < text) {
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1949 ret = g_string_assign(ret, "");
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1950 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1951 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1952 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1953 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1954
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1955 t = g_utf8_find_next_char(c, NULL);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1956
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1957 while (flag) {
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1958 /* iterate forwards grabbing the domain part of an email address */
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1959 g = g_utf8_get_char(t);
10461
e94802fe3eaf [gaim-migrate @ 11733]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10459
diff changeset
1960 if (badchar(*t) || (g >= 127) || (*t == ')') || badentity(t)) {
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1961 char *d;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1962
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1963 url_buf = g_string_free(gurl_buf, FALSE);
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1964
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1965 /* strip off trailing periods */
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1966 if (strlen(url_buf) > 0) {
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1967 for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--)
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1968 *d = '\0';
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1969 }
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1970
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1971 tmpurlbuf = gaim_unescape_html(url_buf);
9045
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
1972 if (gaim_email_is_valid(tmpurlbuf)) {
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
1973 g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>",
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
1974 tmpurlbuf, url_buf);
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
1975 } else {
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
1976 g_string_append(ret, url_buf);
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
1977 }
10902
acaf64c659b9 [gaim-migrate @ 12624]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10876
diff changeset
1978 g_free(url_buf);
8918
c34e4e6128f3 [gaim-migrate @ 9688]
Luke Schierer <lschiere@pidgin.im>
parents: 8907
diff changeset
1979 g_free(tmpurlbuf);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1980 c = t;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1981
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1982 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1983 } else {
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1984 g_string_append_unichar(gurl_buf, g);
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1985 t = g_utf8_find_next_char(t, NULL);
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1986 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1987 }
9222
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1988 }
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1989
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1990 if(*c == ')' && !inside_html) {
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1991 inside_paren--;
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1992 ret = g_string_append_c(ret, *c);
316b1afb5974 [gaim-migrate @ 10018]
Tim Ringenbach <marv@pidgin.im>
parents: 9220
diff changeset
1993 c++;
7107
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1994 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1995
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1996 if (*c == 0)
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1997 break;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1998
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
1999 ret = g_string_append_c(ret, *c);
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
2000 c++;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
2001
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
2002 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
2003 tmp = ret->str;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
2004 g_string_free(ret, FALSE);
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
2005 return tmp;
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
2006 }
9220c7490cd1 [gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents: 7106
diff changeset
2007
8163
da57fb60680a [gaim-migrate @ 8875]
Ethan Blanton <elb@pidgin.im>
parents: 8125
diff changeset
2008 char *
8442
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2009 gaim_unescape_html(const char *html) {
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2010 char *unescaped = NULL;
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2011
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2012 if (html != NULL) {
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2013 const char *c = html;
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2014 GString *ret = g_string_new("");
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2015 while (*c) {
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2016 if (!strncmp(c, "&amp;", 5)) {
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2017 ret = g_string_append_c(ret, '&');
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2018 c += 5;
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2019 } else if (!strncmp(c, "&lt;", 4)) {
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2020 ret = g_string_append_c(ret, '<');
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2021 c += 4;
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2022 } else if (!strncmp(c, "&gt;", 4)) {
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2023 ret = g_string_append_c(ret, '>');
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2024 c += 4;
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2025 } else if (!strncmp(c, "&quot;", 6)) {
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2026 ret = g_string_append_c(ret, '"');
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2027 c += 6;
8625
afff7b6ae29f [gaim-migrate @ 9376]
Nathan Walp <nwalp@pidgin.im>
parents: 8596
diff changeset
2028 } else if (!strncmp(c, "&apos;", 6)) {
afff7b6ae29f [gaim-migrate @ 9376]
Nathan Walp <nwalp@pidgin.im>
parents: 8596
diff changeset
2029 ret = g_string_append_c(ret, '\'');
afff7b6ae29f [gaim-migrate @ 9376]
Nathan Walp <nwalp@pidgin.im>
parents: 8596
diff changeset
2030 c += 6;
8500
a6748565835a [gaim-migrate @ 9236]
Mark Doliner <mark@kingant.net>
parents: 8442
diff changeset
2031 } else if (!strncmp(c, "<br>", 4)) {
a6748565835a [gaim-migrate @ 9236]
Mark Doliner <mark@kingant.net>
parents: 8442
diff changeset
2032 ret = g_string_append_c(ret, '\n');
a6748565835a [gaim-migrate @ 9236]
Mark Doliner <mark@kingant.net>
parents: 8442
diff changeset
2033 c += 4;
8442
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2034 } else {
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2035 ret = g_string_append_c(ret, *c);
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2036 c++;
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2037 }
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2038 }
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2039
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2040 unescaped = ret->str;
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2041 g_string_free(ret, FALSE);
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2042 }
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2043 return unescaped;
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2044
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2045 }
3d0178c4f390 [gaim-migrate @ 9172]
Luke Schierer <lschiere@pidgin.im>
parents: 8433
diff changeset
2046
9175
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2047 char *
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2048 gaim_markup_slice(const char *str, guint x, guint y)
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2049 {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2050 GString *ret;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2051 GQueue *q;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2052 guint z = 0;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2053 gboolean appended = FALSE;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2054 gunichar c;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2055 char *tag;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2056
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2057 g_return_val_if_fail(x <= y, NULL);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2058
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2059 if (x == y)
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2060 return g_strdup("");
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2061
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2062 ret = g_string_new("");
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2063 q = g_queue_new();
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2064
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2065 while (*str && (z < y)) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2066 c = g_utf8_get_char(str);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2067
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2068 if (c == '<') {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2069 char *end = strchr(str, '>');
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2070
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2071 if (!end) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2072 g_string_free(ret, TRUE);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2073 while ((tag = g_queue_pop_head(q)))
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2074 g_free(tag);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2075 g_queue_free(q);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2076 return NULL;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2077 }
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2078
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2079 if (!g_ascii_strncasecmp(str, "<img ", 5)) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2080 z += strlen("[Image]");
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2081 } else if (!g_ascii_strncasecmp(str, "<br", 3)) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2082 z += 1;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2083 } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2084 z += strlen("\n---\n");
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2085 } else if (!g_ascii_strncasecmp(str, "</", 2)) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2086 /* pop stack */
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2087 char *tmp;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2088
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2089 tmp = g_queue_pop_head(q);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2090 if (tmp)
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2091 g_free(tmp);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2092 /* z += 0; */
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2093 } else {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2094 /* push it unto the stack */
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2095 char *tmp;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2096
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2097 tmp = g_strndup(str, end - str + 1);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2098 g_queue_push_head(q, tmp);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2099 /* z += 0; */
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2100 }
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2101
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2102 if (z == x && !appended) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2103 GList *l = q->tail;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2104
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2105 while (l) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2106 tag = l->data;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2107 g_string_append(ret, tag);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2108 l = l->prev;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2109 }
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2110 appended = TRUE;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2111 } else if (z >= x) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2112 g_string_append_len(ret, str, end - str + 1);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2113 }
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2114
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2115 str = end;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2116 } else if (c == '&') {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2117 char *end = strchr(str, ';');
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2118 if (!end) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2119 g_string_free(ret, TRUE);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2120 while ((tag = g_queue_pop_head(q)))
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2121 g_free(tag);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2122 g_queue_free(q);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2123
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2124 return NULL;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2125 }
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2126
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2127 if (z >= x)
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2128 g_string_append_len(ret, str, end - str + 1);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2129
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2130 z++;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2131 str = end;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2132 } else {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2133 if (z >= x)
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2134 g_string_append_unichar(ret, c);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2135 z++;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2136 }
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2137
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2138 str = g_utf8_next_char(str);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2139 }
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2140
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2141 while ((tag = g_queue_pop_head(q))) {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2142 char *name;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2143
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2144 name = gaim_markup_get_tag_name(tag);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2145 g_string_append_printf(ret, "</%s>", name);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2146 g_free(name);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2147 g_free(tag);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2148 }
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2149
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2150 g_queue_free(q);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2151 return g_string_free(ret, FALSE);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2152 }
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2153
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2154 char *
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2155 gaim_markup_get_tag_name(const char *tag)
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2156 {
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2157 int i;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2158 g_return_val_if_fail(tag != NULL, NULL);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2159 g_return_val_if_fail(*tag == '<', NULL);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2160
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2161 for (i = 1; tag[i]; i++)
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2162 if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/')
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2163 break;
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2164
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2165 return g_strndup(tag, i);
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2166 }
3e2ea5b69605 [gaim-migrate @ 9970]
Tim Ringenbach <marv@pidgin.im>
parents: 9170
diff changeset
2167
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2168 /**************************************************************************
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2169 * Path/Filename Functions
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2170 **************************************************************************/
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2171 const char *
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2172 gaim_home_dir(void)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2173 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2174 #ifndef _WIN32
11665
2350fe2bef87 [gaim-migrate @ 13950]
Ethan Blanton <elb@pidgin.im>
parents: 11656
diff changeset
2175 return g_get_home_dir();
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2176 #else
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2177 return wgaim_data_dir();
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2178 #endif
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2179 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2180
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2181 /* returns a string of the form ~/.gaim, where ~ is replaced by the user's home
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2182 * dir. Note that there is no trailing slash after .gaim. */
10332
8d42237564f6 [gaim-migrate @ 11539]
Mark Doliner <mark@kingant.net>
parents: 10258
diff changeset
2183 const char *
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2184 gaim_user_dir(void)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2185 {
8596
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2186 if (custom_home_dir != NULL && strlen(custom_home_dir) > 0) {
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2187 strcpy ((char*) &home_dir, (char*) &custom_home_dir);
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2188 } else {
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2189 const gchar *hd = gaim_home_dir();
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2190
8596
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2191 if (hd) {
9916
4d17a945aab3 [gaim-migrate @ 10808]
Luke Schierer <lschiere@pidgin.im>
parents: 9818
diff changeset
2192 g_strlcpy((char*) &home_dir, hd, sizeof(home_dir));
4d17a945aab3 [gaim-migrate @ 10808]
Luke Schierer <lschiere@pidgin.im>
parents: 9818
diff changeset
2193 g_strlcat((char*) &home_dir, G_DIR_SEPARATOR_S ".gaim",
4d17a945aab3 [gaim-migrate @ 10808]
Luke Schierer <lschiere@pidgin.im>
parents: 9818
diff changeset
2194 sizeof(home_dir));
8596
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2195 }
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2196 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2197
8596
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2198 return home_dir;
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2199 }
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2200
10871
6240d7fd5b2c [gaim-migrate @ 12558]
Mark Doliner <mark@kingant.net>
parents: 10800
diff changeset
2201 void gaim_util_set_user_dir(const char *dir)
8596
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2202 {
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2203 if (dir != NULL && strlen(dir) > 0) {
9916
4d17a945aab3 [gaim-migrate @ 10808]
Luke Schierer <lschiere@pidgin.im>
parents: 9818
diff changeset
2204 g_strlcpy((char*) &custom_home_dir, dir,
4d17a945aab3 [gaim-migrate @ 10808]
Luke Schierer <lschiere@pidgin.im>
parents: 9818
diff changeset
2205 sizeof(custom_home_dir));
8596
56360561af5e [gaim-migrate @ 9347]
Luke Schierer <lschiere@pidgin.im>
parents: 8577
diff changeset
2206 }
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2207 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2208
7622
072feee65244 [gaim-migrate @ 8246]
Nathan Walp <nwalp@pidgin.im>
parents: 7617
diff changeset
2209 int gaim_build_dir (const char *path, int mode)
7612
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2210 {
11361
e4868370c433 [gaim-migrate @ 13585]
Nathan Walp <nwalp@pidgin.im>
parents: 11237
diff changeset
2211 #if GLIB_CHECK_VERSION(2,8,0)
e4868370c433 [gaim-migrate @ 13585]
Nathan Walp <nwalp@pidgin.im>
parents: 11237
diff changeset
2212 return g_mkdir_with_parents(path, mode);
e4868370c433 [gaim-migrate @ 13585]
Nathan Walp <nwalp@pidgin.im>
parents: 11237
diff changeset
2213 #else
7612
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2214 char *dir, **components, delim[] = { G_DIR_SEPARATOR, '\0' };
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2215 int cur, len;
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2216
7632
925c3ad6531b [gaim-migrate @ 8257]
Nathan Walp <nwalp@pidgin.im>
parents: 7631
diff changeset
2217 g_return_val_if_fail(path != NULL, -1);
7612
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2218
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2219 dir = g_new0(char, strlen(path) + 1);
7622
072feee65244 [gaim-migrate @ 8246]
Nathan Walp <nwalp@pidgin.im>
parents: 7617
diff changeset
2220 components = g_strsplit(path, delim, -1);
7612
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2221 len = 0;
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2222 for (cur = 0; components[cur] != NULL; cur++) {
7633
ee6c31982350 [gaim-migrate @ 8258]
Nathan Walp <nwalp@pidgin.im>
parents: 7632
diff changeset
2223 /* If you don't know what you're doing on both
ee6c31982350 [gaim-migrate @ 8258]
Nathan Walp <nwalp@pidgin.im>
parents: 7632
diff changeset
2224 * win32 and *NIX, stay the hell away from this code */
ee6c31982350 [gaim-migrate @ 8258]
Nathan Walp <nwalp@pidgin.im>
parents: 7632
diff changeset
2225 if(cur > 1)
ee6c31982350 [gaim-migrate @ 8258]
Nathan Walp <nwalp@pidgin.im>
parents: 7632
diff changeset
2226 dir[len++] = G_DIR_SEPARATOR;
7612
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2227 strcpy(dir + len, components[cur]);
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2228 len += strlen(components[cur]);
7633
ee6c31982350 [gaim-migrate @ 8258]
Nathan Walp <nwalp@pidgin.im>
parents: 7632
diff changeset
2229 if(cur == 0)
ee6c31982350 [gaim-migrate @ 8258]
Nathan Walp <nwalp@pidgin.im>
parents: 7632
diff changeset
2230 dir[len++] = G_DIR_SEPARATOR;
7632
925c3ad6531b [gaim-migrate @ 8257]
Nathan Walp <nwalp@pidgin.im>
parents: 7631
diff changeset
2231
925c3ad6531b [gaim-migrate @ 8257]
Nathan Walp <nwalp@pidgin.im>
parents: 7631
diff changeset
2232 if(g_file_test(dir, G_FILE_TEST_IS_DIR)) {
925c3ad6531b [gaim-migrate @ 8257]
Nathan Walp <nwalp@pidgin.im>
parents: 7631
diff changeset
2233 continue;
10948
747ef488c600 [gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10905
diff changeset
2234 #ifdef _WIN32
747ef488c600 [gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10905
diff changeset
2235 /* allow us to create subdirs on UNC paths
747ef488c600 [gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10905
diff changeset
2236 * (\\machinename\path\to\blah)
747ef488c600 [gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10905
diff changeset
2237 * g_file_test() doesn't work on "\\machinename" */
747ef488c600 [gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10905
diff changeset
2238 } else if (cur == 2 && dir[0] == '\\' && dir[1] == '\\'
747ef488c600 [gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10905
diff changeset
2239 && components[cur + 1] != NULL) {
747ef488c600 [gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10905
diff changeset
2240 continue;
747ef488c600 [gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10905
diff changeset
2241 #endif
7632
925c3ad6531b [gaim-migrate @ 8257]
Nathan Walp <nwalp@pidgin.im>
parents: 7631
diff changeset
2242 } else if(g_file_test(dir, G_FILE_TEST_EXISTS)) {
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
2243 gaim_debug_warning("build_dir", "bad path: %s\n", path);
7612
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2244 g_strfreev(components);
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2245 g_free(dir);
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2246 return -1;
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2247 }
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2248
10589
0f7452b1f777 [gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10569
diff changeset
2249 if (g_mkdir(dir, mode) < 0) {
9716
8158d148b3c2 [gaim-migrate @ 10577]
Mark Doliner <mark@kingant.net>
parents: 9696
diff changeset
2250 gaim_debug_warning("build_dir", "mkdir: %s\n", strerror(errno));
7612
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2251 g_strfreev(components);
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2252 g_free(dir);
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2253 return -1;
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2254 }
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2255 }
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2256
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2257 g_strfreev(components);
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2258 g_free(dir);
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2259 return 0;
11361
e4868370c433 [gaim-migrate @ 13585]
Nathan Walp <nwalp@pidgin.im>
parents: 11237
diff changeset
2260 #endif
7612
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2261 }
3ae88e96dde2 [gaim-migrate @ 8236]
Ethan Blanton <elb@pidgin.im>
parents: 7564
diff changeset
2262
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2263 /*
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2264 * This function is long and beautiful, like my--um, yeah. Anyway,
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2265 * it includes lots of error checking so as we don't overwrite
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2266 * people's settings if there is a problem writing the new values.
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2267 */
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2268 gboolean
10415
5b7a74d397cc [gaim-migrate @ 11665]
Mark Doliner <mark@kingant.net>
parents: 10414
diff changeset
2269 gaim_util_write_data_to_file(const char *filename, const char *data, size_t size)
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2270 {
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2271 const char *user_dir = gaim_user_dir();
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2272 gchar *filename_temp, *filename_full;
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2273 FILE *file;
10415
5b7a74d397cc [gaim-migrate @ 11665]
Mark Doliner <mark@kingant.net>
parents: 10414
diff changeset
2274 size_t real_size, byteswritten;
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2275 struct stat st;
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2276
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2277 g_return_val_if_fail(user_dir != NULL, FALSE);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2278
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2279 gaim_debug_info("util", "Writing file %s to directory %s\n",
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2280 filename, user_dir);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2281
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2282 /* Ensure the user directory exists */
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2283 if (!g_file_test(user_dir, G_FILE_TEST_IS_DIR))
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2284 {
10589
0f7452b1f777 [gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10569
diff changeset
2285 if (g_mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2286 {
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2287 gaim_debug_error("util", "Error creating directory %s: %s\n",
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2288 user_dir, strerror(errno));
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2289 return FALSE;
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2290 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2291 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2292
10415
5b7a74d397cc [gaim-migrate @ 11665]
Mark Doliner <mark@kingant.net>
parents: 10414
diff changeset
2293 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", user_dir, filename);
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2294 filename_temp = g_strdup_printf("%s.save", filename_full);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2295
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2296 /* Remove an old temporary file, if one exists */
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2297 if (g_file_test(filename_temp, G_FILE_TEST_EXISTS))
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2298 {
10589
0f7452b1f777 [gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10569
diff changeset
2299 if (g_unlink(filename_temp) == -1)
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2300 {
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2301 gaim_debug_error("util", "Error removing old file %s: %s\n",
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2302 filename_temp, strerror(errno));
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2303 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2304 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2305
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2306 /* Open file */
10589
0f7452b1f777 [gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10569
diff changeset
2307 file = g_fopen(filename_temp, "wb");
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2308 if (file == NULL)
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2309 {
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2310 gaim_debug_error("util", "Error opening file %s for writing: %s\n",
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2311 filename_temp, strerror(errno));
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2312 g_free(filename_full);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2313 g_free(filename_temp);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2314 return FALSE;
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2315 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2316
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2317 /* Write to file */
10415
5b7a74d397cc [gaim-migrate @ 11665]
Mark Doliner <mark@kingant.net>
parents: 10414
diff changeset
2318 real_size = (size == -1) ? strlen(data) : size;
5b7a74d397cc [gaim-migrate @ 11665]
Mark Doliner <mark@kingant.net>
parents: 10414
diff changeset
2319 byteswritten = fwrite(data, 1, real_size, file);
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2320
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2321 /* Close file */
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2322 if (fclose(file) != 0)
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2323 {
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2324 gaim_debug_error("util", "Error closing file %s: %s\n",
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2325 filename_temp, strerror(errno));
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2326 g_free(filename_full);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2327 g_free(filename_temp);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2328 return FALSE;
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2329 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2330
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2331 /* Ensure the file is the correct size */
10415
5b7a74d397cc [gaim-migrate @ 11665]
Mark Doliner <mark@kingant.net>
parents: 10414
diff changeset
2332 if (byteswritten != real_size)
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2333 {
11656
f9c5480ad0ce [gaim-migrate @ 13940]
Richard Laager <rlaager@wiktel.com>
parents: 11552
diff changeset
2334 gaim_debug_error("util", "Error writing to file %s: Wrote %" G_GSIZE_FORMAT " bytes "
f9c5480ad0ce [gaim-migrate @ 13940]
Richard Laager <rlaager@wiktel.com>
parents: 11552
diff changeset
2335 "but should have written %" G_GSIZE_FORMAT "; is your disk full?\n",
10415
5b7a74d397cc [gaim-migrate @ 11665]
Mark Doliner <mark@kingant.net>
parents: 10414
diff changeset
2336 filename_temp, byteswritten, real_size);
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2337 g_free(filename_full);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2338 g_free(filename_temp);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2339 return FALSE;
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2340 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2341 /* Use stat to be absolutely sure. */
10589
0f7452b1f777 [gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10569
diff changeset
2342 if ((g_stat(filename_temp, &st) == -1) || (st.st_size != real_size))
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2343 {
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2344 gaim_debug_error("util", "Error writing data to file %s: "
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2345 "Incomplete file written; is your disk full?\n",
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2346 filename_temp);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2347 g_free(filename_full);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2348 g_free(filename_temp);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2349 return FALSE;
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2350 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2351
12622
b7860c9902f2 [gaim-migrate @ 14958]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12620
diff changeset
2352 #ifndef _WIN32
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2353 /* Set file permissions */
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2354 if (chmod(filename_temp, S_IRUSR | S_IWUSR) == -1)
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2355 {
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2356 gaim_debug_error("util", "Error setting permissions of file %s: %s\n",
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2357 filename_temp, strerror(errno));
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2358 }
12622
b7860c9902f2 [gaim-migrate @ 14958]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12620
diff changeset
2359 #endif
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2360
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2361 /* Remove the old file, if it exists */
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2362 if (g_file_test(filename_full, G_FILE_TEST_EXISTS))
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2363 {
10589
0f7452b1f777 [gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10569
diff changeset
2364 if (g_unlink(filename_full) == -1)
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2365 {
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2366 gaim_debug_error("util", "Error removing old file %s: %s\n",
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2367 filename_full, strerror(errno));
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2368 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2369 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2370
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2371 /* Rename to the REAL name */
10589
0f7452b1f777 [gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10569
diff changeset
2372 if (g_rename(filename_temp, filename_full) == -1)
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2373 {
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2374 gaim_debug_error("util", "Error renaming %s to %s: %s\n",
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2375 filename_temp, filename_full, strerror(errno));
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2376 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2377
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2378 g_free(filename_full);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2379 g_free(filename_temp);
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2380
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2381 return TRUE;
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2382 }
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2383
10425
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2384 xmlnode *
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2385 gaim_util_read_xml_from_file(const char *filename, const char *description)
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2386 {
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2387 const char *user_dir = gaim_user_dir();
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2388 gchar *filename_full;
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2389 GError *error;
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2390 gchar *contents = NULL;
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2391 gsize length;
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2392 xmlnode *node = NULL;
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2393
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2394 g_return_val_if_fail(user_dir != NULL, NULL);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2395
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2396 gaim_debug_info("util", "Reading file %s from directory %s\n",
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2397 filename, user_dir);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2398
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2399 filename_full = g_build_filename(user_dir, filename, NULL);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2400
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2401 if (!g_file_test(filename_full, G_FILE_TEST_EXISTS))
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2402 {
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2403 gaim_debug_info("util", "File %s does not exist (this is not "
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2404 "necessarily an error)\n", filename_full);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2405 g_free(filename_full);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2406 return NULL;
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2407 }
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2408
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2409 if (!g_file_get_contents(filename_full, &contents, &length, &error))
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2410 {
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2411 gaim_debug_error("util", "Error reading file %s: %s\n",
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2412 filename_full, error->message);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2413 g_error_free(error);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2414 }
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2415
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2416 if ((contents != NULL) && (length > 0))
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2417 {
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2418 node = xmlnode_from_str(contents, length);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2419
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2420 /* If we were unable to parse the file then save its contents to a backup file */
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2421 if (node == NULL)
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2422 {
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2423 gchar *filename_temp;
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2424
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2425 filename_temp = g_strdup_printf("%s~", filename);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2426 gaim_debug_error("util", "Error parsing file %s. Rrenaming old "
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2427 "file to %s\n", filename_full, filename_temp);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2428 gaim_util_write_data_to_file(filename_temp, contents, length);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2429 g_free(filename_temp);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2430 }
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2431
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2432 g_free(contents);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2433 }
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2434
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2435 /* If we could not parse the file then show the user an error message */
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2436 if (node == NULL)
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2437 {
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2438 gchar *title, *msg;
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2439 title = g_strdup_printf(_("Error Reading %s"), filename);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2440 msg = g_strdup_printf(_("An error was encountered reading your "
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2441 "%s. They have not been loaded, and the old file "
11493
3fe6b56a39cc [gaim-migrate @ 13736]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11407
diff changeset
2442 "has been renamed to %s~."), description, filename_full);
10425
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2443 gaim_notify_error(NULL, NULL, title, msg);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2444 g_free(title);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2445 g_free(msg);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2446 }
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2447
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2448 g_free(filename_full);
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2449
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2450 return node;
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2451 }
9903182f2aac [gaim-migrate @ 11677]
Mark Doliner <mark@kingant.net>
parents: 10415
diff changeset
2452
10414
26eac2362c32 [gaim-migrate @ 11664]
Mark Doliner <mark@kingant.net>
parents: 10372
diff changeset
2453 /*
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2454 * Like mkstemp() but returns a file pointer, uses a pre-set template,
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2455 * uses the semantics of tempnam() for the directory to use and allocates
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2456 * the space for the filepath.
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2457 *
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2458 * Caller is responsible for closing the file and removing it when done,
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2459 * as well as freeing the space pointed-to by "path" with g_free().
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2460 *
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2461 * Returns NULL on failure and cleans up after itself if so.
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2462 */
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2463 static const char *gaim_mkstemp_templ = {"gaimXXXXXX"};
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2464
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2465 FILE *
10203
7ff9b8b22e7d [gaim-migrate @ 11324]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 10112
diff changeset
2466 gaim_mkstemp(char **fpath, gboolean binary)
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2467 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2468 const gchar *tmpdir;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2469 #ifndef _WIN32
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2470 int fd;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2471 #endif
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2472 FILE *fp = NULL;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2473
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2474 g_return_val_if_fail(fpath != NULL, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2475
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2476 if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) {
9427
66b3f54527e6 [gaim-migrate @ 10245]
Luke Schierer <lschiere@pidgin.im>
parents: 9426
diff changeset
2477 if((*fpath = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", tmpdir, gaim_mkstemp_templ)) != NULL) {
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2478 #ifdef _WIN32
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2479 char* result = _mktemp( *fpath );
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2480 if( result == NULL )
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2481 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp",
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2482 "Problem creating the template\n");
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2483 else
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2484 {
10589
0f7452b1f777 [gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10569
diff changeset
2485 if( (fp = g_fopen( result, binary?"wb+":"w+")) == NULL ) {
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2486 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp",
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2487 "Couldn't fopen() %s\n", result);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2488 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2489 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2490 #else
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2491 if((fd = mkstemp(*fpath)) == -1) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2492 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp",
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2493 "Couldn't make \"%s\", error: %d\n",
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2494 *fpath, errno);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2495 } else {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2496 if((fp = fdopen(fd, "r+")) == NULL) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2497 close(fd);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2498 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp",
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2499 "Couldn't fdopen(), error: %d\n", errno);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2500 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2501 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2502 #endif
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2503 if(!fp) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2504 g_free(*fpath);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2505 *fpath = NULL;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2506 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2507 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2508 } else {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2509 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp",
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2510 "g_get_tmp_dir() failed!");
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2511 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2512
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2513 return fp;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2514 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2515
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2516 gboolean
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2517 gaim_program_is_valid(const char *program)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2518 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2519 GError *error = NULL;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2520 char **argv;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2521 gchar *progname;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2522 gboolean is_valid = FALSE;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2523
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2524 g_return_val_if_fail(program != NULL, FALSE);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2525 g_return_val_if_fail(*program != '\0', FALSE);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2526
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2527 if (!g_shell_parse_argv(program, NULL, &argv, &error)) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2528 gaim_debug(GAIM_DEBUG_ERROR, "program_is_valid",
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2529 "Could not parse program '%s': %s\n",
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2530 program, error->message);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2531 g_error_free(error);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2532 return FALSE;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2533 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2534
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2535 if (argv == NULL) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2536 return FALSE;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2537 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2538
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2539 progname = g_find_program_in_path(argv[0]);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2540 is_valid = (progname != NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2541
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2542 g_strfreev(argv);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2543 g_free(progname);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2544
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2545 return is_valid;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2546 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2547
11878
f672349cfc1c [gaim-migrate @ 14169]
Sean Egan <seanegan@gmail.com>
parents: 11780
diff changeset
2548
f672349cfc1c [gaim-migrate @ 14169]
Sean Egan <seanegan@gmail.com>
parents: 11780
diff changeset
2549 gboolean
f672349cfc1c [gaim-migrate @ 14169]
Sean Egan <seanegan@gmail.com>
parents: 11780
diff changeset
2550 gaim_running_gnome(void)
f672349cfc1c [gaim-migrate @ 14169]
Sean Egan <seanegan@gmail.com>
parents: 11780
diff changeset
2551 {
f672349cfc1c [gaim-migrate @ 14169]
Sean Egan <seanegan@gmail.com>
parents: 11780
diff changeset
2552 gchar *tmp = g_find_program_in_path("gnome-open");
12483
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2553
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2554 if (tmp == NULL)
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2555 return FALSE;
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2556 g_free(tmp);
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2557
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2558 return (g_getenv("GNOME_DESKTOP_SESSION_ID") != NULL);
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2559 }
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2560
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2561 gboolean
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2562 gaim_running_kde(void)
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2563 {
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2564 gchar *tmp = g_find_program_in_path("kfmclient");
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2565 const char *session;
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2566
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2567 if (tmp == NULL)
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2568 return FALSE;
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2569 g_free(tmp);
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2570
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2571 session = g_getenv("KDE_FULL_SESSION");
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2572 if (session != NULL && !strcmp(session, "true"))
11878
f672349cfc1c [gaim-migrate @ 14169]
Sean Egan <seanegan@gmail.com>
parents: 11780
diff changeset
2573 return TRUE;
12483
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2574
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2575 /* If you run Gaim from Konsole under !KDE, this will provide a
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2576 * a false positive. Since we do the GNOME checks first, this is
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2577 * only a problem if you're running something !(KDE || GNOME) and
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2578 * you run Gaim from Konsole. This really shouldn't be a problem. */
54448bd2ccc7 [gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents: 12110
diff changeset
2579 return ((g_getenv("KDEDIR") != NULL) || g_getenv("KDEDIRS") != NULL);
11878
f672349cfc1c [gaim-migrate @ 14169]
Sean Egan <seanegan@gmail.com>
parents: 11780
diff changeset
2580 }
f672349cfc1c [gaim-migrate @ 14169]
Sean Egan <seanegan@gmail.com>
parents: 11780
diff changeset
2581
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2582 char *
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2583 gaim_fd_get_ip(int fd)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2584 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2585 struct sockaddr addr;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2586 socklen_t namelen = sizeof(addr);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2587
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2588 g_return_val_if_fail(fd != 0, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2589
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2590 if (getsockname(fd, &addr, &namelen))
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2591 return NULL;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2592
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2593 return g_strdup(inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr));
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2594 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2595
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2596
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2597 /**************************************************************************
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2598 * String Functions
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2599 **************************************************************************/
7162
d14e026611c0 [gaim-migrate @ 7729]
Nathan Walp <nwalp@pidgin.im>
parents: 7134
diff changeset
2600 const char *
11153
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2601 gaim_normalize(const GaimAccount *account, const char *str)
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2602 {
7261
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2603 GaimPlugin *prpl = NULL;
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2604 GaimPluginProtocolInfo *prpl_info = NULL;
7430
783eea64614c [gaim-migrate @ 8034]
Nathan Walp <nwalp@pidgin.im>
parents: 7386
diff changeset
2605 const char *ret = NULL;
7261
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2606
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2607 if(account)
7956
1b8261f374ea [gaim-migrate @ 8631]
Nathan Walp <nwalp@pidgin.im>
parents: 7889
diff changeset
2608 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account));
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2609
7261
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2610 if(prpl)
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2611 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl);
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2612
7430
783eea64614c [gaim-migrate @ 8034]
Nathan Walp <nwalp@pidgin.im>
parents: 7386
diff changeset
2613 if(prpl_info && prpl_info->normalize)
11153
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2614 ret = prpl_info->normalize(account, str);
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2615
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2616 if(!ret)
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2617 {
7261
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2618 static char buf[BUF_LEN];
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2619 char *tmp;
11153
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2620
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2621 tmp = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT);
7261
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2622 g_snprintf(buf, sizeof(buf), "%s", tmp);
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2623 g_free(tmp);
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2624
7430
783eea64614c [gaim-migrate @ 8034]
Nathan Walp <nwalp@pidgin.im>
parents: 7386
diff changeset
2625 ret = buf;
7261
1930e3d00ecd [gaim-migrate @ 7838]
Nathan Walp <nwalp@pidgin.im>
parents: 7162
diff changeset
2626 }
11153
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2627
7430
783eea64614c [gaim-migrate @ 8034]
Nathan Walp <nwalp@pidgin.im>
parents: 7386
diff changeset
2628 return ret;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2629 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2630
11153
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2631 /*
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2632 * You probably don't want to call this directly, it is
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2633 * mainly for use as a PRPL callback function. See the
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2634 * comments in util.h.
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2635 */
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2636 const char *
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2637 gaim_normalize_nocase(const GaimAccount *account, const char *str)
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2638 {
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2639 static char buf[BUF_LEN];
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2640 char *tmp1, *tmp2;
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2641
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2642 g_return_val_if_fail(str != NULL, NULL);
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2643
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2644 tmp1 = g_utf8_strdown(str, -1);
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2645 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT);
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2646 g_snprintf(buf, sizeof(buf), "%s", tmp2);
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2647 g_free(tmp2);
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2648 g_free(tmp1);
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2649
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2650 return buf;
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2651 }
8e600ee6ec61 [gaim-migrate @ 13235]
Mark Doliner <mark@kingant.net>
parents: 11142
diff changeset
2652
8700
ddd2bf87fe8d [gaim-migrate @ 9453]
Mark Doliner <mark@kingant.net>
parents: 8686
diff changeset
2653 gchar *
ddd2bf87fe8d [gaim-migrate @ 9453]
Mark Doliner <mark@kingant.net>
parents: 8686
diff changeset
2654 gaim_str_sub_away_formatters(const char *str, const char *name)
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2655 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2656 char *c;
11237
237d65590d2b [gaim-migrate @ 13388]
Mark Doliner <mark@kingant.net>
parents: 11176
diff changeset
2657 GString *cpy;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2658 time_t t;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2659 struct tm *tme;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2660
8700
ddd2bf87fe8d [gaim-migrate @ 9453]
Mark Doliner <mark@kingant.net>
parents: 8686
diff changeset
2661 g_return_val_if_fail(str != NULL, NULL);
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2662 g_return_val_if_fail(name != NULL, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2663
11237
237d65590d2b [gaim-migrate @ 13388]
Mark Doliner <mark@kingant.net>
parents: 11176
diff changeset
2664 /* Create an empty GString that is hopefully big enough for most messages */
237d65590d2b [gaim-migrate @ 13388]
Mark Doliner <mark@kingant.net>
parents: 11176
diff changeset
2665 cpy = g_string_sized_new(1024);
8700
ddd2bf87fe8d [gaim-migrate @ 9453]
Mark Doliner <mark@kingant.net>
parents: 8686
diff changeset
2666
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2667 t = time(NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2668 tme = localtime(&t);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2669
8700
ddd2bf87fe8d [gaim-migrate @ 9453]
Mark Doliner <mark@kingant.net>
parents: 8686
diff changeset
2670 c = (char *)str;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2671 while (*c) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2672 switch (*c) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2673 case '%':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2674 if (*(c + 1)) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2675 switch (*(c + 1)) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2676 case 'n':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2677 /* append name */
11237
237d65590d2b [gaim-migrate @ 13388]
Mark Doliner <mark@kingant.net>
parents: 11176
diff changeset
2678 g_string_append(cpy, name);
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2679 c++;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2680 break;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2681 case 'd':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2682 /* append date */
13120
f62d14ba98f6 [gaim-migrate @ 15482]
Richard Laager <rlaager@wiktel.com>
parents: 13119
diff changeset
2683 g_string_append(cpy, gaim_date_format_short(tme));
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2684 c++;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2685 break;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2686 case 't':
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2687 /* append time */
13120
f62d14ba98f6 [gaim-migrate @ 15482]
Richard Laager <rlaager@wiktel.com>
parents: 13119
diff changeset
2688 g_string_append(cpy, gaim_time_format(tme));
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2689 c++;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2690 break;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2691 default:
11237
237d65590d2b [gaim-migrate @ 13388]
Mark Doliner <mark@kingant.net>
parents: 11176
diff changeset
2692 g_string_append_c(cpy, *c);
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2693 }
10614
522b719ea774 [gaim-migrate @ 12074]
Mark Doliner <mark@kingant.net>
parents: 10593
diff changeset
2694 } else {
11237
237d65590d2b [gaim-migrate @ 13388]
Mark Doliner <mark@kingant.net>
parents: 11176
diff changeset
2695 g_string_append_c(cpy, *c);
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2696 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2697 break;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2698 default:
11237
237d65590d2b [gaim-migrate @ 13388]
Mark Doliner <mark@kingant.net>
parents: 11176
diff changeset
2699 g_string_append_c(cpy, *c);
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2700 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2701 c++;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2702 }
11237
237d65590d2b [gaim-migrate @ 13388]
Mark Doliner <mark@kingant.net>
parents: 11176
diff changeset
2703
237d65590d2b [gaim-migrate @ 13388]
Mark Doliner <mark@kingant.net>
parents: 11176
diff changeset
2704 return g_string_free(cpy, FALSE);
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2705 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2706
8341
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2707 gchar *
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2708 gaim_strdup_withhtml(const gchar *src)
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2709 {
8341
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2710 gulong destsize, i, j;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2711 gchar *dest;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2712
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2713 g_return_val_if_fail(src != NULL, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2714
8341
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2715 /* New length is (length of src) + (number of \n's * 3) + 1 */
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2716 for (i = 0, j = 0; src[i] != '\0'; i++)
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2717 if (src[i] == '\n')
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2718 j++;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2719
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2720 destsize = i + (j * 3) + 1;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2721 dest = g_malloc(destsize);
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2722
8341
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2723 /* Copy stuff, ignoring \r's, because they are dumb */
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2724 for (i = 0, j = 0; src[i] != '\0'; i++) {
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2725 if (src[i] == '\n') {
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2726 strcpy(&dest[j], "<BR>");
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2727 j += 4;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2728 } else if (src[i] != '\r')
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2729 dest[j++] = src[i];
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2730 }
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2731
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2732 dest[destsize-1] = '\0';
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2733
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2734 return dest;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2735 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2736
7628
e293d0c42ccb [gaim-migrate @ 8252]
Ethan Blanton <elb@pidgin.im>
parents: 7622
diff changeset
2737 gboolean
e293d0c42ccb [gaim-migrate @ 8252]
Ethan Blanton <elb@pidgin.im>
parents: 7622
diff changeset
2738 gaim_str_has_prefix(const char *s, const char *p)
e293d0c42ccb [gaim-migrate @ 8252]
Ethan Blanton <elb@pidgin.im>
parents: 7622
diff changeset
2739 {
11780
8cb75ba77f9d [gaim-migrate @ 14071]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11665
diff changeset
2740 #if GLIB_CHECK_VERSION(2,2,0)
8cb75ba77f9d [gaim-migrate @ 14071]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11665
diff changeset
2741 return g_str_has_prefix(s, p);
8cb75ba77f9d [gaim-migrate @ 14071]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11665
diff changeset
2742 #else
13095
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2743 g_return_val_if_fail(s != NULL, FALSE);
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2744 g_return_val_if_fail(p != NULL, FALSE);
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2745
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2746 return (!strncmp(s, p, strlen(p)));
11780
8cb75ba77f9d [gaim-migrate @ 14071]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11665
diff changeset
2747 #endif
7628
e293d0c42ccb [gaim-migrate @ 8252]
Ethan Blanton <elb@pidgin.im>
parents: 7622
diff changeset
2748 }
e293d0c42ccb [gaim-migrate @ 8252]
Ethan Blanton <elb@pidgin.im>
parents: 7622
diff changeset
2749
e293d0c42ccb [gaim-migrate @ 8252]
Ethan Blanton <elb@pidgin.im>
parents: 7622
diff changeset
2750 gboolean
e293d0c42ccb [gaim-migrate @ 8252]
Ethan Blanton <elb@pidgin.im>
parents: 7622
diff changeset
2751 gaim_str_has_suffix(const char *s, const char *x)
e293d0c42ccb [gaim-migrate @ 8252]
Ethan Blanton <elb@pidgin.im>
parents: 7622
diff changeset
2752 {
11780
8cb75ba77f9d [gaim-migrate @ 14071]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11665
diff changeset
2753 #if GLIB_CHECK_VERSION(2,2,0)
8cb75ba77f9d [gaim-migrate @ 14071]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11665
diff changeset
2754 return g_str_has_suffix(s, x);
8cb75ba77f9d [gaim-migrate @ 14071]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11665
diff changeset
2755 #else
13095
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2756 int off;
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2757
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2758 g_return_val_if_fail(s != NULL, FALSE);
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2759 g_return_val_if_fail(x != NULL, FALSE);
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2760
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2761 off = strlen(s) - strlen(x);
5a7c4570d768 [gaim-migrate @ 15457]
Richard Laager <rlaager@wiktel.com>
parents: 13089
diff changeset
2762 return (off >= 0 && !strcmp(s + off, x));
11780
8cb75ba77f9d [gaim-migrate @ 14071]
Stu Tomlinson <stu@nosnilmot.com>
parents: 11665
diff changeset
2763 #endif
7628
e293d0c42ccb [gaim-migrate @ 8252]
Ethan Blanton <elb@pidgin.im>
parents: 7622
diff changeset
2764 }
e293d0c42ccb [gaim-migrate @ 8252]
Ethan Blanton <elb@pidgin.im>
parents: 7622
diff changeset
2765
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2766 char *
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2767 gaim_str_add_cr(const char *text)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2768 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2769 char *ret = NULL;
7631
ea2d07ad05a9 [gaim-migrate @ 8255]
Nathan Walp <nwalp@pidgin.im>
parents: 7628
diff changeset
2770 int count = 0, j;
ea2d07ad05a9 [gaim-migrate @ 8255]
Nathan Walp <nwalp@pidgin.im>
parents: 7628
diff changeset
2771 guint i;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2772
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2773 g_return_val_if_fail(text != NULL, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2774
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2775 if (text[0] == '\n')
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2776 count++;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2777 for (i = 1; i < strlen(text); i++)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2778 if (text[i] == '\n' && text[i - 1] != '\r')
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2779 count++;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2780
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2781 if (count == 0)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2782 return g_strdup(text);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2783
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2784 ret = g_malloc0(strlen(text) + count + 1);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2785
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2786 i = 0; j = 0;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2787 if (text[i] == '\n')
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2788 ret[j++] = '\r';
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2789 ret[j++] = text[i++];
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2790 for (; i < strlen(text); i++) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2791 if (text[i] == '\n' && text[i - 1] != '\r')
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2792 ret[j++] = '\r';
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2793 ret[j++] = text[i];
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2794 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2795
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2796 gaim_debug_misc("gaim_str_add_cr", "got: %s, leaving with %s\n",
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2797 text, ret);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2798
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2799 return ret;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2800 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2801
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2802 void
11920
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2803 gaim_str_strip_char(char *text, char thechar)
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2804 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2805 int i, j;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2806
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2807 g_return_if_fail(text != NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2808
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2809 for (i = 0, j = 0; text[i]; i++)
11920
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2810 if (text[i] != thechar)
11142
ee059fe9b362 [gaim-migrate @ 13206]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
2811 text[j++] = text[i];
ee059fe9b362 [gaim-migrate @ 13206]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
2812
ee059fe9b362 [gaim-migrate @ 13206]
Mark Doliner <mark@kingant.net>
parents: 11137
diff changeset
2813 text[j++] = '\0';
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2814 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2815
11920
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2816 void
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2817 gaim_util_chrreplace(char *string, char delimiter,
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2818 char replacement)
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2819 {
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2820 int i = 0;
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2821
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2822 g_return_if_fail(string != NULL);
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2823
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2824 while (string[i] != '\0')
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2825 {
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2826 if (string[i] == delimiter)
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2827 string[i] = replacement;
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2828 i++;
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2829 }
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2830 }
52f27ffe68a5 [gaim-migrate @ 14211]
Mark Doliner <mark@kingant.net>
parents: 11878
diff changeset
2831
8341
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2832 gchar *
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2833 gaim_strreplace(const char *string, const char *delimiter,
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2834 const char *replacement)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2835 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2836 gchar **split;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2837 gchar *ret;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2838
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2839 g_return_val_if_fail(string != NULL, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2840 g_return_val_if_fail(delimiter != NULL, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2841 g_return_val_if_fail(replacement != NULL, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2842
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2843 split = g_strsplit(string, delimiter, 0);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2844 ret = g_strjoinv(replacement, split);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2845 g_strfreev(split);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2846
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2847 return ret;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2848 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2849
8341
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2850 gchar *
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2851 gaim_strcasereplace(const char *string, const char *delimiter,
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2852 const char *replacement)
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2853 {
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2854 gchar *ret;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2855 int length_del, length_rep, i, j;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2856
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2857 g_return_val_if_fail(string != NULL, NULL);
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2858 g_return_val_if_fail(delimiter != NULL, NULL);
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2859 g_return_val_if_fail(replacement != NULL, NULL);
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2860
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2861 length_del = strlen(delimiter);
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2862 length_rep = strlen(replacement);
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2863
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2864 /* Count how many times the delimiter appears */
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2865 i = 0; /* position in the source string */
8735
92cbf9713795 [gaim-migrate @ 9490]
Christian Hammond <chipx86@chipx86.com>
parents: 8700
diff changeset
2866 j = 0; /* number of occurrences of "delimiter" */
8341
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2867 while (string[i] != '\0') {
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2868 if (!strncasecmp(&string[i], delimiter, length_del)) {
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2869 i += length_del;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2870 j += length_rep;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2871 } else {
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2872 i++;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2873 j++;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2874 }
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2875 }
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2876
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2877 ret = g_malloc(j+1);
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2878
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2879 i = 0; /* position in the source string */
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2880 j = 0; /* position in the destination string */
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2881 while (string[i] != '\0') {
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2882 if (!strncasecmp(&string[i], delimiter, length_del)) {
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2883 strncpy(&ret[j], replacement, length_rep);
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2884 i += length_del;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2885 j += length_rep;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2886 } else {
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2887 ret[j] = string[i];
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2888 i++;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2889 j++;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2890 }
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2891 }
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2892
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2893 ret[j] = '\0';
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2894
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2895 return ret;
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2896 }
fec4c1fb2ac8 [gaim-migrate @ 9065]
Mark Doliner <mark@kingant.net>
parents: 8163
diff changeset
2897
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2898 const char *
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2899 gaim_strcasestr(const char *haystack, const char *needle)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2900 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2901 size_t hlen, nlen;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2902 const char *tmp, *ret;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2903
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2904 g_return_val_if_fail(haystack != NULL, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2905 g_return_val_if_fail(needle != NULL, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2906
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2907 hlen = strlen(haystack);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2908 nlen = strlen(needle);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2909 tmp = haystack,
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2910 ret = NULL;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2911
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2912 g_return_val_if_fail(hlen > 0, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2913 g_return_val_if_fail(nlen > 0, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2914
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2915 while (*tmp && !ret) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2916 if (!g_ascii_strncasecmp(needle, tmp, nlen))
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2917 ret = tmp;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2918 else
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2919 tmp++;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2920 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2921
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2922 return ret;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2923 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2924
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2925 char *
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2926 gaim_str_size_to_units(size_t size)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2927 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2928 static const char *size_str[4] = { "bytes", "KB", "MB", "GB" };
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2929 float size_mag;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2930 int size_index = 0;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2931
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2932 if (size == -1) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2933 return g_strdup(_("Calculating..."));
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2934 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2935 else if (size == 0) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2936 return g_strdup(_("Unknown."));
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2937 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2938 else {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2939 size_mag = (float)size;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2940
10569
12349012610b [gaim-migrate @ 11957]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10506
diff changeset
2941 while ((size_index < 3) && (size_mag > 1024)) {
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2942 size_mag /= 1024;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2943 size_index++;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2944 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2945
10103
a9521c791afe [gaim-migrate @ 11130]
Luke Schierer <lschiere@pidgin.im>
parents: 9940
diff changeset
2946 if (size_index == 0) {
11656
f9c5480ad0ce [gaim-migrate @ 13940]
Richard Laager <rlaager@wiktel.com>
parents: 11552
diff changeset
2947 return g_strdup_printf("%" G_GSIZE_FORMAT " %s", size, size_str[size_index]);
10103
a9521c791afe [gaim-migrate @ 11130]
Luke Schierer <lschiere@pidgin.im>
parents: 9940
diff changeset
2948 } else {
a9521c791afe [gaim-migrate @ 11130]
Luke Schierer <lschiere@pidgin.im>
parents: 9940
diff changeset
2949 return g_strdup_printf("%.2f %s", size_mag, size_str[size_index]);
a9521c791afe [gaim-migrate @ 11130]
Luke Schierer <lschiere@pidgin.im>
parents: 9940
diff changeset
2950 }
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2951 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2952 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2953
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2954 char *
9233
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2955 gaim_str_seconds_to_string(guint secs)
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2956 {
13097
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2957 char *ret = NULL;
9233
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2958 guint days, hrs, mins;
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2959
9424
79c4acbac4cd [gaim-migrate @ 10242]
Luke Schierer <lschiere@pidgin.im>
parents: 9341
diff changeset
2960 if (secs < 60)
79c4acbac4cd [gaim-migrate @ 10242]
Luke Schierer <lschiere@pidgin.im>
parents: 9341
diff changeset
2961 {
13097
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2962 return g_strdup_printf(ngettext("%d second", "%d seconds", secs), secs);
9424
79c4acbac4cd [gaim-migrate @ 10242]
Luke Schierer <lschiere@pidgin.im>
parents: 9341
diff changeset
2963 }
79c4acbac4cd [gaim-migrate @ 10242]
Luke Schierer <lschiere@pidgin.im>
parents: 9341
diff changeset
2964
9233
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2965 days = secs / (60 * 60 * 24);
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2966 secs = secs % (60 * 60 * 24);
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2967 hrs = secs / (60 * 60);
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2968 secs = secs % (60 * 60);
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2969 mins = secs / 60;
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2970 secs = secs % 60;
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2971
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2972 if (days > 0)
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2973 {
13097
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2974 ret = g_strdup_printf(ngettext("%d day", "%d days", days), days);
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2975 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
2976
9233
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2977 if (hrs > 0)
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2978 {
13097
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2979 if (ret != NULL)
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2980 {
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2981 char *tmp = g_strdup_printf(
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2982 ngettext("%s, %d hour", "%s, %d hours", hrs),
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2983 ret, hrs);
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2984 g_free(ret);
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2985 ret = tmp;
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2986 }
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2987 else
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2988 ret = g_strdup_printf(ngettext("%d hour", "%d hours", hrs), hrs);
9233
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2989 }
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2990
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2991 if (mins > 0)
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
2992 {
13097
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2993 if (ret != NULL)
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2994 {
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2995 char *tmp = g_strdup_printf(
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2996 ngettext("%s, %d minute", "%s, %d minutes", mins),
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2997 ret, mins);
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2998 g_free(ret);
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
2999 ret = tmp;
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
3000 }
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
3001 else
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
3002 ret = g_strdup_printf(ngettext("%d minute", "%d minutes", mins), mins);
9233
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
3003 }
0c352d0e4ddc [gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents: 9230
diff changeset
3004
13097
f1bf8989bbf2 [gaim-migrate @ 15459]
Richard Laager <rlaager@wiktel.com>
parents: 13095
diff changeset
3005 return ret;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3006 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3007
9277
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3008
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3009 char *
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3010 gaim_str_binary_to_ascii(const unsigned char *binary, guint len)
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3011 {
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3012 GString *ret;
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3013 guint i;
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3014
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3015 g_return_val_if_fail(len > 0, NULL);
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3016
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3017 ret = g_string_sized_new(len);
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3018
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3019 for (i = 0; i < len; i++)
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3020 if (binary[i] < 32 || binary[i] > 126)
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3021 g_string_append_printf(ret, "\\x%02hhx", binary[i]);
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3022 else if (binary[i] == '\\')
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3023 g_string_append(ret, "\\\\");
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3024 else
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3025 g_string_append_c(ret, binary[i]);
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3026
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3027 return g_string_free(ret, FALSE);
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3028 }
185fbd8b5998 [gaim-migrate @ 10080]
Tim Ringenbach <marv@pidgin.im>
parents: 9273
diff changeset
3029
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3030 /**************************************************************************
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3031 * URI/URL Functions
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3032 **************************************************************************/
7095
c8bf2da398e3 [gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents: 7094
diff changeset
3033 gboolean
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3034 gaim_url_parse(const char *url, char **ret_host, int *ret_port,
9227
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3035 char **ret_path, char **ret_user, char **ret_passwd)
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3036 {
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3037 char scan_info[255];
8118
52089b055c12 [gaim-migrate @ 8822]
Luke Schierer <lschiere@pidgin.im>
parents: 8088
diff changeset
3038 char port_str[6];
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3039 int f;
9341
cf5b5b63228d [gaim-migrate @ 10149]
Tim Ringenbach <marv@pidgin.im>
parents: 9311
diff changeset
3040 const char *at, *slash;
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3041 const char *turl;
9227
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3042 char host[256], path[256], user[256], passwd[256];
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3043 int port = 0;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3044 /* hyphen at end includes it in control set */
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3045 static char addr_ctrl[] = "A-Za-z0-9.-";
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3046 static char port_ctrl[] = "0-9";
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3047 static char page_ctrl[] = "A-Za-z0-9.~_/:*!@&%%?=+^-";
9227
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3048 static char user_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-";
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3049 static char passwd_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-";
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3050
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3051 g_return_val_if_fail(url != NULL, FALSE);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3052
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3053 if ((turl = strstr(url, "http://")) != NULL ||
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3054 (turl = strstr(url, "HTTP://")) != NULL)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3055 {
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3056 turl += 7;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3057 url = turl;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3058 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3059
9227
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3060 /* parse out authentication information if supplied */
9341
cf5b5b63228d [gaim-migrate @ 10149]
Tim Ringenbach <marv@pidgin.im>
parents: 9311
diff changeset
3061 /* Only care about @ char BEFORE the first / */
cf5b5b63228d [gaim-migrate @ 10149]
Tim Ringenbach <marv@pidgin.im>
parents: 9311
diff changeset
3062 at = strchr(url, '@');
cf5b5b63228d [gaim-migrate @ 10149]
Tim Ringenbach <marv@pidgin.im>
parents: 9311
diff changeset
3063 slash = strchr(url, '/');
cf5b5b63228d [gaim-migrate @ 10149]
Tim Ringenbach <marv@pidgin.im>
parents: 9311
diff changeset
3064 if ((at != NULL) &&
cf5b5b63228d [gaim-migrate @ 10149]
Tim Ringenbach <marv@pidgin.im>
parents: 9311
diff changeset
3065 (((slash != NULL) && (strlen(at) > strlen(slash))) ||
cf5b5b63228d [gaim-migrate @ 10149]
Tim Ringenbach <marv@pidgin.im>
parents: 9311
diff changeset
3066 (slash == NULL))) {
9227
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3067 g_snprintf(scan_info, sizeof(scan_info),
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3068 "%%255[%s]:%%255[%s]^@", user_ctrl, passwd_ctrl);
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3069 f = sscanf(url, scan_info, user, passwd);
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3070
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3071 if (f ==1 ) {
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3072 /* No passwd, possibly just username supplied */
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3073 g_snprintf(scan_info, sizeof(scan_info),
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3074 "%%255[%s]^@", user_ctrl);
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3075 f = sscanf(url, scan_info, user);
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3076 *passwd = '\0';
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3077 }
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3078
9230
b83905afbb55 [gaim-migrate @ 10026]
Nathan Walp <nwalp@pidgin.im>
parents: 9227
diff changeset
3079 url = at+1; /* move pointer after the @ char */
9227
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3080 } else {
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3081 *user = '\0';
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3082 *passwd = '\0';
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3083 }
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3084
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3085 g_snprintf(scan_info, sizeof(scan_info),
8118
52089b055c12 [gaim-migrate @ 8822]
Luke Schierer <lschiere@pidgin.im>
parents: 8088
diff changeset
3086 "%%255[%s]:%%5[%s]/%%255[%s]", addr_ctrl, port_ctrl, page_ctrl);
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3087
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3088 f = sscanf(url, scan_info, host, port_str, path);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3089
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3090 if (f == 1)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3091 {
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3092 g_snprintf(scan_info, sizeof(scan_info),
8118
52089b055c12 [gaim-migrate @ 8822]
Luke Schierer <lschiere@pidgin.im>
parents: 8088
diff changeset
3093 "%%255[%s]/%%255[%s]",
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3094 addr_ctrl, page_ctrl);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3095 f = sscanf(url, scan_info, host, path);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3096 g_snprintf(port_str, sizeof(port_str), "80");
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3097 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3098
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3099 if (f == 1)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3100 *path = '\0';
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3101
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3102 sscanf(port_str, "%d", &port);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3103
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3104 if (ret_host != NULL) *ret_host = g_strdup(host);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3105 if (ret_port != NULL) *ret_port = port;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3106 if (ret_path != NULL) *ret_path = g_strdup(path);
9227
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3107 if (ret_user != NULL) *ret_user = g_strdup(user);
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3108 if (ret_passwd != NULL) *ret_passwd = g_strdup(passwd);
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3109
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3110 return TRUE;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3111 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3112
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3113 static void
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3114 destroy_fetch_url_data(GaimFetchUrlData *gfud)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3115 {
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3116 g_free(gfud->webdata);
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3117 g_free(gfud->url);
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3118 g_free(gfud->user_agent);
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3119 g_free(gfud->website.address);
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3120 g_free(gfud->website.page);
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3121 g_free(gfud->website.user);
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3122 g_free(gfud->website.passwd);
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3123 g_free(gfud->request);
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3124
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3125 g_free(gfud);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3126 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3127
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3128 static gboolean
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3129 parse_redirect(const char *data, size_t data_len, gint sock,
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3130 GaimFetchUrlData *gfud)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3131 {
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3132 gchar *s;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3133
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3134 if ((s = g_strstr_len(data, data_len, "Location: ")) != NULL)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3135 {
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3136 gchar *new_url, *temp_url, *end;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3137 gboolean full;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3138 int len;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3139
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3140 s += strlen("Location: ");
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3141 end = strchr(s, '\r');
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3142
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3143 /* Just in case :) */
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3144 if (end == NULL)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3145 end = strchr(s, '\n');
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3146
10623
14c32196fcfd [gaim-migrate @ 12094]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10614
diff changeset
3147 if (end == NULL)
14c32196fcfd [gaim-migrate @ 12094]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10614
diff changeset
3148 return FALSE;
14c32196fcfd [gaim-migrate @ 12094]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10614
diff changeset
3149
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3150 len = end - s;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3151
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3152 new_url = g_malloc(len + 1);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3153 strncpy(new_url, s, len);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3154 new_url[len] = '\0';
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3155
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3156 full = gfud->full;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3157
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3158 if (*new_url == '/' || g_strstr_len(new_url, len, "://") == NULL)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3159 {
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3160 temp_url = new_url;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3161
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3162 new_url = g_strdup_printf("%s:%d%s", gfud->website.address,
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3163 gfud->website.port, temp_url);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3164
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3165 g_free(temp_url);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3166
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3167 full = FALSE;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3168 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3169
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3170 /* Close the existing stuff. */
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3171 gaim_input_remove(gfud->inpa);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3172 close(sock);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3173
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3174 gaim_debug_info("gaim_url_fetch", "Redirecting to %s\n", new_url);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3175
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3176 /* Try again, with this new location. */
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3177 gaim_url_fetch_request(new_url, full, gfud->user_agent,
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3178 gfud->http11, gfud->request, gfud->include_headers,
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3179 gfud->callback, gfud->user_data);
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3180
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3181 /* Free up. */
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3182 g_free(new_url);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3183 destroy_fetch_url_data(gfud);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3184
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3185 return TRUE;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3186 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3187
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3188 return FALSE;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3189 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3190
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3191 static size_t
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3192 parse_content_len(const char *data, size_t data_len)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3193 {
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3194 size_t content_len = 0;
9240
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3195 const char *p = NULL;
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3196
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3197 /* This is still technically wrong, since headers are case-insensitive
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3198 * [RFC 2616, section 4.2], though this ought to catch the normal case.
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3199 * Note: data is _not_ nul-terminated.
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3200 */
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3201 if (data_len > 16) {
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3202 p = strncmp(data, "Content-Length: ", 16) == 0 ? data : NULL;
9240
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3203 if (!p) {
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3204 p = g_strstr_len(data, data_len, "\nContent-Length: ");
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3205 if (p)
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3206 p += 1;
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3207 }
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3208 if (!p)
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3209 p = (strncmp(data, "CONTENT-LENGTH: ", 16) == 0)
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3210 ? data : NULL;
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3211 if (!p) {
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3212 p = g_strstr_len(data, data_len, "\nContent-Length: ");
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3213 if (p)
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3214 p++;
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3215 }
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3216 if (!p) {
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3217 p = g_strstr_len(data, data_len, "\nCONTENT-LENGTH: ");
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3218 if (p)
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3219 p++;
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3220 }
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3221
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3222 if (p)
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3223 p += 16;
9240
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3224 }
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3225
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3226 /* If we can find a Content-Length header at all, try to sscanf it.
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3227 * Response headers should end with at least \r\n, so sscanf is safe,
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3228 * if we make sure that there is indeed a \n in our header.
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3229 */
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3230 if (p && g_strstr_len(p, data_len - (p - data), "\n")) {
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3231 sscanf(p, "%" G_GSIZE_FORMAT, &content_len);
9818
5206fb21e358 [gaim-migrate @ 10689]
Mark Doliner <mark@kingant.net>
parents: 9804
diff changeset
3232 gaim_debug_misc("parse_content_len", "parsed %u\n", content_len);
9240
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3233 }
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3234
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3235 return content_len;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3236 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3237
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3238
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3239 static void
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3240 url_fetched_cb(gpointer url_data, gint sock, GaimInputCondition cond)
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3241 {
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3242 GaimFetchUrlData *gfud = url_data;
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3243 int len;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3244 char buf[4096];
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3245 char *data_cursor;
9240
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3246 gboolean got_eof = FALSE;
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3247
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3248 while((len = read(sock, buf, sizeof(buf))) > 0) {
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3249 /* If we've filled up our butfer, make it bigger */
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3250 if((gfud->len + len) >= gfud->data_len) {
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3251 while((gfud->len + len) >= gfud->data_len)
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3252 gfud->data_len += sizeof(buf);
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3253
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3254 gfud->webdata = g_realloc(gfud->webdata, gfud->data_len);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3255 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3256
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3257 data_cursor = gfud->webdata + gfud->len;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3258
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3259 gfud->len += len;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3260
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3261 memcpy(data_cursor, buf, len);
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3262
11407
f9c74bf11ae0 [gaim-migrate @ 13642]
Mark Doliner <mark@kingant.net>
parents: 11361
diff changeset
3263 gfud->webdata[gfud->len] = '\0';
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3264
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3265 if(!gfud->got_headers) {
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3266 char *tmp;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3267
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3268 /** See if we've reached the end of the headers yet */
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3269 if((tmp = strstr(gfud->webdata, "\r\n\r\n"))) {
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3270 char * new_data;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3271 guint header_len = (tmp + 4 - gfud->webdata);
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3272 size_t content_len, body_len = 0;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3273
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3274 gaim_debug_misc("gaim_url_fetch", "Response headers: '%.*s'\n",
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3275 header_len, gfud->webdata);
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3276
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3277 /* See if we can find a redirect. */
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3278 if (parse_redirect(gfud->webdata, header_len, sock, gfud))
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3279 return;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3280
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3281 gfud->got_headers = TRUE;
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3282
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3283 /* No redirect. See if we can find a content length. */
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3284 content_len = parse_content_len(gfud->webdata, header_len);
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3285
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3286 if (content_len == 0)
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3287 {
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3288 /* We'll stick with an initial 8192 */
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3289 content_len = 8192;
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3290 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3291 else
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3292 {
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3293 gfud->has_explicit_data_len = TRUE;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3294 }
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3295
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3296 content_len = MAX(content_len, body_len);
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3297
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3298 /* If we're returning the headers too, we don't need to clean them out */
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3299 if (gfud->include_headers) {
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3300 gfud->data_len = content_len + header_len;
12907
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3301 } else {
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3302
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3303 if (gfud->len > (header_len + 1))
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3304 body_len = (gfud->len - header_len);
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3305
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3306
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3307 new_data = g_try_malloc(content_len);
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3308 if (new_data == NULL) {
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3309 gaim_debug_error("gaim_url_fetch", "Failed to allocate %u bytes: %s\n",
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3310 content_len, strerror(errno));
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3311 gaim_input_remove(gfud->inpa);
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3312 close(sock);
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3313 gfud->callback(gfud->user_data, NULL, 0);
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3314 destroy_fetch_url_data(gfud);
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3315
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3316 return;
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3317 }
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3318
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3319 /* We may have read part of the body when reading the headers, don't lose it */
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3320 if (body_len > 0) {
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3321 tmp += 4;
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3322 memcpy(new_data, tmp, body_len);
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3323 }
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3324
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3325 /* Out with the old... */
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3326 g_free(gfud->webdata);
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3327 gfud->webdata = NULL;
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3328
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3329 /* In with the new. */
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3330 gfud->len = body_len;
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3331 gfud->data_len = content_len;
5e338dda872f [gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12887
diff changeset
3332 gfud->webdata = new_data;
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3333 }
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3334 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3335 }
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3336
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3337 if (gfud->has_explicit_data_len && gfud->len >= gfud->data_len)
9240
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3338 {
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3339 got_eof = TRUE;
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3340 break;
9240
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3341 }
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3342 }
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3343
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3344 if(len <= 0) {
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3345 if(errno == EAGAIN) {
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3346 return;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3347 } else if (errno != ETIMEDOUT) {
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3348 got_eof = TRUE;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3349 } else {
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3350 gaim_input_remove(gfud->inpa);
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3351 close(sock);
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3352
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3353 gfud->callback(gfud->user_data, NULL, 0);
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3354
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3355 destroy_fetch_url_data(gfud);
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3356 return;
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3357 }
9240
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3358 }
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3359
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3360 if (got_eof) {
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3361 gfud->webdata = g_realloc(gfud->webdata, gfud->len + 1);
12109
936e3b7cd864 [gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11920
diff changeset
3362 gfud->webdata[gfud->len] = '\0';
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3363
9311
c5fdff22b252 [gaim-migrate @ 10119]
Tim Ringenbach <marv@pidgin.im>
parents: 9277
diff changeset
3364 /* gaim_debug_misc("gaim_url_fetch", "Received: '%s'\n", gfud->webdata); */
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3365
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3366 gaim_input_remove(gfud->inpa);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3367 close(sock);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3368 gfud->callback(gfud->user_data, gfud->webdata, gfud->len);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3369
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3370 destroy_fetch_url_data(gfud);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3371 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3372 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3373
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3374 static void
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3375 url_fetch_connect_cb(gpointer url_data, gint sock, GaimInputCondition cond) {
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3376 GaimFetchUrlData *gfud = url_data;
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3377 int len, total_len;
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3378
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3379 if(sock == -1) {
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3380 gfud->callback(gfud->user_data, NULL, 0);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3381 destroy_fetch_url_data(gfud);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3382 return;
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3383 }
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3384
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3385 if (!gfud->request) {
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3386 if (gfud->user_agent) {
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3387 /* Host header is not forbidden in HTTP/1.0 requests, and HTTP/1.1
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3388 * clients must know how to handle the "chunked" transfer encoding.
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3389 * Gaim doesn't know how to handle "chunked", so should always send
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3390 * the Host header regardless, to get around some observed problems
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3391 */
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3392 gfud->request = g_strdup_printf(
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3393 "GET %s%s HTTP/%s\r\n"
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3394 "Connection: close\r\n"
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3395 "User-Agent: %s\r\n"
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3396 "Host: %s\r\n\r\n",
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3397 (gfud->full ? "" : "/"),
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3398 (gfud->full ? gfud->url : gfud->website.page),
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3399 (gfud->http11 ? "1.1" : "1.0"),
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3400 gfud->user_agent, gfud->website.address);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3401 } else {
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3402 gfud->request = g_strdup_printf(
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3403 "GET %s%s HTTP/%s\r\n"
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3404 "Connection: close\r\n"
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3405 "Host: %s\r\n\r\n",
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3406 (gfud->full ? "" : "/"),
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3407 (gfud->full ? gfud->url : gfud->website.page),
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3408 (gfud->http11 ? "1.1" : "1.0"),
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3409 gfud->website.address);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3410 }
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3411 }
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3412
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3413 gaim_debug_misc("gaim_url_fetch", "Request: '%s'\n", gfud->request);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3414
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3415 if(!gfud->inpa)
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3416 gfud->inpa = gaim_input_add(sock, GAIM_INPUT_WRITE,
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3417 url_fetch_connect_cb, gfud);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3418
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3419 total_len = strlen(gfud->request);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3420
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3421 len = write(sock, gfud->request + gfud->request_written,
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3422 total_len - gfud->request_written);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3423
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3424 if(len < 0 && errno == EAGAIN)
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3425 return;
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3426 else if(len < 0) {
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3427 gaim_input_remove(gfud->inpa);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3428 close(sock);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3429 gfud->callback(gfud->user_data, NULL, 0);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3430 destroy_fetch_url_data(gfud);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3431 return;
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3432 }
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3433 gfud->request_written += len;
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3434
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3435 if(gfud->request_written != total_len)
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3436 return;
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3437
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3438 gaim_input_remove(gfud->inpa);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3439
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3440 gfud->inpa = gaim_input_add(sock, GAIM_INPUT_READ, url_fetched_cb,
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3441 gfud);
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3442 }
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3443
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3444 void
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3445 gaim_url_fetch_request(const char *url, gboolean full,
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3446 const char *user_agent, gboolean http11,
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3447 const char *request, gboolean include_headers,
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3448 GaimURLFetchCallback cb, void *user_data)
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3449 {
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3450 GaimFetchUrlData *gfud;
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3451
9257
15d516d07d57 [gaim-migrate @ 10056]
Tim Ringenbach <marv@pidgin.im>
parents: 9241
diff changeset
3452 g_return_if_fail(url != NULL);
15d516d07d57 [gaim-migrate @ 10056]
Tim Ringenbach <marv@pidgin.im>
parents: 9241
diff changeset
3453 g_return_if_fail(cb != NULL);
9311
c5fdff22b252 [gaim-migrate @ 10119]
Tim Ringenbach <marv@pidgin.im>
parents: 9277
diff changeset
3454
9240
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3455 gaim_debug_info("gaim_url_fetch",
f1d87ab17e41 [gaim-migrate @ 10039]
Tim Ringenbach <marv@pidgin.im>
parents: 9233
diff changeset
3456 "requested to fetch (%s), full=%d, user_agent=(%s), http11=%d\n",
9257
15d516d07d57 [gaim-migrate @ 10056]
Tim Ringenbach <marv@pidgin.im>
parents: 9241
diff changeset
3457 url, full, user_agent?user_agent:"(null)", http11);
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3458
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3459 gfud = g_new0(GaimFetchUrlData, 1);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3460
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3461 gfud->callback = cb;
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3462 gfud->user_data = user_data;
12887
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3463 gfud->url = g_strdup(url);
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3464 gfud->user_agent = user_agent ? g_strdup(user_agent) : NULL;
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3465 gfud->http11 = http11;
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3466 gfud->full = full;
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3467 gfud->request = request ? g_strdup(request) : NULL;
4229503f1cd9 [gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12880
diff changeset
3468 gfud->include_headers = include_headers;
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3469
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3470 gaim_url_parse(url, &gfud->website.address, &gfud->website.port,
9227
9171e528d7e5 [gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents: 9222
diff changeset
3471 &gfud->website.page, &gfud->website.user, &gfud->website.passwd);
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3472
12802
26b31b4c43a2 [gaim-migrate @ 15149]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 12640
diff changeset
3473 if (gaim_proxy_connect(NULL, gfud->website.address,
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3474 gfud->website.port, url_fetch_connect_cb, gfud) != 0) {
7094
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3475 destroy_fetch_url_data(gfud);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3476
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3477 cb(user_data, g_strdup(_("g003: Error opening connection.\n")), 0);
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3478 }
2343c3aa1dec [gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents: 7078
diff changeset
3479 }
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3480
7162
d14e026611c0 [gaim-migrate @ 7729]
Nathan Walp <nwalp@pidgin.im>
parents: 7134
diff changeset
3481 const char *
7134
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3482 gaim_url_decode(const char *str)
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3483 {
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3484 static char buf[BUF_LEN];
7631
ea2d07ad05a9 [gaim-migrate @ 8255]
Nathan Walp <nwalp@pidgin.im>
parents: 7628
diff changeset
3485 guint i, j = 0;
7134
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3486 char *bum;
9804
fe268cb602cb [gaim-migrate @ 10672]
Mark Doliner <mark@kingant.net>
parents: 9786
diff changeset
3487 char hex[3];
7134
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3488
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3489 g_return_val_if_fail(str != NULL, NULL);
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3490
9804
fe268cb602cb [gaim-migrate @ 10672]
Mark Doliner <mark@kingant.net>
parents: 9786
diff changeset
3491 /*
fe268cb602cb [gaim-migrate @ 10672]
Mark Doliner <mark@kingant.net>
parents: 9786
diff changeset
3492 * XXX - This check could be removed and buf could be made
fe268cb602cb [gaim-migrate @ 10672]
Mark Doliner <mark@kingant.net>
parents: 9786
diff changeset
3493 * dynamically allocated, but this is easier.
fe268cb602cb [gaim-migrate @ 10672]
Mark Doliner <mark@kingant.net>
parents: 9786
diff changeset
3494 */
fe268cb602cb [gaim-migrate @ 10672]
Mark Doliner <mark@kingant.net>
parents: 9786
diff changeset
3495 if (strlen(str) >= BUF_LEN)
fe268cb602cb [gaim-migrate @ 10672]
Mark Doliner <mark@kingant.net>
parents: 9786
diff changeset
3496 return NULL;
fe268cb602cb [gaim-migrate @ 10672]
Mark Doliner <mark@kingant.net>
parents: 9786
diff changeset
3497
7134
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3498 for (i = 0; i < strlen(str); i++) {
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3499
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3500 if (str[i] != '%')
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3501 buf[j++] = str[i];
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3502 else {
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3503 strncpy(hex, str + ++i, 2);
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3504 hex[2] = '\0';
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3505
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3506 /* i is pointing to the start of the number */
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3507 i++;
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3508
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3509 /*
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3510 * Now it's at the end and at the start of the for loop
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3511 * will be at the next character.
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3512 */
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3513 buf[j++] = strtol(hex, NULL, 16);
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3514 }
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3515 }
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3516
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3517 buf[j] = '\0';
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3518
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3519 if (!g_utf8_validate(buf, -1, (const char **)&bum))
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3520 *bum = '\0';
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3521
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3522 return buf;
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3523 }
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3524
7162
d14e026611c0 [gaim-migrate @ 7729]
Nathan Walp <nwalp@pidgin.im>
parents: 7134
diff changeset
3525 const char *
7134
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3526 gaim_url_encode(const char *str)
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3527 {
10700
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3528 const char *iter;
7134
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3529 static char buf[BUF_LEN];
10700
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3530 char utf_char[6];
7631
ea2d07ad05a9 [gaim-migrate @ 8255]
Nathan Walp <nwalp@pidgin.im>
parents: 7628
diff changeset
3531 guint i, j = 0;
7134
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3532
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3533 g_return_val_if_fail(str != NULL, NULL);
10700
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3534 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3535
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3536 iter = str;
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3537 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3538 gunichar c = g_utf8_get_char(iter);
10800
8002e95c0809 [gaim-migrate @ 12446]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10732
diff changeset
3539 /* If the character is an ASCII character and is alphanumeric
8002e95c0809 [gaim-migrate @ 12446]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10732
diff changeset
3540 * no need to escape */
8002e95c0809 [gaim-migrate @ 12446]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10732
diff changeset
3541 if (c < 128 && isalnum(c)) {
10700
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3542 buf[j++] = c;
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3543 } else {
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3544 int bytes = g_unichar_to_utf8(c, utf_char);
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3545 for (i = 0; i < bytes; i++) {
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3546 if (j > (BUF_LEN - 4))
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3547 break;
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3548 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3549 j += 3;
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3550 }
7134
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3551 }
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3552 }
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3553
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3554 buf[j] = '\0';
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3555
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3556 return buf;
67f9b43c402a [gaim-migrate @ 7701]
Sean Egan <seanegan@gmail.com>
parents: 7110
diff changeset
3557 }
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3558
9054
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3559 /* Originally lifted from
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3560 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3561 * ... and slightly modified to be a bit more rfc822 compliant
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3562 * ... and modified a bit more to make domain checking rfc1035 compliant
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3563 * with the exception permitted in rfc1101 for domains to start with digit
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3564 * but not completely checking to avoid conflicts with IP addresses
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3565 */
9045
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3566 gboolean
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3567 gaim_email_is_valid(const char *address)
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3568 {
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3569 const char *c, *domain;
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3570 static char *rfc822_specials = "()<>@,;:\\\"[]";
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3571
9054
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3572 /* first we validate the name portion (name@domain) (rfc822)*/
9045
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3573 for (c = address; *c; c++) {
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3574 if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) {
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3575 while (*++c) {
9054
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3576 if (*c == '\\') {
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3577 if (*c++ && *c < 127 && *c != '\n' && *c != '\r') continue;
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3578 else return FALSE;
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3579 }
9045
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3580 if (*c == '\"') break;
9054
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3581 if (*c < ' ' || *c >= 127) return FALSE;
9045
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3582 }
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3583 if (!*c++) return FALSE;
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3584 if (*c == '@') break;
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3585 if (*c != '.') return FALSE;
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3586 continue;
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3587 }
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3588 if (*c == '@') break;
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3589 if (*c <= ' ' || *c >= 127) return FALSE;
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3590 if (strchr(rfc822_specials, *c)) return FALSE;
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3591 }
9054
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3592 /* strictly we should return false if (*(c - 1) == '.') too, but I think
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3593 * we should permit user.@domain type addresses - they do work :) */
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3594 if (c == address) return FALSE;
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3595
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3596 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */
9045
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3597 if (!*(domain = ++c)) return FALSE;
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3598 do {
9054
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3599 if (*c == '.' && (c == domain || *(c - 1) == '.' || *(c - 1) == '-'))
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3600 return FALSE;
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3601 if (*c == '-' && *(c - 1) == '.') return FALSE;
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3602 if ((*c < '0' && *c != '-' && *c != '.') || (*c > '9' && *c < 'A') ||
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3603 (*c > 'Z' && *c < 'a') || (*c > 'z')) return FALSE;
9045
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3604 } while (*++c);
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3605
9054
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3606 if (*(c - 1) == '-') return FALSE;
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3607
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3608 return ((c - domain) > 3 ? TRUE : FALSE);
9045
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3609 }
38d022e5eb19 [gaim-migrate @ 9821]
Tim Ringenbach <marv@pidgin.im>
parents: 8976
diff changeset
3610
11135
f2755f58a7da [gaim-migrate @ 13196]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
3611 /* Stolen from gnome_uri_list_extract_uris */
f2755f58a7da [gaim-migrate @ 13196]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
3612 GList *
f2755f58a7da [gaim-migrate @ 13196]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
3613 gaim_uri_list_extract_uris(const gchar *uri_list)
f2755f58a7da [gaim-migrate @ 13196]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
3614 {
9670
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3615 const gchar *p, *q;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3616 gchar *retval;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3617 GList *result = NULL;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3618
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3619 g_return_val_if_fail (uri_list != NULL, NULL);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3620
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3621 p = uri_list;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3622
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3623 /* We don't actually try to validate the URI according to RFC
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3624 * 2396, or even check for allowed characters - we just ignore
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3625 * comments and trim whitespace off the ends. We also
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3626 * allow LF delimination as well as the specified CRLF.
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3627 */
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3628 while (p) {
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3629 if (*p != '#') {
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3630 while (isspace(*p))
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3631 p++;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3632
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3633 q = p;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3634 while (*q && (*q != '\n') && (*q != '\r'))
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3635 q++;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3636
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3637 if (q > p) {
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3638 q--;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3639 while (q > p && isspace(*q))
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3640 q--;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3641
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3642 retval = (gchar*)g_malloc (q - p + 2);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3643 strncpy (retval, p, q - p + 1);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3644 retval[q - p + 1] = '\0';
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3645
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3646 result = g_list_prepend (result, retval);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3647 }
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3648 }
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3649 p = strchr (p, '\n');
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3650 if (p)
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3651 p++;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3652 }
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3653
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3654 return g_list_reverse (result);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3655 }
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3656
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3657
11135
f2755f58a7da [gaim-migrate @ 13196]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
3658 /* Stolen from gnome_uri_list_extract_filenames */
f2755f58a7da [gaim-migrate @ 13196]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
3659 GList *
f2755f58a7da [gaim-migrate @ 13196]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
3660 gaim_uri_list_extract_filenames(const gchar *uri_list)
f2755f58a7da [gaim-migrate @ 13196]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
3661 {
9670
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3662 GList *tmp_list, *node, *result;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3663
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3664 g_return_val_if_fail (uri_list != NULL, NULL);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3665
11135
f2755f58a7da [gaim-migrate @ 13196]
Mark Doliner <mark@kingant.net>
parents: 11132
diff changeset
3666 result = gaim_uri_list_extract_uris(uri_list);
9670
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3667
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3668 tmp_list = result;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3669 while (tmp_list) {
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3670 gchar *s = (gchar*)tmp_list->data;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3671
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3672 node = tmp_list;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3673 tmp_list = tmp_list->next;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3674
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3675 if (!strncmp (s, "file:", 5)) {
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3676 node->data = g_filename_from_uri (s, NULL, NULL);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3677 /* not sure if this fallback is useful at all */
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3678 if (!node->data) node->data = g_strdup (s+5);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3679 } else {
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3680 result = g_list_remove_link(result, node);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3681 g_list_free_1 (node);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3682 }
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3683 g_free (s);
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3684 }
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3685 return result;
ed3bbf36de75 [gaim-migrate @ 10522]
Luke Schierer <lschiere@pidgin.im>
parents: 9642
diff changeset
3686 }
9054
96415a3aa93c [gaim-migrate @ 9830]
Tim Ringenbach <marv@pidgin.im>
parents: 9045
diff changeset
3687
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3688 /**************************************************************************
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3689 * UTF8 String Functions
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3690 **************************************************************************/
9642
8901ef16f310 [gaim-migrate @ 10490]
Mark Doliner <mark@kingant.net>
parents: 9427
diff changeset
3691 gchar *
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3692 gaim_utf8_try_convert(const char *str)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3693 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3694 gsize converted;
9642
8901ef16f310 [gaim-migrate @ 10490]
Mark Doliner <mark@kingant.net>
parents: 9427
diff changeset
3695 gchar *utf8;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3696
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3697 g_return_val_if_fail(str != NULL, NULL);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3698
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3699 if (g_utf8_validate(str, -1, NULL)) {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3700 return g_strdup(str);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3701 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3702
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3703 utf8 = g_locale_to_utf8(str, -1, &converted, NULL, NULL);
9642
8901ef16f310 [gaim-migrate @ 10490]
Mark Doliner <mark@kingant.net>
parents: 9427
diff changeset
3704 if (utf8 != NULL)
8901ef16f310 [gaim-migrate @ 10490]
Mark Doliner <mark@kingant.net>
parents: 9427
diff changeset
3705 return utf8;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3706
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3707 utf8 = g_convert(str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL);
9642
8901ef16f310 [gaim-migrate @ 10490]
Mark Doliner <mark@kingant.net>
parents: 9427
diff changeset
3708 if ((utf8 != NULL) && (converted == strlen(str)))
8901ef16f310 [gaim-migrate @ 10490]
Mark Doliner <mark@kingant.net>
parents: 9427
diff changeset
3709 return utf8;
8901ef16f310 [gaim-migrate @ 10490]
Mark Doliner <mark@kingant.net>
parents: 9427
diff changeset
3710
8901ef16f310 [gaim-migrate @ 10490]
Mark Doliner <mark@kingant.net>
parents: 9427
diff changeset
3711 if (utf8 != NULL)
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3712 g_free(utf8);
9642
8901ef16f310 [gaim-migrate @ 10490]
Mark Doliner <mark@kingant.net>
parents: 9427
diff changeset
3713
8901ef16f310 [gaim-migrate @ 10490]
Mark Doliner <mark@kingant.net>
parents: 9427
diff changeset
3714 return NULL;
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3715 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3716
10258
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3717 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3718 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf)
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3719 gchar *
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3720 gaim_utf8_salvage(const char *str)
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3721 {
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3722 GString *workstr;
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3723 const char *end;
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3724
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3725 g_return_val_if_fail(str != NULL, NULL);
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3726
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3727 workstr = g_string_sized_new(strlen(str));
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3728
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3729 do {
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3730 g_utf8_validate(str, -1, &end);
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3731 workstr = g_string_append_len(workstr, str, end - str);
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3732 str = end;
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3733 if (*str == '\0')
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3734 break;
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3735 do {
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3736 workstr = g_string_append_c(workstr, '?');
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3737 str++;
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3738 } while (!utf8_first(*str));
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3739 } while (*str != '\0');
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3740
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3741 return g_string_free(workstr, FALSE);
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3742 }
357d4fa1bfbe [gaim-migrate @ 11400]
Ethan Blanton <elb@pidgin.im>
parents: 10203
diff changeset
3743
12813
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3744
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3745 char *
12880
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3746 gaim_utf8_ncr_encode(const char *str)
12813
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3747 {
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3748 GString *out;
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3749
12880
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3750 g_return_val_if_fail(str != NULL, NULL);
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3751 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
12813
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3752
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3753 out = g_string_new("");
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3754
12880
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3755 for(; *str; str = g_utf8_next_char(str)) {
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3756 gunichar wc = g_utf8_get_char(str);
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3757
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3758 /* super simple check. hopefully not too wrong. */
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3759 if(wc >= 0x80) {
12813
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3760 g_string_append_printf(out, "&#%u;", (guint32) wc);
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3761 } else {
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3762 g_string_append_unichar(out, wc);
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3763 }
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3764 }
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3765
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3766 return g_string_free(out, FALSE);
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3767 }
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3768
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3769
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
3770 char *
12880
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3771 gaim_utf8_ncr_decode(const char *str)
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
3772 {
12813
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3773 GString *out;
12880
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3774 char *buf, *b;
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3775
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3776 g_return_val_if_fail(str != NULL, NULL);
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3777 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3778
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3779 buf = (char *) str;
12813
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3780 out = g_string_new("");
547c199072c8 [gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents: 12802
diff changeset
3781
12880
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3782 while( (b = strstr(buf, "&#")) ) {
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3783 gunichar wc;
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3784 int base = 0;
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3785
12880
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3786 /* append everything leading up to the &# */
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3787 g_string_append_len(out, buf, b-buf);
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3788
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3789 b += 2; /* skip past the &# */
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3790
12880
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3791 /* strtoul will handle 0x prefix as hex, but not x */
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3792 if(*b == 'x' || *b == 'X')
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3793 base = 16;
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3794
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3795 /* advances buf to the end of the ncr segment */
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3796 wc = (gunichar) strtoul(b, &buf, base);
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3797
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3798 /* this mimics the previous impl of ncr_decode */
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3799 if(*buf == ';') {
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3800 g_string_append_unichar(out, wc);
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3801 buf++;
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
3802 }
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
3803 }
12880
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3804
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3805 /* append whatever's left */
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3806 g_string_append(out, buf);
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3807
9161
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
3808 return g_string_free(out, FALSE);
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
3809 }
c3fa2ad099a2 [gaim-migrate @ 9946]
Tim Ringenbach <marv@pidgin.im>
parents: 9160
diff changeset
3810
12880
bad785371fa5 [gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents: 12813
diff changeset
3811
7108
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3812 int
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3813 gaim_utf8_strcasecmp(const char *a, const char *b)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3814 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3815 char *a_norm = NULL;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3816 char *b_norm = NULL;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3817 int ret = -1;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3818
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3819 if(!a && b)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3820 return -1;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3821 else if(!b && a)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3822 return 1;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3823 else if(!a && !b)
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3824 return 0;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3825
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3826 if(!g_utf8_validate(a, -1, NULL) || !g_utf8_validate(b, -1, NULL))
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3827 {
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3828 gaim_debug_error("gaim_utf8_strcasecmp",
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3829 "One or both parameters are invalid UTF8\n");
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3830 return ret;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3831 }
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3832
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3833 a_norm = g_utf8_casefold(a, -1);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3834 b_norm = g_utf8_casefold(b, -1);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3835 ret = g_utf8_collate(a_norm, b_norm);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3836 g_free(a_norm);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3837 g_free(b_norm);
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3838
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3839 return ret;
6faeeecab0dc [gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents: 7107
diff changeset
3840 }
7162
d14e026611c0 [gaim-migrate @ 7729]
Nathan Walp <nwalp@pidgin.im>
parents: 7134
diff changeset
3841
11552
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3842 /* previously conversation::find_nick() */
13200
33bef17125c2 [gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 13163
diff changeset
3843 gboolean
11552
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3844 gaim_utf8_has_word(const char *haystack, const char *needle)
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3845 {
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3846 char *hay, *pin, *p;
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3847 int n;
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3848 gboolean ret = FALSE;
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3849
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3850 hay = g_utf8_strdown(haystack, -1);
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3851
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3852 pin = g_utf8_strdown(needle, -1);
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3853 n = strlen(pin);
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3854
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3855 if ((p = strstr(hay, pin)) != NULL) {
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3856 if ((p == hay || !isalnum(*(p - 1))) && !isalnum(*(p + n))) {
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3857 ret = TRUE;
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3858 }
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3859 }
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3860
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3861 g_free(pin);
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3862 g_free(hay);
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3863
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3864 return ret;
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3865 }
11d30825c1bb [gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents: 11493
diff changeset
3866
13089
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3867 void
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3868 gaim_print_utf8_to_console(FILE *filestream, char *message)
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3869 {
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3870 gchar *message_conv;
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3871 GError *error = NULL;
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3872
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3873 /* Try to convert 'message' to user's locale */
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3874 message_conv = g_locale_from_utf8(message, -1, NULL, NULL, &error);
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3875 if (message_conv != NULL) {
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3876 fputs(message_conv, filestream);
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3877 g_free(message_conv);
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3878 }
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3879 else
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3880 {
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3881 /* use 'message' as a fallback */
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3882 g_warning("%s\n", error->message);
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3883 g_error_free(error);
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3884 fputs(message, filestream);
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3885 }
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3886 }
b553326bc468 [gaim-migrate @ 15451]
Mark Doliner <mark@kingant.net>
parents: 13033
diff changeset
3887
7564
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3888 gboolean gaim_message_meify(char *message, size_t len)
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3889 {
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3890 char *c;
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3891 gboolean inside_html = FALSE;
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3892
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3893 g_return_val_if_fail(message != NULL, FALSE);
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3894
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3895 if(len == -1)
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3896 len = strlen(message);
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3897
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3898 for (c = message; *c; c++, len--) {
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3899 if(inside_html) {
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3900 if(*c == '>')
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3901 inside_html = FALSE;
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3902 } else {
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3903 if(*c == '<')
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3904 inside_html = TRUE;
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3905 else
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3906 break;
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3907 }
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3908 }
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3909
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3910 if(*c && !g_ascii_strncasecmp(c, "/me ", 4)) {
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3911 memmove(c, c+4, len-3);
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3912 return TRUE;
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3913 }
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3914
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3915 return FALSE;
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3916 }
54b370f7d9bf [gaim-migrate @ 8180]
Nathan Walp <nwalp@pidgin.im>
parents: 7478
diff changeset
3917
7889
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3918 char *gaim_text_strip_mnemonic(const char *in)
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3919 {
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3920 char *out;
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3921 char *a;
8561
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3922 char *a0;
7889
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3923 const char *b;
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3924
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3925 g_return_val_if_fail(in != NULL, NULL);
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3926
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3927 out = g_malloc(strlen(in)+1);
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3928 a = out;
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3929 b = in;
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3930
8561
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3931 a0 = a; /* The last non-space char seen so far, or the first char */
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3932
7889
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3933 while(*b) {
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3934 if(*b == '_') {
8561
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3935 if(a > out && b > in && *(b-1) == '(' && *(b+1) && !(*(b+1) & 0x80) && *(b+2) == ')') {
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3936 /* Detected CJK style shortcut (Bug 875311) */
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3937 a = a0; /* undo the left parenthesis */
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3938 b += 3; /* and skip the whole mess */
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3939 } else if(*(b+1) == '_') {
7889
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3940 *(a++) = '_';
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3941 b += 2;
8561
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3942 a0 = a;
7889
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3943 } else {
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3944 b++;
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3945 }
8561
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3946 /* We don't want to corrupt the middle of UTF-8 characters */
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3947 } else if (!(*b & 0x80)) { /* other 1-byte char */
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3948 if (*b != ' ')
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3949 a0 = a;
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3950 *(a++) = *(b++);
7889
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3951 } else {
8561
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3952 /* Multibyte utf8 char, don't look for _ inside these */
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3953 int n = 0;
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3954 int i;
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3955 if ((*b & 0xe0) == 0xc0) {
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3956 n = 2;
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3957 } else if ((*b & 0xf0) == 0xe0) {
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3958 n = 3;
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3959 } else if ((*b & 0xf8) == 0xf0) {
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3960 n = 4;
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3961 } else if ((*b & 0xfc) == 0xf8) {
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3962 n = 5;
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3963 } else if ((*b & 0xfe) == 0xfc) {
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3964 n = 6;
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3965 } else { /* Illegal utf8 */
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3966 n = 1;
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3967 }
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3968 a0 = a; /* unless we want to delete CJK spaces too */
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3969 for (i = 0; i < n && *b; i += 1) {
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3970 *(a++) = *(b++);
2d4ccd94e298 [gaim-migrate @ 9305]
Luke Schierer <lschiere@pidgin.im>
parents: 8538
diff changeset
3971 }
7889
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3972 }
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3973 }
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3974 *a = '\0';
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3975
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3976 return out;
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3977 }
7e1bb4deca24 [gaim-migrate @ 8547]
Nathan Walp <nwalp@pidgin.im>
parents: 7858
diff changeset
3978
9926
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3979 const char* gaim_unescape_filename(const char *escaped) {
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3980 return gaim_url_decode(escaped);
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3981 }
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3982
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3983
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3984 /* this is almost identical to gaim_url_encode (hence gaim_url_decode
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3985 * being used above), but we want to keep certain characters unescaped
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3986 * for compat reasons */
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3987 const char *
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3988 gaim_escape_filename(const char *str)
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3989 {
10700
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3990 const char *iter;
9926
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3991 static char buf[BUF_LEN];
10700
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3992 char utf_char[6];
9926
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3993 guint i, j = 0;
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3994
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
3995 g_return_val_if_fail(str != NULL, NULL);
10700
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3996 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3997
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3998 iter = str;
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
3999 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4000 gunichar c = g_utf8_get_char(iter);
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4001 /* If the character is an ASCII character and is alphanumeric,
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4002 * or one of the specified values, no need to escape */
10800
8002e95c0809 [gaim-migrate @ 12446]
Stu Tomlinson <stu@nosnilmot.com>
parents: 10732
diff changeset
4003 if (c < 128 && (isalnum(c) || c == '@' || c == '-' ||
10700
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4004 c == '_' || c == '.' || c == '#')) {
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4005 buf[j++] = c;
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4006 } else {
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4007 int bytes = g_unichar_to_utf8(c, utf_char);
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4008 for (i = 0; i < bytes; i++) {
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4009 if (j > (BUF_LEN - 4))
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4010 break;
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4011 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4012 j += 3;
9505695acff8 [gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 10636
diff changeset
4013 }
9926
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
4014 }
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
4015 }
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
4016
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
4017 buf[j] = '\0';
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
4018
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
4019 return buf;
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
4020 }
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
4021
b23e70bd1215 [gaim-migrate @ 10818]
Nathan Walp <nwalp@pidgin.im>
parents: 9916
diff changeset
4022