root / libsylph / procmime.c @ 690
History | View | Annotate | Download (28.1 kB)
| 1 | /*
|
|---|---|
| 2 | * LibSylph -- E-Mail client library |
| 3 | * Copyright (C) 1999-2005 Hiroyuki Yamamoto |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H
|
| 21 | # include "config.h" |
| 22 | #endif
|
| 23 | |
| 24 | #include "defs.h" |
| 25 | |
| 26 | #include <glib.h> |
| 27 | #include <glib/gi18n.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <locale.h> |
| 31 | #include <ctype.h> |
| 32 | |
| 33 | #include "procmime.h" |
| 34 | #include "procheader.h" |
| 35 | #include "base64.h" |
| 36 | #include "quoted-printable.h" |
| 37 | #include "uuencode.h" |
| 38 | #include "html.h" |
| 39 | #include "codeconv.h" |
| 40 | #include "utils.h" |
| 41 | #include "prefs_common.h" |
| 42 | |
| 43 | static GHashTable *procmime_get_mime_type_table (void); |
| 44 | static GList *procmime_get_mime_type_list (const gchar *file); |
| 45 | |
| 46 | |
| 47 | MimeInfo *procmime_mimeinfo_new(void)
|
| 48 | {
|
| 49 | MimeInfo *mimeinfo; |
| 50 | |
| 51 | mimeinfo = g_new0(MimeInfo, 1);
|
| 52 | mimeinfo->mime_type = MIME_UNKNOWN; |
| 53 | mimeinfo->encoding_type = ENC_UNKNOWN; |
| 54 | |
| 55 | return mimeinfo;
|
| 56 | } |
| 57 | |
| 58 | void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
|
| 59 | {
|
| 60 | while (mimeinfo != NULL) { |
| 61 | MimeInfo *next; |
| 62 | |
| 63 | g_free(mimeinfo->encoding); |
| 64 | g_free(mimeinfo->content_type); |
| 65 | g_free(mimeinfo->charset); |
| 66 | g_free(mimeinfo->name); |
| 67 | g_free(mimeinfo->boundary); |
| 68 | g_free(mimeinfo->content_disposition); |
| 69 | g_free(mimeinfo->filename); |
| 70 | |
| 71 | g_free(mimeinfo->plaintextfile); |
| 72 | g_free(mimeinfo->sigstatus); |
| 73 | g_free(mimeinfo->sigstatus_full); |
| 74 | |
| 75 | procmime_mimeinfo_free_all(mimeinfo->sub); |
| 76 | procmime_mimeinfo_free_all(mimeinfo->children); |
| 77 | procmime_mimeinfo_free_all(mimeinfo->plaintext); |
| 78 | |
| 79 | next = mimeinfo->next; |
| 80 | g_free(mimeinfo); |
| 81 | mimeinfo = next; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo) |
| 86 | {
|
| 87 | MimeInfo *child = parent->children; |
| 88 | |
| 89 | if (!child)
|
| 90 | parent->children = mimeinfo; |
| 91 | else {
|
| 92 | while (child->next != NULL) |
| 93 | child = child->next; |
| 94 | |
| 95 | child->next = mimeinfo; |
| 96 | } |
| 97 | |
| 98 | mimeinfo->parent = parent; |
| 99 | mimeinfo->level = parent->level + 1;
|
| 100 | |
| 101 | return mimeinfo;
|
| 102 | } |
| 103 | |
| 104 | void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
|
| 105 | {
|
| 106 | MimeInfo *parent = old->parent; |
| 107 | MimeInfo *child; |
| 108 | |
| 109 | g_return_if_fail(parent != NULL);
|
| 110 | g_return_if_fail(new->next == NULL);
|
| 111 | |
| 112 | for (child = parent->children; child && child != old;
|
| 113 | child = child->next) |
| 114 | ; |
| 115 | if (!child) {
|
| 116 | g_warning("oops: parent can't find it's own child");
|
| 117 | return;
|
| 118 | } |
| 119 | procmime_mimeinfo_free_all(old); |
| 120 | |
| 121 | if (child == parent->children) {
|
| 122 | new->next = parent->children->next; |
| 123 | parent->children = new; |
| 124 | } else {
|
| 125 | new->next = child->next; |
| 126 | child = new; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo) |
| 131 | {
|
| 132 | if (!mimeinfo) return NULL; |
| 133 | |
| 134 | if (mimeinfo->children)
|
| 135 | return mimeinfo->children;
|
| 136 | if (mimeinfo->sub)
|
| 137 | return mimeinfo->sub;
|
| 138 | if (mimeinfo->next)
|
| 139 | return mimeinfo->next;
|
| 140 | |
| 141 | if (mimeinfo->main) {
|
| 142 | mimeinfo = mimeinfo->main; |
| 143 | if (mimeinfo->next)
|
| 144 | return mimeinfo->next;
|
| 145 | } |
| 146 | |
| 147 | for (mimeinfo = mimeinfo->parent; mimeinfo != NULL; |
| 148 | mimeinfo = mimeinfo->parent) {
|
| 149 | if (mimeinfo->next)
|
| 150 | return mimeinfo->next;
|
| 151 | if (mimeinfo->main) {
|
| 152 | mimeinfo = mimeinfo->main; |
| 153 | if (mimeinfo->next)
|
| 154 | return mimeinfo->next;
|
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | #if 0
|
| 162 | void procmime_dump_mimeinfo(MimeInfo *mimeinfo) |
| 163 | {
|
| 164 | gint i; |
| 165 | |
| 166 | g_print("\n");
|
| 167 | |
| 168 | for (; mimeinfo != NULL; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
|
| 169 | for (i = 0; i < mimeinfo->level; i++) |
| 170 | g_print(" ");
|
| 171 | g_print("%s%s\n", mimeinfo->main ? "sub: " : "",
|
| 172 | mimeinfo->content_type); |
| 173 | } |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | MimeInfo *procmime_scan_message(MsgInfo *msginfo) |
| 178 | {
|
| 179 | FILE *fp; |
| 180 | MimeInfo *mimeinfo; |
| 181 | |
| 182 | g_return_val_if_fail(msginfo != NULL, NULL); |
| 183 | |
| 184 | if ((fp = procmsg_open_message_decrypted(msginfo, &mimeinfo)) == NULL) |
| 185 | return NULL; |
| 186 | |
| 187 | if (mimeinfo) {
|
| 188 | mimeinfo->size = msginfo->size; |
| 189 | mimeinfo->content_size = get_left_file_size(fp); |
| 190 | if (mimeinfo->encoding_type == ENC_BASE64)
|
| 191 | mimeinfo->content_size = mimeinfo->content_size / 4 * 3; |
| 192 | if (mimeinfo->mime_type == MIME_MULTIPART ||
|
| 193 | mimeinfo->mime_type == MIME_MESSAGE_RFC822) |
| 194 | procmime_scan_multipart_message(mimeinfo, fp); |
| 195 | } |
| 196 | |
| 197 | fclose(fp); |
| 198 | |
| 199 | return mimeinfo;
|
| 200 | } |
| 201 | |
| 202 | void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
|
| 203 | {
|
| 204 | gchar *p; |
| 205 | gchar *boundary; |
| 206 | gint boundary_len = 0;
|
| 207 | gchar buf[BUFFSIZE]; |
| 208 | glong fpos, prev_fpos; |
| 209 | |
| 210 | g_return_if_fail(mimeinfo != NULL);
|
| 211 | g_return_if_fail(mimeinfo->mime_type == MIME_MULTIPART || |
| 212 | mimeinfo->mime_type == MIME_MESSAGE_RFC822); |
| 213 | |
| 214 | if (mimeinfo->mime_type == MIME_MULTIPART) {
|
| 215 | g_return_if_fail(mimeinfo->boundary != NULL);
|
| 216 | g_return_if_fail(mimeinfo->sub == NULL);
|
| 217 | } |
| 218 | g_return_if_fail(fp != NULL);
|
| 219 | |
| 220 | boundary = mimeinfo->boundary; |
| 221 | |
| 222 | if (boundary) {
|
| 223 | boundary_len = strlen(boundary); |
| 224 | |
| 225 | /* look for first boundary */
|
| 226 | while ((p = fgets(buf, sizeof(buf), fp)) != NULL) |
| 227 | if (IS_BOUNDARY(buf, boundary, boundary_len)) break; |
| 228 | if (!p) return; |
| 229 | } else if (mimeinfo->parent && mimeinfo->parent->boundary) { |
| 230 | boundary = mimeinfo->parent->boundary; |
| 231 | boundary_len = strlen(boundary); |
| 232 | } |
| 233 | |
| 234 | if ((fpos = ftell(fp)) < 0) { |
| 235 | perror("ftell");
|
| 236 | return;
|
| 237 | } |
| 238 | |
| 239 | for (;;) {
|
| 240 | MimeInfo *partinfo; |
| 241 | gboolean eom = FALSE; |
| 242 | glong content_pos; |
| 243 | gboolean is_base64; |
| 244 | gint len; |
| 245 | guint b64_content_len = 0;
|
| 246 | gint b64_pad_len = 0;
|
| 247 | |
| 248 | prev_fpos = fpos; |
| 249 | debug_print("prev_fpos: %ld\n", fpos);
|
| 250 | |
| 251 | /* scan part header */
|
| 252 | if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
|
| 253 | MimeInfo *sub; |
| 254 | |
| 255 | mimeinfo->sub = sub = procmime_scan_mime_header(fp); |
| 256 | if (!sub) break; |
| 257 | |
| 258 | debug_print("message/rfc822 part found\n");
|
| 259 | sub->level = mimeinfo->level + 1;
|
| 260 | sub->parent = mimeinfo->parent; |
| 261 | sub->main = mimeinfo; |
| 262 | |
| 263 | partinfo = sub; |
| 264 | } else {
|
| 265 | partinfo = procmime_scan_mime_header(fp); |
| 266 | if (!partinfo) break; |
| 267 | procmime_mimeinfo_insert(mimeinfo, partinfo); |
| 268 | debug_print("content-type: %s\n",
|
| 269 | partinfo->content_type); |
| 270 | } |
| 271 | |
| 272 | /* begin content */
|
| 273 | content_pos = ftell(fp); |
| 274 | debug_print("content_pos: %ld\n", content_pos);
|
| 275 | |
| 276 | if (partinfo->mime_type == MIME_MULTIPART ||
|
| 277 | partinfo->mime_type == MIME_MESSAGE_RFC822) {
|
| 278 | if (partinfo->level < 8) |
| 279 | procmime_scan_multipart_message(partinfo, fp); |
| 280 | } |
| 281 | |
| 282 | /* look for next boundary */
|
| 283 | buf[0] = '\0'; |
| 284 | is_base64 = partinfo->encoding_type == ENC_BASE64; |
| 285 | while ((p = fgets(buf, sizeof(buf), fp)) != NULL) { |
| 286 | if (IS_BOUNDARY(buf, boundary, boundary_len)) {
|
| 287 | if (buf[2 + boundary_len] == '-' && |
| 288 | buf[2 + boundary_len + 1] == '-') |
| 289 | eom = TRUE; |
| 290 | break;
|
| 291 | } else if (is_base64) { |
| 292 | const gchar *s;
|
| 293 | for (s = buf; *s && *s != '\r' && *s != '\n'; |
| 294 | ++s) |
| 295 | if (*s == '=') |
| 296 | ++b64_pad_len; |
| 297 | b64_content_len += s - buf; |
| 298 | } |
| 299 | } |
| 300 | if (p == NULL) { |
| 301 | /* broken MIME, or single part MIME message */
|
| 302 | buf[0] = '\0'; |
| 303 | eom = TRUE; |
| 304 | } |
| 305 | debug_print("boundary: %s\n", buf);
|
| 306 | |
| 307 | fpos = ftell(fp); |
| 308 | debug_print("fpos: %ld\n", fpos);
|
| 309 | |
| 310 | len = strlen(buf); |
| 311 | partinfo->size = fpos - prev_fpos - len; |
| 312 | if (is_base64)
|
| 313 | partinfo->content_size = |
| 314 | b64_content_len / 4 * 3 - b64_pad_len; |
| 315 | else
|
| 316 | partinfo->content_size = fpos - content_pos - len; |
| 317 | debug_print("partinfo->size: %d\n", partinfo->size);
|
| 318 | debug_print("partinfo->content_size: %d\n",
|
| 319 | partinfo->content_size); |
| 320 | if (partinfo->sub && !partinfo->sub->sub &&
|
| 321 | !partinfo->sub->children) {
|
| 322 | partinfo->sub->size = |
| 323 | fpos - partinfo->sub->fpos - strlen(buf); |
| 324 | debug_print("partinfo->sub->size: %d\n",
|
| 325 | partinfo->sub->size); |
| 326 | } |
| 327 | |
| 328 | if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
|
| 329 | if (len > 0 && fseek(fp, fpos - len, SEEK_SET) < 0) |
| 330 | perror("fseek");
|
| 331 | break;
|
| 332 | } |
| 333 | |
| 334 | if (eom) break; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding) |
| 339 | {
|
| 340 | gchar *buf; |
| 341 | |
| 342 | Xstrdup_a(buf, encoding, return);
|
| 343 | |
| 344 | g_free(mimeinfo->encoding); |
| 345 | |
| 346 | mimeinfo->encoding = g_strdup(g_strstrip(buf)); |
| 347 | if (!g_ascii_strcasecmp(buf, "7bit")) |
| 348 | mimeinfo->encoding_type = ENC_7BIT; |
| 349 | else if (!g_ascii_strcasecmp(buf, "8bit")) |
| 350 | mimeinfo->encoding_type = ENC_8BIT; |
| 351 | else if (!g_ascii_strcasecmp(buf, "quoted-printable")) |
| 352 | mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE; |
| 353 | else if (!g_ascii_strcasecmp(buf, "base64")) |
| 354 | mimeinfo->encoding_type = ENC_BASE64; |
| 355 | else if (!g_ascii_strcasecmp(buf, "x-uuencode")) |
| 356 | mimeinfo->encoding_type = ENC_X_UUENCODE; |
| 357 | else
|
| 358 | mimeinfo->encoding_type = ENC_UNKNOWN; |
| 359 | |
| 360 | } |
| 361 | |
| 362 | void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type) |
| 363 | {
|
| 364 | g_free(mimeinfo->content_type); |
| 365 | g_free(mimeinfo->charset); |
| 366 | g_free(mimeinfo->name); |
| 367 | g_free(mimeinfo->boundary); |
| 368 | mimeinfo->content_type = NULL;
|
| 369 | mimeinfo->charset = NULL;
|
| 370 | mimeinfo->name = NULL;
|
| 371 | mimeinfo->boundary = NULL;
|
| 372 | |
| 373 | procmime_scan_content_type_str(content_type, &mimeinfo->content_type, |
| 374 | &mimeinfo->charset, &mimeinfo->name, |
| 375 | &mimeinfo->boundary); |
| 376 | |
| 377 | mimeinfo->mime_type = procmime_scan_mime_type(mimeinfo->content_type); |
| 378 | if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
|
| 379 | mimeinfo->mime_type = MIME_TEXT; |
| 380 | } |
| 381 | |
| 382 | void procmime_scan_content_type_str(const gchar *content_type, |
| 383 | gchar **mime_type, gchar **charset, |
| 384 | gchar **name, gchar **boundary) |
| 385 | {
|
| 386 | gchar *delim, *p; |
| 387 | gchar *buf; |
| 388 | |
| 389 | Xstrdup_a(buf, content_type, return);
|
| 390 | |
| 391 | if ((delim = strchr(buf, ';'))) *delim = '\0'; |
| 392 | if (mime_type)
|
| 393 | *mime_type = g_strdup(g_strstrip(buf)); |
| 394 | |
| 395 | if (!delim) return; |
| 396 | p = delim + 1;
|
| 397 | |
| 398 | for (;;) {
|
| 399 | gchar *eq; |
| 400 | gchar *attr, *value; |
| 401 | |
| 402 | if ((delim = strchr(p, ';'))) *delim = '\0'; |
| 403 | |
| 404 | if (!(eq = strchr(p, '='))) break; |
| 405 | |
| 406 | *eq = '\0';
|
| 407 | attr = p; |
| 408 | g_strstrip(attr); |
| 409 | value = eq + 1;
|
| 410 | g_strstrip(value); |
| 411 | |
| 412 | if (*value == '"') |
| 413 | extract_quote(value, '"');
|
| 414 | else {
|
| 415 | eliminate_parenthesis(value, '(', ')'); |
| 416 | g_strstrip(value); |
| 417 | } |
| 418 | |
| 419 | if (*value) {
|
| 420 | if (charset && !g_ascii_strcasecmp(attr, "charset")) |
| 421 | *charset = g_strdup(value); |
| 422 | else if (name && !g_ascii_strcasecmp(attr, "name")) |
| 423 | *name = conv_unmime_header(value, NULL);
|
| 424 | else if (boundary && |
| 425 | !g_ascii_strcasecmp(attr, "boundary"))
|
| 426 | *boundary = g_strdup(value); |
| 427 | } |
| 428 | |
| 429 | if (!delim) break; |
| 430 | p = delim + 1;
|
| 431 | } |
| 432 | } |
| 433 | |
| 434 | void procmime_scan_content_disposition(MimeInfo *mimeinfo,
|
| 435 | const gchar *content_disposition)
|
| 436 | {
|
| 437 | gchar *delim, *p, *dispos; |
| 438 | gchar *buf; |
| 439 | |
| 440 | Xstrdup_a(buf, content_disposition, return);
|
| 441 | |
| 442 | if ((delim = strchr(buf, ';'))) *delim = '\0'; |
| 443 | mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf)); |
| 444 | |
| 445 | if (!delim) return; |
| 446 | p = delim + 1;
|
| 447 | |
| 448 | for (;;) {
|
| 449 | gchar *eq; |
| 450 | gchar *attr, *value; |
| 451 | |
| 452 | if ((delim = strchr(p, ';'))) *delim = '\0'; |
| 453 | |
| 454 | if (!(eq = strchr(p, '='))) break; |
| 455 | |
| 456 | *eq = '\0';
|
| 457 | attr = p; |
| 458 | g_strstrip(attr); |
| 459 | value = eq + 1;
|
| 460 | g_strstrip(value); |
| 461 | |
| 462 | if (*value == '"') |
| 463 | extract_quote(value, '"');
|
| 464 | else {
|
| 465 | eliminate_parenthesis(value, '(', ')'); |
| 466 | g_strstrip(value); |
| 467 | } |
| 468 | |
| 469 | if (*value) {
|
| 470 | if (!g_ascii_strcasecmp(attr, "filename")) { |
| 471 | g_free(mimeinfo->filename); |
| 472 | mimeinfo->filename = |
| 473 | conv_unmime_header(value, NULL);
|
| 474 | break;
|
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if (!delim) break; |
| 479 | p = delim + 1;
|
| 480 | } |
| 481 | } |
| 482 | |
| 483 | enum
|
| 484 | {
|
| 485 | H_CONTENT_TRANSFER_ENCODING = 0,
|
| 486 | H_CONTENT_TYPE = 1,
|
| 487 | H_CONTENT_DISPOSITION = 2
|
| 488 | }; |
| 489 | |
| 490 | MimeInfo *procmime_scan_mime_header(FILE *fp) |
| 491 | {
|
| 492 | static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:", |
| 493 | NULL, FALSE},
|
| 494 | {"Content-Type:", NULL, TRUE},
|
| 495 | {"Content-Disposition:",
|
| 496 | NULL, TRUE},
|
| 497 | {NULL, NULL, FALSE}};
|
| 498 | gchar buf[BUFFSIZE]; |
| 499 | gint hnum; |
| 500 | HeaderEntry *hp; |
| 501 | MimeInfo *mimeinfo; |
| 502 | |
| 503 | g_return_val_if_fail(fp != NULL, NULL); |
| 504 | |
| 505 | mimeinfo = procmime_mimeinfo_new(); |
| 506 | mimeinfo->mime_type = MIME_TEXT; |
| 507 | mimeinfo->encoding_type = ENC_7BIT; |
| 508 | mimeinfo->fpos = ftell(fp); |
| 509 | |
| 510 | while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry)) |
| 511 | != -1) {
|
| 512 | hp = hentry + hnum; |
| 513 | |
| 514 | if (H_CONTENT_TRANSFER_ENCODING == hnum) {
|
| 515 | procmime_scan_encoding |
| 516 | (mimeinfo, buf + strlen(hp->name)); |
| 517 | } else if (H_CONTENT_TYPE == hnum) { |
| 518 | procmime_scan_content_type |
| 519 | (mimeinfo, buf + strlen(hp->name)); |
| 520 | } else if (H_CONTENT_DISPOSITION == hnum) { |
| 521 | procmime_scan_content_disposition |
| 522 | (mimeinfo, buf + strlen(hp->name)); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
|
| 527 | mimeinfo->name) {
|
| 528 | const gchar *type;
|
| 529 | type = procmime_get_mime_type(mimeinfo->name); |
| 530 | if (type)
|
| 531 | mimeinfo->mime_type = procmime_scan_mime_type(type); |
| 532 | } |
| 533 | |
| 534 | if (!mimeinfo->content_type)
|
| 535 | mimeinfo->content_type = g_strdup("text/plain");
|
| 536 | |
| 537 | return mimeinfo;
|
| 538 | } |
| 539 | |
| 540 | FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo) |
| 541 | {
|
| 542 | gchar buf[BUFFSIZE]; |
| 543 | gchar *boundary = NULL;
|
| 544 | gint boundary_len = 0;
|
| 545 | gboolean tmp_file = FALSE; |
| 546 | |
| 547 | g_return_val_if_fail(infp != NULL, NULL); |
| 548 | g_return_val_if_fail(mimeinfo != NULL, NULL); |
| 549 | |
| 550 | if (!outfp) {
|
| 551 | outfp = my_tmpfile(); |
| 552 | if (!outfp) {
|
| 553 | perror("tmpfile");
|
| 554 | return NULL; |
| 555 | } |
| 556 | tmp_file = TRUE; |
| 557 | } |
| 558 | |
| 559 | if (mimeinfo->parent && mimeinfo->parent->boundary) {
|
| 560 | boundary = mimeinfo->parent->boundary; |
| 561 | boundary_len = strlen(boundary); |
| 562 | } |
| 563 | |
| 564 | if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
|
| 565 | while (fgets(buf, sizeof(buf), infp) != NULL && |
| 566 | (!boundary || |
| 567 | !IS_BOUNDARY(buf, boundary, boundary_len))) {
|
| 568 | gint len; |
| 569 | len = qp_decode_line(buf); |
| 570 | fwrite(buf, len, 1, outfp);
|
| 571 | } |
| 572 | } else if (mimeinfo->encoding_type == ENC_BASE64) { |
| 573 | gchar outbuf[BUFFSIZE]; |
| 574 | gint len; |
| 575 | Base64Decoder *decoder; |
| 576 | gboolean uncanonicalize = FALSE; |
| 577 | FILE *tmpfp = outfp; |
| 578 | ContentType content_type; |
| 579 | |
| 580 | content_type = procmime_scan_mime_type(mimeinfo->content_type); |
| 581 | if (content_type == MIME_TEXT ||
|
| 582 | content_type == MIME_TEXT_HTML || |
| 583 | content_type == MIME_MESSAGE_RFC822) {
|
| 584 | uncanonicalize = TRUE; |
| 585 | tmpfp = my_tmpfile(); |
| 586 | if (!tmpfp) {
|
| 587 | perror("tmpfile");
|
| 588 | if (tmp_file) fclose(outfp);
|
| 589 | return NULL; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | decoder = base64_decoder_new(); |
| 594 | while (fgets(buf, sizeof(buf), infp) != NULL && |
| 595 | (!boundary || |
| 596 | !IS_BOUNDARY(buf, boundary, boundary_len))) {
|
| 597 | len = base64_decoder_decode(decoder, buf, |
| 598 | (guchar *)outbuf); |
| 599 | if (len < 0) { |
| 600 | g_warning("Bad BASE64 content\n");
|
| 601 | break;
|
| 602 | } |
| 603 | fwrite(outbuf, sizeof(gchar), len, tmpfp);
|
| 604 | } |
| 605 | base64_decoder_free(decoder); |
| 606 | |
| 607 | if (uncanonicalize) {
|
| 608 | rewind(tmpfp); |
| 609 | while (fgets(buf, sizeof(buf), tmpfp) != NULL) { |
| 610 | strcrchomp(buf); |
| 611 | fputs(buf, outfp); |
| 612 | } |
| 613 | fclose(tmpfp); |
| 614 | } |
| 615 | } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) { |
| 616 | gchar outbuf[BUFFSIZE]; |
| 617 | gint len; |
| 618 | gboolean flag = FALSE; |
| 619 | |
| 620 | while (fgets(buf, sizeof(buf), infp) != NULL && |
| 621 | (!boundary || |
| 622 | !IS_BOUNDARY(buf, boundary, boundary_len))) {
|
| 623 | if(!flag && strncmp(buf,"begin ", 6)) continue; |
| 624 | |
| 625 | if (flag) {
|
| 626 | len = fromuutobits(outbuf, buf); |
| 627 | if (len <= 0) { |
| 628 | if (len < 0) |
| 629 | g_warning("Bad UUENCODE content(%d)\n", len);
|
| 630 | break;
|
| 631 | } |
| 632 | fwrite(outbuf, sizeof(gchar), len, outfp);
|
| 633 | } else
|
| 634 | flag = TRUE; |
| 635 | } |
| 636 | } else {
|
| 637 | while (fgets(buf, sizeof(buf), infp) != NULL && |
| 638 | (!boundary || |
| 639 | !IS_BOUNDARY(buf, boundary, boundary_len))) {
|
| 640 | fputs(buf, outfp); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | if (tmp_file) rewind(outfp);
|
| 645 | return outfp;
|
| 646 | } |
| 647 | |
| 648 | gint procmime_get_part(const gchar *outfile, const gchar *infile, |
| 649 | MimeInfo *mimeinfo) |
| 650 | {
|
| 651 | FILE *infp; |
| 652 | gint ret; |
| 653 | |
| 654 | g_return_val_if_fail(outfile != NULL, -1); |
| 655 | g_return_val_if_fail(infile != NULL, -1); |
| 656 | g_return_val_if_fail(mimeinfo != NULL, -1); |
| 657 | |
| 658 | if ((infp = g_fopen(infile, "rb")) == NULL) { |
| 659 | FILE_OP_ERROR(infile, "fopen");
|
| 660 | return -1; |
| 661 | } |
| 662 | ret = procmime_get_part_fp(outfile, infp, mimeinfo); |
| 663 | fclose(infp); |
| 664 | |
| 665 | return ret;
|
| 666 | } |
| 667 | |
| 668 | gint procmime_get_part_fp(const gchar *outfile, FILE *infp, MimeInfo *mimeinfo)
|
| 669 | {
|
| 670 | FILE *outfp; |
| 671 | gchar buf[BUFFSIZE]; |
| 672 | |
| 673 | g_return_val_if_fail(outfile != NULL, -1); |
| 674 | g_return_val_if_fail(infp != NULL, -1); |
| 675 | g_return_val_if_fail(mimeinfo != NULL, -1); |
| 676 | |
| 677 | if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) { |
| 678 | FILE_OP_ERROR("procmime_get_part_fp()", "fseek"); |
| 679 | return -1; |
| 680 | } |
| 681 | if ((outfp = g_fopen(outfile, "wb")) == NULL) { |
| 682 | FILE_OP_ERROR(outfile, "fopen");
|
| 683 | return -1; |
| 684 | } |
| 685 | |
| 686 | while (fgets(buf, sizeof(buf), infp) != NULL) |
| 687 | if (buf[0] == '\r' || buf[0] == '\n') break; |
| 688 | |
| 689 | procmime_decode_content(outfp, infp, mimeinfo); |
| 690 | |
| 691 | if (fclose(outfp) == EOF) { |
| 692 | FILE_OP_ERROR(outfile, "fclose");
|
| 693 | g_unlink(outfile); |
| 694 | return -1; |
| 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | gint procmime_get_all_parts(const gchar *dir, const gchar *infile, |
| 701 | MimeInfo *mimeinfo) |
| 702 | {
|
| 703 | FILE *fp; |
| 704 | MimeInfo *partinfo; |
| 705 | gchar *base, *filename; |
| 706 | |
| 707 | g_return_val_if_fail(dir != NULL, -1); |
| 708 | g_return_val_if_fail(infile != NULL, -1); |
| 709 | g_return_val_if_fail(mimeinfo != NULL, -1); |
| 710 | |
| 711 | if (!is_dir_exist(dir)) {
|
| 712 | g_warning("%s: directory not exist.\n", dir);
|
| 713 | return -1; |
| 714 | } |
| 715 | |
| 716 | if ((fp = g_fopen(infile, "rb")) == NULL) { |
| 717 | FILE_OP_ERROR(infile, "fopen");
|
| 718 | return -1; |
| 719 | } |
| 720 | |
| 721 | for (partinfo = mimeinfo; partinfo != NULL; |
| 722 | partinfo = procmime_mimeinfo_next(partinfo)) {
|
| 723 | if (partinfo->filename || partinfo->name) {
|
| 724 | gint count = 1;
|
| 725 | |
| 726 | base = procmime_get_part_file_name(partinfo); |
| 727 | filename = g_strconcat(dir, G_DIR_SEPARATOR_S, base, |
| 728 | NULL);
|
| 729 | |
| 730 | while (is_file_entry_exist(filename)) {
|
| 731 | gchar *base_alt; |
| 732 | |
| 733 | base_alt = get_alt_filename(base, count++); |
| 734 | g_free(filename); |
| 735 | filename = g_strconcat |
| 736 | (dir, G_DIR_SEPARATOR_S, base_alt, |
| 737 | NULL);
|
| 738 | g_free(base_alt); |
| 739 | } |
| 740 | |
| 741 | procmime_get_part_fp(filename, fp, partinfo); |
| 742 | |
| 743 | g_free(filename); |
| 744 | g_free(base); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | fclose(fp); |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp, |
| 754 | const gchar *encoding)
|
| 755 | {
|
| 756 | FILE *tmpfp, *outfp; |
| 757 | const gchar *src_encoding;
|
| 758 | gboolean conv_fail = FALSE; |
| 759 | gchar buf[BUFFSIZE]; |
| 760 | |
| 761 | g_return_val_if_fail(mimeinfo != NULL, NULL); |
| 762 | g_return_val_if_fail(infp != NULL, NULL); |
| 763 | g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT || |
| 764 | mimeinfo->mime_type == MIME_TEXT_HTML, NULL);
|
| 765 | |
| 766 | if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) { |
| 767 | perror("fseek");
|
| 768 | return NULL; |
| 769 | } |
| 770 | |
| 771 | while (fgets(buf, sizeof(buf), infp) != NULL) |
| 772 | if (buf[0] == '\r' || buf[0] == '\n') break; |
| 773 | |
| 774 | tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
|
| 775 | if (!tmpfp)
|
| 776 | return NULL; |
| 777 | |
| 778 | if ((outfp = my_tmpfile()) == NULL) { |
| 779 | perror("tmpfile");
|
| 780 | fclose(tmpfp); |
| 781 | return NULL; |
| 782 | } |
| 783 | |
| 784 | src_encoding = prefs_common.force_charset |
| 785 | ? prefs_common.force_charset : mimeinfo->charset; |
| 786 | |
| 787 | if (mimeinfo->mime_type == MIME_TEXT) {
|
| 788 | while (fgets(buf, sizeof(buf), tmpfp) != NULL) { |
| 789 | gchar *str; |
| 790 | |
| 791 | str = conv_codeset_strdup(buf, src_encoding, encoding); |
| 792 | if (str) {
|
| 793 | fputs(str, outfp); |
| 794 | g_free(str); |
| 795 | } else {
|
| 796 | conv_fail = TRUE; |
| 797 | fputs(buf, outfp); |
| 798 | } |
| 799 | } |
| 800 | } else if (mimeinfo->mime_type == MIME_TEXT_HTML) { |
| 801 | HTMLParser *parser; |
| 802 | CodeConverter *conv; |
| 803 | const gchar *str;
|
| 804 | |
| 805 | conv = conv_code_converter_new(src_encoding, encoding); |
| 806 | parser = html_parser_new(tmpfp, conv); |
| 807 | while ((str = html_parse(parser)) != NULL) { |
| 808 | fputs(str, outfp); |
| 809 | } |
| 810 | html_parser_destroy(parser); |
| 811 | conv_code_converter_destroy(conv); |
| 812 | } |
| 813 | |
| 814 | if (conv_fail)
|
| 815 | g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
|
| 816 | |
| 817 | fclose(tmpfp); |
| 818 | rewind(outfp); |
| 819 | |
| 820 | return outfp;
|
| 821 | } |
| 822 | |
| 823 | /* search the first text part of (multipart) MIME message,
|
| 824 | decode, convert it and output to outfp. */ |
| 825 | FILE *procmime_get_first_text_content(MsgInfo *msginfo, const gchar *encoding)
|
| 826 | {
|
| 827 | FILE *infp, *outfp = NULL;
|
| 828 | MimeInfo *mimeinfo, *partinfo; |
| 829 | |
| 830 | g_return_val_if_fail(msginfo != NULL, NULL); |
| 831 | |
| 832 | mimeinfo = procmime_scan_message(msginfo); |
| 833 | if (!mimeinfo) return NULL; |
| 834 | |
| 835 | if ((infp = procmsg_open_message(msginfo)) == NULL) { |
| 836 | procmime_mimeinfo_free_all(mimeinfo); |
| 837 | return NULL; |
| 838 | } |
| 839 | |
| 840 | partinfo = mimeinfo; |
| 841 | while (partinfo && partinfo->mime_type != MIME_TEXT)
|
| 842 | partinfo = procmime_mimeinfo_next(partinfo); |
| 843 | if (!partinfo) {
|
| 844 | partinfo = mimeinfo; |
| 845 | while (partinfo && partinfo->mime_type != MIME_TEXT_HTML)
|
| 846 | partinfo = procmime_mimeinfo_next(partinfo); |
| 847 | } |
| 848 | |
| 849 | if (partinfo)
|
| 850 | outfp = procmime_get_text_content(partinfo, infp, encoding); |
| 851 | |
| 852 | fclose(infp); |
| 853 | procmime_mimeinfo_free_all(mimeinfo); |
| 854 | |
| 855 | return outfp;
|
| 856 | } |
| 857 | |
| 858 | gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
|
| 859 | const gchar *str, StrFindFunc find_func)
|
| 860 | {
|
| 861 | |
| 862 | FILE *infp, *outfp; |
| 863 | gchar buf[BUFFSIZE]; |
| 864 | |
| 865 | g_return_val_if_fail(mimeinfo != NULL, FALSE);
|
| 866 | g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT || |
| 867 | mimeinfo->mime_type == MIME_TEXT_HTML, FALSE); |
| 868 | g_return_val_if_fail(str != NULL, FALSE);
|
| 869 | g_return_val_if_fail(find_func != NULL, FALSE);
|
| 870 | |
| 871 | if ((infp = g_fopen(filename, "rb")) == NULL) { |
| 872 | FILE_OP_ERROR(filename, "fopen");
|
| 873 | return FALSE;
|
| 874 | } |
| 875 | |
| 876 | outfp = procmime_get_text_content(mimeinfo, infp, NULL);
|
| 877 | fclose(infp); |
| 878 | |
| 879 | if (!outfp)
|
| 880 | return FALSE;
|
| 881 | |
| 882 | while (fgets(buf, sizeof(buf), outfp) != NULL) { |
| 883 | strretchomp(buf); |
| 884 | if (find_func(buf, str)) {
|
| 885 | fclose(outfp); |
| 886 | return TRUE;
|
| 887 | } |
| 888 | } |
| 889 | |
| 890 | fclose(outfp); |
| 891 | |
| 892 | return FALSE;
|
| 893 | } |
| 894 | |
| 895 | gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
|
| 896 | StrFindFunc find_func) |
| 897 | {
|
| 898 | MimeInfo *mimeinfo; |
| 899 | MimeInfo *partinfo; |
| 900 | gchar *filename; |
| 901 | gboolean found = FALSE; |
| 902 | |
| 903 | g_return_val_if_fail(msginfo != NULL, FALSE);
|
| 904 | g_return_val_if_fail(str != NULL, FALSE);
|
| 905 | g_return_val_if_fail(find_func != NULL, FALSE);
|
| 906 | |
| 907 | filename = procmsg_get_message_file(msginfo); |
| 908 | if (!filename) return FALSE; |
| 909 | mimeinfo = procmime_scan_message(msginfo); |
| 910 | |
| 911 | for (partinfo = mimeinfo; partinfo != NULL; |
| 912 | partinfo = procmime_mimeinfo_next(partinfo)) {
|
| 913 | if (partinfo->mime_type == MIME_TEXT ||
|
| 914 | partinfo->mime_type == MIME_TEXT_HTML) {
|
| 915 | if (procmime_find_string_part
|
| 916 | (partinfo, filename, str, find_func) == TRUE) {
|
| 917 | found = TRUE; |
| 918 | break;
|
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | procmime_mimeinfo_free_all(mimeinfo); |
| 924 | g_free(filename); |
| 925 | |
| 926 | return found;
|
| 927 | } |
| 928 | |
| 929 | gchar *procmime_get_part_file_name(MimeInfo *mimeinfo) |
| 930 | {
|
| 931 | gchar *base; |
| 932 | const gchar *base_;
|
| 933 | |
| 934 | base_ = mimeinfo->filename ? mimeinfo->filename |
| 935 | : mimeinfo->name ? mimeinfo->name : "mimetmp";
|
| 936 | base_ = g_basename(base_); |
| 937 | if (*base_ == '\0') base_ = "mimetmp"; |
| 938 | base = conv_filename_from_utf8(base_); |
| 939 | subst_for_filename(base); |
| 940 | |
| 941 | return base;
|
| 942 | } |
| 943 | |
| 944 | gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo) |
| 945 | {
|
| 946 | static guint32 id = 0; |
| 947 | gchar *base; |
| 948 | gchar *filename; |
| 949 | gchar f_prefix[10];
|
| 950 | |
| 951 | g_return_val_if_fail(mimeinfo != NULL, NULL); |
| 952 | |
| 953 | g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++); |
| 954 | |
| 955 | if (MIME_TEXT_HTML == mimeinfo->mime_type)
|
| 956 | base = g_strdup("mimetmp.html");
|
| 957 | else
|
| 958 | base = procmime_get_part_file_name(mimeinfo); |
| 959 | |
| 960 | filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S, |
| 961 | f_prefix, base, NULL);
|
| 962 | |
| 963 | g_free(base); |
| 964 | |
| 965 | return filename;
|
| 966 | } |
| 967 | |
| 968 | ContentType procmime_scan_mime_type(const gchar *mime_type)
|
| 969 | {
|
| 970 | ContentType type; |
| 971 | |
| 972 | if (!g_ascii_strncasecmp(mime_type, "text/html", 9)) |
| 973 | type = MIME_TEXT_HTML; |
| 974 | else if (!g_ascii_strncasecmp(mime_type, "text/", 5)) |
| 975 | type = MIME_TEXT; |
| 976 | else if (!g_ascii_strncasecmp(mime_type, "message/rfc822", 14)) |
| 977 | type = MIME_MESSAGE_RFC822; |
| 978 | else if (!g_ascii_strncasecmp(mime_type, "message/", 8)) |
| 979 | type = MIME_TEXT; |
| 980 | else if (!g_ascii_strncasecmp(mime_type, "application/octet-stream", |
| 981 | 24))
|
| 982 | type = MIME_APPLICATION_OCTET_STREAM; |
| 983 | else if (!g_ascii_strncasecmp(mime_type, "application/", 12)) |
| 984 | type = MIME_APPLICATION; |
| 985 | else if (!g_ascii_strncasecmp(mime_type, "multipart/", 10)) |
| 986 | type = MIME_MULTIPART; |
| 987 | else if (!g_ascii_strncasecmp(mime_type, "image/", 6)) |
| 988 | type = MIME_IMAGE; |
| 989 | else if (!g_ascii_strncasecmp(mime_type, "audio/", 6)) |
| 990 | type = MIME_AUDIO; |
| 991 | else if (!g_ascii_strcasecmp(mime_type, "text")) |
| 992 | type = MIME_TEXT; |
| 993 | else
|
| 994 | type = MIME_UNKNOWN; |
| 995 | |
| 996 | return type;
|
| 997 | } |
| 998 | |
| 999 | static GList *mime_type_list = NULL; |
| 1000 | |
| 1001 | gchar *procmime_get_mime_type(const gchar *filename)
|
| 1002 | {
|
| 1003 | static GHashTable *mime_type_table = NULL; |
| 1004 | MimeType *mime_type; |
| 1005 | const gchar *p;
|
| 1006 | gchar *ext; |
| 1007 | static gboolean no_mime_type_table = FALSE;
|
| 1008 | |
| 1009 | if (no_mime_type_table)
|
| 1010 | return NULL; |
| 1011 | |
| 1012 | if (!mime_type_table) {
|
| 1013 | mime_type_table = procmime_get_mime_type_table(); |
| 1014 | if (!mime_type_table) {
|
| 1015 | no_mime_type_table = TRUE; |
| 1016 | return NULL; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | filename = g_basename(filename); |
| 1021 | p = strrchr(filename, '.');
|
| 1022 | if (!p) return NULL; |
| 1023 | |
| 1024 | Xstrdup_a(ext, p + 1, return NULL); |
| 1025 | g_strdown(ext); |
| 1026 | mime_type = g_hash_table_lookup(mime_type_table, ext); |
| 1027 | if (mime_type) {
|
| 1028 | gchar *str; |
| 1029 | |
| 1030 | str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
|
| 1031 | NULL);
|
| 1032 | return str;
|
| 1033 | } |
| 1034 | |
| 1035 | return NULL; |
| 1036 | } |
| 1037 | |
| 1038 | static GHashTable *procmime_get_mime_type_table(void) |
| 1039 | {
|
| 1040 | GHashTable *table = NULL;
|
| 1041 | GList *cur; |
| 1042 | MimeType *mime_type; |
| 1043 | gchar **exts; |
| 1044 | |
| 1045 | if (!mime_type_list) {
|
| 1046 | GList *list; |
| 1047 | gchar *dir; |
| 1048 | |
| 1049 | #ifdef G_OS_WIN32
|
| 1050 | dir = g_strconcat(get_startup_dir(), |
| 1051 | G_DIR_SEPARATOR_S "etc" G_DIR_SEPARATOR_S
|
| 1052 | "mime.types", NULL); |
| 1053 | mime_type_list = procmime_get_mime_type_list(dir); |
| 1054 | g_free(dir); |
| 1055 | #else
|
| 1056 | mime_type_list = |
| 1057 | procmime_get_mime_type_list(SYSCONFDIR "/mime.types");
|
| 1058 | if (!mime_type_list) {
|
| 1059 | list = procmime_get_mime_type_list("/etc/mime.types");
|
| 1060 | mime_type_list = g_list_concat(mime_type_list, list); |
| 1061 | } |
| 1062 | #endif
|
| 1063 | dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, |
| 1064 | "mime.types", NULL); |
| 1065 | list = procmime_get_mime_type_list(dir); |
| 1066 | g_free(dir); |
| 1067 | mime_type_list = g_list_concat(mime_type_list, list); |
| 1068 | |
| 1069 | if (!mime_type_list) {
|
| 1070 | debug_print("mime.types not found\n");
|
| 1071 | return NULL; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | table = g_hash_table_new(g_str_hash, g_str_equal); |
| 1076 | |
| 1077 | for (cur = mime_type_list; cur != NULL; cur = cur->next) { |
| 1078 | gint i; |
| 1079 | gchar *key; |
| 1080 | |
| 1081 | mime_type = (MimeType *)cur->data; |
| 1082 | |
| 1083 | if (!mime_type->extension) continue; |
| 1084 | |
| 1085 | exts = g_strsplit(mime_type->extension, " ", 16); |
| 1086 | for (i = 0; exts[i] != NULL; i++) { |
| 1087 | /* make the key case insensitive */
|
| 1088 | g_strdown(exts[i]); |
| 1089 | /* use previously dup'd key on overwriting */
|
| 1090 | if (g_hash_table_lookup(table, exts[i]))
|
| 1091 | key = exts[i]; |
| 1092 | else
|
| 1093 | key = g_strdup(exts[i]); |
| 1094 | g_hash_table_insert(table, key, mime_type); |
| 1095 | } |
| 1096 | g_strfreev(exts); |
| 1097 | } |
| 1098 | |
| 1099 | return table;
|
| 1100 | } |
| 1101 | |
| 1102 | static GList *procmime_get_mime_type_list(const gchar *file) |
| 1103 | {
|
| 1104 | GList *list = NULL;
|
| 1105 | FILE *fp; |
| 1106 | gchar buf[BUFFSIZE]; |
| 1107 | gchar *p; |
| 1108 | gchar *delim; |
| 1109 | MimeType *mime_type; |
| 1110 | |
| 1111 | if ((fp = g_fopen(file, "rb")) == NULL) return NULL; |
| 1112 | |
| 1113 | debug_print("Reading %s ...\n", file);
|
| 1114 | |
| 1115 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 1116 | p = strchr(buf, '#');
|
| 1117 | if (p) *p = '\0'; |
| 1118 | g_strstrip(buf); |
| 1119 | |
| 1120 | p = buf; |
| 1121 | while (*p && !g_ascii_isspace(*p)) p++;
|
| 1122 | if (*p) {
|
| 1123 | *p = '\0';
|
| 1124 | p++; |
| 1125 | } |
| 1126 | delim = strchr(buf, '/');
|
| 1127 | if (delim == NULL) continue; |
| 1128 | *delim = '\0';
|
| 1129 | |
| 1130 | mime_type = g_new(MimeType, 1);
|
| 1131 | mime_type->type = g_strdup(buf); |
| 1132 | mime_type->sub_type = g_strdup(delim + 1);
|
| 1133 | |
| 1134 | while (*p && g_ascii_isspace(*p)) p++;
|
| 1135 | if (*p)
|
| 1136 | mime_type->extension = g_strdup(p); |
| 1137 | else
|
| 1138 | mime_type->extension = NULL;
|
| 1139 | |
| 1140 | list = g_list_append(list, mime_type); |
| 1141 | } |
| 1142 | |
| 1143 | fclose(fp); |
| 1144 | |
| 1145 | if (!list)
|
| 1146 | g_warning("Can't read mime.types\n");
|
| 1147 | |
| 1148 | return list;
|
| 1149 | } |
| 1150 | |
| 1151 | EncodingType procmime_get_encoding_for_charset(const gchar *charset)
|
| 1152 | {
|
| 1153 | if (!charset)
|
| 1154 | return ENC_8BIT;
|
| 1155 | else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) || |
| 1156 | !g_ascii_strcasecmp(charset, "US-ASCII"))
|
| 1157 | return ENC_7BIT;
|
| 1158 | else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") || |
| 1159 | !g_ascii_strncasecmp(charset, "KOI8-", 5) || |
| 1160 | !g_ascii_strcasecmp(charset, "Windows-1251"))
|
| 1161 | return ENC_8BIT;
|
| 1162 | else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9)) |
| 1163 | return ENC_QUOTED_PRINTABLE;
|
| 1164 | else
|
| 1165 | return ENC_8BIT;
|
| 1166 | } |
| 1167 | |
| 1168 | EncodingType procmime_get_encoding_for_text_file(const gchar *file)
|
| 1169 | {
|
| 1170 | FILE *fp; |
| 1171 | guchar buf[BUFFSIZE]; |
| 1172 | size_t len; |
| 1173 | size_t octet_chars = 0;
|
| 1174 | size_t total_len = 0;
|
| 1175 | gfloat octet_percentage; |
| 1176 | |
| 1177 | if ((fp = g_fopen(file, "rb")) == NULL) { |
| 1178 | FILE_OP_ERROR(file, "fopen");
|
| 1179 | return ENC_UNKNOWN;
|
| 1180 | } |
| 1181 | |
| 1182 | while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) { |
| 1183 | guchar *p; |
| 1184 | gint i; |
| 1185 | |
| 1186 | for (p = buf, i = 0; i < len; ++p, ++i) { |
| 1187 | if (*p & 0x80) |
| 1188 | ++octet_chars; |
| 1189 | } |
| 1190 | total_len += len; |
| 1191 | } |
| 1192 | |
| 1193 | fclose(fp); |
| 1194 | |
| 1195 | if (total_len > 0) |
| 1196 | octet_percentage = (gfloat)octet_chars / (gfloat)total_len; |
| 1197 | else
|
| 1198 | octet_percentage = 0.0; |
| 1199 | |
| 1200 | debug_print("procmime_get_encoding_for_text_file(): "
|
| 1201 | "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
|
| 1202 | 100.0 * octet_percentage); |
| 1203 | |
| 1204 | if (octet_percentage > 0.20) { |
| 1205 | debug_print("using BASE64\n");
|
| 1206 | return ENC_BASE64;
|
| 1207 | } else if (octet_chars > 0) { |
| 1208 | debug_print("using quoted-printable\n");
|
| 1209 | return ENC_QUOTED_PRINTABLE;
|
| 1210 | } else {
|
| 1211 | debug_print("using 7bit\n");
|
| 1212 | return ENC_7BIT;
|
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | const gchar *procmime_get_encoding_str(EncodingType encoding)
|
| 1217 | {
|
| 1218 | static const gchar *encoding_str[] = { |
| 1219 | "7bit", "8bit", "quoted-printable", "base64", "x-uuencode", |
| 1220 | NULL
|
| 1221 | }; |
| 1222 | |
| 1223 | if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
|
| 1224 | return encoding_str[encoding];
|
| 1225 | else
|
| 1226 | return NULL; |
| 1227 | } |