summaryrefslogtreecommitdiff
path: root/common_lib.pl
blob: 4b7f41a99df8ad74dbbf22d63f0ad6364056e522 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
package main;

#
# ウェブメーラーの全てのインタフェースで共通する変数
#

# ウェブメーラーのバージョン
$version = '0.954';

# メールのリストの表示数を格納している配列(数値ではなく文字列で定義)
#   '0'は全てのメールを表示する
@list_number_array = ('10', '50', '100', '0');

# このソースのキャラクターセット(漢字コード)
$charset = &jcode::getcode(\'漢字');
$charset2 = uc($charset);

#
# ウェブメーラーの全てのインタフェースで共通する関数
#

# form_analysis()
#   引数
#     なし
#   戻り値
#     なし
sub form_analysis{
	my $buf;
	my $tmp;
	my @tmp_array;
	my $name;
	my $value;

	if($ENV{'REQUEST_METHOD'} eq 'POST'){
		read(STDIN, $buf, $ENV{'CONTENT_LENGTH'});
	}else{
		$buf = $ENV{'QUERY_STRING'};
	}

	undef %FORM;

	@tmp_array = split(/&/, $buf);
	foreach $tmp (@tmp_array){
		($name, $value) = split(/=/, $tmp);
		# フォームの場合にスペースが%20ではなく+に置換されるので再びスペースに戻す
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		&jcode::convert(\$value, $charset);
		if($name eq 'target'){
			push(@target_array, $value);
		}elsif($name eq 'to' || $name eq 'cc' || $name eq 'bcc'){
			if(defined($FORM{$name})){
				$FORM{$name} .= ", $value";
			}else{
				$FORM{$name} = $value;
			}
		}else{
			$FORM{$name} = $value;
		}
	}
}

# $sessionid = user_info_encode($user, $passwd, $ipaddr, $type, $salt)
#   引数
#     $user   - ユーザー名
#     $passwd - パスワード
#     $ipaddr - IPアドレス
#     $type   - 認証タイプ
#     $salt   - crypt関数で使うsalt
#   戻り値
#     $sessionid - セッションID
sub user_info_encode{
	(my($user), my($passwd), my($ipaddr), my($type), my($reverse), my($ipcheck), my($salt)) = @_;
	my $time;
	my $salt2;
	my $cryptuser;
	my $cryptpasswd;
	my $sessionid1;
	my $sessionid2;

	$time = int(rand(time));
	$salt2 = "$time";
	$cryptuser = crypt($user, $salt2);
	$cryptpasswd = crypt($passwd, $salt);
	$sessionid1 = crypt($cryptuser, $cryptpasswd);
	$sessionid2 = crypt($sessionid1, $salt2);
	$sessionid1 =~ s/\//\-/g; $sessionid1 =~ s/\./\_/g;
	$sessionid2 =~ s/\//\-/g; $sessionid2 =~ s/\./\_/g;
	$sessionid1 = &wm_rand_id(24);
	$sessionid2 = &wm_rand_id(24);

	$session_file = $session_folder."/".$sessionid1."\.tmp";

	$user = &encode($user, $sessionid2);
	$passwd = &encode($passwd, $sessionid2);
	$ipaddr = &encode($ipaddr, $sessionid2);

	if(!open(FILE,">$session_file")){
		&error('エラー', 'セッション用ファイルが作成できませんでした。', 0);
	}

	print FILE "$user\0$passwd\0$ipaddr\0$type\0$reverse\0$ipcheck";
	close(FILE);

	return "$sessionid1\@$sessionid2";
}

# ($user, $passwd, $ipaddr, $type) = user_info_decode($sessionid)
#   引数
#     $sessionid - セッションID
#   戻り値
#     $user   - ユーザー名
#     $passwd - パスワード
#     $ipaddr - IPアドレス
#     $type   - 認証タイプ
sub user_info_decode{
	my $sessionid;
	my $sessionid1;
	my $sessionid2;
	my $line;
	my @tmp_array;

	$sessionid = $_[0];
	($sessionid1, $sessionid2) = split("\@", $sessionid);
	if($sessionid1 !~ /^[A-Za-z0-9_\-]{8,}$/ || $sessionid2 !~ /^[A-Za-z0-9_\-]{8,}$/){ &error("Error", "Invalid session.", 0); }
	$session_file = $session_folder."\/".$sessionid1."\.tmp";

	if(!open(FILE, "$session_file")){
		if($FORM{'action'} eq 'command'){
			&print_header;
			&print_footer;
			exit;
		}else{
			&error('エラー','セッション用ファイルが開けませんでした。'."\n".'ログインしなおしてください。', 1);
		}
	}

	$line = <FILE>;
	close(FILE);

	@tmp_array = split("\0", $line);

	return (&decode($tmp_array[0], $sessionid2),
			&decode($tmp_array[1], $sessionid2),
			&decode($tmp_array[2], $sessionid2),
			$tmp_array[3], $tmp_array[4], $tmp_array[5]);
}

sub header_analysis{
	my $header = $_[0];
	my %header_hash;
	my $i;

	my @tmp_array = split(/\r\n([^\:^\s]+)\s*:/, $header);
	shift(@tmp_array);
	for($i=0; $i<$#tmp_array; $i=$i+2){
		$key = $tmp_array[$i];
		$value = $tmp_array[($i+1)];

		$key = uc($key);
		&jcode::convert(\$value, "euc");
		$value = deltag($value);
		$value =~ s/^\s*//;       # 先頭のスペースを削除
		$value =~ s/\r\n /\r\n/g; # スペースは改行の後のもののみ削除
		$value =~ s/\r\n//g; $value =~ s/\t//g;
		&jcode::convert(\$value, $charset);
		$value = &mimedecode($value, $charset2);

		if(($key eq 'TO' || $key eq 'CC' || $key eq 'REPLY-TO') && exists($header_hash{$key})){
			$header_hash{$key} .= ", $value";
		}else{
			$header_hash{$key} = $value;
		}
	}
	if(!exists($header_hash{'FROM'})){ $header_hash{'FROM'} = '(Fromなし)'; }
	if(!exists($header_hash{'SUBJECT'})){ $header_hash{'SUBJECT'} = '(Subjectなし)'; }
	if($header_hash{'SUBJECT'} eq ''){ $header_hash{'SUBJECT'} = '(空文字列のSubject)'; }

	return %header_hash;
}

sub date_analysis{
	my $date = $_[0];

	my @youbi_array_en = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
	my @youbi_array_ja = ('日','月','火','水','木','金','土');
	my @tsuki_array_en = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	my @tsuki_array_ja = ('1','2','3','4','5','6','7','8','9','10','11','12');
	my $i;
	my $nichi_flag = 0;

	my %date_array = '';
	@tmp_array = split(/[\ \,]+/, $date);
	foreach(@tmp_array){
		if(/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)$/i){ $date_array{'youbi'} = $_; } # 曜日
		elsif(/^\d{1,2}$/ && !$nichi_flag){ $date_array{'nichi'} = $_; $nichi_flag = 1; } # 日
		elsif(/^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)$/i){ $date_array{'tsuki'} = $_; } # 月
		elsif(/^\d{2,4}$/){
			if(/^\d{2}$/){
				$_ = "20".$_;
			}
			$date_array{'nen'} = $_;
		} # 年
		elsif(/^\d{1,2}:\d{1,2}:\d{1,2}$/){ $date_array{'jikan'} = $_; } # 時、分、秒
		elsif(/^(\+|\-)\d{4}(\(.+\))?$/){ $date_array{'jisa'} = $_; } # 時差
	}

	$i=0;
	foreach(@youbi_array_en){
		if($date_array{'youbi'} =~ /$_/){ $date_array{'youbi'}= $youbi_array_ja[$i]; }
		$i++;
	}
	$i=0;
	foreach(@tsuki_array_en){
		if($date_array{'tsuki'} =~ /$_/){ $date_array{'tsuki'}= $tsuki_array_ja[$i]; }
		$i++;
	}

	return %date_array;
}

sub print_header{
#	$print_buf = "HTTP/1.1 200 OK\n";
	$print_buf = "Cache-Control: no-cache\n";
	$print_buf .= "Pragma: no-cache\n";
	$print_buf .= "Expires: 0\n";
	if($charset eq 'euc'){
		$print_buf .= "Content-type: text/html; charset=EUC-JP\n\n";
	}elsif($charset eq 'sjis'){
		$print_buf .= "Content-type: text/html; charset=Shift_JIS\n\n";
	}else{
		$print_buf .= "Content-type: text/html\n\n";
	}
	$print_buf .= "<HTML>\n";
	$print_buf .= "<HEAD><TITLE>$title</TITLE></HEAD>\n\n";
	$print_buf .= "$body_tag\n";

	print $print_buf;
}

sub print_footer{
	if($FORM{'action'} ne 'blankpage' && $FORM{'action'} ne 'command'){
		print "<BR><DIV ALIGN=right><A HREF=http://www.ai.is.saga-u.ac.jp/~takeda/software.phtml TARGET=_blank>ウェブメーラー Ver$version by Akihiko Takeda</A></DIV>\n";
	}
	print "</BODY></HTML>\n";
}

# error($error_title, $error_msg, $del_session_file)
#   引数
#     $error_title      - エラー画面のタイトル
#     $error_msg        - エラーメッセージ
#     $del_session_file - セッション用ファイルの消去 0:消去しない 1:消去する
#   戻り値
#     なし
sub error{
	(my($error_title), my($error_msg), my($del_session_file)) = @_;

	$error_msg = deltag($error_msg);
	$error_msg =~ s/\n/\n<BR>/g;

	if($del_session_file ne '0'){
		unlink($session_file);
	}

#	$print_buf = "HTTP/1.1 403 Forbidden\n";
	$print_buf = "Status: 403 Forbidden\n";
	$print_buf .= "Cache-Control: no-cache\n";
	$print_buf .= "Pragma: no-cache\n";
	$print_buf .= "Expires: 0\n";
	if($charset eq 'euc'){
		$print_buf .= "Content-type: text/html; charset=EUC-JP\n\n";
	}elsif($charset eq 'sjis'){
		$print_buf .= "Content-type: text/html; charset=Shift_JIS\n\n";
	}else{
		$print_buf .= "Content-type: text/html\n\n";
	}

	$print_buf .= "<HTML>\n";
	$print_buf .= "<HEAD><TITLE>$title</TITLE></HEAD>\n\n";
	$print_buf .= "<SCRIPT LANGUAGE=\"JavaScript\">\n";
	$print_buf .= "<!--\n";

	if($del_session_file eq '1'){
		$print_buf .= "parent.LogoutFlag = 1;\n";
	}

	$print_buf .= "function command(n){\n";
	$print_buf .= "\talert(\"エラーが発生した場合、コマンドウィンドウは使用できません。\");\n";
	$print_buf .= "}\n";
	$print_buf .= "//-->\n";
	$print_buf .= "</SCRIPT>\n\n";
	$print_buf .= "$body_tag\n\n";
	$print_buf .= "<H1>$error_title</H1>\n";
	$print_buf .= "$error_msg<BR><BR>\n";

	print $print_buf;

	if($del_session_file eq '0'){
		if($type eq '2'){
			print "<A HREF=\"JavaScript:history.back();\">戻る(要JavaScript)</A><BR><BR>\n<A HREF=$imap4_url?sessionid=$sessionid\&action=logout TARGET=_top>ログアウト</A>\n";
		}else{
			print "<A HREF=\"JavaScript:history.back();\">戻る(要JavaScript)</A><BR><BR>\n<A HREF=$pop3_url?sessionid=$sessionid\&action=logout TARGET=_top>ログアウト</A>\n";
		}
	}else{
		print "<A HREF=$login_url TARGET=_top>ログイン画面へ</A><BR><BR>\n<A HREF=$home_url TARGET=_top>ホームページへ</A>\n";
	}

	&print_footer;

	if($type eq '2'){
		&command_history($Imap4_lib::gCommands);
	}else{
		&command_history($Pop3_lib::gCommands);
	}

	exit;
}

sub blankpage{
	&print_header;

	print "<H1>$title</H1>\n";
	print 'ここにメールの内容等が表示されます。';

	&print_footer;

	exit;
}

sub redirect{
	if(!$FORM{'url'}){
		&error('エラー', '転送するURLが指定されていません。', 0);
	}

	$print_buf = "Cache-Control: no-cache\n";
	$print_buf .= "Pragma: no-cache\n";
	$print_buf .= "Expires: 0\n";
	if($charset eq 'euc'){
		$print_buf .= "Content-type: text/html; charset=EUC-JP\n\n";
	}elsif($charset eq 'sjis'){
		$print_buf .= "Content-type: text/html; charset=Shift_JIS\n\n";
	}else{
		$print_buf .= "Content-type: text/html\n\n";
	}
	$print_buf .= "<HTML><HEAD>\n";
	if($FORM{'url'} !~ m{^https?://[^\s"'<>]+$}i){ &error("Error", "Invalid URL.", 0); }
	$wm_url_e = &tag_escape($FORM{'url'});
	$print_buf .= "<META HTTP-EQUIV=\"Refresh\" content=\"1;url=$wm_url_e\">\n";
	$print_buf .= "<TITLE>$title</TITLE></HEAD>\n\n";
	$print_buf .= "$body_tag\n";
	$print_buf .= "<H1>URLリダイレクト</H1>\n";
	$print_buf .= "現在 $wm_url_e に転送中です。<BR>しばらくお待ちください。<BR>\n";

	print $print_buf;

	&print_footer;

	exit;
}

sub encode{
	(my($data), my($key)) = @_;
	my $i;
	my $j;
	my $ret;
	my @ks;
	my @tmpa1;
	my @tmpa2;
	my @tmpa3;

	$data = "" . length($data) . "\0\0\0" . $data;

	$data =~ s/([\x00-\xff])/unpack("H2", $1)/eg;

	@tmpa1 = split(/ */, $data);
	for($i = 0, $j = 0; $i <= $#tmpa1 + 1; $i = $i + 8, $j++){
		$tmpa2[$j] = $tmpa1[$i].$tmpa1[$i+1].$tmpa1[$i+2].$tmpa1[$i+3].$tmpa1[$i+4].$tmpa1[$i+5].$tmpa1[$i+6].$tmpa1[$i+7];
	}

	@ks =  &des::des_set_key($key);

	foreach(@tmpa2){
		@tmpa3 = unpack("C*", &des::des_ecb_encrypt(\@ks, 1, $_));

		foreach(@tmpa3){
			$ret .= sprintf("%XS", $_);
		}
	}

	return $ret;
}

sub decode{
	(my($datae), my($key)) = @_;
	my $i;
	my $length;
	my $ret;
	my @ks;
	my @tmpa1;
	my @tmpa2;

	@tmpa1 = split("S", $datae);
	foreach(@tmpa1){
		$_ = oct("0x$_");
	}

	@ks =  &des::des_set_key($key);

	while(@tmpa1){
		undef(@tmpa2);
		for($i = 0; $i <= 7; $i++){
			push(@tmpa2, shift(@tmpa1));
		}
		$ret .= &des::des_ecb_encrypt(\@ks, 0, pack("C*", @tmpa2))
	}

	$ret =~ s/([\w][\w])/pack("C", hex($1))/eg;

	($length, $ret) = split("\0\0\0", $ret, 2);
	$ret = substr($ret, 0, $length);

	return $ret;
}

sub crc{
	my @CRCTable=(
	0x0000,0x1189,0x2312,0x329b,0x4624,0x57ad,0x6536,0x74bf,
	0x8c48,0x9dc1,0xaf5a,0xbed3,0xca6c,0xdbe5,0xe97e,0xf8f7,
	0x1081,0x0108,0x3393,0x221a,0x56a5,0x472c,0x75b7,0x643e,
	0x9cc9,0x8d40,0xbfdb,0xae52,0xdaed,0xcb64,0xf9ff,0xe876,
	0x2102,0x308b,0x0210,0x1399,0x6726,0x76af,0x4434,0x55bd,
	0xad4a,0xbcc3,0x8e58,0x9fd1,0xeb6e,0xfae7,0xc87c,0xd9f5,
	0x3183,0x200a,0x1291,0x0318,0x77a7,0x662e,0x54b5,0x453c,
	0xbdcb,0xac42,0x9ed9,0x8f50,0xfbef,0xea66,0xd8fd,0xc974,
	0x4204,0x538d,0x6116,0x709f,0x0420,0x15a9,0x2732,0x36bb,
	0xce4c,0xdfc5,0xed5e,0xfcd7,0x8868,0x99e1,0xab7a,0xbaf3,
	0x5285,0x430c,0x7197,0x601e,0x14a1,0x0528,0x37b3,0x263a,
	0xdecd,0xcf44,0xfddf,0xec56,0x98e9,0x8960,0xbbfb,0xaa72,
	0x6306,0x728f,0x4014,0x519d,0x2522,0x34ab,0x0630,0x17b9,
	0xef4e,0xfec7,0xcc5c,0xddd5,0xa96a,0xb8e3,0x8a78,0x9bf1,
	0x7387,0x620e,0x5095,0x411c,0x35a3,0x242a,0x16b1,0x0738,
	0xffcf,0xee46,0xdcdd,0xcd54,0xb9eb,0xa862,0x9af9,0x8b70,
	0x8408,0x9581,0xa71a,0xb693,0xc22c,0xd3a5,0xe13e,0xf0b7,
	0x0840,0x19c9,0x2b52,0x3adb,0x4e64,0x5fed,0x6d76,0x7cff,
	0x9489,0x8500,0xb79b,0xa612,0xd2ad,0xc324,0xf1bf,0xe036,
	0x18c1,0x0948,0x3bd3,0x2a5a,0x5ee5,0x4f6c,0x7df7,0x6c7e,
	0xa50a,0xb483,0x8618,0x9791,0xe32e,0xf2a7,0xc03c,0xd1b5,
	0x2942,0x38cb,0x0a50,0x1bd9,0x6f66,0x7eef,0x4c74,0x5dfd,
	0xb58b,0xa402,0x9699,0x8710,0xf3af,0xe226,0xd0bd,0xc134,
	0x39c3,0x284a,0x1ad1,0x0b58,0x7fe7,0x6e6e,0x5cf5,0x4d7c,
	0xc60c,0xd785,0xe51e,0xf497,0x8028,0x91a1,0xa33a,0xb2b3,
	0x4a44,0x5bcd,0x6956,0x78df,0x0c60,0x1de9,0x2f72,0x3efb,
	0xd68d,0xc704,0xf59f,0xe416,0x90a9,0x8120,0xb3bb,0xa232,
	0x5ac5,0x4b4c,0x79d7,0x685e,0x1ce1,0x0d68,0x3ff3,0x2e7a,
	0xe70e,0xf687,0xc41c,0xd595,0xa12a,0xb0a3,0x8238,0x93b1,
	0x6b46,0x7acf,0x4854,0x59dd,0x2d62,0x3ceb,0x0e70,0x1ff9,
	0xf78f,0xe606,0xd49d,0xc514,0xb1ab,0xa022,0x92b9,0x8330,
	0x7bc7,0x6a4e,0x58d5,0x495c,0x3de3,0x2c6a,0x1ef1,0x0f78
	);
	my $str = $_[0];
	my $value = 0xFFFF;
	my $cnt;

	for ($cnt = 0; $cnt < length($str); $cnt++){
		$value = $CRCTable[($value%256) ^ vec($str, $cnt, 8)] ^ (($value%65536) >> 8);
	}

	return sprintf("%04x", $value);
}

sub deltag{
	my $tmp = $_[0];

	$tmp =~ s/</&lt;/g;
	$tmp =~ s/>/&gt;/g;

	return $tmp;
}

sub tag_escape{
	my $tmp = $_[0];

	$tmp =~ s/</&lt;/g;
	$tmp =~ s/>/&gt;/g;
	$tmp =~ s/"/&#34;/g;
	$tmp =~ s/'/&#39;/g;

	return $tmp;
}

sub url_encode{
	my $tmp = $_[0];

	$tmp =~ s/ /\%20/g;
	$tmp =~ s/\#/\%23/g;
	$tmp =~ s/\&/\%26/g;
	$tmp =~ s/\=/\%3D/g;

	return $tmp;
}

sub chop_ret{
	my $tmp = $_[0];

	chop($tmp);
	chop($tmp);

	return $tmp;
}

sub command_history{
	if($command_history){
		my $rand;
		my $print_buf = "";
		my @command_history_array;

		my $tmp = $_[0];

		@command_history_array = split("\r\n", $tmp);

		$print_buf = "<BR>\n<SCRIPT LANGUAGE=JavaScript>\n";
		$print_buf .= "function command_history(){\n";
		$print_buf .= "\tvar w = window.open(\"\", \"_blank\");\n";
		$print_buf .= "\tw.document.write(\"<HTML><HEAD><TITLE>$title</TITLE></HEAD>\\n$body_tag\\n\");\n";
		$print_buf .= "\tw.document.write(\"<H1>コマンド履歴</H1>\\n\");\n";

		foreach(@command_history_array){
			s/\\/\\\\/g;
			s/\"/\\\"/g;
			$print_buf .= "\tw.document.write(\"$_<BR>\\n\");\n";
		}

		$print_buf .= "\tw.document.write(\"</BODY></HTML>\\n\");\n";
		$print_buf .= "\tw.document.close();\n";
		$print_buf .= "}\n";
		$print_buf .= "</SCRIPT>\n";
		$print_buf .= "<A HREF=\"JavaScript:command_history();\" TARGET=_self>コマンド履歴(要JavaScript)</A>\n";

		print $print_buf;
	}
}

sub re_message_format{
	(my($str), my(%header_hash)) = @_;

	$str =~ s/%{2}/\0/g;
	$str =~ s/%f/$header_hash{'FROM'}/g;
	$str =~ s/%d/$header_hash{'DATE'}/g;
	$str =~ s/%s/$header_hash{'SUBJECT'}/g;
	$str =~ s/%m/$header_hash{'MESSAGE-ID'}/g;
	$str =~ s/%t/$header_hash{'TO'}/g;
	$str =~ s/%c/$header_hash{'CC'}/g;
	$str =~ s/%n/\r\n/g;
	$str =~ s/\0/%/g;

	return $str;
}

# @maddr_array = maddr_split($mddr_str, $kcode);
#   引数
#     $mdaar_str : 複数のメールアドレスが連結された文字列
#     $kcode     : $mdaar_str の漢字コード
#   戻り値
#     @maddr_array : 分割したメールアドレスを格納した配列
#                    (漢字が含まれている場合漢字コードはEUCに変換される)
sub maddr_split{
	my @maddr_array;
	my @tmp_array;
	my $i; # カウンタ変数
	my $j = 0;
	my $flag = 0;
	my $tmp = "";

	(my($maddr_str), my($kcode)) = @_;

	&jcode::convert(\$maddr_str, "euc", $kcode, "z");
	@tmp_array = split("\,", $maddr_str);
	for($i=0; $i<=$#tmp_array; $i++){
		if($tmp_array[$i] =~ /\(/ && $tmp_array[$i] !~ /\)/){
			$maddr_array[$j] = $tmp_array[$i];
		}elsif($tmp_array[$i] !~ /\(/ && $tmp_array[$i] =~ /\)/){
			$maddr_array[$j] .= ','.$tmp_array[$i];
			$j++;
		}elsif(($tmp_array[$i] =~ tr/"/"/) == 1 && $flag == 0){
			$maddr_array[$j] = $tmp_array[$i];
			$flag = 1;
		}elsif(($tmp_array[$i] =~ tr/"/"/) == 1 && $flag == 1){
			$maddr_array[$j] .= ','.$tmp_array[$i];
			$j++;
			$flag = 0;
		}else{
			$maddr_array[$j] = $tmp_array[$i];
			$j++;
		}
	}

	# メールアドレスの先頭・末尾のスペースを削除
	foreach $tmp (@maddr_array){
		$tmp =~ s/^[ \t]*//g;
		$tmp =~ s/[ \t]*$//g;
	}

	return @maddr_array;
}

sub renew_session_file{
	my $line;
	local *FILE;

	my $session_file = $_[0];

	if(!open(FILE, "$session_file")){
		return 0;
	}

	$line = <FILE>;
	close(FILE);

	if(!unlink($session_file)){
		return 0;
	}

	if(!open(FILE,">$session_file")){
		return 0;
	}

	print FILE "$line";
	close(FILE);

	return 1;
}

sub get_configure{
	my $configure_file;
	my $tmp;
	my $conf_encrypt_passdwd;
	my @conf_array;
	my %ret_hash;
	local *FILE;

	if($configure){
		return %ret_hash;
	}

	$configure_file = $configure_folder . "/" . $user . $configure_ext;
	if(open(FILE, "$configure_file")){
		binmode(FILE);

		while(<FILE>){
			$tmp .= $_;
		}
		close(FILE);

		$conf_encrypt_passwd = &md5::convert($user.'WM'.$passwd);
		$conf_encrypt_passwd = sprintf("%08x%08x%08x%08x", unpack('NNNN', $conf_encrypt_passwd));
		$conf_encrypt_passwd = &md5::convert($conf_encrypt_passwd);
		$conf_encrypt_passwd = sprintf("%08x%08x%08x%08x", unpack('NNNN', $conf_encrypt_passwd));
		@conf_array = split("\0", &decode($tmp, $conf_encrypt_passwd));

		if($#conf_array == 0){
			$ret_hash{'err_msg'} = '警告:設定ファイルが復元できません。もう一度設定しなおして下さい。';
		}else{
			if($conf_array[0] ne 'WM'){
				$ret_hash{'err_msg'} = '警告:設定ファイルが復元できません。もう一度設定しなおして下さい。';
			}else{
				$ret_hash{'name'} = $conf_array[1];
				$ret_hash{'signature'} = $conf_array[2];
				$ret_hash{'address'} = $conf_array[3];

				&jcode::convert(\$ret_hash{'name'}, $charset, "euc");
				&jcode::convert(\$ret_hash{'signature'}, $charset, "euc");
				&jcode::convert(\$ret_hash{'address'}, $charset, "euc");
			}
		}
	}

	return %ret_hash;
}

sub i_get_mime_header{
	my $value;
	my $tmp;
	my $i; # カウンタ変数

	(my($part), my(%tmp_hash)) = @_;

	$i = 1;
	while(1){
		$tmp = "$part$i.MIME";
		$value = &Imap4_lib::ifetch($imap4_tag++, $FORM{'no'}, "BODY[$tmp]", *SERVER);
		if($value){
			if($value =~ /^Content-Type:\s*multipart\//im){
				%tmp_hash = &i_get_mime_header(substr($tmp, 0, -4), %tmp_hash);
			}
			$tmp = substr($tmp, 0, -5);
			$tmp_hash{$tmp} = $value;
		}else{
			last;
		}
		$i++;
	}

	return %tmp_hash;
}

sub p_mime_analysis{
	my $value;
	my $tmp1;
	my $tmp2;
	my $tmp3;
	my $tmp4;
	my $i; # カウンタ変数

	my $header;
	my $mbody = "\r\n";
	my $delimiter;
	my @tmp_array;

	(my($part), my($buf), my(%tmp_hash)) = @_;

	# ヘッダーと本体を分離
	$buf =~ /\r\n\r\n/g;
	$header = $`."\r\n";
	$mbody .= $';

	if($header =~ /^Content-Type:\s*multipart\/[^;]*;[^\:]*boundary=\"([^\"^\r]*)\"/im){
		$delimiter = $1;
		$delimiter_re = $delimiter;
		$delimiter_re =~ s/(\W)/\\$1/g;
		@tmp_array = split(/\r\n\-\-$delimiter_re/, $mbody);

		$i = 1;
		foreach(@tmp_array[1..$#tmp_array-1]){
			$tmp1 = "$part$i.";
			$tmp3 = substr($tmp1, 0, -1);
			$tmp4 = $tmp1."BODY";

			($tmp_hash{$tmp3}, $tmp_hash{$tmp4}) = split(/\r\n\r\n/, $_, 2);
			# 処理の都合上、ヘッダーの最初にはMIMEの境界文字列、最後には改行を付ける
			$tmp_hash{$tmp3} = "$delimiter\r\n".$tmp_hash{$tmp3}."\r\n";
			%tmp_hash = &p_mime_analysis($tmp1, $_, %tmp_hash);

			$i++;
		}
	}

	return %tmp_hash;
}


sub wm_rand_id{
	my $len = $_[0];
	my @ch = ("A".."Z","a".."z","0".."9");
	my $buf = "";
	open(my $r, "<", "/dev/urandom") or die "no urandom";
	binmode $r; read($r, $buf, $len); close $r;
	return join("", map { $ch[ord($_) % 62] } split(//, $buf));
}
1;