1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
|
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
*
* Copyright (C) 1997-2002 Stuart Parmenter and others,
* See the file AUTHORS for a list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include <string.h>
#include <ctype.h>
#include "libbalsa.h"
#include "misc.h"
#include "html.h"
#include <glib/gi18n.h>
/* FIXME: The content of this file could go to message.c */
static GString *process_mime_multipart(LibBalsaMessage * message,
LibBalsaMessageBody * body,
gchar * reply_prefix_str,
gint llen, gboolean ignore_html,
gboolean flow);
/* process_mime_part:
returns string representation of given message part.
NOTE: may return NULL(!).
*/
GString *
process_mime_part(LibBalsaMessage * message, LibBalsaMessageBody * body,
gchar * reply_prefix_str, gint llen, gboolean ignore_html,
gboolean flow)
{
gchar *res = NULL;
GString *reply = NULL;
gchar *mime_type;
LibBalsaHTMLType html_type;
ssize_t allocated;
switch (libbalsa_message_body_type(body)) {
case LIBBALSA_MESSAGE_BODY_TYPE_OTHER:
case LIBBALSA_MESSAGE_BODY_TYPE_AUDIO:
case LIBBALSA_MESSAGE_BODY_TYPE_APPLICATION:
case LIBBALSA_MESSAGE_BODY_TYPE_IMAGE:
case LIBBALSA_MESSAGE_BODY_TYPE_MODEL:
case LIBBALSA_MESSAGE_BODY_TYPE_MESSAGE:
case LIBBALSA_MESSAGE_BODY_TYPE_VIDEO:
break;
case LIBBALSA_MESSAGE_BODY_TYPE_MULTIPART:
reply = process_mime_multipart(message, body, reply_prefix_str,
llen, ignore_html, flow);
break;
case LIBBALSA_MESSAGE_BODY_TYPE_TEXT:
/* don't return text/html stuff... */
mime_type = libbalsa_message_body_get_mime_type(body);
html_type = libbalsa_html_type(mime_type);
g_free(mime_type);
if (ignore_html && html_type)
break;
allocated = libbalsa_message_body_get_content(body, &res, NULL);
if (!res || allocated == -1)
return NULL;
#ifdef HAVE_HTML_WIDGET
if (html_type) {
allocated = libbalsa_html_filter(html_type, &res, allocated);
libbalsa_html_to_string(&res, allocated);
}
#endif /* HAVE_HTML_WIDGET */
if (flow && libbalsa_message_body_is_flowed(body)) {
/* we're making a `format=flowed' message, and the
* message we're quoting was flowed
*
* we'll assume it's going to the screen */
gboolean delsp = libbalsa_message_body_is_delsp(body);
reply =
libbalsa_process_text_rfc2646(res, G_MAXINT, FALSE, TRUE,
reply_prefix_str != NULL,
delsp);
g_free(res);
break;
}
if (llen > 0) {
if (reply_prefix_str)
llen -= strlen(reply_prefix_str);
libbalsa_wrap_string(res, llen);
}
if (reply_prefix_str || flow) {
gchar *str, *ptr;
/* prepend the prefix to all the lines */
reply = g_string_new("");
str = res;
do {
ptr = strchr(str, '\n');
if (ptr)
*ptr = '\0';
if (reply_prefix_str)
reply = g_string_append(reply, reply_prefix_str);
if (flow) {
gchar *p;
/* we're making a `format=flowed' message, but the
* message we're quoting was `format=fixed', so we
* must make sure all lines are `fixed'--that is,
* trim any trailing ' ' characters */
for (p = str; *p; ++p);
while (*--p == ' ');
*++p = '\0';
}
reply = g_string_append(reply, str);
reply = g_string_append_c(reply, '\n');
str = ptr;
} while (str++);
} else
reply = g_string_new(res);
g_free(res);
break;
}
return reply;
}
static GString *
process_mime_multipart(LibBalsaMessage * message,
LibBalsaMessageBody * body,
gchar * reply_prefix_str, gint llen,
gboolean ignore_html, gboolean flow)
{
LibBalsaMessageBody *part;
GString *res = NULL, *s;
for (part = body->parts; part; part = part->next) {
s = process_mime_part(message, part, reply_prefix_str, llen,
ignore_html, flow);
if (!s)
continue;
if (res) {
if (res->str[res->len - 1] != '\n')
g_string_append_c(res, '\n');
g_string_append_c(res, '\n');
g_string_append(res, s->str);
g_string_free(s, TRUE);
} else
res = s;
}
return res;
}
GString *
content2reply(LibBalsaMessageBody * root, gchar * reply_prefix_str,
gint llen, gboolean ignore_html, gboolean flow)
{
LibBalsaMessage *message = root->message;
LibBalsaMessageBody *body;
GString *reply = NULL, *res;
libbalsa_message_body_ref(message, FALSE, FALSE);
for (body = root; body; body = body->next) {
res = process_mime_part(message, body, reply_prefix_str, llen,
ignore_html, flow);
if (!res)
continue;
if (reply) {
reply = g_string_append(reply, res->str);
g_string_free(res, TRUE);
} else
reply = res;
}
libbalsa_message_body_unref(message);
return reply;
}
/*
* implement RFC2646 `text=flowed'
* first version by Emmanuel <e allaud wanadoo fr>
* this version by Peter <PeterBloomfield mindspring com>
*
* Note : the buffer pointed by par is not modified, the returned string
* is newly allocated (so we can directly wrap from message body)
*
* Note about quoted material:
* >>>text with no space separating the `>>>' and the `text...' is ugly,
* so we'll space-stuff all quoted lines both for the screen and for the
* wire (except the usenet signature separator `-- ', which wouldn't be
* recognized if stuffed!). That means we must destuff *quoted* lines
* coming off the screen, but we'll assume that leading spaces on
* *unquoted* lines were put there intentionally by the user.
* The stuffing space isn't logically part of the text of the paragraph,
* so this doesn't change the content.
* */
/* Updated 2003 to implement DelSp=Yes as in
* http://www.ietf.org/internet-drafts/draft-gellens-format-bis-01.txt
*/
/* Now documented in RFC 3676:
* http://www.ietf.org/rfc/rfc3676.txt
* or
* http://www.faqs.org/rfcs/rfc3676.html
*/
#define MAX_WIDTH 997 /* enshrined somewhere */
#define QUOTE_CHAR '>'
/*
* we'll use one routine to parse text into paragraphs
*
* if the text is coming off the wire, use the RFC specs
* if it's coming off the screen, don't destuff unquoted lines
* */
typedef struct {
gint quote_depth;
gchar *str;
} rfc2646text;
static GList *
unwrap_rfc2646(gchar * str, gboolean from_screen, gboolean delsp)
{
GList *list = NULL;
while (*str) {
/* make a line of output */
rfc2646text *text = g_new(rfc2646text, 1);
GString *string = g_string_new(NULL);
gboolean flowed;
text->quote_depth = -1;
for (flowed = TRUE; flowed && *str; ) {
/* process a line of input */
gboolean sig_sep;
gchar *p;
gint len;
for (p = str; *p == QUOTE_CHAR; p++)
/* nothing */;
len = p - str;
sig_sep = (p[0] == '-' && p[1] == '-' && p[2] == ' '
&& (p[3] == '\n'
|| (p[3] == '\r' && p[4] == '\n')));
if (text->quote_depth < 0)
text->quote_depth = len;
else if (len != text->quote_depth || sig_sep)
/* broken! a flowed line was followed by either a line
* with different quote depth, or a sig separator
*
* we'll break before updating str, so we pick up this
* line again on the next pass through this loop */
break;
if (*p == ' ' && (!from_screen || len > 0))
/* space stuffed */
p++;
/* move str to the start of the next line, if any */
for (str = p; *str && *str != '\n'; str++)
/* nothing */;
len = str - p;
if (*str)
str++;
if(len>0 && p[len-1] == '\r')
len--; /* take care of '\r\n' line endings */
flowed = (len > 0 && p[len - 1] == ' ' && !sig_sep);
if (flowed && delsp)
/* Don't include the last space. */
--len;
g_string_append_len(string, p, len);
}
text->str = g_string_free(string, FALSE);
if (flowed) {
/* Broken: remove trailing spaces. */
gchar *p = text->str;
while (*p)
p++;
while (--p >= text->str && *p == ' ')
/* nothing */ ;
*++p = '\0';
}
list = g_list_append(list, text);
}
return list;
}
/*
* we'll use one routine to wrap the paragraphs
*
* if the text is going to the wire, use the RFC specs
* if it's going to the screen, don't space-stuff unquoted lines
* */
static void
dowrap_rfc2646(GList * list, gint width, gboolean to_screen,
gboolean quote, GString * result)
{
const gint max_width = to_screen ? G_MAXINT : MAX_WIDTH - 4;
/* outer loop over paragraphs */
while (list) {
rfc2646text *text = list->data;
gchar *str;
gint qd;
str = text->str;
qd = text->quote_depth;
if (quote)
qd++;
/* one output line per middle loop */
do { /* ... while (*str); */
gboolean first_word = TRUE;
gchar *start = str;
gchar *line_break = start;
gint len = qd;
gint i;
/* start of line: emit quote string */
for (i = 0; i < qd; i++)
g_string_append_c(result, QUOTE_CHAR);
/* space-stuffing:
* - for the wire, stuffing is required for lines beginning
* with ` ', `>', or `From '
* - for the screen and for the wire, we'll use optional
* stuffing of quoted lines to provide a visual separation
* of quoting string and text
* - ...but we mustn't stuff `-- ' */
if (((!to_screen
&& (*str == ' ' || *str == QUOTE_CHAR
|| !strncmp(str, "From ", 5))) || len > 0)
&& strcmp(str, "-- ")) {
g_string_append_c(result, ' ');
++len;
}
/*
* wrapping strategy:
* break line into words, each with its trailing whitespace;
* emit words while they don't break the width;
*
* first word (with its trailing whitespace) is allowed to
* break the width
*
* one word per inner loop
* */
while (*str) {
while (*str && !isspace((int)*str)
&& (str - start) < max_width) {
len++;
str = g_utf8_next_char(str);
}
while ((str - start) < max_width && isspace((int)*str)) {
if (*str == '\t')
len += 8 - len % 8;
else
len++;
str = g_utf8_next_char(str);;
}
/*
* to avoid some unnecessary space-stuffing,
* we won't wrap at '>', ' ', or "From "
* (we already passed any spaces, so just check for '>'
* and "From ")
* */
if ((str - start) < max_width && *str
&& (*str == QUOTE_CHAR
|| !strncmp(str, "From ", 5)))
continue;
if (!*str || len > width || (str - start) >= max_width) {
/* allow an overlong first word, otherwise back up
* str */
if (len > width && !first_word)
str = line_break;
g_string_append_len(result, start, str - start);
break;
}
first_word = FALSE;
line_break = str;
} /* end of loop over words */
if (*str) { /* line separator */
if (to_screen || str == start)
g_string_append_c(result, '\n');
else /* DelSP = Yes */
g_string_append(result, " \n");
}
} while (*str); /* end of loop over output lines */
g_free(text->str);
g_free(text);
list = g_list_next(list);
if (list) /* paragraph separator */
g_string_append_c(result, '\n');
} /* end of paragraph */
}
/* GString *libbalsa_process_text_rfc2646:
re-wrap given flowed string to required width.
Parameters:
gchar * par: string to be wrapped
gint width: maximum length of wrapped line
gboolean from_screen: is par from the text input area of
a sendmsg-window?
gboolean to_screen: is the wrapped text going to be
displayed in the text area of a sendmsg-window
or of a received message window?
gboolean quote: should the wrapped lines be prefixed
with the RFC 2646 quote character '>'?
gboolean delsp: was the message formatted with DelSp=Yes?
*/
GString *
libbalsa_process_text_rfc2646(gchar * par, gint width,
gboolean from_screen,
gboolean to_screen, gboolean quote,
gboolean delsp)
{
gint len = strlen(par);
GString *result = g_string_sized_new(len);
GList *list;
list = unwrap_rfc2646(par, from_screen, delsp);
dowrap_rfc2646(list, width, to_screen, quote, result);
g_list_free(list);
return result;
}
/* libbalsa_wrap_rfc2646:
wraps given string using soft breaks according to rfc2646
convenience function, uses libbalsa_process_text_rfc2646 to do all
the work, but returns a gchar * and g_free's the string passed in
*/
gchar *
libbalsa_wrap_rfc2646(gchar * par, gint width, gboolean from_screen,
gboolean to_screen, gboolean delsp)
{
GString *result;
result = libbalsa_process_text_rfc2646(par, width, from_screen,
to_screen, FALSE, delsp);
g_free(par);
return g_string_free(result, FALSE);
}
/*
* libbalsa_wrap_view(GtkTextView * view, gint length)
*
* Wrap the text in a GtkTextView to the given line length.
*/
/* Forward references: */
static GtkTextTag *get_quote_tag(GtkTextIter * iter);
static gint get_quote_depth(GtkTextIter * iter, gchar ** quote_string);
static gchar *get_line(GtkTextBuffer * buffer, GtkTextIter * iter);
static gboolean is_in_url(GtkTextIter * iter, gint offset,
GtkTextTag * url_tag);
void
libbalsa_wrap_view(GtkTextView * view, gint length)
{
GtkTextBuffer *buffer = gtk_text_view_get_buffer(view);
GtkTextTagTable *table = gtk_text_buffer_get_tag_table(buffer);
GtkTextTag *url_tag = gtk_text_tag_table_lookup(table, "url");
GtkTextIter iter;
PangoContext *context = gtk_widget_get_pango_context(GTK_WIDGET(view));
PangoLanguage *language = pango_context_get_language(context);
PangoLogAttr *log_attrs = NULL;
gtk_text_buffer_get_start_iter(buffer, &iter);
/* Loop over original lines. */
while (!gtk_text_iter_is_end(&iter)) {
GtkTextTag *quote_tag;
gchar *quote_string;
gint quote_len;
quote_tag = get_quote_tag(&iter);
quote_len = get_quote_depth(&iter, "e_string);
if (quote_string) {
if (quote_string[quote_len])
quote_len++;
else {
gchar *tmp = g_strconcat(quote_string, " ", NULL);
g_free(quote_string);
quote_string = tmp;
}
}
/* Loop over breaks within the original line. */
while (!gtk_text_iter_ends_line(&iter)) {
gchar *line;
gint num_chars;
gint attrs_len;
gint offset;
gint len;
gint brk_offset = 0;
gunichar c = 0;
gboolean in_space = FALSE;
line = get_line(buffer, &iter);
num_chars = g_utf8_strlen(line, -1);
attrs_len = num_chars + 1;
log_attrs = g_renew(PangoLogAttr, log_attrs, attrs_len);
pango_get_log_attrs(line, -1, -1, language, log_attrs, attrs_len);
g_free(line);
for (len = offset = quote_len;
len < length && offset < num_chars; offset++) {
gtk_text_iter_set_line_offset(&iter, offset);
c = gtk_text_iter_get_char(&iter);
if (c == '\t')
len = ((len + 8) / 8) * 8;
else if (g_unichar_isprint(c))
len++;
if (log_attrs[offset].is_line_break
&& !is_in_url(&iter, offset, url_tag))
brk_offset = offset;
}
if (len < length)
break;
in_space = g_unichar_isspace(c);
if (brk_offset > quote_len && !in_space)
/* Break at the last break we saw. */
gtk_text_iter_set_line_offset(&iter, brk_offset);
else {
GtkTextIter start = iter;
/* Break at the next line break. */
if (offset <= quote_len)
offset = quote_len + 1;
while (offset < num_chars
&& (is_in_url(&iter, offset, url_tag)
|| !log_attrs[offset].is_line_break))
offset++;
if (offset >= num_chars)
/* No next line break. */
break;
/* Trim extra trailing whitespace */
gtk_text_iter_forward_char(&start);
gtk_text_buffer_delete(buffer, &start, &iter);
}
gtk_text_buffer_insert(buffer, &iter, "\n", 1);
if (quote_string)
gtk_text_buffer_insert_with_tags(buffer, &iter,
quote_string, -1,
quote_tag, NULL);
}
g_free(quote_string);
gtk_text_iter_forward_line(&iter);
}
g_free(log_attrs);
}
/* Find the quote tag, if any, at iter; doesn't move iter; returns tag
* or NULL. */
static GtkTextTag *
get_quote_tag(GtkTextIter * iter)
{
GtkTextTag *quote_tag = NULL;
GSList *list;
GSList *tag_list = gtk_text_iter_get_tags(iter);
for (list = tag_list; list; list = list->next) {
GtkTextTag *tag = list->data;
gchar *name;
g_object_get(G_OBJECT(tag), "name", &name, NULL);
if (name) {
if (!strncmp(name, "quote-", 6))
quote_tag = tag_list->data;
g_free(name);
}
}
g_slist_free(tag_list);
return quote_tag;
}
/* Move the iter over a string of consecutive '>' characters, and an
* optional ' ' character; returns the number of '>'s, and, if
* quote_string is not NULL, stores an allocated copy of the string
* (including the trailing ' ') at quote_string (or NULL, if there were
* no '>'s). */
static gint
get_quote_depth(GtkTextIter * iter, gchar ** quote_string)
{
gint quote_depth = 0;
GtkTextIter start = *iter;
while (gtk_text_iter_get_char(iter) == QUOTE_CHAR) {
quote_depth++;
gtk_text_iter_forward_char(iter);
}
if (quote_depth > 0 && gtk_text_iter_get_char(iter) == ' ')
gtk_text_iter_forward_char(iter);
if (quote_string) {
if (quote_depth > 0)
*quote_string = gtk_text_iter_get_text(&start, iter);
else
*quote_string = NULL;
}
return quote_depth;
}
/* Move the iter to the start of the line it's on; returns an allocated
* copy of the line (utf-8, including invisible characters and image
* markers). */
static gchar *
get_line(GtkTextBuffer * buffer, GtkTextIter * iter)
{
GtkTextIter end;
gtk_text_iter_set_line_offset(iter, 0);
end = *iter;
if (!gtk_text_iter_ends_line(&end))
gtk_text_iter_forward_to_line_end(&end);
return gtk_text_buffer_get_slice(buffer, iter, &end, TRUE);
}
/* Move the iter to position offset in the current line, and test
* whether it's in an URL. */
static gboolean
is_in_url(GtkTextIter * iter, gint offset, GtkTextTag * url_tag)
{
gtk_text_iter_set_line_offset(iter, offset);
return url_tag ? (gtk_text_iter_has_tag(iter, url_tag)
&& !gtk_text_iter_begins_tag(iter, url_tag)) : FALSE;
}
/* Remove soft newlines and associated quote strings from num_paras
* paragraphs in the buffer, starting at the line before iter; if
* num_paras < 0, process the whole buffer. */
/* Forward references: */
static gboolean prescanner(const gchar * p, guint len);
static void mark_urls(GtkTextBuffer * buffer, GtkTextIter * iter,
GtkTextTag * tag, const gchar * p);
#if USE_GREGEX
static GRegex *get_url_reg(void);
#else /* USE_GREGEX */
static regex_t *get_url_reg(void);
#endif /* USE_GREGEX */
void
libbalsa_unwrap_buffer(GtkTextBuffer * buffer, GtkTextIter * iter,
gint num_paras)
{
GtkTextTagTable *table = gtk_text_buffer_get_tag_table(buffer);
GtkTextTag *url_tag = gtk_text_tag_table_lookup(table, "url");
/* Check whether the previous line flowed into this one. */
gtk_text_iter_set_line_offset(iter, 0);
for (; num_paras; num_paras--) {
gint quote_depth;
GtkTextIter start;
gchar *line;
gtk_text_iter_set_line_offset(iter, 0);
quote_depth = get_quote_depth(iter, NULL);
/* Move to the end of the line, if not there already. */
if (!gtk_text_iter_ends_line(iter)
&& !gtk_text_iter_forward_to_line_end(iter))
return;
/* Save this iter as the start of a possible deletion. */
start = *iter;
/* Move to the start of the next line. */
if (!gtk_text_iter_forward_line(iter))
return;
if (num_paras < 0) {
/* This is a wrap_body call, not a continuous wrap, so we'll
* remove spaces before a hard newline. */
GtkTextIter tmp_iter;
/* Make sure it's not a usenet sig separator: */
tmp_iter = start;
if (!(gtk_text_iter_backward_char(&tmp_iter)
&& gtk_text_iter_get_char(&tmp_iter) == ' '
&& gtk_text_iter_backward_char(&tmp_iter)
&& gtk_text_iter_get_char(&tmp_iter) == '-'
&& gtk_text_iter_backward_char(&tmp_iter)
&& gtk_text_iter_get_char(&tmp_iter) == '-'
&& gtk_text_iter_get_line_offset(&tmp_iter) ==
quote_depth)) {
*iter = start;
while (gtk_text_iter_get_line_offset(&start) >
(quote_depth ? quote_depth + 1 : 0)) {
gtk_text_iter_backward_char(&start);
if (gtk_text_iter_get_char(&start) != ' ') {
gtk_text_iter_forward_char(&start);
break;
}
}
gtk_text_buffer_delete(buffer, &start, iter);
}
if (!gtk_text_iter_forward_line(iter))
return;
}
line = get_line(buffer, &start);
if (prescanner(line, strlen(line)))
mark_urls(buffer, &start, url_tag, line);
g_free(line);
}
}
/* Mark URLs in one line of the buffer */
static void
mark_urls(GtkTextBuffer * buffer, GtkTextIter * iter, GtkTextTag * tag,
const gchar * line)
{
const gchar *p = line;
const gchar * const line_end = line + strlen(line);
#if USE_GREGEX
GRegex *url_reg = get_url_reg();
GMatchInfo *url_match;
GtkTextIter start = *iter;
GtkTextIter end = *iter;
while (g_regex_match(url_reg, p, 0, &url_match)) {
gint start_pos, end_pos;
if (g_match_info_fetch_pos(url_match, 0, &start_pos, &end_pos)) {
glong offset = g_utf8_pointer_to_offset(line, p + start_pos);
gtk_text_iter_set_line_offset(&start, offset);
offset = g_utf8_pointer_to_offset(line, p + end_pos);
gtk_text_iter_set_line_offset(&end, offset);
gtk_text_buffer_apply_tag(buffer, tag, &start, &end);
p += end_pos;
}
if (!prescanner(p, line_end - p))
break;
g_match_info_free(url_match);
}
g_match_info_free(url_match);
#else /* USE_GREGEX */
regex_t *url_reg = get_url_reg();
regmatch_t url_match;
GtkTextIter start = *iter;
GtkTextIter end = *iter;
while (!regexec(url_reg, p, 1, &url_match, 0)) {
glong offset = g_utf8_pointer_to_offset(line, p + url_match.rm_so);
gtk_text_iter_set_line_offset(&start, offset);
offset = g_utf8_pointer_to_offset(line, p + url_match.rm_eo);
gtk_text_iter_set_line_offset(&end, offset);
gtk_text_buffer_apply_tag(buffer, tag, &start, &end);
p += url_match.rm_eo;
if (!prescanner(p, line_end - p))
break;
}
#endif /* USE_GREGEX */
}
/*
* End of wrap/unwrap view.
*/
/* libbalsa_insert_with_url:
* do a gtk_text_buffer_insert, but mark URL's with balsa_app.url_color
*
* prescanner:
* used to find candidates for lines containing URL's.
* Empirially, this approach is faster (by factor of 8) than scanning
* entire message with regexec. YMMV.
* s - is the line to scan.
* returns TRUE if the line may contain an URL.
*/
static gboolean
prescanner(const gchar * s, guint len)
{
gint left = len - 5;
while (--left > 0) {
switch (tolower(*s++)) {
case 'f': /* ftp:/, ftps: */
if (tolower(*s) == 't' &&
tolower(*(s + 1)) == 'p' &&
(*(s + 2) == ':' || tolower(*(s + 2)) == 's') &&
(*(s + 3) == ':' || *(s + 3) == '/'))
return TRUE;
break;
case 'h': /* http:, https */
if (tolower(*s) == 't' &&
tolower(*(s + 1)) == 't' &&
tolower(*(s + 2)) == 'p' &&
(*(s + 3) == ':' || tolower(*(s + 3)) == 's'))
return TRUE;
break;
case 'm': /* mailt */
if (tolower(*s) == 'a' &&
tolower(*(s + 1)) == 'i' &&
tolower(*(s + 2)) == 'l' && tolower(*(s + 3)) == 't')
return TRUE;
break;
case 'n': /* news:, nntp: */
if ((tolower(*s) == 'e' || tolower(*s) == 'n') &&
(tolower(*(s + 1)) == 'w' || tolower(*(s + 1)) == 't') &&
(tolower(*(s + 2)) == 's' || tolower(*(s + 2)) == 'p') &&
*(s + 3) == ':')
return TRUE;
break;
}
}
return FALSE;
}
#if USE_GREGEX
struct url_regex_info {
GRegex *url_reg;
const gchar *str;
const gchar *func;
const gchar *msg;
};
static GRegex *
get_url_helper(struct url_regex_info *info)
{
if (!info->url_reg) {
GError *err = NULL;
info->url_reg = g_regex_new(info->str, G_REGEX_CASELESS, 0, &err);
if (err) {
g_warning("%s %s: %s", info->func, info->msg, err->message);
g_error_free(err);
}
}
return info->url_reg;
}
static GRegex *
get_url_reg(void)
{
static struct url_regex_info info = {
NULL,
"(((https?|ftps?|nntp)://)|(mailto:|news:))"
"(%[0-9A-F]{2}|[-_.!~*';/?:@&=+$,#[:alnum:]])+",
__func__,
"url regex compilation failed"
};
return get_url_helper(&info);
}
static GRegex *
get_ml_url_reg(void)
{
static struct url_regex_info info = {
NULL,
"("
"%[0-9A-F]{2}|[-_.!~*';/?:@&=+$,#[:alnum:]]|[ \t]*[\r\n]+[ \t>]*"
")+"
"(%[0-9A-F]{2}|[-_.!~*';/?:@&=+$,#[:alnum:]])>",
__func__,
"multiline url regex compilation failed"
};
return get_url_helper(&info);
}
static GRegex *
get_ml_flowed_url_reg(void)
{
static struct url_regex_info info = {
NULL,
"(%[0-9A-F]{2}|[-_.!~*';/?:@&=+$,#[:alnum:]]|[ \t]+)+>",
__func__,
"multiline url regex compilation failed"
};
return get_url_helper(&info);
}
#else /* USE_GREGEX */
static regex_t *
get_url_reg(void)
{
static regex_t *url_reg = NULL;
if (!url_reg) {
/* one-time compilation of a constant url_str expression */
static const char url_str[] =
"(((https?|ftps?|nntp)://)|(mailto:|news:))"
"(%[0-9A-F]{2}|[-_.!~*';/?:@&=+$,#[:alnum:]])+";
url_reg = g_new(regex_t, 1);
if (regcomp(url_reg, url_str, REG_EXTENDED | REG_ICASE) != 0)
g_warning("libbalsa_insert_with_url: "
"url regex compilation failed.");
}
return url_reg;
}
static regex_t *
get_ml_url_reg(void)
{
static regex_t *url_reg = NULL;
if (!url_reg) {
/* one-time compilation of a constant url_str expression */
static const char url_str[] =
"("
"%[0-9A-F]{2}|[-_.!~*';/?:@&=+$,#[:alnum:]]|[ \t]*[\r\n]+[ \t>]*"
")+"
"(%[0-9A-F]{2}|[-_.!~*';/?:@&=+$,#[:alnum:]])>";
url_reg = g_new(regex_t, 1);
if (regcomp(url_reg, url_str, REG_EXTENDED | REG_ICASE) != 0)
g_warning("libbalsa_insert_with_url: "
"multiline url regex compilation failed.");
}
return url_reg;
}
static regex_t *
get_ml_flowed_url_reg(void)
{
static regex_t *url_reg = NULL;
if (!url_reg) {
/* one-time compilation of a constant url_str expression */
static const char url_str[] =
"(%[0-9A-F]{2}|[-_.!~*';/?:@&=+$,#[:alnum:]]|[ \t]+)+>";
url_reg = g_new(regex_t, 1);
if (regcomp(url_reg, url_str, REG_EXTENDED | REG_ICASE) != 0)
g_warning("libbalsa_insert_with_url: "
"multiline url regex compilation failed.");
}
return url_reg;
}
#endif /* USE_GREGEX */
gboolean
libbalsa_insert_with_url(GtkTextBuffer * buffer,
const char *chars,
guint len,
GtkTextTag * tag,
LibBalsaUrlInsertInfo *url_info)
{
GtkTextIter iter;
GtkTextTagTable *table = gtk_text_buffer_get_tag_table(buffer);
GtkTextTag *url_tag = gtk_text_tag_table_lookup(table, "url");
gboolean match;
#if USE_GREGEX
gint start_pos, end_pos;
GRegex *url_reg;
GMatchInfo *url_match;
#else /* USE_GREGEX */
regex_t *url_reg;
regmatch_t url_match;
#endif /* USE_GREGEX */
const gchar * const line_end = chars + len;
gtk_text_buffer_get_iter_at_mark(buffer, &iter,
gtk_text_buffer_get_insert(buffer));
if (url_info->ml_url_buffer) {
const gchar *url_end;
gchar *url, *q, *r;
if (!(url_end = strchr(chars, '>')) || url_end >= line_end) {
g_string_append_len(url_info->ml_url_buffer, chars,
line_end - chars);
g_string_append_c(url_info->ml_url_buffer, '\n');
return TRUE;
}
g_string_append_len(url_info->ml_url_buffer, chars,
url_end - chars);
gtk_text_buffer_insert_with_tags(buffer, &iter,
url_info->ml_url_buffer->str,
url_info->ml_url_buffer->len,
url_tag, tag, NULL);
q = url = g_new(gchar, url_info->ml_url_buffer->len);
for (r = url_info->ml_url_buffer->str; *r; r++)
if (*r > ' ')
*q++ = *r;
url_info->callback(buffer, &iter, url, q - url,
url_info->callback_data);
g_free(url);
g_string_free(url_info->ml_url_buffer, TRUE);
url_info->ml_url_buffer = NULL;
chars = url_end;
}
if (!prescanner(chars, line_end - chars)) {
gtk_text_buffer_insert_with_tags(buffer, &iter, chars,
line_end - chars, tag, NULL);
return FALSE;
}
#if USE_GREGEX
url_reg = get_url_reg();
match = g_regex_match(url_reg, chars, 0, &url_match)
&& g_match_info_fetch_pos(url_match, 0, &start_pos, &end_pos)
&& chars + start_pos < line_end;
while (match) {
gchar *spc;
gtk_text_buffer_insert_with_tags(buffer, &iter, chars,
start_pos, tag, NULL);
/* check if we hit a multi-line URL... (see RFC 1738) */
if ((start_pos > 0 && (chars[start_pos - 1] == '<')) ||
(start_pos > 4 &&
!g_ascii_strncasecmp(chars + start_pos - 5, "<URL:", 5))) {
GMatchInfo *ml_url_match;
gint ml_start_pos, ml_end_pos;
/* if the input is flowed, we may see a space at
* url_match.rm_eo - in this case the complete remainder
* of the ml uri should be in the passed buffer... */
if (url_info->buffer_is_flowed && chars[end_pos] == ' ') {
if (g_regex_match(get_ml_flowed_url_reg(), chars + end_pos,
0, &ml_url_match)
&& g_match_info_fetch_pos(ml_url_match, 0,
&ml_start_pos, &ml_end_pos)
&& ml_start_pos == 0)
end_pos += ml_end_pos - 1;
g_match_info_free(ml_url_match);
} else if (chars[end_pos] != '>') {
if (g_regex_match(get_ml_url_reg(), chars + end_pos,
0, &ml_url_match)
&& g_match_info_fetch_pos(ml_url_match, 0,
&ml_start_pos, NULL)
&& ml_start_pos == 0) {
chars += start_pos;
url_info->ml_url_buffer =
g_string_new_len(chars, line_end - chars);
g_string_append_c(url_info->ml_url_buffer, '\n');
}
g_match_info_free(ml_url_match);
if (url_info->ml_url_buffer)
return TRUE;
}
}
/* add the url - it /may/ contain spaces if the text is flowed */
if ((spc = strchr(chars + start_pos, ' ')) && spc < chars + end_pos) {
GString *uri_real = g_string_new("");
gchar *q, *buf;
q = buf = g_strndup(chars + start_pos, end_pos - start_pos);
spc = buf + (spc - (chars + start_pos));
do {
*spc = '\n';
g_string_append_len(uri_real, q, spc - q);
q = spc + 1;
} while ((spc = strchr(q, ' ')));
g_string_append(uri_real, q);
gtk_text_buffer_insert_with_tags(buffer, &iter, buf, -1,
url_tag, tag, NULL);
g_free(buf);
url_info->callback(buffer, &iter,
uri_real->str, uri_real->len,
url_info->callback_data);
g_string_free(uri_real, TRUE);
} else {
gtk_text_buffer_insert_with_tags(buffer, &iter,
chars + start_pos,
end_pos - start_pos,
url_tag, tag, NULL);
/* remember the URL and its position within the text */
url_info->callback(buffer, &iter, chars + start_pos,
end_pos - start_pos,
url_info->callback_data);
}
chars += end_pos;
if (prescanner(chars, line_end - chars)) {
g_match_info_free(url_match);
match = g_regex_match(url_reg, chars, 0, &url_match)
&& g_match_info_fetch_pos(url_match, 0, &start_pos,
&end_pos)
&& chars + start_pos < line_end;
} else
match = FALSE;
}
g_match_info_free(url_match);
#else /* USE_GREGEX */
url_reg = get_url_reg();
match = regexec(url_reg, chars, 1, &url_match, 0) == 0
&& chars + url_match.rm_so < line_end;
while (match) {
gchar *spc;
gint start_pos, end_pos;
start_pos = url_match.rm_so;
end_pos = url_match.rm_eo;
gtk_text_buffer_insert_with_tags(buffer, &iter, chars,
start_pos, tag, NULL);
/* check if we hit a multi-line URL... (see RFC 1738) */
if ((start_pos > 0 && (chars[start_pos - 1] == '<')) ||
(start_pos > 4 &&
!g_ascii_strncasecmp(chars + start_pos - 5, "<URL:", 5))) {
regex_t *ml_url_reg;
regmatch_t ml_url_match;
/* if the input is flowed, we may see a space at
* url_match.rm_eo - in this case the complete remainder
* of the ml uri should be in the passed buffer... */
if (url_info->buffer_is_flowed && chars[end_pos] == ' ') {
ml_url_reg = get_ml_flowed_url_reg();
if (!regexec(ml_url_reg, chars + end_pos, 1,
&ml_url_match, 0)
&& ml_url_match.rm_so == 0)
end_pos += ml_url_match.rm_eo - 1;
} else if (chars[end_pos] != '>') {
ml_url_reg = get_ml_url_reg();
if (!regexec(ml_url_reg, chars + end_pos, 1,
&ml_url_match, 0)
&& ml_url_match.rm_so == 0) {
chars += start_pos;
url_info->ml_url_buffer =
g_string_new_len(chars, line_end - chars);
g_string_append_c(url_info->ml_url_buffer, '\n');
return TRUE;
}
}
}
/* add the url - it /may/ contain spaces if the text is flowed */
if ((spc = strchr(chars + start_pos, ' ')) && spc < chars + end_pos) {
GString *uri_real = g_string_new("");
gchar *q, *buf;
q = buf = g_strndup(chars + start_pos, end_pos - start_pos);
spc = buf + (spc - (chars + start_pos));
do {
*spc = '\n';
g_string_append_len(uri_real, q, spc - q);
q = spc + 1;
} while ((spc = strchr(q, ' ')));
g_string_append(uri_real, q);
gtk_text_buffer_insert_with_tags(buffer, &iter, buf, -1,
url_tag, tag, NULL);
g_free(buf);
url_info->callback(buffer, &iter,
uri_real->str, uri_real->len,
url_info->callback_data);
g_string_free(uri_real, TRUE);
} else {
gtk_text_buffer_insert_with_tags(buffer, &iter,
chars + start_pos,
end_pos - start_pos,
url_tag, tag, NULL);
/* remember the URL and its position within the text */
url_info->callback(buffer, &iter, chars + start_pos,
end_pos - start_pos,
url_info->callback_data);
}
chars += end_pos;
if (prescanner(chars, line_end - chars))
match = regexec(url_reg, chars, 1, &url_match, 0) == 0
&& chars + url_match.rm_so < line_end;
else
match = FALSE;
}
#endif /* USE_GREGEX */
gtk_text_buffer_insert_with_tags(buffer, &iter, chars,
line_end - chars, tag, NULL);
return FALSE;
}
void
#if USE_GREGEX
libbalsa_unwrap_selection(GtkTextBuffer * buffer, GRegex * rex)
#else /* USE_GREGEX */
libbalsa_unwrap_selection(GtkTextBuffer * buffer, regex_t * rex)
#endif /* USE_GREGEX */
{
GtkTextIter start, end;
gchar *line;
guint quote_depth;
guint index;
GtkTextMark *selection_end;
gboolean ins_quote;
gtk_text_buffer_get_selection_bounds(buffer, &start, &end);
gtk_text_iter_order(&start, &end);
selection_end = gtk_text_buffer_create_mark(buffer, NULL, &end, FALSE);
/* Find quote depth and index of first non-quoted character. */
line = get_line(buffer, &start);
if (libbalsa_match_regex(line, rex, "e_depth, &index)) {
/* skip one regular space following the quote characters */
if (line[index] == ' ')
index++;
/* Replace quote string with standard form. */
end = start;
gtk_text_iter_set_line_index(&end, index);
gtk_text_buffer_delete(buffer, &start, &end);
do
gtk_text_buffer_insert(buffer, &start, ">", 1);
while (--quote_depth);
gtk_text_buffer_insert(buffer, &start, " ", 1);
}
g_free(line);
/* Unwrap remaining lines. */
ins_quote = FALSE;
while (gtk_text_iter_ends_line(&start)
|| gtk_text_iter_forward_to_line_end(&start)) {
gtk_text_buffer_get_iter_at_mark(buffer, &end, selection_end);
if (gtk_text_iter_compare(&start, &end) >= 0)
break;
end = start;
if (!gtk_text_iter_forward_line(&end))
break;
line = get_line(buffer, &end);
if (libbalsa_match_regex(line, rex, "e_depth, &index) &&
line[index] == ' ')
index++;
gtk_text_iter_set_line_index(&end, index);
gtk_text_buffer_delete(buffer, &start, &end);
/* empty lines separate paragraphs */
if (line[index] == '\0') {
gtk_text_buffer_insert(buffer, &start, "\n", 1);
while (quote_depth--)
gtk_text_buffer_insert(buffer, &start, ">", 1);
gtk_text_buffer_insert(buffer, &start, "\n", 1);
ins_quote = TRUE;
} else if (ins_quote) {
while (quote_depth--)
gtk_text_buffer_insert(buffer, &start, ">", 1);
gtk_text_buffer_insert(buffer, &start, " ", 1);
ins_quote = FALSE;
}
g_free(line);
/* Insert a space, if the line didn't end with one. */
if (!gtk_text_iter_starts_line(&start)) {
gtk_text_iter_backward_char(&start);
if (gtk_text_iter_get_char(&start) != ' ') {
start = end;
gtk_text_buffer_insert(buffer, &start, " ", 1);
}
}
}
}
#if USE_GREGEX
gboolean
libbalsa_match_regex(const gchar * line, GRegex * rex, guint * count,
guint * index)
{
GMatchInfo *rm;
gint c;
const gchar *p;
gint end_pos;
c = 0;
for (p = line;
g_regex_match(rex, p, 0, &rm)
&& g_match_info_fetch_pos(rm, 0, NULL, &end_pos)
&& end_pos > 0;
p += end_pos) {
c++;
g_match_info_free(rm);
}
g_match_info_free(rm);
if (count)
*count = c;
if (index)
*index = p - line;
return c > 0;
}
#else /* USE_GREGEX */
gboolean
libbalsa_match_regex(const gchar * line, regex_t * rex, guint * count,
guint * index)
{
regmatch_t rm;
gint c;
const gchar *p;
c = 0;
for (p = line; !regexec(rex, p, 1, &rm, 0) && rm.rm_eo > 0;
p += rm.rm_eo)
c++;
if (count)
*count = c;
if (index)
*index = p - line;
return c > 0;
}
#endif /* USE_GREGEX */
GString *
libbalsa_html_encode_hyperlinks(GString * paragraph)
{
GString * retval;
gchar * p;
#if USE_GREGEX
GRegex *url_reg = get_url_reg();
GMatchInfo *url_match;
#else
regex_t *url_reg = get_url_reg();
regmatch_t url_match;
#endif
gboolean match;
gchar * markup;
/* check for any url */
if (!prescanner(paragraph->str, paragraph->len)) {
markup = g_markup_escape_text(paragraph->str, -1);
g_string_assign(paragraph, markup);
g_free(markup);
return paragraph;
}
/* got some url's... */
retval = g_string_new("");
p = paragraph->str;
#if USE_GREGEX
match = g_regex_match(url_reg, p, 0, &url_match);
#else
match = regexec(url_reg, p, 1, &url_match, 0) == 0;
#endif
while (match) {
gint start_pos, end_pos;
#if USE_GREGEX
if (!g_match_info_fetch_pos(url_match, 0, &start_pos, &end_pos))
break;
#else
start_pos = url_match.rm_so;
end_pos = url_match.rm_eo;
#endif
/* add the url to the result */
if (start_pos > 0) {
markup = g_markup_escape_text(p, start_pos);
retval = g_string_append(retval, markup);
g_free(markup);
}
retval = g_string_append(retval, "<a href=\"");
retval = g_string_append_len(retval, p + start_pos, end_pos - start_pos);
retval = g_string_append(retval, "\">");
retval = g_string_append_len(retval, p + start_pos, end_pos - start_pos);
retval = g_string_append(retval, "</a>");
/* find next (if any) */
p += end_pos;
if (prescanner(p, paragraph->len - (p - paragraph->str))) {
#if USE_GREGEX
g_match_info_free(url_match);
match = g_regex_match(url_reg, p, 0, &url_match);
#else
match = regexec(url_reg, p, 1, &url_match, 0) == 0;
#endif
} else
match = FALSE;
}
#if USE_GREGEX
g_match_info_free(url_match);
#endif /* USE_GREGEX */
/* copy remainder */
if (*p != '\0') {
markup = g_markup_escape_text(p, -1);
retval = g_string_append(retval, markup);
g_free(markup);
}
/* done - free original, return new */
g_string_free(paragraph, TRUE);
return retval;
}
gchar *
libbalsa_text_to_html(const gchar * title, const gchar * body, const gchar * lang)
{
GString * html_body =
g_string_new("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n");
gchar * html_subject;
const gchar * start = body;
gchar * html_lang;
gchar * p;
/* set the html header, including the primary language and the title */
if (lang) {
html_lang = g_strdup(lang);
if ((p = strchr(html_lang, '_')))
*p = '-';
} else
html_lang = g_strdup("x-unknown");
html_subject = g_markup_escape_text(title, -1);
g_string_append_printf(html_body,
"<html lang=\"%s\"><head>\n"
"<title>%s</title>\n"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n"
"<style type=\"text/css\">\n"
" p { margin-top: 0px; margin-bottom: 0px; }\n"
"</style></head>\n"
"<body>\n", html_lang, html_subject);
g_free(html_subject);
g_free(html_lang);
/* add the lines of the message body */
while (*start) {
const gchar * eol = strchr(start, '\n');
const gchar * p = start;
PangoDirection direction = PANGO_DIRECTION_NEUTRAL;
GString * html;
gsize idx;
if (!eol)
eol = start + strlen(start);
/* find the first real char to determine the paragraph direction */
while (p < eol && direction == PANGO_DIRECTION_NEUTRAL) {
direction = pango_unichar_direction(g_utf8_get_char(p));
p = g_utf8_next_char(p);
}
/* html escape the line */
html = g_string_new_len(start, eol - start);
/* encode hyperlinks */
html = libbalsa_html_encode_hyperlinks(html);
/* replace a series of n spaces by (n - 1) and one space */
idx = 0;
while (idx < html->len) {
if (html->str[idx] == ' ' && (idx == 0 || html->str[idx + 1] == ' ')) {
html->str[idx++] = '&';
html = g_string_insert(html, idx, "nbsp;");
idx += 5;
} else
idx = g_utf8_next_char(html->str + idx) - html->str;
}
/* append the paragraph, always stating the proper direction */
g_string_append_printf(html_body, "<p dir=\"%s\">%s</p>\n",
direction == PANGO_DIRECTION_RTL ? "rtl" : "ltr",
*html->str ? html->str : " ");
g_string_free(html, TRUE);
/* next line */
start = eol;
if (*start)
start++;
}
/* close the html context */
html_body = g_string_append(html_body, "</body></html>\n");
/* return the utf-8 encoded text/html */
return g_string_free(html_body, FALSE);
}
|