enable IMAP folder dnd
[claws.git] / src / codeconv.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <stdlib.h>
28
29 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
30 #  include <wchar.h>
31 #  include <wctype.h>
32 #endif
33
34 #if HAVE_LOCALE_H
35 #  include <locale.h>
36 #endif
37
38 #if HAVE_LIBJCONV
39 #  include <jconv.h>
40 #endif
41
42 #include "intl.h"
43 #include "codeconv.h"
44 #include "unmime.h"
45 #include "base64.h"
46 #include "utils.h"
47 #include "prefs_common.h"
48
49 typedef enum
50 {
51         JIS_ASCII,
52         JIS_KANJI,
53         JIS_HWKANA,
54         JIS_AUXKANJI
55 } JISState;
56
57 #define SUBST_CHAR      '_'
58 #define ESC             '\033'
59
60 #define iseuckanji(c) \
61         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xfe)
62 #define iseuchwkana1(c) \
63         (((c) & 0xff) == 0x8e)
64 #define iseuchwkana2(c) \
65         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
66 #define iseucaux(c) \
67         (((c) & 0xff) == 0x8f)
68 #define isunprintableeuckanji(c) \
69         (((c) & 0xff) >= 0xa9 && ((c) & 0xff) <= 0xaf)
70 #define issjiskanji1(c) \
71         ((((c) & 0xff) >= 0x81 && ((c) & 0xff) <= 0x9f) || \
72          (((c) & 0xff) >= 0xe0 && ((c) & 0xff) <= 0xfc))
73 #define issjiskanji2(c) \
74         ((((c) & 0xff) >= 0x40 && ((c) & 0xff) <= 0x7e) || \
75          (((c) & 0xff) >= 0x80 && ((c) & 0xff) <= 0xfc))
76 #define issjishwkana(c) \
77         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
78
79 #define K_IN()                          \
80         if (state != JIS_KANJI) {       \
81                 *out++ = ESC;           \
82                 *out++ = '$';           \
83                 *out++ = 'B';           \
84                 state = JIS_KANJI;      \
85         }
86
87 #define K_OUT()                         \
88         if (state != JIS_ASCII) {       \
89                 *out++ = ESC;           \
90                 *out++ = '(';           \
91                 *out++ = 'B';           \
92                 state = JIS_ASCII;      \
93         }
94
95 #define HW_IN()                         \
96         if (state != JIS_HWKANA) {      \
97                 *out++ = ESC;           \
98                 *out++ = '(';           \
99                 *out++ = 'I';           \
100                 state = JIS_HWKANA;     \
101         }
102
103 #define AUX_IN()                        \
104         if (state != JIS_AUXKANJI) {    \
105                 *out++ = ESC;           \
106                 *out++ = '$';           \
107                 *out++ = '(';           \
108                 *out++ = 'D';           \
109                 state = JIS_AUXKANJI;   \
110         }
111
112 void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
113 {
114         const guchar *in = inbuf;
115         guchar *out = outbuf;
116         JISState state = JIS_ASCII;
117
118         while (*in != '\0') {
119                 if (*in == ESC) {
120                         in++;
121                         if (*in == '$') {
122                                 if (*(in + 1) == '@' || *(in + 1) == 'B') {
123                                         state = JIS_KANJI;
124                                         in += 2;
125                                 } else if (*(in + 1) == '(' &&
126                                            *(in + 2) == 'D') {
127                                         state = JIS_AUXKANJI;
128                                         in += 3;
129                                 } else {
130                                         /* unknown escape sequence */
131                                         state = JIS_ASCII;
132                                 }
133                         } else if (*in == '(') {
134                                 if (*(in + 1) == 'B' || *(in + 1) == 'J') {
135                                         state = JIS_ASCII;
136                                         in += 2;
137                                 } else if (*(in + 1) == 'I') {
138                                         state = JIS_HWKANA;
139                                         in += 2;
140                                 } else {
141                                         /* unknown escape sequence */
142                                         state = JIS_ASCII;
143                                 }
144                         } else {
145                                 /* unknown escape sequence */
146                                 state = JIS_ASCII;
147                         }
148                 } else if (*in == 0x0e) {
149                         state = JIS_HWKANA;
150                         in++;
151                 } else if (*in == 0x0f) {
152                         state = JIS_ASCII;
153                         in++;
154                 } else {
155                         switch (state) {
156                         case JIS_ASCII:
157                                 *out++ = *in++;
158                                 break;
159                         case JIS_KANJI:
160                                 *out++ = *in++ | 0x80;
161                                 if (*in == '\0') break;
162                                 *out++ = *in++ | 0x80;
163                                 break;
164                         case JIS_HWKANA:
165                                 *out++ = 0x8e;
166                                 *out++ = *in++ | 0x80;
167                                 break;
168                         case JIS_AUXKANJI:
169                                 *out++ = 0x8f;
170                                 *out++ = *in++ | 0x80;
171                                 if (*in == '\0') break;
172                                 *out++ = *in++ | 0x80;
173                                 break;
174                         }
175                 }
176         }
177
178         *out = '\0';
179 }
180
181 void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
182 {
183         const guchar *in = inbuf;
184         guchar *out = outbuf;
185         JISState state = JIS_ASCII;
186
187         while (*in != '\0') {
188                 if (isascii(*in)) {
189                         K_OUT();
190                         *out++ = *in++;
191                 } else if (iseuckanji(*in)) {
192                         if (iseuckanji(*(in + 1))) {
193                                 K_IN();
194                                 *out++ = *in++ & 0x7f;
195                                 *out++ = *in++ & 0x7f;
196                         } else {
197                                 K_OUT();
198                                 *out++ = SUBST_CHAR;
199                                 in++;
200                                 if (*in != '\0' && !isascii(*in)) {
201                                         *out++ = SUBST_CHAR;
202                                         in++;
203                                 }
204                         }
205                 } else if (iseuchwkana1(*in)) {
206                         in++;
207                         if (iseuchwkana2(*in)) {
208                                 HW_IN();
209                                 *out++ = *in++ & 0x7f;
210                         } else {
211                                 K_OUT();
212                                 if (*in != '\0' && !isascii(*in)) {
213                                         *out++ = SUBST_CHAR;
214                                         in++;
215                                 }
216                         }
217                 } else if (iseucaux(*in)) {
218                         in++;
219                         if (iseuckanji(*in) && iseuckanji(*(in + 1))) {
220                                 AUX_IN();
221                                 *out++ = *in++ & 0x7f;
222                                 *out++ = *in++ & 0x7f;
223                         } else {
224                                 K_OUT();
225                                 if (*in != '\0' && !isascii(*in)) {
226                                         *out++ = SUBST_CHAR;
227                                         in++;
228                                         if (*in != '\0' && !isascii(*in)) {
229                                                 *out++ = SUBST_CHAR;
230                                                 in++;
231                                         }
232                                 }
233                         }
234                 } else {
235                         K_OUT();
236                         *out++ = SUBST_CHAR;
237                         in++;
238                 }
239         }
240
241         K_OUT();
242         *out = '\0';
243 }
244
245 void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
246 {
247         const guchar *in = inbuf;
248         guchar *out = outbuf;
249
250         while (*in != '\0') {
251                 if (isascii(*in)) {
252                         *out++ = *in++;
253                 } else if (issjiskanji1(*in)) {
254                         if (issjiskanji2(*(in + 1))) {
255                                 guchar out1 = *in;
256                                 guchar out2 = *(in + 1);
257                                 guchar row;
258
259                                 row = out1 < 0xa0 ? 0x70 : 0xb0;
260                                 if (out2 < 0x9f) {
261                                         out1 = (out1 - row) * 2 - 1;
262                                         out2 -= out2 > 0x7f ? 0x20 : 0x1f;
263                                 } else {
264                                         out1 = (out1 - row) * 2;
265                                         out2 -= 0x7e;
266                                 }
267
268                                 *out++ = out1 | 0x80;
269                                 *out++ = out2 | 0x80;
270                                 in += 2;
271                         } else {
272                                 *out++ = SUBST_CHAR;
273                                 in++;
274                                 if (*in != '\0' && !isascii(*in)) {
275                                         *out++ = SUBST_CHAR;
276                                         in++;
277                                 }
278                         }
279                 } else if (issjishwkana(*in)) {
280                         *out++ = 0x8e;
281                         *out++ = *in++;
282                 } else {
283                         *out++ = SUBST_CHAR;
284                         in++;
285                 }
286         }
287
288         *out = '\0';
289 }
290
291 void conv_anytoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
292 {
293         switch (conv_guess_encoding(inbuf)) {
294         case C_ISO_2022_JP:
295                 conv_jistoeuc(outbuf, outlen, inbuf);
296                 break;
297         case C_SHIFT_JIS:
298                 conv_sjistoeuc(outbuf, outlen, inbuf);
299                 break;
300         default:
301                 strncpy2(outbuf, inbuf, outlen);
302                 break;
303         }
304 }
305
306 void conv_anytojis(gchar *outbuf, gint outlen, const gchar *inbuf)
307 {
308         switch (conv_guess_encoding(inbuf)) {
309         case C_EUC_JP:
310                 conv_euctojis(outbuf, outlen, inbuf);
311                 break;
312         default:
313                 strncpy2(outbuf, inbuf, outlen);
314                 break;
315         }
316 }
317
318 void conv_unreadable_eucjp(gchar *str)
319 {
320         register guchar *p = str;
321
322         while (*p != '\0') {
323                 if (isascii(*p)) {
324                         /* convert CR+LF -> LF */
325                         if (*p == '\r' && *(p + 1) == '\n')
326                                 memmove(p, p + 1, strlen(p));
327                         /* printable 7 bit code */
328                         p++;
329                 } else if (iseuckanji(*p)) {
330                         if (iseuckanji(*(p + 1)) && !isunprintableeuckanji(*p))
331                                 /* printable euc-jp code */
332                                 p += 2;
333                         else {
334                                 /* substitute unprintable code */
335                                 *p++ = SUBST_CHAR;
336                                 if (*p != '\0') {
337                                         if (isascii(*p))
338                                                 p++;
339                                         else
340                                                 *p++ = SUBST_CHAR;
341                                 }
342                         }
343                 } else if (iseuchwkana1(*p)) {
344                         if (iseuchwkana2(*(p + 1)))
345                                 /* euc-jp hankaku kana */
346                                 p += 2;
347                         else
348                                 *p++ = SUBST_CHAR;
349                 } else if (iseucaux(*p)) {
350                         if (iseuckanji(*(p + 1)) && iseuckanji(*(p + 2))) {
351                                 /* auxiliary kanji */
352                                 p += 3;
353                         } else
354                                 *p++ = SUBST_CHAR;
355                 } else
356                         /* substitute unprintable 1 byte code */
357                         *p++ = SUBST_CHAR;
358         }
359 }
360
361 void conv_unreadable_8bit(gchar *str)
362 {
363         register guchar *p = str;
364
365         while (*p != '\0') {
366                 /* convert CR+LF -> LF */
367                 if (*p == '\r' && *(p + 1) == '\n')
368                         memmove(p, p + 1, strlen(p));
369                 else if (!isascii(*p)) *p = SUBST_CHAR;
370                 p++;
371         }
372 }
373
374 void conv_unreadable_latin(gchar *str)
375 {
376         register guchar *p = str;
377
378         while (*p != '\0') {
379                 /* convert CR+LF -> LF */
380                 if (*p == '\r' && *(p + 1) == '\n')
381                         memmove(p, p + 1, strlen(p));
382                 else if ((*p & 0xff) >= 0x80 && (*p & 0xff) <= 0x9f)
383                         *p = SUBST_CHAR;
384                 p++;
385         }
386 }
387
388 #define NCV     '\0'
389
390 void conv_mb_alnum(gchar *str)
391 {
392         static guchar char_tbl[] = {
393                 /* 0xa0 - 0xaf */
394                 NCV, ' ', NCV, NCV, ',', '.', NCV, ':',
395                 ';', '?', '!', NCV, NCV, NCV, NCV, NCV,
396                 /* 0xb0 - 0xbf */
397                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
398                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
399                 /* 0xc0 - 0xcf */
400                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
401                 NCV, NCV, '(', ')', NCV, NCV, '[', ']',
402                 /* 0xd0 - 0xdf */
403                 '{', '}', NCV, NCV, NCV, NCV, NCV, NCV,
404                 NCV, NCV, NCV, NCV, '+', '-', NCV, NCV,
405                 /* 0xe0 - 0xef */
406                 NCV, '=', NCV, '<', '>', NCV, NCV, NCV,
407                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV
408         };
409
410         register guchar *p = str;
411         register gint len;
412
413         len = strlen(str);
414
415         while (len > 1) {
416                 if (*p == 0xa3) {
417                         register guchar ch = *(p + 1);
418
419                         if (ch >= 0xb0 && ch <= 0xfa) {
420                                 /* [a-zA-Z] */
421                                 *p = ch & 0x7f;
422                                 p++;
423                                 len--;
424                                 memmove(p, p + 1, len);
425                                 len--;
426                         } else  {
427                                 p += 2;
428                                 len -= 2;
429                         }
430                 } else if (*p == 0xa1) {
431                         register guchar ch = *(p + 1);
432
433                         if (ch >= 0xa0 && ch <= 0xef &&
434                             NCV != char_tbl[ch - 0xa0]) {
435                                 *p = char_tbl[ch - 0xa0];
436                                 p++;
437                                 len--;
438                                 memmove(p, p + 1, len);
439                                 len--;
440                         } else {
441                                 p += 2;
442                                 len -= 2;
443                         }
444                 } else if (iseuckanji(*p)) {
445                         p += 2;
446                         len -= 2;
447                 } else {
448                         p++;
449                         len--;
450                 }
451         }
452 }
453
454 CharSet conv_guess_encoding(const gchar *str)
455 {
456         const guchar *p = str;
457         CharSet guessed = C_US_ASCII;
458
459         while (*p != '\0') {
460                 if (*p == ESC && (*(p + 1) == '$' || *(p + 1) == '(')) {
461                         if (guessed == C_US_ASCII)
462                                 return C_ISO_2022_JP;
463                         p += 2;
464                 } else if (isascii(*p)) {
465                         p++;
466                 } else if (iseuckanji(*p) && iseuckanji(*(p + 1))) {
467                         if (*p >= 0xfd && *p <= 0xfe)
468                                 return C_EUC_JP;
469                         else if (guessed == C_SHIFT_JIS) {
470                                 if ((issjiskanji1(*p) &&
471                                      issjiskanji2(*(p + 1))) ||
472                                     issjishwkana(*p))
473                                         guessed = C_SHIFT_JIS;
474                                 else
475                                         guessed = C_EUC_JP;
476                         } else
477                                 guessed = C_EUC_JP;
478                         p += 2;
479                 } else if (issjiskanji1(*p) && issjiskanji2(*(p + 1))) {
480                         if (iseuchwkana1(*p) && iseuchwkana2(*(p + 1)))
481                                 guessed = C_SHIFT_JIS;
482                         else
483                                 return C_SHIFT_JIS;
484                         p += 2;
485                 } else if (issjishwkana(*p)) {
486                         guessed = C_SHIFT_JIS;
487                         p++;
488                 } else {
489                         p++;
490                 }
491         }
492
493         return guessed;
494 }
495
496 void conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
497 {
498         conv_jistoeuc(outbuf, outlen, inbuf);
499         conv_unreadable_eucjp(outbuf);
500 }
501
502 void conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
503 {
504         conv_sjistoeuc(outbuf, outlen, inbuf);
505         conv_unreadable_eucjp(outbuf);
506 }
507
508 void conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
509 {
510         strncpy2(outbuf, inbuf, outlen);
511         conv_unreadable_eucjp(outbuf);
512 }
513
514 void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
515 {
516         conv_anytoeuc(outbuf, outlen, inbuf);
517         conv_unreadable_eucjp(outbuf);
518 }
519
520 void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
521 {
522         strncpy2(outbuf, inbuf, outlen);
523         conv_unreadable_8bit(outbuf);
524 }
525
526 void conv_latintodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
527 {
528         strncpy2(outbuf, inbuf, outlen);
529         conv_unreadable_latin(outbuf);
530 }
531
532 void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
533 {
534         strncpy2(outbuf, inbuf, outlen);
535 }
536
537 CodeConverter *conv_code_converter_new(const gchar *charset)
538 {
539         CodeConverter *conv;
540
541         conv = g_new0(CodeConverter, 1);
542 #if !HAVE_LIBJCONV
543         conv->code_conv_func = conv_get_code_conv_func(charset);
544 #endif
545         conv->charset_str = g_strdup(charset);
546         conv->charset = conv_get_charset_from_str(charset);
547
548         return conv;
549 }
550
551 void conv_code_converter_destroy(CodeConverter *conv)
552 {
553         g_free(conv->charset_str);
554         g_free(conv);
555 }
556
557 gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
558                   const gchar *inbuf)
559 {
560 #if HAVE_LIBJCONV
561         gchar *str;
562
563         str = conv_codeset_strdup(inbuf, conv->charset_str, NULL);
564         if (!str)
565                 return -1;
566         else {
567                 strncpy2(outbuf, str, outlen);
568                 g_free(str);
569         }
570 #else /* !HAVE_LIBJCONV */
571         conv->code_conv_func(outbuf, outlen, inbuf);
572 #endif
573
574         return 0;
575 }
576
577 gchar *conv_codeset_strdup(const gchar *inbuf,
578                            const gchar *src_codeset, const gchar *dest_codeset)
579 {
580         gchar *buf;
581         size_t len;
582 #if HAVE_LIBJCONV
583         gint actual_codeset;
584         const gchar *const *codesets;
585         gint n_codesets;
586 #else /* !HAVE_LIBJCONV */
587         CharSet src_charset = C_AUTO, dest_charset = C_AUTO;
588 #endif
589
590         if (!dest_codeset) {
591                 CodeConvFunc func;
592
593                 func = conv_get_code_conv_func(src_codeset);
594                 if (func != conv_noconv) {
595                         if (func == conv_jistodisp ||
596                             func == conv_sjistodisp ||
597                             func == conv_anytodisp)
598                                 len = strlen(inbuf) * 2 + 1;
599                         else
600                                 len = strlen(inbuf) + 1;
601                         buf = g_malloc(len);
602                         if (!buf) return NULL;
603                         func(buf, len, inbuf);
604                         buf = g_realloc(buf, strlen(buf) + 1);
605                         return buf;
606                 }
607         }
608
609         /* don't convert if src and dest codeset are identical */
610         if (src_codeset && dest_codeset &&
611             !strcasecmp(src_codeset, dest_codeset))
612                 return g_strdup(inbuf);
613
614 #if HAVE_LIBJCONV
615         if (src_codeset) {
616                 codesets = &src_codeset;
617                 n_codesets = 1;
618         } else
619                 codesets = jconv_info_get_pref_codesets(&n_codesets);
620         if (!dest_codeset) {
621                 dest_codeset = conv_get_current_charset_str();
622                 /* don't convert if current codeset is US-ASCII */
623                 if (!strcasecmp(dest_codeset, CS_US_ASCII))
624                         return g_strdup(inbuf);
625         }
626
627         if (jconv_alloc_conv(inbuf, strlen(inbuf), &buf, &len,
628                              codesets, n_codesets,
629                              &actual_codeset, dest_codeset)
630             == 0)
631                 return buf;
632         else {
633 #if 0
634                 g_warning("code conversion from %s to %s failed\n",
635                           codesets && codesets[0] ? codesets[0] : "(unknown)",
636                           dest_codeset);
637 #endif /* 0 */
638                 return NULL;
639         }
640 #else /* !HAVE_LIBJCONV */
641         if (src_codeset) {
642                 if (!strcasecmp(src_codeset, CS_EUC_JP) ||
643                     !strcasecmp(src_codeset, CS_EUCJP))
644                         src_charset = C_EUC_JP;
645                 else if (!strcasecmp(src_codeset, CS_SHIFT_JIS) ||
646                          !strcasecmp(src_codeset, "SHIFT-JIS") ||
647                          !strcasecmp(src_codeset, "SJIS"))
648                         src_charset = C_SHIFT_JIS;
649                 if (dest_codeset && !strcasecmp(dest_codeset, CS_ISO_2022_JP))
650                         dest_charset = C_ISO_2022_JP;
651         }
652
653         if ((src_charset == C_EUC_JP || src_charset == C_SHIFT_JIS) &&
654             dest_charset == C_ISO_2022_JP) {
655                 len = (strlen(inbuf) + 1) * 3;
656                 buf = g_malloc(len);
657                 if (buf) {
658                         if (src_charset == C_EUC_JP)
659                                 conv_euctojis(buf, len, inbuf);
660                         else
661                                 conv_anytojis(buf, len, inbuf);
662                         buf = g_realloc(buf, strlen(buf) + 1);
663                 }
664         } else
665                 buf = g_strdup(inbuf);
666
667         return buf;
668 #endif /* !HAVE_LIBJCONV */
669 }
670
671 CodeConvFunc conv_get_code_conv_func(const gchar *charset)
672 {
673         CodeConvFunc code_conv;
674         CharSet cur_charset;
675
676         if (!charset) {
677                 cur_charset = conv_get_current_charset();
678                 if (cur_charset == C_EUC_JP || cur_charset == C_SHIFT_JIS)
679                         return conv_anytodisp;
680                 else
681                         return conv_noconv;
682         }
683
684         if (!strcasecmp(charset, CS_ISO_2022_JP) ||
685             !strcasecmp(charset, CS_ISO_2022_JP_2))
686                 code_conv = conv_jistodisp;
687         else if (!strcasecmp(charset, CS_US_ASCII))
688                 code_conv = conv_ustodisp;
689         else if (!strncasecmp(charset, CS_ISO_8859_1, 10))
690                 code_conv = conv_latintodisp;
691 #if !HAVE_LIBJCONV
692         else if (!strncasecmp(charset, "ISO-8859-", 9))
693                 code_conv = conv_latintodisp;
694 #endif
695         else if (!strcasecmp(charset, CS_SHIFT_JIS) ||
696                  !strcasecmp(charset, "SHIFT-JIS")  ||
697                  !strcasecmp(charset, "SJIS")       ||
698                  !strcasecmp(charset, "X-SJIS"))
699                 code_conv = conv_sjistodisp;
700         else if (!strcasecmp(charset, CS_EUC_JP) ||
701                  !strcasecmp(charset, CS_EUCJP))
702                 code_conv = conv_euctodisp;
703         else
704                 code_conv = conv_noconv;
705
706         return code_conv;
707 }
708
709 static const struct {
710         CharSet charset;
711         gchar *const name;
712 } charsets[] = {
713         {C_US_ASCII,            CS_US_ASCII},
714         {C_US_ASCII,            CS_ANSI_X3_4_1968},
715         {C_UTF_8,               CS_UTF_8},
716         {C_ISO_8859_1,          CS_ISO_8859_1},
717         {C_ISO_8859_2,          CS_ISO_8859_2},
718         {C_ISO_8859_4,          CS_ISO_8859_4},
719         {C_ISO_8859_5,          CS_ISO_8859_5},
720         {C_ISO_8859_7,          CS_ISO_8859_7},
721         {C_ISO_8859_8,          CS_ISO_8859_8},
722         {C_ISO_8859_9,          CS_ISO_8859_9},
723         {C_ISO_8859_11,         CS_ISO_8859_11},
724         {C_ISO_8859_13,         CS_ISO_8859_13},
725         {C_ISO_8859_15,         CS_ISO_8859_15},
726         {C_BALTIC,              CS_BALTIC},
727         {C_CP1251,              CS_CP1251},
728         {C_WINDOWS_1251,        CS_WINDOWS_1251},
729         {C_KOI8_R,              CS_KOI8_R},
730         {C_KOI8_U,              CS_KOI8_U},
731         {C_ISO_2022_JP,         CS_ISO_2022_JP},
732         {C_ISO_2022_JP_2,       CS_ISO_2022_JP_2},
733         {C_EUC_JP,              CS_EUC_JP},
734         {C_EUC_JP,              CS_EUCJP},
735         {C_SHIFT_JIS,           CS_SHIFT_JIS},
736         {C_ISO_2022_KR,         CS_ISO_2022_KR},
737         {C_EUC_KR,              CS_EUC_KR},
738         {C_ISO_2022_CN,         CS_ISO_2022_CN},
739         {C_EUC_CN,              CS_EUC_CN},
740         {C_GB2312,              CS_GB2312},
741         {C_EUC_TW,              CS_EUC_TW},
742         {C_BIG5,                CS_BIG5},
743         {C_TIS_620,             CS_TIS_620},
744         {C_WINDOWS_874,         CS_WINDOWS_874},
745 };
746
747 #if !HAVE_LIBJCONV
748 static const struct {
749         gchar *const locale;
750         CharSet charset;
751 } locale_table[] = {
752         {"ja_JP.eucJP"  , C_EUC_JP},
753         {"ja_JP.ujis"   , C_EUC_JP},
754         {"ja_JP.EUC"    , C_EUC_JP},
755         {"ja_JP.SJIS"   , C_SHIFT_JIS},
756         {"ja_JP.JIS"    , C_ISO_2022_JP},
757         {"ja_JP"        , C_EUC_JP},
758         {"ko_KR"        , C_EUC_KR},
759         {"zh_CN.GB2312" , C_GB2312},
760         {"zh_CN"        , C_GB2312},
761         {"zh_TW.eucTW"  , C_EUC_TW},
762         {"zh_TW.Big5"   , C_BIG5},
763         {"zh_TW"        , C_BIG5},
764
765         {"ru_RU.KOI8-R" , C_KOI8_R},
766         {"ru_RU.CP1251" , C_WINDOWS_1251},
767
768         {"bg_BG"        , C_WINDOWS_1251},
769
770         {"en_US"        , C_ISO_8859_1},
771         {"ca_ES"        , C_ISO_8859_1},
772         {"da_DK"        , C_ISO_8859_1},
773         {"de_DE"        , C_ISO_8859_1},
774         {"nl_NL"        , C_ISO_8859_1},
775         {"et_EE"        , C_ISO_8859_1},
776         {"fi_FI"        , C_ISO_8859_1},
777         {"fr_FR"        , C_ISO_8859_1},
778         {"is_IS"        , C_ISO_8859_1},
779         {"it_IT"        , C_ISO_8859_1},
780         {"no_NO"        , C_ISO_8859_1},
781         {"pt_PT"        , C_ISO_8859_1},
782         {"pt_BR"        , C_ISO_8859_1},
783         {"es_ES"        , C_ISO_8859_1},
784         {"sv_SE"        , C_ISO_8859_1},
785
786         {"hr_HR"        , C_ISO_8859_2},
787         {"hu_HU"        , C_ISO_8859_2},
788         {"pl_PL"        , C_ISO_8859_2},
789         {"ro_RO"        , C_ISO_8859_2},
790         {"sk_SK"        , C_ISO_8859_2},
791         {"sl_SI"        , C_ISO_8859_2},
792         {"ru_RU"        , C_ISO_8859_5},
793         {"el_GR"        , C_ISO_8859_7},
794         {"iw_IL"        , C_ISO_8859_8},
795         {"tr_TR"        , C_ISO_8859_9},
796
797         {"th_TH"        , C_TIS_620},
798         /* {"th_TH"     , C_WINDOWS_874}, */
799         /* {"th_TH"     , C_ISO_8859_11}, */
800
801         {"lt_LT.iso88594"       , C_ISO_8859_4},
802         {"lt_LT.ISO8859-4"      , C_ISO_8859_4},
803         {"lt_LT.ISO_8859-4"     , C_ISO_8859_4},
804         {"lt_LT"                , C_ISO_8859_13},
805         {"lv_LV"                , C_ISO_8859_13},
806
807         {"C"                    , C_US_ASCII},
808         {"POSIX"                , C_US_ASCII},
809         {"ANSI_X3.4-1968"       , C_US_ASCII},
810 };
811 #endif /* !HAVE_LIBJCONV */
812
813 const gchar *conv_get_charset_str(CharSet charset)
814 {
815         gint i;
816
817         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
818                 if (charsets[i].charset == charset)
819                         return charsets[i].name;
820         }
821
822         return NULL;
823 }
824
825 CharSet conv_get_charset_from_str(const gchar *charset)
826 {
827         gint i;
828
829         if (!charset) return C_AUTO;
830
831         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
832                 if (!strcasecmp(charsets[i].name, charset))
833                         return charsets[i].charset;
834         }
835
836         return C_AUTO;
837 }
838
839 CharSet conv_get_current_charset(void)
840 {
841         static CharSet cur_charset = -1;
842         gint i;
843
844 #if HAVE_LIBJCONV
845         const gchar *cur_codeset;
846 #else
847         const gchar *cur_locale;
848 #endif
849
850         if (cur_charset != -1)
851                 return cur_charset;
852
853 #if HAVE_LIBJCONV
854         cur_codeset = jconv_info_get_current_codeset();
855         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
856                 if (!strcasecmp(cur_codeset, charsets[i].name)) {
857                         cur_charset = charsets[i].charset;
858                         return cur_charset;
859                 }
860         }
861 #else
862         cur_locale = g_getenv("LC_ALL");
863         if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
864         if (!cur_locale) cur_locale = g_getenv("LANG");
865         if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
866
867         debug_print("current locale: %s\n",
868                     cur_locale ? cur_locale : "(none)");
869
870         if (!cur_locale) {
871                 cur_charset = C_US_ASCII;
872                 return cur_charset;
873         }
874
875         if (strcasestr(cur_locale, "UTF-8")) {
876                 cur_charset = C_UTF_8;
877                 return cur_charset;
878         }
879
880         for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
881                 const gchar *p;
882
883                 /* "ja_JP.EUC" matches with "ja_JP.eucJP" and "ja_JP.EUC" */
884                 /* "ja_JP" matches with "ja_JP.xxxx" and "ja" */
885                 if (!strncasecmp(cur_locale, locale_table[i].locale,
886                                  strlen(locale_table[i].locale))) {
887                         cur_charset = locale_table[i].charset;
888                         return cur_charset;
889                 } else if ((p = strchr(locale_table[i].locale, '_')) &&
890                          !strchr(p + 1, '.')) {
891                         if (strlen(cur_locale) == 2 &&
892                             !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
893                                 cur_charset = locale_table[i].charset;
894                                 return cur_charset;
895                         }
896                 }
897         }
898 #endif
899
900         cur_charset = C_AUTO;
901         return cur_charset;
902 }
903
904 const gchar *conv_get_current_charset_str(void)
905 {
906         static const gchar *codeset = NULL;
907
908         if (!codeset)
909                 codeset = conv_get_charset_str(conv_get_current_charset());
910
911         return codeset ? codeset : "US-ASCII";
912 }
913
914 CharSet conv_get_outgoing_charset(void)
915 {
916         static CharSet out_charset = -1;
917
918 #if HAVE_LIBJCONV
919         gint i, j, n_pref_codesets;
920         const gchar *const *pref_codesets;
921 #else
922         CharSet cur_charset;
923 #endif
924
925         if (out_charset != -1)
926                 return out_charset;
927
928 #if HAVE_LIBJCONV
929         /* skip US-ASCII and UTF-8 */
930         pref_codesets = jconv_info_get_pref_codesets(&n_pref_codesets);
931         for (i = 0; i < n_pref_codesets; i++) {
932                 for (j = 3; j < sizeof(charsets) / sizeof(charsets[0]); j++) {
933                         if (!strcasecmp(pref_codesets[i], charsets[j].name)) {
934                                 out_charset = charsets[j].charset;
935                                 return out_charset;
936                         }
937                 }
938         }
939
940         for (i = 0; i < n_pref_codesets; i++) {
941                 if (!strcasecmp(pref_codesets[i], "UTF-8")) {
942                         out_charset = C_UTF_8;
943                         return out_charset;
944                 }
945         }
946
947         out_charset = C_AUTO;
948 #else
949         cur_charset = conv_get_current_charset();
950         switch (cur_charset) {
951         case C_EUC_JP:
952         case C_SHIFT_JIS:
953                 out_charset = C_ISO_2022_JP;
954                 break;
955         default:
956                 out_charset = cur_charset;
957         }
958 #endif
959
960         return out_charset;
961 }
962
963 const gchar *conv_get_outgoing_charset_str(void)
964 {
965         CharSet out_charset;
966         const gchar *str;
967
968         if (prefs_common.outgoing_charset) {
969                 if (!isalpha(prefs_common.outgoing_charset[0])) {
970                         g_free(prefs_common.outgoing_charset);
971                         prefs_common.outgoing_charset = g_strdup(CS_AUTO);
972                 } else if (strcmp(prefs_common.outgoing_charset, CS_AUTO) != 0)
973                         return prefs_common.outgoing_charset;
974         }
975
976         out_charset = conv_get_outgoing_charset();
977         str = conv_get_charset_str(out_charset);
978
979         return str ? str : "US-ASCII";
980 }
981
982 const gchar *conv_get_current_locale(void)
983 {
984         gchar *cur_locale;
985
986         cur_locale = g_getenv("LC_ALL");
987         if (!cur_locale) cur_locale = g_getenv("LANG");
988         if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
989
990         debug_print("current locale: %s\n",
991                     cur_locale ? cur_locale : "(none)");
992
993         return cur_locale;
994 }
995
996 void conv_unmime_header_overwrite(gchar *str)
997 {
998         gchar *buf;
999         gint buflen;
1000         CharSet cur_charset;
1001
1002         cur_charset = conv_get_current_charset();
1003
1004         if (cur_charset == C_EUC_JP) {
1005                 buflen = strlen(str) * 2 + 1;
1006                 Xalloca(buf, buflen, return);
1007                 conv_anytodisp(buf, buflen, str);
1008                 unmime_header(str, buf);
1009         } else {
1010                 buflen = strlen(str) + 1;
1011                 Xalloca(buf, buflen, return);
1012                 unmime_header(buf, str);
1013                 strncpy2(str, buf, buflen);
1014         }
1015 }
1016
1017 void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
1018                         const gchar *charset)
1019 {
1020         CharSet cur_charset;
1021
1022         cur_charset = conv_get_current_charset();
1023
1024         if (cur_charset == C_EUC_JP) {
1025                 gchar *buf;
1026                 gint buflen;
1027
1028                 buflen = strlen(str) * 2 + 1;
1029                 Xalloca(buf, buflen, return);
1030                 conv_anytodisp(buf, buflen, str);
1031                 unmime_header(outbuf, buf);
1032         } else
1033                 unmime_header(outbuf, str);
1034 }
1035
1036 #define MAX_ENCLEN      75
1037 #define MAX_LINELEN     76
1038
1039 #define B64LEN(len)     ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
1040
1041 #if HAVE_LIBJCONV
1042 void conv_encode_header(gchar *dest, gint len, const gchar *src,
1043                         gint header_len)
1044 {
1045         wchar_t *wsrc;
1046         wchar_t *wsrcp;
1047         gchar *destp;
1048         size_t line_len, mimehdr_len, mimehdr_begin_len;
1049         gchar *mimehdr_init = "=?";
1050         gchar *mimehdr_end = "?=";
1051         gchar *mimehdr_enctype = "?B?";
1052         const gchar *mimehdr_charset;
1053
1054         /* g_print("src = %s\n", src); */
1055         mimehdr_charset = conv_get_outgoing_charset_str();
1056
1057         /* convert to wide-character string */
1058         wsrcp = wsrc = strdup_mbstowcs(src);
1059         if (!wsrc) {
1060                 g_warning("Can't convert string to wide characters.\n");
1061                 strncpy2(dest, src, len);
1062                 return;
1063         }
1064
1065         mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
1066                 strlen(mimehdr_charset) + strlen(mimehdr_enctype);
1067         mimehdr_begin_len = strlen(mimehdr_init) +
1068                 strlen(mimehdr_charset) + strlen(mimehdr_enctype);
1069         line_len = header_len;
1070         destp = dest;
1071         *dest = '\0';
1072
1073         while (*wsrcp) {
1074                 wchar_t *wp, *wtmp, *wtmpp;
1075                 gint nspc = 0;
1076                 gboolean str_is_non_ascii;
1077
1078                 /* irresponsible buffer overrun check */
1079                 if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
1080
1081                 /* encode string including space
1082                    if non-ASCII string follows */
1083                 if (is_next_nonascii(wsrcp)) {
1084                         wp = wsrcp;
1085                         while ((wp = find_wspace(wp)) != NULL)
1086                                 if (!is_next_nonascii(wp)) break;
1087                         str_is_non_ascii = TRUE;
1088                 } else {
1089                         wp = find_wspace(wsrcp);
1090                         str_is_non_ascii = FALSE;
1091                 }
1092
1093                 if (wp != NULL) {
1094                         wtmp = wcsndup(wsrcp, wp - wsrcp);
1095                         wsrcp = wp + 1;
1096                         while (iswspace(wsrcp[nspc])) nspc++;
1097                 } else {
1098                         wtmp = wcsdup(wsrcp);
1099                         wsrcp += wcslen(wsrcp);
1100                 }
1101
1102                 wtmpp = wtmp;
1103
1104                 do {
1105                         gint tlen = 0;
1106                         gchar *tmp; /* internal codeset */
1107                         gchar *raw; /* converted, but not base64 encoded */
1108                         register gchar *tmpp;
1109                         gint raw_len;
1110
1111                         tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
1112                         *tmp = '\0';
1113                         raw = g_strdup("");
1114                         raw_len = 0;
1115
1116                         while (*wtmpp != (wchar_t)0) {
1117                                 gint mbl;
1118                                 gint dummy;
1119                                 gchar *raw_new = NULL;
1120                                 int raw_new_len = 0;
1121                                 const gchar *src_codeset;
1122
1123                                 mbl = wctomb(tmpp, *wtmpp);
1124                                 if (mbl == -1) {
1125                                         g_warning("invalid wide character\n");
1126                                         wtmpp++;
1127                                         continue;
1128                                 }
1129                                 /* g_free(raw); */
1130                                 src_codeset = conv_get_current_charset_str();
1131                                 /* printf ("tmp = %s, tlen = %d, mbl\n",
1132                                         tmp, tlen, mbl); */
1133                                 if (jconv_alloc_conv(tmp, tlen + mbl,
1134                                                      &raw_new, &raw_new_len,
1135                                                      &src_codeset, 1,
1136                                                      &dummy, mimehdr_charset)
1137                                     != 0) {
1138                                         g_warning("can't convert\n");
1139                                         tmpp[0] = '\0';
1140                                         wtmpp++;
1141                                         continue;
1142                                 }
1143                                 if (str_is_non_ascii) {
1144                                         gint dlen = mimehdr_len +
1145                                                 B64LEN(raw_len);
1146                                         if ((line_len + dlen +
1147                                              (*(wtmpp + 1) ? 0 : nspc) +
1148                                              (line_len > 1 ? 1 : 0))
1149                                             > MAX_LINELEN) {
1150                                                 g_free(raw_new);
1151                                                 if (tlen == 0) {
1152                                                         *destp++ = '\n';
1153                                                         *destp++ = ' ';
1154                                                         line_len = 1;
1155                                                         continue;
1156                                                 } else {
1157                                                         *tmpp = '\0';
1158                                                         break;
1159                                                 }
1160                                         }
1161                                 } else if ((line_len + tlen + mbl +
1162                                             (*(wtmpp + 1) ? 0 : nspc) +
1163                                             (line_len > 1 ? 1 : 0))
1164                                            > MAX_LINELEN) {
1165                                         g_free(raw_new);
1166                                         if (1 + tlen + mbl +
1167                                             (*(wtmpp + 1) ? 0 : nspc)
1168                                             >= MAX_LINELEN) {
1169                                                 *tmpp = '\0';
1170                                                 break;
1171                                         }
1172                                         *destp++ = '\n';
1173                                         *destp++ = ' ';
1174                                         line_len = 1;
1175                                         continue;
1176                                 }
1177
1178                                 tmpp += mbl;
1179                                 *tmpp = '\0';
1180
1181                                 tlen += mbl;
1182
1183                                 g_free(raw);
1184                                 raw = raw_new;
1185                                 raw_len = raw_new_len;
1186
1187                                 wtmpp++;
1188                         }
1189                         /* g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
1190                                 tmp, tlen, mb_seqlen); */
1191
1192                         if (tlen == 0 || raw_len == 0) {
1193                                 g_free(tmp);
1194                                 g_free(raw);
1195                                 continue;
1196                         }
1197
1198                         if (line_len > 1 && destp > dest) {
1199                                 *destp++ = ' ';
1200                                 *destp = '\0';
1201                                 line_len++;
1202                         }
1203
1204                         if (str_is_non_ascii) {
1205                                 g_snprintf(destp, len - strlen(dest), "%s%s%s",
1206                                            mimehdr_init, mimehdr_charset,
1207                                            mimehdr_enctype);
1208                                 destp += mimehdr_begin_len;
1209                                 line_len += mimehdr_begin_len;
1210
1211                                 base64_encode(destp, raw, raw_len);
1212                                 line_len += strlen(destp);
1213                                 destp += strlen(destp);
1214
1215                                 strcpy(destp, mimehdr_end);
1216                                 destp += strlen(mimehdr_end);
1217                                 line_len += strlen(mimehdr_end);
1218                         } else {
1219                                 strcpy(destp, tmp);
1220                                 line_len += strlen(destp);
1221                                 destp += strlen(destp);
1222                         }
1223
1224                         g_free(tmp);
1225                         g_free(raw);
1226                         /* g_print("line_len = %d\n\n", line_len); */
1227                 } while (*wtmpp != (wchar_t)0);
1228
1229                 while (iswspace(*wsrcp)) {
1230                         gint mbl;
1231
1232                         mbl = wctomb(destp, *wsrcp++);
1233                         if (mbl != -1) {
1234                                 destp += mbl;
1235                                 line_len += mbl;
1236                         }
1237                 }
1238                 *destp = '\0';
1239
1240                 g_free(wtmp);
1241         }
1242
1243         g_free(wsrc);
1244
1245         /* g_print("dest = %s\n", dest); */
1246 }
1247 #else /* !HAVE_LIBJCONV */
1248
1249 #define JIS_SEQLEN      3
1250
1251 void conv_encode_header(gchar *dest, gint len, const gchar *src,
1252                         gint header_len)
1253 {
1254         wchar_t *wsrc;
1255         wchar_t *wsrcp;
1256         gchar *destp;
1257         size_t line_len, mimehdr_len, mimehdr_begin_len;
1258         gchar *mimehdr_init = "=?";
1259         gchar *mimehdr_end = "?=";
1260         gchar *mimehdr_enctype = "?B?";
1261         const gchar *mimehdr_charset;
1262         gboolean do_conv = FALSE;
1263
1264         /* g_print("src = %s\n", src); */
1265         mimehdr_charset = conv_get_outgoing_charset_str();
1266         if (strcmp(mimehdr_charset, "ISO-2022-JP") == 0)
1267                 do_conv = TRUE;
1268         else if (strcmp(mimehdr_charset, "US-ASCII") == 0)
1269                 mimehdr_charset = "ISO-8859-1";
1270
1271         /* convert to wide-character string */
1272         wsrcp = wsrc = strdup_mbstowcs(src);
1273         if (!wsrc) {
1274                 g_warning("Can't convert string to wide characters.\n");
1275                 strncpy2(dest, src, len);
1276                 return;
1277         }
1278
1279         mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
1280                       strlen(mimehdr_charset) + strlen(mimehdr_enctype);
1281         mimehdr_begin_len = strlen(mimehdr_init) +
1282                             strlen(mimehdr_charset) + strlen(mimehdr_enctype);
1283         line_len = header_len;
1284         destp = dest;
1285         *dest = '\0';
1286
1287         while (*wsrcp) {
1288                 wchar_t *wp, *wtmp, *wtmpp;
1289                 gint nspc = 0;
1290                 gboolean str_is_non_ascii;
1291
1292                 /* irresponsible buffer overrun check */
1293                 if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
1294
1295                 /* encode string including space
1296                    if non-ASCII string follows */
1297                 if (is_next_nonascii(wsrcp)) {
1298                         wp = wsrcp;
1299                         while ((wp = find_wspace(wp)) != NULL)
1300                                 if (!is_next_nonascii(wp)) break;
1301                         str_is_non_ascii = TRUE;
1302                 } else {
1303                         wp = find_wspace(wsrcp);
1304                         str_is_non_ascii = FALSE;
1305                 }
1306
1307                 if (wp != NULL) {
1308                         wtmp = wcsndup(wsrcp, wp - wsrcp);
1309                         wsrcp = wp + 1;
1310                         while (iswspace(wsrcp[nspc])) nspc++;
1311                 } else {
1312                         wtmp = wcsdup(wsrcp);
1313                         wsrcp += wcslen(wsrcp);
1314                 }
1315
1316                 wtmpp = wtmp;
1317
1318                 do {
1319                         gint prev_mbl = 1, tlen = 0, mb_seqlen = 0;
1320                         gchar *tmp;
1321                         register gchar *tmpp;
1322
1323                         tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
1324                         *tmp = '\0';
1325
1326                         while (*wtmpp != (wchar_t)0) {
1327                                 gint mbl;
1328
1329                                 mbl = wctomb(tmpp, *wtmpp);
1330                                 if (mbl == -1) {
1331                                         g_warning("invalid wide character\n");
1332                                         wtmpp++;
1333                                         continue;
1334                                 }
1335
1336                                 /* length of KI + KO */
1337                                 if (do_conv && prev_mbl == 1 && mbl == 2)
1338                                         mb_seqlen += JIS_SEQLEN * 2;
1339
1340                                 if (str_is_non_ascii) {
1341                                         gint dlen = mimehdr_len +
1342                                                 B64LEN(tlen + mb_seqlen + mbl);
1343
1344                                         if ((line_len + dlen +
1345                                              (*(wtmpp + 1) ? 0 : nspc) +
1346                                              (line_len > 1 ? 1 : 0))
1347                                             > MAX_LINELEN) {
1348                                                 if (tlen == 0) {
1349                                                         *destp++ = '\n';
1350                                                         *destp++ = ' ';
1351                                                         line_len = 1;
1352                                                         mb_seqlen = 0;
1353                                                         continue;
1354                                                 } else {
1355                                                         *tmpp = '\0';
1356                                                         break;
1357                                                 }
1358                                         }
1359                                 } else if ((line_len + tlen + mbl +
1360                                             (*(wtmpp + 1) ? 0 : nspc) +
1361                                             (line_len > 1 ? 1 : 0))
1362                                            > MAX_LINELEN) {
1363                                         if (1 + tlen + mbl +
1364                                             (*(wtmpp + 1) ? 0 : nspc)
1365                                             >= MAX_LINELEN) {
1366                                                 *tmpp = '\0';
1367                                                 break;
1368                                         }
1369                                         *destp++ = '\n';
1370                                         *destp++ = ' ';
1371                                         line_len = 1;
1372                                         continue;
1373                                 }
1374
1375                                 tmpp += mbl;
1376                                 *tmpp = '\0';
1377
1378                                 tlen += mbl;
1379                                 prev_mbl = mbl;
1380
1381                                 wtmpp++;
1382                         }
1383                         /* g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
1384                                 tmp, tlen, mb_seqlen); */
1385
1386                         if (tlen == 0) {
1387                                 g_free(tmp);
1388                                 continue;
1389                         }
1390
1391                         if (line_len > 1 && destp > dest) {
1392                                 *destp++ = ' ';
1393                                 *destp = '\0';
1394                                 line_len++;
1395                         }
1396
1397                         if (str_is_non_ascii) {
1398                                 gchar *raw;
1399
1400                                 raw = g_new(gchar, tlen + mb_seqlen + 1);
1401                                 if (do_conv)
1402                                         conv_euctojis(raw, tlen + mb_seqlen + 1,
1403                                                       tmp);
1404                                 else
1405                                         strcpy(raw, tmp);
1406                                 g_snprintf(destp, len - strlen(dest), "%s%s%s",
1407                                            mimehdr_init, mimehdr_charset,
1408                                            mimehdr_enctype);
1409                                 destp += mimehdr_begin_len;
1410                                 line_len += mimehdr_begin_len;
1411
1412                                 base64_encode(destp, raw, strlen(raw));
1413                                 line_len += strlen(destp);
1414                                 destp += strlen(destp);
1415
1416                                 strcpy(destp, mimehdr_end);
1417                                 destp += strlen(mimehdr_end);
1418                                 line_len += strlen(mimehdr_end);
1419
1420                                 g_free(raw);
1421                         } else {
1422                                 strcpy(destp, tmp);
1423                                 line_len += strlen(destp);
1424                                 destp += strlen(destp);
1425                         }
1426
1427                         g_free(tmp);
1428                         /* g_print("line_len = %d\n\n", line_len); */
1429                 } while (*wtmpp != (wchar_t)0);
1430
1431                 while (iswspace(*wsrcp)) {
1432                         gint mbl;
1433
1434                         mbl = wctomb(destp, *wsrcp++);
1435                         if (mbl != -1) {
1436                                 destp += mbl;
1437                                 line_len += mbl;
1438                         }
1439                 }
1440                 *destp = '\0';
1441
1442                 g_free(wtmp);
1443         }
1444
1445         g_free(wsrc);
1446
1447         /* g_print("dest = %s\n", dest); */
1448 }
1449 #endif /* HAVE_LIBJCONV */