Tue, 08 May 2007 00:28:22 +0000
Patch #265. Fix to Google Talk formatting
| 15225 | 1 | |
| 2 | /** | |
| 15884 | 3 | * Purple is the legal property of its developers, whose names are too numerous |
| 15225 | 4 | * to list here. Please refer to the COPYRIGHT file distributed with this |
| 5 | * source distribution. | |
| 6 | * | |
| 7 | * This program is free software; you can redistribute it and/or modify | |
| 8 | * it under the terms of the GNU General Public License as published by | |
| 9 | * the Free Software Foundation; either version 2 of the License, or | |
| 10 | * (at your option) any later version. | |
| 11 | * | |
| 12 | * This program is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU General Public License | |
| 18 | * along with this program; if not, write to the Free Software | |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 20 | */ | |
| 21 | ||
| 22 | #include "internal.h" | |
| 23 | #include "debug.h" | |
|
15587
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
24 | #include "util.h" |
| 15265 | 25 | #include "privacy.h" |
| 26 | ||
| 27 | #include "buddy.h" | |
| 15225 | 28 | #include "google.h" |
| 29 | #include "jabber.h" | |
| 15265 | 30 | #include "presence.h" |
| 15225 | 31 | #include "iq.h" |
| 32 | ||
| 33 | static void | |
| 34 | jabber_gmail_parse(JabberStream *js, xmlnode *packet, gpointer nul) | |
| 35 | { | |
| 36 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 37 | xmlnode *child; | |
| 38 | xmlnode *message, *sender_node, *subject_node; | |
| 39 | const char *from, *to, *subject, *url, *tid; | |
| 40 | const char *in_str; | |
| 41 | char *to_name; | |
| 42 | int i, count = 1, returned_count; | |
| 43 | ||
| 44 | const char **tos, **froms, **subjects, **urls; | |
| 45 | ||
| 46 | if (strcmp(type, "result")) | |
| 47 | return; | |
| 48 | ||
| 49 | child = xmlnode_get_child(packet, "mailbox"); | |
| 50 | if (!child) | |
| 51 | return; | |
| 52 | ||
| 53 | in_str = xmlnode_get_attrib(child, "total-matched"); | |
| 54 | if (in_str && *in_str) | |
| 55 | count = atoi(in_str); | |
| 56 | ||
| 57 | if (count == 0) | |
| 58 | return; | |
| 59 | ||
| 60 | message = xmlnode_get_child(child, "mail-thread-info"); | |
| 61 | ||
| 62 | /* Loop once to see how many messages were returned so we can allocate arrays | |
| 63 | * accordingly */ | |
| 64 | if (!message) | |
| 65 | return; | |
| 66 | for (returned_count = 0; message; returned_count++, message=xmlnode_get_next_twin(message)); | |
| 67 | ||
| 68 | froms = g_new0(const char* , returned_count); | |
| 69 | tos = g_new0(const char* , returned_count); | |
| 70 | subjects = g_new0(const char* , returned_count); | |
| 71 | urls = g_new0(const char* , returned_count); | |
| 72 | ||
| 73 | to = xmlnode_get_attrib(packet, "to"); | |
| 74 | to_name = jabber_get_bare_jid(to); | |
| 75 | url = xmlnode_get_attrib(child, "url"); | |
| 76 | if (!url || !*url) | |
| 77 | url = "http://www.gmail.com"; | |
| 15261 | 78 | |
| 15225 | 79 | message= xmlnode_get_child(child, "mail-thread-info"); |
| 80 | for (i=0; message; message = xmlnode_get_next_twin(message), i++) { | |
| 81 | subject_node = xmlnode_get_child(message, "subject"); | |
| 82 | sender_node = xmlnode_get_child(message, "senders"); | |
| 83 | sender_node = xmlnode_get_child(sender_node, "sender"); | |
| 84 | ||
| 85 | while (sender_node && (!xmlnode_get_attrib(sender_node, "unread") || | |
| 86 | !strcmp(xmlnode_get_attrib(sender_node, "unread"),"0"))) | |
| 87 | sender_node = xmlnode_get_next_twin(sender_node); | |
| 88 | ||
| 89 | if (!sender_node) { | |
| 90 | i--; | |
| 91 | continue; | |
| 92 | } | |
| 93 | ||
| 94 | from = xmlnode_get_attrib(sender_node, "name"); | |
| 95 | if (!from || !*from) | |
| 96 | from = xmlnode_get_attrib(sender_node, "address"); | |
| 97 | subject = xmlnode_get_data(subject_node); | |
| 98 | /* | |
| 99 | * url = xmlnode_get_attrib(message, "url"); | |
| 100 | */ | |
| 101 | tos[i] = (to_name != NULL ? to_name : ""); | |
| 102 | froms[i] = (from != NULL ? from : ""); | |
| 103 | subjects[i] = (subject != NULL ? subject : ""); | |
| 104 | urls[i] = (url != NULL ? url : ""); | |
| 105 | ||
| 106 | tid = xmlnode_get_attrib(message, "tid"); | |
| 107 | if (tid && | |
| 108 | (js->gmail_last_tid == NULL || strcmp(tid, js->gmail_last_tid) > 0)) { | |
| 109 | g_free(js->gmail_last_tid); | |
| 110 | js->gmail_last_tid = g_strdup(tid); | |
| 111 | } | |
| 112 | } | |
| 113 | ||
| 15261 | 114 | if (i>0) |
| 15884 | 115 | purple_notify_emails(js->gc, count, count == returned_count, subjects, froms, tos, |
| 15261 | 116 | urls, NULL, NULL); |
| 117 | ||
| 15225 | 118 | g_free(to_name); |
| 119 | g_free(tos); | |
| 120 | g_free(froms); | |
| 121 | g_free(subjects); | |
| 122 | g_free(urls); | |
| 123 | ||
| 124 | in_str = xmlnode_get_attrib(child, "result-time"); | |
| 125 | if (in_str && *in_str) { | |
| 126 | g_free(js->gmail_last_time); | |
| 127 | js->gmail_last_time = g_strdup(in_str); | |
| 128 | } | |
| 129 | } | |
| 130 | ||
| 131 | void | |
| 132 | jabber_gmail_poke(JabberStream *js, xmlnode *packet) | |
| 133 | { | |
| 134 | const char *type; | |
| 135 | xmlnode *query; | |
| 136 | JabberIq *iq; | |
| 137 | ||
| 138 | /* bail if the user isn't interested */ | |
| 15884 | 139 | if (!purple_account_get_check_mail(js->gc->account)) |
| 15225 | 140 | return; |
| 141 | ||
| 142 | type = xmlnode_get_attrib(packet, "type"); | |
| 143 | ||
| 144 | ||
| 145 | /* Is this an initial incoming mail notification? If so, send a request for more info */ | |
| 146 | if (strcmp(type, "set") || !xmlnode_get_child(packet, "new-mail")) | |
| 147 | return; | |
| 148 | ||
| 15884 | 149 | purple_debug(PURPLE_DEBUG_MISC, "jabber", |
| 15225 | 150 | "Got new mail notification. Sending request for more info\n"); |
| 151 | ||
| 152 | iq = jabber_iq_new_query(js, JABBER_IQ_GET, "google:mail:notify"); | |
| 153 | jabber_iq_set_callback(iq, jabber_gmail_parse, NULL); | |
| 154 | query = xmlnode_get_child(iq->node, "query"); | |
| 155 | ||
| 156 | if (js->gmail_last_time) | |
| 157 | xmlnode_set_attrib(query, "newer-than-time", js->gmail_last_time); | |
| 158 | if (js->gmail_last_tid) | |
| 159 | xmlnode_set_attrib(query, "newer-than-tid", js->gmail_last_tid); | |
| 160 | ||
| 161 | jabber_iq_send(iq); | |
| 162 | return; | |
| 163 | } | |
| 164 | ||
| 165 | void jabber_gmail_init(JabberStream *js) { | |
| 166 | JabberIq *iq; | |
| 167 | ||
| 15884 | 168 | if (!purple_account_get_check_mail(js->gc->account)) |
| 15225 | 169 | return; |
| 170 | ||
| 171 | iq = jabber_iq_new_query(js, JABBER_IQ_GET, "google:mail:notify"); | |
| 172 | jabber_iq_set_callback(iq, jabber_gmail_parse, NULL); | |
| 173 | jabber_iq_send(iq); | |
| 174 | } | |
| 15265 | 175 | |
| 176 | void jabber_google_roster_init(JabberStream *js) | |
| 177 | { | |
| 178 | JabberIq *iq; | |
| 179 | xmlnode *query; | |
| 180 | ||
| 181 | iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:roster"); | |
| 182 | query = xmlnode_get_child(iq->node, "query"); | |
| 183 | ||
| 184 | xmlnode_set_attrib(query, "xmlns:gr", "google:roster"); | |
| 185 | xmlnode_set_attrib(query, "gr:ext", "2"); | |
| 186 | ||
| 187 | jabber_iq_send(iq); | |
| 188 | } | |
| 189 | ||
| 190 | void jabber_google_roster_outgoing(JabberStream *js, xmlnode *query, xmlnode *item) | |
| 191 | { | |
| 15884 | 192 | PurpleAccount *account = purple_connection_get_account(js->gc); |
| 15265 | 193 | GSList *list = account->deny; |
| 194 | const char *jid = xmlnode_get_attrib(item, "jid"); | |
| 195 | char *jid_norm = g_strdup(jabber_normalize(account, jid)); | |
| 196 | ||
| 197 | while (list) { | |
| 198 | if (!strcmp(jid_norm, (char*)list->data)) { | |
| 199 | xmlnode_set_attrib(query, "xmlns:gr", "google:roster"); | |
| 200 | xmlnode_set_attrib(item, "gr:t", "B"); | |
| 201 | xmlnode_set_attrib(query, "xmlns:gr", "google:roster"); | |
| 202 | xmlnode_set_attrib(query, "gr:ext", "2"); | |
| 203 | return; | |
| 204 | } | |
| 205 | list = list->next; | |
| 206 | } | |
| 207 | ||
| 208 | } | |
| 209 | ||
|
15530
9355a1be068e
Make deleting Google Talk buddies work
Sean Egan <seanegan@pidgin.im>
parents:
15435
diff
changeset
|
210 | gboolean jabber_google_roster_incoming(JabberStream *js, xmlnode *item) |
| 15265 | 211 | { |
| 15884 | 212 | PurpleAccount *account = purple_connection_get_account(js->gc); |
| 15265 | 213 | GSList *list = account->deny; |
| 214 | const char *jid = xmlnode_get_attrib(item, "jid"); | |
| 215 | gboolean on_block_list = FALSE; | |
| 216 | ||
| 217 | char *jid_norm = g_strdup(jabber_normalize(account, jid)); | |
| 218 | ||
| 219 | const char *grt = xmlnode_get_attrib_with_namespace(item, "t", "google:roster"); | |
| 220 | ||
| 221 | while (list) { | |
| 222 | if (!strcmp(jid_norm, (char*)list->data)) { | |
| 223 | on_block_list = TRUE; | |
| 224 | break; | |
| 225 | } | |
| 226 | list = list->next; | |
| 227 | } | |
| 228 | ||
|
15530
9355a1be068e
Make deleting Google Talk buddies work
Sean Egan <seanegan@pidgin.im>
parents:
15435
diff
changeset
|
229 | if (grt && (*grt == 'H' || *grt == 'h')) { |
| 15884 | 230 | PurpleBuddy *buddy = purple_find_buddy(account, jid_norm); |
| 231 | purple_blist_remove_buddy(buddy); | |
|
15530
9355a1be068e
Make deleting Google Talk buddies work
Sean Egan <seanegan@pidgin.im>
parents:
15435
diff
changeset
|
232 | return FALSE; |
|
9355a1be068e
Make deleting Google Talk buddies work
Sean Egan <seanegan@pidgin.im>
parents:
15435
diff
changeset
|
233 | } |
|
9355a1be068e
Make deleting Google Talk buddies work
Sean Egan <seanegan@pidgin.im>
parents:
15435
diff
changeset
|
234 | |
| 15265 | 235 | if (!on_block_list && (grt && (*grt == 'B' || *grt == 'b'))) { |
| 15884 | 236 | purple_debug_info("jabber", "Blocking %s\n", jid_norm); |
| 237 | purple_privacy_deny_add(account, jid_norm, TRUE); | |
| 15265 | 238 | } else if (on_block_list && (!grt || (*grt != 'B' && *grt != 'b' ))){ |
| 15884 | 239 | purple_debug_info("jabber", "Unblocking %s\n", jid_norm); |
| 240 | purple_privacy_deny_remove(account, jid_norm, TRUE); | |
| 15265 | 241 | } |
|
15530
9355a1be068e
Make deleting Google Talk buddies work
Sean Egan <seanegan@pidgin.im>
parents:
15435
diff
changeset
|
242 | return TRUE; |
| 15265 | 243 | } |
| 244 | ||
| 15884 | 245 | void jabber_google_roster_add_deny(PurpleConnection *gc, const char *who) |
| 15265 | 246 | { |
| 247 | JabberStream *js; | |
| 248 | GSList *buddies; | |
| 249 | JabberIq *iq; | |
| 250 | xmlnode *query; | |
| 251 | xmlnode *item; | |
| 252 | xmlnode *group; | |
| 15884 | 253 | PurpleBuddy *b; |
| 15265 | 254 | JabberBuddy *jb; |
| 255 | ||
| 256 | js = (JabberStream*)(gc->proto_data); | |
| 257 | ||
| 258 | if (!js || !js->server_caps & JABBER_CAP_GOOGLE_ROSTER) | |
| 259 | return; | |
| 260 | ||
| 261 | jb = jabber_buddy_find(js, who, TRUE); | |
| 262 | ||
| 15884 | 263 | buddies = purple_find_buddies(js->gc->account, who); |
| 15265 | 264 | if(!buddies) |
| 265 | return; | |
| 266 | ||
| 267 | b = buddies->data; | |
| 268 | ||
| 269 | iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster"); | |
| 270 | ||
| 271 | query = xmlnode_get_child(iq->node, "query"); | |
| 272 | item = xmlnode_new_child(query, "item"); | |
| 273 | ||
| 274 | while(buddies) { | |
| 15884 | 275 | PurpleGroup *g; |
| 15265 | 276 | |
| 277 | b = buddies->data; | |
| 15884 | 278 | g = purple_buddy_get_group(b); |
| 15265 | 279 | |
| 280 | group = xmlnode_new_child(item, "group"); | |
| 281 | xmlnode_insert_data(group, g->name, -1); | |
| 282 | ||
| 283 | buddies = buddies->next; | |
| 284 | } | |
| 285 | ||
| 286 | iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster"); | |
| 287 | ||
| 288 | query = xmlnode_get_child(iq->node, "query"); | |
| 289 | item = xmlnode_new_child(query, "item"); | |
| 290 | ||
| 291 | xmlnode_set_attrib(item, "jid", who); | |
| 292 | xmlnode_set_attrib(item, "name", b->alias ? b->alias : ""); | |
| 293 | xmlnode_set_attrib(item, "gr:t", "B"); | |
| 294 | xmlnode_set_attrib(query, "xmlns:gr", "google:roster"); | |
| 295 | xmlnode_set_attrib(query, "gr:ext", "2"); | |
| 296 | ||
| 297 | jabber_iq_send(iq); | |
| 298 | ||
| 299 | /* Synthesize a sign-off */ | |
| 300 | if (jb) { | |
| 301 | JabberBuddyResource *jbr; | |
| 302 | GList *l = jb->resources; | |
| 303 | while (l) { | |
| 304 | jbr = l->data; | |
|
15346
ef48613b5b9b
[gaim-migrate @ 18074]
Evan Schoenberg <evands@pidgin.im>
parents:
15265
diff
changeset
|
305 | if (jbr && jbr->name) |
|
ef48613b5b9b
[gaim-migrate @ 18074]
Evan Schoenberg <evands@pidgin.im>
parents:
15265
diff
changeset
|
306 | { |
| 15884 | 307 | purple_debug(PURPLE_DEBUG_MISC, "jabber", "Removing resource %s\n", jbr->name); |
|
15346
ef48613b5b9b
[gaim-migrate @ 18074]
Evan Schoenberg <evands@pidgin.im>
parents:
15265
diff
changeset
|
308 | jabber_buddy_remove_resource(jb, jbr->name); |
|
ef48613b5b9b
[gaim-migrate @ 18074]
Evan Schoenberg <evands@pidgin.im>
parents:
15265
diff
changeset
|
309 | } |
| 15265 | 310 | l = l->next; |
| 311 | } | |
| 312 | } | |
| 15884 | 313 | purple_prpl_got_user_status(purple_connection_get_account(gc), who, "offline", NULL); |
| 15265 | 314 | } |
| 315 | ||
| 15884 | 316 | void jabber_google_roster_rem_deny(PurpleConnection *gc, const char *who) |
| 15265 | 317 | { |
| 318 | JabberStream *js; | |
| 319 | GSList *buddies; | |
| 320 | JabberIq *iq; | |
| 321 | xmlnode *query; | |
| 322 | xmlnode *item; | |
| 323 | xmlnode *group; | |
| 15884 | 324 | PurpleBuddy *b; |
| 15265 | 325 | |
| 326 | g_return_if_fail(gc != NULL); | |
| 327 | g_return_if_fail(who != NULL); | |
| 328 | ||
| 329 | js = (JabberStream*)(gc->proto_data); | |
| 330 | ||
| 331 | if (!js || !js->server_caps & JABBER_CAP_GOOGLE_ROSTER) | |
| 332 | return; | |
| 333 | ||
| 15884 | 334 | buddies = purple_find_buddies(js->gc->account, who); |
| 15265 | 335 | if(!buddies) |
| 336 | return; | |
| 337 | ||
| 338 | b = buddies->data; | |
| 339 | ||
| 340 | iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster"); | |
| 341 | ||
| 342 | query = xmlnode_get_child(iq->node, "query"); | |
| 343 | item = xmlnode_new_child(query, "item"); | |
| 344 | ||
| 345 | while(buddies) { | |
| 15884 | 346 | PurpleGroup *g; |
| 15265 | 347 | |
| 348 | b = buddies->data; | |
| 15884 | 349 | g = purple_buddy_get_group(b); |
| 15265 | 350 | |
| 351 | group = xmlnode_new_child(item, "group"); | |
| 352 | xmlnode_insert_data(group, g->name, -1); | |
| 353 | ||
| 354 | buddies = buddies->next; | |
| 355 | } | |
| 356 | ||
| 357 | iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster"); | |
| 358 | ||
| 359 | query = xmlnode_get_child(iq->node, "query"); | |
| 360 | item = xmlnode_new_child(query, "item"); | |
| 361 | ||
| 362 | xmlnode_set_attrib(item, "jid", who); | |
| 363 | xmlnode_set_attrib(item, "name", b->alias ? b->alias : ""); | |
| 364 | xmlnode_set_attrib(query, "xmlns:gr", "google:roster"); | |
| 365 | xmlnode_set_attrib(query, "gr:ext", "2"); | |
| 366 | ||
| 367 | jabber_iq_send(iq); | |
| 368 | ||
| 369 | /* See if he's online */ | |
| 370 | jabber_presence_subscription_set(js, who, "probe"); | |
| 371 | } | |
|
15587
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
372 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
373 | /* This does two passes on the string. The first pass goes through |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
374 | * and determine if all the structured text is properly balanced, and |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
375 | * how many instances of each there is. The second pass goes and converts |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
376 | * everything to HTML, depending on what's figured out by the first pass. |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
377 | * It will short circuit once it knows it has no more replacements to make |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
378 | */ |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
379 | char *jabber_google_format_to_html(const char *text) |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
380 | { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
381 | const char *p; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
382 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
383 | /* The start of the screen may be consdiered a space for this purpose */ |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
384 | gboolean preceding_space = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
385 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
386 | gboolean in_bold = FALSE, in_italic = FALSE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
387 | gboolean in_tag = FALSE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
388 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
389 | gint bold_count = 0, italic_count = 0; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
390 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
391 | GString *str; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
392 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
393 | for (p = text; *p != '\0'; p = g_utf8_next_char(p)) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
394 | gunichar c = g_utf8_get_char(p); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
395 | if (c == '*' && !in_tag) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
396 | if (in_bold && (g_unichar_isspace(*(p+1)) || |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
397 | *(p+1) == '\0' || |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
398 | *(p+1) == '<')) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
399 | bold_count++; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
400 | in_bold = FALSE; |
|
16988
1aaf51bf0f23
Patch #265. Fix to Google Talk formatting
Sean Egan <seanegan@pidgin.im>
parents:
15884
diff
changeset
|
401 | } else if (preceding_space && !in_bold && !g_unichar_isspace(*(p+1))) { |
|
15587
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
402 | bold_count++; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
403 | in_bold = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
404 | } |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
405 | preceding_space = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
406 | } else if (c == '_' && !in_tag) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
407 | if (in_italic && (g_unichar_isspace(*(p+1)) || |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
408 | *(p+1) == '\0' || |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
409 | *(p+1) == '<')) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
410 | italic_count++; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
411 | in_italic = FALSE; |
|
16988
1aaf51bf0f23
Patch #265. Fix to Google Talk formatting
Sean Egan <seanegan@pidgin.im>
parents:
15884
diff
changeset
|
412 | } else if (preceding_space && !in_italic && !g_unichar_isspace(*(p+1))) { |
|
15587
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
413 | italic_count++; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
414 | in_italic = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
415 | } |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
416 | preceding_space = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
417 | } else if (c == '<' && !in_tag) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
418 | in_tag = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
419 | } else if (c == '>' && in_tag) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
420 | in_tag = FALSE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
421 | } else if (!in_tag) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
422 | if (g_unichar_isspace(c)) |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
423 | preceding_space = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
424 | else |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
425 | preceding_space = FALSE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
426 | } |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
427 | } |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
428 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
429 | str = g_string_new(NULL); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
430 | in_bold = in_italic = in_tag = FALSE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
431 | preceding_space = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
432 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
433 | for (p = text; *p != '\0'; p = g_utf8_next_char(p)) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
434 | gunichar c = g_utf8_get_char(p); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
435 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
436 | if (bold_count < 2 && italic_count < 2 && !in_bold && !in_italic) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
437 | g_string_append(str, p); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
438 | return g_string_free(str, FALSE); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
439 | } |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
440 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
441 | |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
442 | if (c == '*' && !in_tag) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
443 | if (in_bold && |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
444 | (g_unichar_isspace(*(p+1))||*(p+1)=='<')) { /* This is safe in UTF-8 */ |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
445 | str = g_string_append(str, "</b>"); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
446 | in_bold = FALSE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
447 | bold_count--; |
|
16988
1aaf51bf0f23
Patch #265. Fix to Google Talk formatting
Sean Egan <seanegan@pidgin.im>
parents:
15884
diff
changeset
|
448 | } else if (preceding_space && bold_count > 1 && !g_unichar_isspace(*(p+1))) { |
|
15587
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
449 | str = g_string_append(str, "<b>"); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
450 | bold_count--; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
451 | in_bold = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
452 | } else { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
453 | str = g_string_append_unichar(str, c); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
454 | } |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
455 | preceding_space = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
456 | } else if (c == '_' && !in_tag) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
457 | if (in_italic && |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
458 | (g_unichar_isspace(*(p+1))||*(p+1)=='<')) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
459 | str = g_string_append(str, "</i>"); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
460 | italic_count--; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
461 | in_italic = FALSE; |
|
16988
1aaf51bf0f23
Patch #265. Fix to Google Talk formatting
Sean Egan <seanegan@pidgin.im>
parents:
15884
diff
changeset
|
462 | } else if (preceding_space && italic_count > 1 && !g_unichar_isspace(*(p+1))) { |
|
15587
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
463 | str = g_string_append(str, "<i>"); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
464 | italic_count--; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
465 | in_italic = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
466 | } else { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
467 | str = g_string_append_unichar(str, c); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
468 | } |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
469 | preceding_space = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
470 | } else if (c == '<' && !in_tag) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
471 | str = g_string_append_unichar(str, c); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
472 | in_tag = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
473 | } else if (c == '>' && in_tag) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
474 | str = g_string_append_unichar(str, c); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
475 | in_tag = FALSE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
476 | } else if (!in_tag) { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
477 | str = g_string_append_unichar(str, c); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
478 | if (g_unichar_isspace(c)) |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
479 | preceding_space = TRUE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
480 | else |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
481 | preceding_space = FALSE; |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
482 | } else { |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
483 | str = g_string_append_unichar(str, c); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
484 | } |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
485 | } |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
486 | return g_string_free(str, FALSE); |
|
cbedd543bfae
Google Talk uses structured text formatting where *this* is bold
Sean Egan <seanegan@pidgin.im>
parents:
15530
diff
changeset
|
487 | } |