#!/bin/sh
#This program is free software; you can redistribute it and/or modify it under
#the terms of the GNU General Public License as published by the Free Software
#Foundation; either version 2 of the License, or (at your option) any later version.
#Author: yoshinobu syouzi <yoshino@rita.karing.jp>

ex_name=clsmtpc
dev_date="April 2014"
version=0.1.5-pre5
clsmtpc_info="License: GPLv2+; Author: yoshinobu syouzi<yoshino@rita.karing.jp>"

time_wait=60
eof_ex=$RANDOM$RANDOM

get_md5sum() {
	chk_sum=`echo -n "$1"|md5sum -`
	echo ${chk_sum%% *}
}

get_e_md5sum() {
	chk_sum=`echo -ne "$1"|md5sum -`
	echo ${chk_sum%% *}
}

low2up() {
    if [ "$1" ];then
	echo -n "$1" |tr [:lower:] [:upper:]
    else
	tr [:lower:] [:upper:]
    fi
}

line_get() {
    echo -n "$1" |head -$2 |tail -1
}

check_NL() {
    max_n=255
    i=0
    r=0
    raw_data=`dd bs=$(($max_n+1)) count=1 2>/dev/null`
    while [ loop ];do
	hex_data=`echo -n "$raw_data" |od -h -A n -j${i} -N1`
	if [ ! "$hex_data" ];then
	    echo 0
	    return 1
	elif [ $hex_data = 000a ];then
	    if [ $r = 1 ];then
		echo rn
	    else
		echo n
	    fi
	    break
	elif [ $hex_data = 000d ];then
	    if [ $r = 0 ];then
		r=1
	    fi
	else
	    if [ $r = 1 ];then
		echo r
		break
	    fi
	fi
	i=$(($i+1))

	if [ $i = $max_n ];then
	    echo u
	    return 2
	fi
    done
    return 0
}

#for base64 encode only
trans_NL() {
    if [ $1 = n ];then
        cat -E | tr '$' '\r'
    elif [ $1 = r ];then
        cat -E | tr '$' '\n'
    elif [ $1 = rn ];then
        cat
    else
        cat -E | tr '$' '\r'
    fi
}

echo_rn2term_NL() {
    if [ $1 = n ];then
	tr -d '\r'
    elif [ $1 = r ];then
	tr -d '\n'
    else
	cat
    fi
}

check_ascii() {
    check_data=`dd bs=1 skip=$1 count=$2 2>/dev/null |od -h -A n`
    s=0
    i=0
    for t in $check_data ;do
	i=$(($i+2))
	h1=${t:0:2};d1=$((0x$h1))
	h2=${t:2:2};d2=$((0x$h2))
	if [ $d1 -lt 32 ]||[ $d1 -gt 126 ];then
	    if [ $d1 = 0 ];then
		continue
	    else
		if [ "$3" = "-a" ]||[ "$3" = "-m" ];then
		    if [ $d1 = 10 ]||[ $d1 = 13 ];then
			s=1
		    else
			s=2
			break
		    fi
		elif [ "$3" = "-m" ];then
			if [ $d1 = 22 ];then
                        #for " escape
			    s=2
			    break
			elif [ $d1 -gt 26 ]&&[ $d1 -lt 30 ];then
                        #for '() escape
			    s=2
			    break
			elif [ $d1 -gt 57 ]&&[ $d1 -lt 65 ];then
                        #for :;<=>?@ escape
			    s=2
			    break
			elif [ $d1 = 95 ];then
                        #for nkf MIME underscore problem
			    s=2
			    break
			else
			    if [ $d1 = 10 ]||[ $d1 = 13 ];then
				s=1
			    else
				s=2
				break
			    fi
			    continue
			fi
		else
		    s=2
		    break
		fi
	    fi
	elif [ $d1 = 32 ];then
	    s=1
	elif [ "$3" = "-m" ];then
	    if [ $d1 = 22 ];then
            #for " escape
		s=2
		break
	    elif [ $d1 -gt 26 ]&&[ $d1 -lt 30 ];then
            #for '() escape
		s=2
		break
	    elif [ $d1 -gt 57 ]&&[ $d1 -lt 65 ];then
            #for :;<=>?@ escape
		s=2
		break
	    elif [ $d1 = 95 ];then
            #for nkf MIME underscore problem
		s=2
		break
	    fi
	fi

	if [ $d2 -lt 32 ]||[ $d2 -gt 126 ];then

	    if [ "$3" = "-a" ]||[ "$3" = "-m" ];then
		if [ $d2 = 10 ]||[ $d2 = 13 ];then
		    s=1
		else
		    s=2
		    break
		fi
	    else
		s=2
		break
	    fi
	elif [ $d2 = 32 ];then
	    s=1
	elif [ "$3" = "-m" ];then
	    if [ $d2 = 22 ];then
            #for " escape
		s=2
		break
	    elif [ $d2 -gt 26 ]&&[ $d2 -lt 30 ];then
            #for '() escape
		s=2
		break
	    elif [ $d2 -gt 57 ]&&[ $d2 -lt 65 ];then
            #for :;<=>?@ escape
		s=2
		break
	    elif [ $d2 = 95 ];then
            #for nkf MIME underscore problem
		s=2
		break
	    fi
	fi
    done

    if [ $i -gt $(($2-$1)) ];then
	s=$(($s+4))
    fi

    echo $s
    return $s
}

clm_cram_md5() {
#RFC 2104 CRAM-MD5
    key="$1"
    challenge=$2
    key_length_hmac=64
    key_org_byte=${#key}

    if [ $key_org_byte -gt $key_length_hmac ];then
	key_str=`get_md5sum $key`
    else
	key_org=`echo -n $key |od -h -A n`

	i=0
	for u in $key_org ;do

	    u1=${u:2:2};u2=${u:0:2}
	    for uu in $u1 $u2 ;do
		if [ $uu = 00 ];then
		    break
		fi
		key_str="${key_str}$uu"
		i=$(($i+1))
	    done
	done
    fi

    i=0
    while [ $i != 64 ];do
	offset=$((2*$i))
	key_part=${key_str:$offset:2}
	dec_v=$((0x${key_part:-00}^0x36))
	xor_h1_str="$xor_h1_str\x`printf '%x' $dec_v`"
	i=$(($i+1))
    done

    xor_h1_md5=`get_e_md5sum "$xor_h1_str$challenge"`

    i=0
    while [ $i != 16 ];do
	offset=$((2*$i))
	hex_org=${xor_h1_md5:$offset:2}
	xor_h1x_md5="$xor_h1x_md5\x$hex_org"
	i=$(($i+1))
    done

    i=0
    while [ $i != 64 ];do
	offset=$((2*$i))
	key_part=${key_str:$offset:2}
	dec_v=$((0x$key_part^0x5c))
	xor_h2_str="$xor_h2_str\x`printf '%x' $dec_v`"
	i=$(($i+1))
    done

    xor_h2_md5=`get_e_md5sum "$xor_h2_str$xor_h1x_md5"`
    echo $xor_h2_md5
    
    return 0
}

clm_digest_md5() {
#RFC 2831 DIGEST-MD5
    key="$1"
    username=$2
    key_pass=$3
    mail_server=$4

    auth_str=`echo -n $key |$base64_command -d`
    auth_str_l=`echo -n $auth_str |tr ',' ' '`
    for t in $auth_str_l ;do
	
	if [ "${t##*=*}" ];then
	    continue
	fi
	l_v=${t%%=*}

	if [ ! "${l_v##*\$*}" ]||[ ! "${l_v##*\(*}" ]||[ ! "${l_v##*\`*}" ];then
	    continue
	fi

	l_v=${t%%=*}

	r_v=${t#*=}
	r_v=${r_v#\"}
	r_v=${r_v%\"}
	eval $l_v='$'r_v
    done

    nc=00000001

    cnonce=`get_md5sum $RANDOM$RANDOM`

    passwd=$key_pass
    uri=smtp/$mail_server

    md5_A1_0=`get_md5sum "$username:$realm:$passwd"`
   
    i=0
    while [ $i != 16 ];do
	offset=$((2*$i))
	hex_str="$hex_str\x${md5_A1_0:$offset:2}"
	i=$(($i+1))
    done

    md5_A1=`get_e_md5sum "$hex_str:$nonce:$cnonce"`
    md5_A2=`get_md5sum "AUTHENTICATE:$uri"`
    dgst_md5_rsp=`get_md5sum "${md5_A1}:${nonce}:$nc:$cnonce:$qop:$md5_A2"`

    org_auth_rtrn="username=\"$username\",realm=\"$realm\",nonce=\"$nonce\",digest-uri=\"${uri}\",qop=$qop,nc=$nc,cnonce=\"$cnonce\",response=${dgst_md5_rsp:-unknown}"

    echo -n $org_auth_rtrn |$base64_command -w 0
    return 0
}

base64_internal() {
    if [ "$1" = '-w' ];then
	header_v=1
	mode_decode=0
    else
	header_v=0
	if [ "$1" = '-d' ];then
	    mode_decode=1
	else
	    mode_decode=0
	fi
    fi

    if [ $mode_decode = 1 ];then 

	base64_enc_str=`cat ;echo -n .`
	base64_enc_str=${base64_enc_str%.}
	while [ $((${#base64_enc_str}%4)) != 0 ];do
	    base64_enc_str=${base64_enc_str}'='
	done

	n=0
	while [ loop ];do
	    decode_str=${base64_enc_str:$n:4}
	    if [ ${#decode_str} = 0 ];then
		break
	    else
		c1=${decode_str:0:1}
		c2=${decode_str:1:1}
		c3=${decode_str:2:1}
		c4=${decode_str:3:1}
  
		i=3
		b_or=0
		for t in $c1 $c2 $c3 $c4 ;do
		    if [ $t = '=' ];then
			break
		    fi

		    hex=`echo -n $t |od -h -A n`
		    dec=$((0x${hex#*00}))
		    if [ $dec = 43 ];then
			b_trans=62
		    elif [ $dec = 47 ];then
			b_trans=63
		    elif [ $dec -lt 58 ];then
			b_trans=$(($dec+4))
		    elif [ $dec -lt 91 ];then
			b_trans=$(($dec-65))
		    elif [ $dec -lt 123 ];then
			b_trans=$(($dec-71))
		    else
			b_trans=61
		    fi

		    b_org=$(($b_trans<<$(($i*6))))
		    b_or=$(($b_or|$b_org))
		    i=$(($i-1))
		done

		p1=$(($b_or>>16))
		if [ $c3 = '=' ];then
		    p2=''
		    p3=''
		else
		    p2=$(($(($b_or&0x00ff00))>>8))
		    if [ $c4 = '=' ];then
			p3=''
		    else
			p3=$(($b_or&0x0000ff))
		    fi
		fi

		for t in $p1 $p2 $p3 ;do
		    bc1=`printf '%x' $t`
		    echo -ne \\x$bc1
		done

		if [ ${#decode_str} -lt 4 ];then
		    break
		fi
	    fi
	    n=$(($n+4))
	done
    else
	byte_str=`od -h -A n`
	hex_str=""
	hex_str_pre=""
	i=0
	for t in $byte_str ;do
	    h1=${t:0:2}
	    h2=${t:2:2}
	    i=$(($i+1))
	    hex_str_pre=$hex_str
	    hex_str=$hex_str$h2$h1
	done

	if [ $h1 = 00 ];then
	    hex_str=$hex_str_pre$h2
	fi

	i=0
	while [ loop ];do

	    hex=${hex_str:$(($i*6)):6}
	    hex_c=${#hex}
	    hex_str_output=""
	    if [ $hex_c = 2 ];then
		hex=${hex}0000
	    elif [ $hex_c = 4 ];then
		hex=${hex}00
	    elif [ $hex_c = 0 ];then
		break
	    fi

	    d1=$((0x$hex>>18))
	    d2=$(($((0x03ffff&0x$hex))>>12))
	    d3=$(($((0x000fff&0x$hex))>>6))
	    d4=$((0x00003f&0x$hex))

	    if [ ${hex_c} = 2 ];then
		d3=256
		d4=256
	    elif [ ${hex_c} = 4 ];then
       		d4=256
	    fi

	    for t in $d1 $d2 $d3 $d4 ;do
		if [ $t = 63 ];then
		    dec_trans=47
		elif [ $t = 62 ];then
		    dec_trans=43
		else
		    if [ $t -lt 26 ];then
			dec_trans=$(($t+65))
		    elif [ $t -lt 52 ];then
			dec_trans=$(($t+71))
		    elif [ $t -lt 62 ];then
			dec_trans=$(($t-4))
		    else
			dec_trans=61
		    fi
		fi

		hex_unit=`printf '%x' $dec_trans`
		if [ ${#hex_unit} = 1 ];then
		    hex_unit=0$hex_unit
		fi
		
		hex_str_output=$hex_str_output'\x'$hex_unit
	    done
	    echo -ne $hex_str_output
	    
	    if [ $hex_c -lt 6 ];then
		break
	    fi
	    i=$(($i+1))
	    if [ $(($i%19)) = 0 ]&&[ $header_v = 0 ];then
		echo -n $'\r'$'\n' 
	    fi
	done
    fi

    return 0
}

qp_enc() {
#RFC2045 6.7
    line_limit=74
    org_LANG=$LANG

    org_str=`cat ;echo -n .`
    org_str=${org_str%.}

    qp_charset=${LANG#*.}
    if [ "$1" = -w ];then
	header_v=1
	qp_charset=$2
	max_c=$(($line_limit-${#qp_charset}-7-1))
    else
	header_v=0
	qp_charset=$1
	max_c=$(($line_limit-1))
    fi

    qp_charset=`low2up $qp_charset`
    if [ ${qp_charset:0:8} = ISO-2022 ];then
	mode_2022=1
	if [ $header_v = 1 ];then
	    org_str=`echo -n "$org_str" |iconv -f $term_charset -t $qp_charset ;echo -n .`
	    org_str=${org_str%.}
	fi
	export LANG=$qp_charset

	qp_hex_str=""
	qp_hex_str_pre=""
	for t in `echo -n "$org_str" |od -h -A n |low2up` ;do
	    h1=${t:0:2}
	    h2=${t:2:2}
	    qp_hex_str_pre=$qp_hex_str
	    qp_hex_str=$qp_hex_str$h2$h1
	done
	
	if [ $h1 = 00 ];then
	    qp_hex_str=$qp_hex_str_pre$h2
	fi
	qp_hex_str_num=${#qp_hex_str}
    
    else
	mode_2022=0
	if [ $header_v = 1 ];then
	    org_str_utf8=`echo -n "$org_str" |iconv -f $term_charset -t utf8 ;echo -n .`
	    org_str_utf8=${org_str_utf8%.}
	    org_str=`echo -n "$org_str" |iconv -f $term_charset -t $qp_charset ;echo -n .`
	    org_str=${org_str%.}
	    export LANG=utf8
	else
	    export LANG=$qp_charset
	fi
    fi

    if [ $mode_2022 = 0 ];then

	vertical_hex_str=""
	i=0
	while [ loop ];do

	    qp_nl_v=0
	    str_end=0
	    if [ $header_v = 0 ];then
		org_a_char=${org_str:$i:1}
		if [ ! "$org_a_char" ];then
		    break
		fi
	    else
		n=1
		while [ loop ];do
		    org_a_char_utf8=${org_str_utf8:$i:$n}
		    if [ ! "$org_a_char_utf8" ];then
			str_end=1
			break
		    fi
		    org_a_char=`echo -n "$org_a_char_utf8" |iconv -f utf-8 -t $qp_charset 2>/dev/null`;iconv_v=$?
		    if [ $iconv_v = 0 ];then
			i=$(($i+$n-1))
			break
		    else
			n=$(($n+1))
		    fi
		done
	    fi

	    if [ $str_end = 1 ];then
		break
	    fi
	    hex_str=`echo -n "$org_a_char" |od -h -A n |low2up`

	    a_char_hex_str=""
	    a_char_hex_str_pre=""
	    for t in $hex_str ;do
		h1=${t:0:2}
		h2=${t:2:2}
		a_char_hex_str_pre=$a_char_hex_str
		a_char_hex_str=$a_char_hex_str$h2$h1
	    done

	    if [ $h1 = 00 ];then
		a_char_hex_str=$a_char_hex_str_pre$h2
	    fi

	    if [ "$vertical_hex_str" ];then
		vertical_hex_str="$vertical_hex_str
$a_char_hex_str"
	    else
		vertical_hex_str=$a_char_hex_str
	    fi

	    i=$(($i+1))
	done

	c=0
	n=0
	qp_nl_v=0
	for t in $vertical_hex_str ;do
	    a_char_pre=$a_char
	    a_char=$t
	    char_len=$((${#a_char}/2))

	    i=0
	    qp_a_char=""
	    c_char=0
	    while [ loop ];do
		a_char_p=${a_char:$(($i*2)):2}
		a_char_d=$((0x$a_char_p))
		if [ $a_char_p = 3D ];then
		    qp_a_char_p='='3D
		    c_p=3
		elif [ $a_char_d -gt 32 ]&&[ $a_char_d -lt 127 ];then
		    if [ $header_v = 1 ];then
			if [ $a_char_p = 5F ];then
                        #for nkf MIME underscore problem
			    qp_a_char_p='=5F'
			    c_p=3
			elif [ $a_char_p = 3A ]||[ $a_char_p = 3B ];then
                        #for colon and semi colon confusion
			    qp_a_char_p='='$a_char_p
			    c_p=3
			elif [ $a_char_p = 40 ];then
                        #for atmark confusion
			    qp_a_char_p='=40'
			    c_p=3
			elif [ $a_char_p = 28 ]||[ $a_char_p = 29 ];then
                        #for brackets confusion
			    qp_a_char_p='='$a_char_p
			    c_p=3
			elif [ $a_char_p = 3C ]||[ $a_char_p = 3E ];then
                        #for angle brackets confusion
			    qp_a_char_p='='$a_char_p
			    c_p=3
			elif [ $a_char_p = 3F ];then
                        #for ? confusion
			    qp_a_char_p='=3F'
			    c_p=3
			elif [ $a_char_p = 22 ]||[ $a_char_p = 27 ];then
                        #for single and double quotation confusion
			    qp_a_char_p='='$a_char_p
			    c_p=3
			else
			    qp_a_char_p='\x'$a_char_p
			    c_p=1
			fi
		    else
			qp_a_char_p='\x'$a_char_p
			c_p=1
		    fi
		elif [ $a_char_p = 20 ]||[ $a_char_p = 09 ];then
		    if [ $header_v = 1 ];then
			qp_a_char_p='='$a_char_p
			c_p=3
		    else
			next_a_char=`line_get "$vertical_hex_str" $(($n+2))`
			if [ $next_a_char = 0A ]||[ $next_a_char = 0D ];then
			    qp_a_char_p='='$a_char_p
			    c_p=3
			else
			    qp_a_char_p='\x'$a_char_p
			    c_p=1
			fi
		    fi
		elif [ $a_char_p = 0A ];then
		    if [ $a_char_pre = 0D ];then
			qp_nl_v=1
			qp_a_char_p='\x0A'
			c_p=1
		    else
			qp_a_char_p='=0A'
			c_p=3
		    fi
		elif [ $a_char_p = 0D ];then
		    next_a_char=`line_get "$vertical_hex_str" $(($n+2))`
		    if [ $next_a_char = 0A ];then
			qp_nl_v=2
			qp_a_char_p='\x0D'
			c_p=1
#			qp_a_char_p='=0D'
#			c_p=3
		    else
			qp_a_char_p='=0D'
			c_p=3
		    fi
		else
		    qp_a_char_p='='$a_char_p
		    c_p=3
		fi
		qp_a_char=$qp_a_char$qp_a_char_p
		c_char=$(($c_char+$c_p))
		i=$(($i+1))
		if [ $i = $char_len ];then
		    break
		fi
	    done

	    if [ $header_v = 1 ];then
		qp_eol=""$'\r'$'\n'
	    else
		qp_eol="="$'\r'$'\n'
	    fi

	    a_char_str_pre=$a_char_str
	    a_char_str=$a_char_str$qp_a_char
	    if [ $qp_nl_v = 0 ];then
		c=$(($c+$c_char))
		if [ $c -gt $max_c ];then
		    a_char_str="${a_char_str_pre}$qp_eol$qp_a_char"
		    c=${#qp_a_char}
		fi

	    elif [ $qp_nl_v = 1 ];then
		c=0
	    elif [ $qp_nl_v = 2 ];then
		c=0
	    fi
	    n=$(($n+1))
	done
	echo -ne "${a_char_str}"
    else
	mode_mbc=0
	mbc=0

	cc=${qp_charset:9:2}
	if [ $cc = KR ]||[ $$cc = CN ];then
	    if [ $mode_2022_cjk = CN ];then	
		mode_2022_cjk=c
		if [ $header_v = 1 ];then
		    esc_2022_qp_shift_m='=1B$=29A=0E'
		else
		    esc_2022_qp_shift_m='=1B$)A=0E'
		fi
	    else
		mode_2022_cjk=k
		if [ $header_v = 1 ];then
		    esc_2022_qp_shift_m='=1B$=29C=0E'
		else
		    esc_2022_qp_shift_m='=1B$)C=0E'
		fi
	    fi
	    mbc_un=2

	    esc_2022_hex_ascii='0F'
	    esc_2022_hex_single='0F'
	    esc_2022_hex_multi='1B2429'

	    esc_2022_qp_ascii='=0F'
	    esc_2022_qp_single='=0F'
	    if [ $header_v = 1 ];then
		esc_2022_qp_multi='=1B$=29'
	    else
		esc_2022_qp_multi='=1B$)'
	    fi
	    esc_2022_qp_shift_a='=0F'
	    esc_2022_qp_shift_s='=0F'
	else
	    mode_2022_cjk=j
	    mbc_un=2

	    esc_2022_hex_ascii='1B28'
	    esc_2022_hex_single='1B28'
	    esc_2022_hex_multi='1B24'

	    esc_2022_qp_multi='=1B$'
	    if [ $header_v = 1 ];then
		esc_2022_qp_ascii='=1B=28'
		esc_2022_qp_single='=1B=28'
		esc_2022_qp_shift_a='=1B=28B'
		esc_2022_qp_shift_s='=1B=28B'
	    else
		esc_2022_qp_ascii='=1B('
		esc_2022_qp_single='=1B('
		esc_2022_qp_shift_a='=1B(B'
		esc_2022_qp_shift_s='=1B(B'
	    fi
	    esc_2022_qp_shift_m='=1B$B'
	fi

	if [ $header_v = 1 ];then
	    esc_2022_qp_eol=$esc_2022_qp_shift_a
	else
	    esc_2022_qp_eol="="
	fi

	i=0;c=0;qp_nl_v=0
	while [ loop ];do
	    qp_str_pre=$qp_str
	    if [ $qp_nl_v = 2 ];then 
		qp_char_enc_pre='\x0D'
	    elif [ $qp_nl_v = 1 ];then
		qp_char_enc_pre='\x0A'
	    else
		qp_char_enc_pre=$qp_char_enc
	    fi
	    c_plus_pre=$c_plus
	    qp_nl_v=0

	    t=${qp_hex_str:$i:2}
	    tn=${qp_hex_str:$(($i+2)):2};tn=${tn:-00}
	    d=$((0x$t))
	    dn=$((0x$tn))

	    if [ $mode_2022_cjk = j ];then
		esc_2022_check_single=${qp_hex_str:$i:4}
		esc_2022_check_multi=$esc_2022_check_single
		if [ $esc_2022_check_multi = '1B2E' ];then
		    esc_2022_hex_multi='1B2E'
		else
		    esc_2022_hex_multi='1B24'
		fi
	    else
		esc_2022_check_single=${qp_hex_str:$i:2}
		esc_2022_check_multi=${qp_hex_str:$i:6}
		if [ $esc_2022_check_multi = '1B242A' ];then
		    esc_2022_hex_multi='1B242A'
		elif [ $esc_2022_check_multi = '1B242B' ];then
		    esc_2022_hex_multi='1B242B'
		else
		    esc_2022_hex_multi='1B2429'
		fi
	    fi

	    if [ "$esc_2022_check_single" = $esc_2022_hex_single ];then

		mode_mbc=0
		mbc=0

		if [ $mode_2022_cjk = j ];then
		    esc_2022_qp_s3=`echo -e '\x'${qp_hex_str:$(($i+4)):2}`
		    esc_2022_qp_shift_s=$esc_2022_qp_single$esc_2022_qp_s3

		    i_inc=6
		else
		    i_inc=2
		fi

		cn=$(($c+${#esc_2022_qp_shift_s}))
		i=$(($i+$i_inc))
		if [ $cn -gt $(($max_c-${#esc_2022_qp_eol})) ];then
		    if [ $i = $qp_hex_str_num ];then
			qp_str="${qp_str}$esc_2022_qp_eol"
		    else
			if [ $mode_2022_cjk = k ]&&[ $header_v = 1 ];then
			    qp_str="${qp_str}$esc_2022_qp_eol"\
$'\r'$'\n'"$esc_2022_qp_shift_m$esc_2022_qp_shift_s"
			    c=$((${#esc_2022_qp_shift_m}+${#esc_2022_qp_shift_s}))
			else
			    qp_str="${qp_str}$esc_2022_qp_eol"\
$'\r'$'\n'"$esc_2022_qp_shift_s"
			    c=${#esc_2022_qp_shift_s}
			fi
		    fi
		else
		    qp_str=${qp_str}$esc_2022_qp_shift_s
		    c=$cn
		fi

		if [ $i = $qp_hex_str_num ];then
		    break
		else
		    continue
		fi

	    elif  [ "$esc_2022_check_multi" = $esc_2022_hex_multi ];then

		mode_mbc=1
		mbc=0

		if [ $mode_2022_cjk = j ];then
		    esc_2022_qp_m3=`echo -e '\x'${qp_hex_str:$(($i+4)):2}`
		    if [ $esc_2022_qp_m3 = '(' ];then
			esc_2022_qp_m4=`echo -e '\x'${qp_hex_str:$(($i+6)):2}`
			if [ $header_v = 1 ];then
			    esc_2022_qp_shift_m='=1B$=28'$esc_2022_qp_m4
			else
			    esc_2022_qp_shift_m='=1B$('$esc_2022_qp_m4
			fi
			i_inc=8
		    elif [ $esc_2022_check_multi = '1B2E' ];then
			esc_2022_qp_m3=`echo -e '\x'${qp_hex_str:$(($i+4)):2}`
			esc_2022_qp_shift_m='=1B.'$esc_2022_qp_m3'=1BN'
			i_inc=10
		    else
			esc_2022_qp_shift_m='=1B$'$esc_2022_qp_m3
			i_inc=6
		    fi
		else
		    if [ $esc_2022_check_multi = '1B242A' ];then
			esc_2022_qp_shift_m='=1B$*H=1BN'
			i_inc=12
		    elif [ $esc_2022_check_multi = '1B242B' ];then
			esc_2022_qp_m4=`echo -e '\x'${qp_hex_str:$(($i+6)):2}`
			esc_2022_qp_shift_m='=1B$+'$esc_2022_qp_m4'=1BO'
			i_inc=12
		    else
			esc_2022_qp_m4=`echo -e '\x'${qp_hex_str:$(($i+6)):2}`
			esc_2022_qp_shift_m=$esc_2022_qp_multi$esc_2022_qp_m4'=0E'
			i_inc=10
		    fi
		fi

		cn=$(($c+${#esc_2022_qp_shift_m}))
		i=$(($i+$i_inc))
		if [ $cn -gt $(($max_c-${#esc_2022_qp_eol})) ];then
		    qp_str="${qp_str}$esc_2022_qp_eol"\
$'\r'$'\n'"$esc_2022_qp_shift_m"
		    c=${#esc_2022_qp_shift_m}
		else
		    qp_str=${qp_str}$esc_2022_qp_shift_m
		    c=$cn
		fi

		if [ $i = $qp_hex_str_num ];then
                #impossible
		    break
		else
		    continue
		fi
	    fi
	    
	    if [ $t = 3D ];then
		qp_char_enc='=3D'
		c_plus=3
	    elif [ $t = 0A ];then
		if [ $header_v = 1 ];then
		    continue
		fi

		if [ $qp_char_enc_pre = '\x0D' ];then
		    qp_nl_v=1
		    qp_char_enc='\x0A'
		    c=0
		    c_plus=0
		else
		    qp_char_enc='=0A'
		    c_plus=3
		fi
	    elif [ $t = 0D ];then
		if [ $header_v = 1 ];then
		    continue
		fi

		if [ $tn = 0A ];then
		    qp_nl_v=2
		    qp_char_enc='\x0D'
		    c=0
		    c_plus=0
		else
		    qp_char_enc='=0D'
		    c_plus=3
		fi
	    elif [ $t = 09 ]||[ $t = 20 ];then
		if [ $tn = 0A ]||[ $tn = 0D ]||[ $header_v = 1 ];then
		    qp_char_enc='='$t
		    c_plus=3
		else
		    qp_char_enc='\x'$t
		    c_plus=1
		fi
	    elif [ $d -gt 32 ]&&[ $d -lt 127 ];then
		if [ $header_v = 1 ];then
		    if [ $t = 5F ];then
                    #for nkf MIME underscore problem
			qp_char_enc='=5F'
			c_plus=3
		    elif [ $t = 3A ]||[ $t = 3B ];then
                    #for colon and semi colon confusion
			qp_char_enc='='$t
			c_plus=3
		    elif [ $t = 40 ];then
                    #for atmark confusion
			qp_char_enc='=40'
			c_plus=3
		    elif [ $t = 28 ]||[ $t = 29 ];then
                    #for brackets confusion
			qp_char_enc='='$t
			c_plus=3
		    elif [ $t = 3C ]||[ $t = 3E ];then
                    #for angle brackets confusion
			qp_char_enc='='$t
			c_plus=3
		    elif [ $t = 3F ];then
                    #for ? confusion
			qp_char_enc='='$t
			c_plus=3
		    elif [ $t = 22 ]||[ $t = 27 ];then
                    #for single and double quotation confusion
			qp_char_enc='='$t
			c_plus=3
		    else
			qp_char_enc='\x'$t
			c_plus=1
		    fi
		else
		    qp_char_enc='\x'$t
		    c_plus=1
		fi
	    else
		qp_char_enc='='$t
		c_plus=3
	    fi

	    cn=$(($c+$c_plus))
	    if [ $cn -gt $(($max_c-${#esc_2022_qp_eol})) ];then
		if [ $mode_mbc = 1 ];then
		    if [ $header_v = 0 ];then
			if [ $(($mbc%$mbc_un)) = 0 ];then
			    qp_str="${qp_str}$esc_2022_qp_eol"\
$'\r'$'\n'"$qp_char_enc"
			    c=$(($c_plus))
			    mbc=1
			else
			    qp_str="${qp_str_pre2}$esc_2022_qp_eol"\
$'\r'$'\n'"$qp_char_enc_pre$qp_char_enc"
			    c=$(($c_plus_pre+$c_plus))
			    mbc=2
			fi
		    else
			if [ $(($mbc%$mbc_un)) = 0 ];then
			    qp_str="${qp_str}$esc_2022_qp_eol"\
$'\r'$'\n'"$esc_2022_qp_shift_m$qp_char_enc"
			    c=$((${#esc_2022_qp_shift_m}+$c_plus_pre))
			    mbc=1
			else
			    qp_str="${qp_str_pre2}$esc_2022_qp_eol"\
$'\r'$'\n'"$esc_2022_qp_shift_m$qp_char_enc_pre$qp_char_enc"
			    c=$((${#esc_2022_qp_shift_m}+$c_plus_pre+$c_plus))
			    mbc=2
			fi
		    fi
		else
		    if [ $header_v = 0 ];then
			qp_str="${qp_str}$esc_2022_qp_eol"\
$'\r'$'\n'"$qp_char_enc"
			c=$c_plus
		    else
			if [ $mode_2022_cjk = k ];then
			    qp_str="${qp_str}$esc_2022_qp_eol"\
$'\r'$'\n'"$esc_2022_qp_shift_m$esc_2022_qp_shift_s$qp_char_enc"
			    c=$((${#esc_2022_qp_shift_m}+${#esc_2022_qp_shift_s}+$c_plus))
			else
			    qp_str="${qp_str}$esc_2022_qp_eol"\
$'\r'$'\n'"$esc_2022_qp_shift_s$qp_char_enc"
			    c=$((${#esc_2022_qp_shift_s}+$c_plus))
			fi
		    fi
		fi
	    else
		if [ $mode_mbc = 1 ];then
		    mbc=$(($mbc+1))
		fi
		qp_str=$qp_str$qp_char_enc
		c=$cn
	    fi

	    qp_str_pre2=$qp_str_pre1
	    qp_str_pre1=$qp_str
	    i=$(($i+2))
	    if [ $i = $qp_hex_str_num ];then
		break
	    fi
	done
	echo -ne "${qp_str}"
    fi

    export LANG=$org_LANG
    return 0
}

base64_header_enc() {
    line_limit=76
    org_LANG=$LANG

    if [ "$2" ];then
	b_LANG=$2
    else
	b_LANG=$LANG
    fi
    b_country=${b_LANG%.*}
    b_charset=${b_LANG#*.}
    b_charset=`low2up $b_charset`
    param=`cat ;echo -n .`
    param=${param%.}

    line_limit_base64=$(($line_limit-${#b_charset}-8))
    max_c=$(($(($line_limit_base64/4))*3))

    if [ ${b_charset:0:8} = ISO-2022 ];then
	mode_2022=1
	if [ ${b_charset:9:2} = CN ];then
	    mode_2022_cjk=c
	elif  [ ${b_charset:9:2} = KR ];then
	    mode_2022_cjk=k
	else
	    mode_2022_cjk=j
	fi

	param=`echo -n "$param" |iconv -f $term_charset -t $b_charset ;echo -n .`
	param=${param%.}
	export LANG=$b_charset
    else
	mode_2022=0
	param=`echo -n "$param" |iconv -f $term_charset -t utf8 ;echo -n .`
	param=${param%.}
	export LANG=utf8
    fi

    vertical_hex_str=""
    param_c=${#param}
    i=0
    while [ loop ];do
	hex_str=""
	hex_str_pre=""

	n=1
	param_end=0
	while [ loop ];do
	    a_char=${param:$i:$n}
	    if [ "$a_char" ];then
		if [ $mode_2022 = 0 ];then

		    a_char_test=`echo -n "$a_char" |iconv -f utf8 -t $b_charset 2>/dev/null`;iconv_v=$?
		    if [ $iconv_v = 0 ];then

			hex_data=`echo -n "$a_char_test" |od -h -A n |low2up`
			i=$(($i+$n-1))
			break
		    else
			n=$(($n+1))
		    fi
		else
		    hex_data=`echo -n "$a_char" |od -h -A n |low2up`
		    break
		fi
	    else
		param_end=1
		break
	    fi
	done

	if [ $param_end = 1 ];then
	    break
	fi

	for t in $hex_data ;do
	    h1=${t:0:2}
	    h2=${t:2:2}
	    hex_str_pre=$hex_str
	    hex_str=$hex_str\\x$h2\\x$h1
	done

	if [ $h1 = 00 ];then
	    hex_str=$hex_str_pre\\x$h2
	fi

	liner_hex_str=$liner_hex_str$hex_str
	if [ "$vertical_hex_str" ];then
	    vertical_hex_str="$vertical_hex_str
$hex_str"
	else
	    vertical_hex_str=$hex_str
	fi

	i=$(($i+1))
	if [ $i = $param_c ];then
	    break
	fi
    done

    if [ $mode_2022 = 0 ];then
	
	line_len=0
	for t in $vertical_hex_str ;do
	    char_hex_str=$t
	    char_len=$((${#char_hex_str}/4))
	    line_len=$(($line_len+$char_len))
	    base64_line_str_pre=$base64_line_str
	    base64_line_str=$base64_line_str$t
	    if [ $line_len -gt $max_c ];then
		base64_line_str="$base64_line_str_pre
$t"
		line_len=$char_len
	    fi
	done
	
	for t in $base64_line_str ;do
	    echo -ne "$t" |$base64_command -w 0
	    echo -n $'\r'$'\n'
	done
    else
	mode_mbc=0
	mbc=0
	if [ $mode_2022_cjk = k ]||[ $mode_2022_cjk = c ];then
	    mbc_un=2

	    esc_2022_shift_a='\x0F'
	    esc_2022_shift_s='\x0F'
	    if [ $mode_2022_cjk = c ];then
		esc_2022_shift_m='\x1B\x24\x29\x41\x0E'
	    else
		esc_2022_shift_m='\x1B\x24\x29\x43\x0E'
	    fi
	else
	    mbc_un=2

	    esc_2022_shift_a='\x1B\x28\x42'
	    esc_2022_shift_s='\x1B\x28\x42'
	    esc_2022_shift_m='\x1B\x24\x42'
	fi

	max_i=$((${#liner_hex_str}/4))

	c=0
	i=0
	while [ loop ];do
	    base64_2022_pre2=$base64_2022_pre1
	    base64_2022_pre1=$base64_2022

	    a_char_pre=$a_char

	    i4=$(($i*4))
	    a_char=${liner_hex_str:$i4:4}

	    if [ $mode_2022_cjk = j ];then
		esc_2022_check_S=${liner_hex_str:$i4:8}
		esc_2022_check_s='\x1B\x28'

		esc_2022_check_M=${liner_hex_str:$i4:8}
		if [ $esc_2022_check_M = '\x1B\x2E' ];then
		    esc_2022_check_m='\x1B\x2E'
		else
		    esc_2022_check_m='\x1B\x24'
		fi
	    else
		esc_2022_check_S=${liner_hex_str:$i4:4}
		esc_2022_check_s='\x0F'

		esc_2022_check_M=${liner_hex_str:$i4:12}
		if [ $esc_2022_check_M = '\x1B\x24\x2A' ];then
		    esc_2022_check_m='\x1B\x24\x2A'
		elif [ $esc_2022_check_M = '\x1B\x24\x2B' ];then
		    esc_2022_check_m='\x1B\x24\x2B'
		else
		    esc_2022_check_m='\x1B\x24\x29'
		fi
	    fi

	    if [ "$esc_2022_check_S" = $esc_2022_check_s ];then
		mode_mbc=0
		mbc=0

		if [ $mode_2022_cjk = j ];then
		    esc_2022_shift_s=${liner_hex_str:$i4:12}
		    esc_c=3
		else
		    esc_2022_shift_s='\x0F'
		    esc_c=1
		fi

		cn=$(($c+$esc_c))
		if [ $cn -gt $max_c ];then
		    if [ $mode_2022_cjk = k ];then
			base64_2022="$base64_2022
\x1B\x24\x29\x43\x0F"
			c=5
		    else
			base64_2022="$base64_2022
$esc_2022_shift_s"
			c=$esc_c
		    fi
		else
		    base64_2022=$base64_2022$esc_2022_shift_s
		    c=$cn
		fi
		i=$(($i+$esc_c))
	    elif [ "$esc_2022_check_M" = $esc_2022_check_m ];then
		mode_mbc=1
		mbc=0
		if [ $mode_2022_cjk = j ];then
		    esc_2022_shift_m=${liner_hex_str:$i4:12}
		    if [ $esc_2022_shift_m = '\x1B\x24\x28' ];then
			esc_2022_shift_m=${liner_hex_str:$i4:16}
			esc_c=4
		    elif [ $esc_2022_check_M = '\x1B\x2E' ];then
			esc_2022_shift_m=${liner_hex_str:$i4:20}
			esc_c=5
		    else
			esc_c=3
		    fi
		else
		    if [ $esc_2022_check_M = '\x1B\x24\x2A' ];then
			esc_2022_shift_m=${liner_hex_str:$i4:24}
			esc_c=6
		    elif [ $esc_2022_check_M = '\x1B\x24\x2B' ];then
			esc_2022_shift_m=${liner_hex_str:$i4:24}
			esc_c=6
		    else
			esc_2022_shift_m=${liner_hex_str:$i4:20}
			esc_c=5
		    fi
		fi

		cn=$(($c+$esc_c))
		if [ $cn -gt $max_c ];then
		    base64_2022="$base64_2022
$esc_2022_shift_m"
		    c=$esc_c
		else
		    base64_2022=$base64_2022$esc_2022_shift_m
		    c=$cn
		fi
		i=$(($i+$esc_c))
	    else
		cn=$(($c+1))
		if [ $cn -gt $max_c ];then
		    if [ $mode_mbc = 0 ];then
			if [ $mode_2022_cjk = k ];then
			    base64_2022="$base64_2022
\x1B\x24\x29\x43\x0F$a_char"
			    c=6
			else 
			    base64_2022="$base64_2022
$esc_2022_shift_s$a_char"
			    c=$(($esc_c+1))
			fi
		    else
			if [ $(($mbc%$mbc_un)) = 0 ];then
			    base64_2022="$base64_2022
$esc_2022_shift_m$a_char"
			    c=$(($esc_c+1))
			    mbc=1
			else
			    base64_2022="$base64_2022_pre2
$esc_2022_shift_m$a_char_pre$a_char"
			    c=$(($esc_c+2))
			    mbc=2
			fi
		    fi
		else
		    if [ $mode_mbc = 1 ];then
			mbc=$(($mbc+1))
		    fi
		    base64_2022=$base64_2022$a_char
		    c=$cn
		fi
		i=$(($i+1))
	    fi

	    if [ $i = $max_i ];then
		break
	    fi
	done
	
	i=0
	for t in $base64_2022 ;do
	    echo -ne "$t" |$base64_command -w 0
	    echo -n $'\r'$'\n'
	done
    fi

    LANG=$org_LANG
    return 0
}

param_enc() {
#RFC2231
    line_limit=74
    org_LANG=$LANG

    uri_LANG=$1
    uri_country=${uri_LANG%.*}
    uri_charset=${uri_LANG#*.}
    uri_charset=`low2up $uri_charset`

    param=`cat ;echo -n .`
    param=${param%.}

    param_name=$2
    #param_name=filename, param_name_num=8
    param_name_num=${#param_name}

    max_c_0=$(($line_limit-$param_name_num-1))
    max_c_1=$(($max_c_0-1))
    max_c_2=$(($max_c_0-2))
    max_c_3=$(($max_c_0-3))
    max_c_4=$(($max_c_0-4))
    max_c_s1=$(($max_c_0-${#uri_charset}-${#uri_country}-3))
    max_c_s2=$(($max_c_s1-2))

    if [ ${uri_charset:0:8} = ISO-2022 ];then
	mode_2022=1
	if [ ${uri_charset:9:2} = CN ];then
	    mode_2022_cjk=c
	elif [  ${uri_charset:9:2} = KR ];then
	    mode_2022_cjk=k
	else
	    mode_2022_cjk=j
	fi

	param_test=$param
	export LANG=$uri_LANG
    else
	mode_2022=0
	param_test=$param
	param=`echo -n "$param" |iconv -f $uri_charset -t utf8 ;echo -n .`
	param=${param%.}
	export LANG=UTF8
    fi

    param_c=`LANG=C;echo -n ${#param_test}.`
    param_c=${param_c%.}
    if [ $param_c -lt $max_c_0 ];then
	pl=0
    else
	pl=1
    fi

    ca_v=`echo -n "$param" |check_ascii 0 $max_c_0`
    if [ $ca_v = 2 ];then
	if [ $pl = 0 ];then
	    max_c=$max_c_s1
	else
	    max_c=$max_c_s2
	fi
	line_check_v=1
    else
	if [ $pl = 0 ];then
	    max_c=$max_c_2
	else
	    max_c=$max_c_4
	fi
	line_check_v=0
    fi

    c=0
    i=0
    n=1
    while [ loop ];do
	esc_str=""
	esc_str_pre=""
	a_char=${param:$i:$n}
	if [ "$a_char" ];then
	    if [ $mode_2022 = 0 ];then
		a_char_test=`echo -n "$a_char" |iconv -f utf8 -t $uri_charset 2>/dev/null`;iconv_v=$?
		if [ $iconv_v = 0 ];then
		    hex_data=`echo -n "$a_char_test" |od -h -A n |low2up`
		    i=$(($i+$n-1))
		    n=1
		else
		    n=$(($n+1))
		    continue
		fi
	    else
		hex_data=`echo -n "$a_char" |od -h -A n |low2up`
	    fi
	else
	    break
	fi

	for t in $hex_data ;do
	    h1=${t:0:2};d1=$((0x$h1))
	    h2=${t:2:2};d2=$((0x$h2))
	    if [ $h1 = 25 ];then
            #%escape
		p1='%25'
	    elif [ $h1 = 22 ]||[ $h1 = 27 ]||[ $h1 = 60 ];then
            #"'` escape
		p1='%'$h1
	    elif [ $h1 = 28 ]||[ $h1 = 29 ];then
            #() escape
		p1='%'$h1
  	    elif [ $d1 -gt 57 ]&&[ $d1 -lt 65 ];then
            #:;<=>?@ escape
		p1='%'$h1
	    elif [ $h1 = 7E ];then
            #~ escape
		p1='%'$h1
	    elif [ $d1 -gt 32 ]&&[ $d1 -lt 127 ];then
		p1=$a_char
	    else
		p1=%$h1
	    fi

	    if [ $h2 = 25 ];then
            #%escape
		p2='%25'
	    elif [ $h2 = 22 ]||[ $h2 = 27 ]||[ $h2 = 60 ];then
            #"'` escape
		p2='%'$h2
	    elif [ $h2 = 28 ]||[ $h2 = 29 ];then
            #() escape
		p2='%'$h2
  	    elif [ $d2 -gt 57 ]&&[ $d2 -lt 65 ];then
            #:;<=>?@ escape
		p2='%'$h2
	    elif [ $h2 = 7E ];then
            #~ escape
		p2='%'$h2
	    elif [ $d2 -gt 32 ]&&[ $d2 -lt 127 ];then
		p2=$a_char
	    else
		p2=%$h2
	    fi

	    esc_str_pre=$esc_str
	    esc_str=$esc_str$p2$p1
	done

	if [ $p1 = '%00' ];then
	    esc_str=$esc_str_pre$p2
	fi
	liner_str=$liner_str$esc_str

	if [ "$vertical_esc_str" ];then
	    vertical_esc_str="$vertical_esc_str
$esc_str"
	else
	    vertical_esc_str=$esc_str
	fi

	i=$(($i+1))
    done

    if [ $mode_2022 = 1 ];then
	mode_mbc=0
	mbc=0
	if [ $mode_2022_cjk = c ];then
	    mbc_un=2
	    esc_2022_uri_shift_s='%0F'
	    esc_2022_uri_shift_m='%1B$%29A%0E'
	elif [ $mode_2022_cjk = k ];then
	    mbc_un=2
	    esc_2022_uri_shift_s='%0F'
	    esc_2022_uri_shift_m='%1B$%29C%0E'
	else
	    mbc_un=2
	    esc_2022_uri_shift_s='%1B%28B'
	    esc_2022_uri_shift_m='%1B$B'
	fi

	liner_str_num=${#liner_str}
	i=0
	while [ loop ];do
	    uri_esc_pre2=$uri_esc_pre1
	    uri_esc_pre1=$uri_esc

	    a_char_pre=$a_char
	    a_char_pre_c=${#next_char}
	    a_char=${liner_str:$i:1}

	    if [ $mode_2022_cjk = j ];then
		esc_2022_uri_check_s=${liner_str:$i:6}
		esc_2022_uri_s='%1B%28'

		esc_2022_uri_check_m=${liner_str:$i:4}
		if [ $esc_2022_uri_check_m = '%1B.' ];then
		    esc_2022_uri_m='%1B.'
		else
		    esc_2022_uri_m='%1B$'
		fi
	    else
		esc_2022_uri_check_s=${liner_str:$i:3}
		esc_2022_uri_s='%1F'

		esc_2022_uri_check_m=${liner_str:$i:5}
		if [ $esc_2022_uri_check_m = '%1B$*' ];then
		    esc_2022_uri_m='%1B$*'
		elif [ $esc_2022_uri_check_m = '%1B$+' ];then
		    esc_2022_uri_m='%1B$+'
		else
		    esc_2022_uri_check_m=${liner_str:$i:7}
		    esc_2022_uri_m='%1B$%29'
		fi
	    fi

	    if [ "$esc_2022_uri_check_s" = $esc_2022_uri_s ];then
		mode_mbc=0
		mbc=0
		if [ $mode_2022_cjk = j ];then
		    esc_2022_uri_shift_s=${liner_str:$i:7}
		    esc_uri_c=7
		else
		    esc_2022_uri_shift_s='%0F'
		    esc_uri_c=3
		fi

		cn=$(($c+$esc_uri_c))
		if [ $cn -gt $max_c ];then
		    if [ $mode_2022_cjk = k ];then
			uri_esc="$uri_esc
%1B$%29C%0F"
			c=11
		    else
			uri_esc="$uri_esc
$esc_2022_uri_shift_s"
			c=$esc_uri_c
		    fi

		    ca_v=`echo -n "$param" |check_ascii $c $max_c_4`
		    if [ $ca_v = 2 ];then
			max_c=$max_c_3
			line_check_v=${line_check_v}1
		    else
			max_c=$max_c_4
			line_check_v=${line_check_v}0
		    fi
		else
		    uri_esc=$uri_esc$esc_2022_uri_shift_s
		    c=$cn
		fi
		i=$(($i+$esc_uri_c))
	    elif [ "$esc_2022_uri_check_m" = $esc_2022_uri_m ];then
		mode_mbc=1
		mbc=0

		if [ $mode_2022_cjk = j ];then
		    esc_2022_uri_shift_m=${liner_str:$i:7}
		    if [ $esc_2022_uri_shift_m = '%1B$%28' ];then
			esc_2022_uri_shift_m=${liner_str:$i:8}
			esc_uri_c=8
		    elif [ $esc_2022_uri_check_m = '%1B.' ];then
			esc_2022_uri_shift_m=${liner_str:$i:9}
			esc_uri_c=9
		    else
			esc_uri_c=7
		    fi
		else
		    if [ $esc_2022_uri_check_m = '%1B$*' ];then
			esc_2022_uri_shift_m=${liner_str:$i:10}
			esc_uri_c=10
		    elif [ $esc_2022_uri_check_m = '%1B$+' ];then
			esc_2022_uri_shift_m=${liner_str:$i:10}
			esc_uri_c=10
		    else
			esc_2022_uri_shift_m=${liner_str:$i:9}
			esc_uri_c=9
		    fi
		fi

		cn=$(($c+$esc_uri_c))
		if [ $cn -gt $max_c ];then
		    uri_esc="$uri_esc
$esc_2022_uri_shift_m"
		    c=$esc_uri_c
		    l=$(($l+1))
		    line_check_v=${line_check_v}1
		    max_c=$max_c_3
		else
		    uri_esc=$uri_esc$esc_2022_uri_shift_m
		    c=$cn
		fi
		i=$(($i+$esc_uri_c))
	    else
		if [ $a_char = '%' ];then
		    c_inc=3
		    next_char=${liner_str:$i:3}
		else
		    c_inc=1
		    next_char=$a_char
		fi

		cn=$(($c+$c_inc))
		if [ $cn -gt $max_c ];then
		    if [ $mode_mbc = 0 ];then
			uri_esc="$uri_esc
$next_char"
			c=$c_inc
			ca_v=`echo -n "$param" |check_ascii $c $max_c_4 `
			if [ $ca_v = 2 ];then
			    max_c=$max_c_3
			    line_check_v=${line_check_v}1
			else
			    max_c=$max_c_4
			    line_check_v=${line_check_v}0
			fi
		    else
			if [ $(($mbc%$mbc_un)) = 0 ];then
			    uri_esc="$uri_esc
$esc_2022_uri_shift_m$next_char"
			    c=$(($esc_uri_c+$c_inc))
			    mbc=1
			else
			    uri_esc="$uri_esc_pre2
$esc_2022_uri_shift_m$a_char_pre$next_char"
			    c=$(($esc_uri_c+$a_char_pre_c+$c_inc))
			    mbc=2
			fi
			max_c=$max_c_3
			line_check_v=${line_check_v}1
		    fi
		else
		    if [ $mode_mbc = 1 ];then
			mbc=$(($mbc+1))
		    fi
		    uri_esc=$uri_esc$next_char
		    c=$cn
		fi
		i=$(($i+$c_inc))
	    fi

	    if [ $i = $liner_str_num ];then
		break
	    fi
	done
    else
	byte_str_l=0
	for t in $vertical_esc_str ;do
	    esc_str=$t
	    esc_str_l=$((${#esc_str}))
	    byte_l=$((${esc_str_l}/3))
	    if [ $byte_l = 0 ];then
		byte_l=1
	    fi

	    esc_str_c=$(($esc_str_c+$esc_str_l))
	    byte_str_l_pre=$byte_str_l
	    byte_str_l=$(($byte_str_l+$byte_l))
	    cn=$(($c+$esc_str_l))
	    if [ $cn -gt $max_c ];then
		uri_esc="$uri_esc
$esc_str"
		c=0
		ca_v=`echo -n "$param" |check_ascii $byte_str_l_pre $max_c_4`
		if [ $ca_v = 2 ];then
		    max_c=$max_c_s1
		    line_check_v=${line_check_v}1
		else
		    max_c=$max_c_4
		    line_check_v=${line_check_v}0
		fi
	    else
		uri_esc=$uri_esc$esc_str
		c=$cn
	    fi
	done
    fi

    max_l=${#line_check_v}
    l=0
    for t in $uri_esc ;do

	if [ $max_l = 1 ];then
	    if [ $line_check_v = 0 ]&&[ ${t%\%*} = $t ];then
		echo $param_name=\"$t\"
	    else
		echo $param_name'*'=$uri_charset\'$uri_country\'$t
	    fi
	else
	    if [ ${line_check_v:$l:1} = 0 ]&&[ ${t%\%*} = $t ];then
		if [ $(($l+1)) = $max_l ];then
		    echo $param_name'*'$l=\"$t\"
		else
		    echo $param_name'*'$l=\"$t\"";"
		fi
	    else
		if [ $l = 0 ];then
		    echo $param_name'*0*'=$uri_charset\'$uri_country\'$t";"
		elif [ $(($l+1)) = $max_l ];then
		    echo $param_name'*'$l'*'=$t
		else
		    echo $param_name'*'$l'*'=$t";"
		fi
	    fi
	fi
	l=$(($l+1))
    done

    export LANG=$org_LANG
    return 0
}

help_info() {
    echo "$ex_name: $version
usage:
$ex_name [options] \"mail text\"
$ex_name [options] -m <file>
echo \"mail text\" | $ex_name [options]

ex. $ex_name -M mail.example.com -F my@mail.address -T recipient@mail.address -s \"Re: test mail\" \"this is test mail\"

options;
-H/--from_host <hostname>
    Specify a hostname to access SMTP server. It is used at \"EHLO\" command.
    By default, it is \$HOSTNAME or localhost.

-F/--mail_from <mail address>
    Set a email address. It is used at \"MAIL FROM\" command. Generally it is
    an email address of sender.

-T/--rcpt_to <mail recipient address>
    Set a recipient address. It is used at \"RCPT TO\" command.

-M/--mail_server <SMTP server>
    Specify a SMTP server. By default, it is localhost.

-P/--mail_port <port number>
    Change default mail port(smtp 25). Generally this option is used to use
    submission port(submission 587).

-A/--auth_type <type of SMTP AUTH>
    Select a type of SMTP AUTH. Available: none, plain, login, cram-md5,
    digest-md5, auto. By default, it is none.

-Z/--encrypt_mode <type of encryption>
    Select an encryption type of SMTP. Available: none, smtps, tls, tls1. If
    smtps is selected, port is changed to 465 by default. If STARTTLS is not
    impelemented, tls aborts the session, and tls1 proceeds without TLS.

-U/--smtp_user <smtp user>
    Set a username on the SMTP server for SMTP AUTH. By default, it is the
    username of sender's email address.

-r/--raw_passwd <password>
    Set a password for SMTP AUTH. *** CAUTION! *** The password set by this
    option can be shown by ps command, and overrides password file. This
    option is bad security. You should use password file only.

-p/--passwd_file <password file>
    Specify a password file. By default, it is /etc/$ex_name/clmpasswd. If a
    raw password is set, this option is ignored.
    Password file format;
    username1:smtp_server1:password1
    username2:smtp_server2:password2
    username3:smtp_server3:password3
    ...

-N/--from_mail_name <sender name>
    Set a sender name. It is used in mail header.

-n/--mail_to_name <recipient name>
    Set a recipient name. It is used in mail header.

-s/--subject <subject>
    Set a subject.

-m/--mail_data <file>
    Specify a filename of mail text.

-i/--input_mail_body <mail body>
    Specify a mail body definitely. Ordinarily, this option is needless.

-t/--content_type <type>
    Specify a Content-Type of mail body. By default, it is text/plain.

-e/--encode_type <encode type>
    Select an encode type of mail body. Available: 7BIT, 8BIT, BASE64,
    QUOTED-PRINTABLE(QP). By default, US-ASCII and ISO-2022-* is 7BIT,
    ISO-8859-* is QUOTED-PRINTABLE, others are BASE64.

-C/--mbody_charset <charset>
    Specify a charset of mail body. Available charset depends on iconv. By
    default, it is UTF-8.

-c/--input_charset <charset>
    Specify a charset of input text. Available charset depends on iconv. By 
    default, it is the charset of the terminal.

-L/--new_line <newline_char>
    Specify a newline character of the mail body. Available: n=\\n, rn=\\r\\n,
    r=\\r. By default, it is autodetected from the mail body.

-f/--conf_file <file>
    Add a configuration file. By default, it is /etc/$ex_name/$ex_name.conf
    and \$HOME/.$ex_name/$ex_name.conf.
    Configuration file format;
    The variable name is a word removed \"--\" from the long option. To enable
    a option which dosen't need value is to set 1, and to disable is to set 0.
    ex.
    mail_server=mail.example.com
    silence=1

-a/--attached_files <file_path>
    Attache files to a  mail. Attached files can be separated by ','.
    Content-type and charset can be specified by \":\" separator,
    filepath:content-type:charset. \":\" must be zero or two in one file value.
    If the value of Content-type or charset is unknown, discribe only \":\"
    separator. Content-type and charset are not specified, they are autodetected
    by file command.
    ex.
    /tmp/test.txt:text/html:iso-8859,/home/alice/exmple.pdf

-W/--time_wait <second>
    Set wait time(second). By default, it is 60s. If it is set to 0, $ex_name
    waits infinitely.

-x/--filename_env <env_LANG>
    Specify the environment of filename attached to the mail. By default, it is
    the terminal LANG environment variable.

-R/--raw_nl
    Disable auto complementation of newlines. Without this option, in sending
    a mail, \\n and \\r are changed to \\r\\n.

-d/--no_header
    Stop to create mail header. If you want original mail header, add the mail
    header to mail data yourself before the mail body.
 
-z/--ok_nodata
    Permit zero length mail text. If mail text is zero length, by default,
    standard input is opened.

-y/--sup_op_attached_files
    Suppress outputs of attached files to the terminal. 

-v/--verify_server
    Verify the SMTP server and exit.

-S/--silence
    Silent mode.

-O/--use_helo
    Use \"HELO\" command instead of \"EHLO\".

-V/--version
    Display version information and exit.

-h/--help
    Display this help and exit.

-D/--debug
    Display debug information and exit.
"
}

# send_mail 6bit error code.
# lower 3bit error type.
# PHASE error=1, PHASE NO RETURN=0, QUIT O.K.=0, QUIT error=2, QUIT NO RETURN=4
# upper 3bits SMTP PHASE.
# authentication=32, MAIL FROM=24, RCPT TO=16, DATA=8, QUIT=0, EHLO=48, 8BIT=40.
# option error code
# invalid value=1, file not found=3, not implemented=5, others=7
# external error
# external error=64, iconv error=0, Ctrl+C=1, SMTPs/TLS=2
iconv_err_v=64

output_wait() {
    if [ $encrypt_mode = none ];then
	return 0
    fi

    wait_num=$((4*$time_wait))
    timeout=0
    ii=0
    while [ ! -s $1 ];do
	sleep 0.25s
	ii=$(($ii+1))
	if [ $ii = $wait_num ];then
	    timeout=1
	    break
	fi
    done

    return $timeout
}

read_smtp_return() {
    if [ $encrypt_mode = none ];then

	if [ $time_wait = 0 ];then
	    read_opt=""
	else
	    read_opt="-t $time_wait"
	fi

	read $read_opt smtp_return <&3 ;read_v=$?
	if [ $read_v != 0 ];then
	    if [ $read_v = 142 ];then
		rsr_err=1
	    else
		if [ ! "$smtp_return" ];then
		    rsr_err=2
		else
		    rsr_err=3
		fi
	    fi
	else
	    rsr_err=0
	fi
    else
	output_wait $output ;ow_v=$?
	if [ $ow_v = 0 ];then
	    while read smtp_return ;do
		break
	    done <<${eof_ex}_EOF
`cat $output`
${eof_ex}_EOF
	    echo -n >$output
	    if [ "$smtp_return" ];then
		rsr_err=0
	    else
		rsr_err=3
	    fi
	else
	    rsr_err=1
	fi
    fi

    if [ $rsr_err = 0 ];then
	smtp_return=${smtp_return%$'\n'}
	smtp_return=${smtp_return%$'\r'}
    elif [ $rsr_err = 1 ];then
	smtp_return="********* TIMEOUT OCCURED **********"
    elif [ $rsr_err = 2 ];then
	smtp_return="********* NO RETURN **********"
    else
	smtp_return="********* UNKNOWN ERROR OCCURED **********"
    fi

    if [ "$1" != "-a" ];then
	if [ $rsr_err = 0 ];then
	    echo $clsmtpc_version\<: "$smtp_return"
	else
	    echo $clsmtpc_version-: "$smtp_return"
	fi
    fi
    return $rsr_err
}

quit_error() {
    err_status=$1
    smtp_phase_err=$2
    if [ $err_status != 0 ];then

	write_smtp_command -n "QUIT";wsc_v=$?
	if [ $wsc_v = 0 ];then
	    read_smtp_return;rsr_v=$?
	else
	    rsr_v=1
	fi

	if [ $rsr_v = 0 ];then
	    if [ ${smtp_return:0:1} = 2 ];then
            # QUIT success
		return_v=$smtp_phase_err
	    else
            # QUIT error
		return_v=$(($smtp_phase_err+2))
	    fi
	else
	    if [ $wsc_v != 0 ];then
            # write error
		echo $clsmtpc_version-: "********* SESSION ABORTED **********"
	    fi
            # QUIT no return
	    return_v=$(($smtp_phase_err+6))
	fi
	return 1
    else
	return_v=0
	return 0
    fi
}

write_smtp_command() {
    write_mesg=$2
    if [ $encrypt_mode = none ];then
	echo "$1" "$2"$'\r'$'\n' >&3
    else
	ps -p $bg_pid >/dev/null;pv=$?
	if [ $pv = 0 ];then
	    echo "$1" "$2"$'\r'$'\n' >$input 
	else
	    echo $clsmtpc_version\>: "$write_mesg ;WRITE FAILED"
	    return 1
	fi
    fi

    if [ "$3" = "-d" ];then
	echo "$write_mesg" |echo_rn2term_NL $term_NL
    else
	echo $clsmtpc_version\>: "$write_mesg" |echo_rn2term_NL $term_NL
    fi
    return 0
}

send_mail() {
    clsmtpc_version="$ex_name-$version"
    clm_header="$1"
    clm_mbody="$2"
    if [ "$p_mbody_final" ];then
	p_clm_mbody=$p_mbody_final
    else
	p_clm_mbody=$clm_mbody
    fi

    if [ $auth_type != none ];then
	if [ "$raw_passwd" ];then
	    smtp_auth_passwd=$raw_passwd
	else
	    if [ -f $clmpasswd ];then

		smtp_auth_passwd=`clm_getpass $smtp_user $mail_server $clmpasswd`;pv=$?
		if [ $pv = 0 ];then
		    smtp_auth_passwd=${smtp_auth_passwd#*:}
		else
		    return 1
		fi
	    else
		echo $ex_name:$FUNCNAME: $clmpasswd is not found. >&2
		return 1
	    fi
	fi
    fi

    if [ $encrypt_mode = smtps ]||[ $encrypt_mode = TLS ];then

	if [ "$encrypt_mode" = smtps ];then
	    openssl_options="s_client -quiet -connect $mail_server:$mail_port"
	else
	    openssl_options="s_client -quiet -connect $mail_server:$mail_port -starttls smtp"
	fi
	clsmtpc_version="$clsmtpc_version $encrypt_mode"
	pid=$$

	i=0
	while [ loop ];do
	    pid_p=$(($pid+$i))
	    input=/tmp/smtp.$pid_p.input
	    output=/tmp/smtp.$pid_p.output
	    stderr=/tmp/smtp.$pid_p.stderr
	    if [ ! -e $output ]&&[ ! -e $input ]&&[ ! -e $stderr ];then
		break
	    fi
	    i=$(($i+1))
	done
	
	mkfifo -m 600 $input
	echo -n >$output
	echo -n >$stderr
	chmod 600 $output $stderr

	openssl $openssl_options <$input >$output 2>$stderr &
	bg_pid=$!
	trap "kill -9 $bg_pid ;rm $input $output $stderr ;exit 67" SIGINT
	
	if [ $encrypt_mode = smtps ];then
	    echo -n "" >$input
	    sleep 0.2s
	fi
    else
	trap "exit 65" SIGINT
	exec 3<>/dev/tcp/${mail_server}/${mail_port}
    fi

############ start smtp session ############
    clm_s_date=`LANG=C date '+%Y-%m-%d %a %H:%M'`
    ok_8bit=0

    echo
    echo "$clsmtpc_version-: ************ $clm_s_date ************"
 
############ open smtp session ############
    if [ $encrypt_mode = TLS ];then
	echo $clsmtpc_version-: "STARTTLS"
    else
	read_smtp_return -a ;rsr_v=$?
	if [ $rsr_v = 0 ];then
	    echo $clsmtpc_version\<: "$smtp_return"
	    if [ ${smtp_return:0:1} = 2 ];then
		return_v=0
	    else
		quit_error 1 57
		return $return_v
	    fi
	else
	    echo $clsmtpc_version\<: "SMTP SESSION CAN'T START"
	    return 56
	fi
    fi
############### EHLO command ################
    if [ $use_helo = 1 ];then
	write_smtp_command -n "HELO $from_host"
	helo_command=HELO
    else 
	write_smtp_command -n "EHLO $from_host"
	helo_command=EHLO
    fi

    if [ $encrypt_mode = none ];then
	ehlo_return=""
	if [ $time_wait = 0 ];then
	    read_opt=""
	else
	    read_opt="-t $time_wait"
	fi

	while read $read_opt smtp_return ;do
	    smtp_return=${smtp_return%$'\n'}
	    smtp_return=${smtp_return%$'\r'}
	    if [ "$ehlo_return" ];then
		ehlo_return="$ehlo_return
$smtp_return"
	    else
		ehlo_return=$smtp_return
	    fi

	    if [ "${smtp_return:3:1}" = " " ];then
		break
	    fi
	done <&3
    else
	output_wait $output;ow_v=$?
        ehlo_return=`cat $output`
	echo -n >$output
    fi

    if [ ! "$ehlo_return" ];then
	quit_error 1 48
	return $return_v
    fi

    helo_err=0
    tls_ok=0
    while read smtp_return ;do
	smtp_return=${smtp_return%$'\n'}
	smtp_return=${smtp_return%$'\r'}
	echo $clsmtpc_version\<: $smtp_return

	if [ ${smtp_return:0:3} != 250 ];then
	    helo_err=1
	fi

	if [ "${smtp_return:4}" = 8BITMIME ];then
	    ok_8bit=1
	fi

	if [ "${smtp_return:4}" = STARTTLS ];then
	   tls_ok=1
	fi

	if [ "${smtp_return:4:4}" = AUTH ]&&[ $auth_type = auto ];then
	    auth_type_auto=none
	    wp=0
	    for t in ${smtp_return:8} ;do
		if [ $t = DIGEST-MD5 ];then
		    auth_type_auto_can=digest_md5
		    w=5
		elif [ $t = CRAM-MD5 ];then
		    auth_type_auto_can=cram_md5
		    w=4
		elif [ $t = PLAIN ];then
		    auth_type_auto_can=plain
		    w=3
		elif [ $t = LOGIN ];then
		    auth_type_auto_can=login
		    w=2
		else
		    auth_type_auto_can=none
		    w=0
		fi
		
		if [ $w -gt $wp ];then
		    auth_type_auto=$auth_type_auto_can
		    wp=$w
		fi
	    done
	    auth_type=$auth_type_auto
	fi
	smtp_return_pre=$smtp_return
    done <<${eof_ex}_EOF
$ehlo_return
${eof_ex}_EOF

    if [ "${smtp_return_pre:3:1}" != ' ' ]||[ $helo_err = 1 ];then
	quit_error 1 49
	return $return_v
    fi

    if [ $verify_server = 1 ];then
	quit_error 1 0
	return $return_v
    fi
############## STARTTLS ###############
    if [ ${tls_mode:-0} -gt 0 ];then
	if [ $tls_ok = 1 ];then
	    openssl_options="s_client -quiet -connect $mail_server:$mail_port -starttls smtp"
	    clsmtpc_version="$clsmtpc_version $encrypt_str"
	    pid=$$

	    i=0
	    while [ loop ];do
		pid_p=$(($pid+$i))
		input=/tmp/smtp.$pid_p.input
		output=/tmp/smtp.$pid_p.output
		stderr=/tmp/smtp.$pid_p.stderr
		if [ ! -e $output ]&&[ ! -e $input ]&&[ ! -e $stderr ];then
		    break
		fi
		i=$(($i+1))
	    done
	
	    mkfifo -m 600 $input
	    echo -n >$output
	    echo -n >$stderr
	    chmod 600 $output $stderr

	    openssl $openssl_options <$input >$output 2>$stderr &
	    bg_pid=$!
	    trap "kill -9 $bg_pid ;rm $input $output $stderr ;exit 67" SIGINT
	    echo $clsmtpc_version\>: "STARTTLS"
	    encrypt_mode=tls
	else
	    if [ $tls_mode = 1 ];then
		next_str=aborts
	    else
		next_str=proceeds
	    fi

	    echo $clsmtpc_version-: "STARTTLS not implemented, $next_str"
	    if [ $tls_mode = 1 ];then
		quit_error 1 5
		return 5
	    fi
	fi
    fi

############## SMTP AUTH ##############
    if [ $auth_type = pop_b_smtp ];then
	pop3_read_write() {
	    echo -n "$1"$'\r'$'\n' >&4
	    echo $clsmtpc_version\>: "$1"
	    read pop3_return <&4
	    pop3_return=${pop3_return%$'\n'}
	    pop3_return=${pop3_return%$'\r'}
	    if [ "$2" != "-a" ];then
		echo $clsmtpc_version\<: "$pop3_return"
	    fi
	}
	pop3_user=${pop3_user:-$smtp_user}
	pop3_server=${pop3_server:-$mail_server}
	pop3_passwd=${pop3_passwd:-$smtp_auth_passwd}

	echo $clsmtpc_version-: "POP BEFORE SMTP STARTS"
	exec 4<>/dev/tcp/$pop3_server/110
	read pop3_return <&4
	pop3_return=${pop3_return%$'\n'}
	pop3_return=${pop3_return%$'\r'}
	echo $clsmtpc_version\<: "$pop3_return"

	pop3_read_write "user $pop3_user"
	pop3_read_write "pass $pop3_passwd"
	if [ "${pop3_return:0:3}" = "+OK" ];then
	    echo -n "retr 1"$'\r'$'\n' >&4
	    echo $clsmtpc_version\>: "retr 1"
	    i=0
	    while read pop3_return ;do
		pop3_return=${pop3_return%$'\n'}
		pop3_return=${pop3_return%$'\r'}
		if [ $i = 0 ];then
		    echo $clsmtpc_version\<: "$pop3_return"
		else
		    if [ "$pop3_return" = "." ];then
			echo $clsmtpc_version\<: "a mail, $i lines retrieved."
			break
		    else
			echo "$pop3_return" >/dev/null
		    fi
		fi
		i=$(($i+1))
	    done <&4
	    smtp_auth_return="235 $pop3_return"
	else
	    smtp_auth_return="535 $pop3_return"
	fi

	pop3_read_write "quit" -a
	if [ "$pop3_return" ];then
	    echo $clsmtpc_version\<: "$pop3_return"
	    if [ ${smtp_auth_return:0:3} = 235 ];then
		echo $clsmtpc_version-: "POP BEFORE SMTP O.K."
	    else
		echo $clsmtpc_version-: "POP BEFORE SMTP FAILED"
	    fi
	else
	    if [ ${smtp_auth_return:0:3} = 235 ];then
		echo $clsmtpc_version-: "POP BEFORE SMTP O.K., quit NO RETURN"
	    else
		echo $clsmtpc_version-: "POP BEFORE SMTP FAILED, quit NO RETURN"
	    fi
	fi

    elif [ $auth_type = plain ];then

	mm_passwd=`echo -ne "$smtp_user\0$smtp_user\0$smtp_auth_passwd" |$base64_command -w 0`
	smtp_plain_passwd="AUTH PLAIN $mm_passwd"
	write_smtp_command -n "$smtp_plain_passwd"
	read_smtp_return
	smtp_auth_return=$smtp_return

    elif [ $auth_type = login ];then
	write_smtp_command -n "AUTH LOGIN"
	read_smtp_return

	base64_smtp_user=`echo -n $smtp_user |$base64_command -w 0`
	write_smtp_command -n "$base64_smtp_user"
	read_smtp_return

	base64_login_passwd=`echo -n $smtp_auth_passwd |$base64_command -w 0`
	write_smtp_command -n "$base64_login_passwd"
	read_smtp_return
	smtp_auth_return=$smtp_return    	

    elif [ $auth_type = cram_md5 ];then
	write_smtp_command -n "AUTH CRAM-MD5"
	read_smtp_return
	cram_md5_return=$smtp_return

	if [ "${cram_md5_return:0:4}" = "334 " ];then
	    challenge=`echo -n ${cram_md5_return:4} |$base64_command -d`
	    hex_hmac=`clm_cram_md5 $smtp_auth_passwd $challenge`
	    mm_hmac=`echo -n "$smtp_user $hex_hmac"|$base64_command`

	    write_smtp_command -n "$mm_hmac"
	    read_smtp_return
	fi
	smtp_auth_return=$smtp_return 
    elif [ $auth_type = digest_md5 ];then
	write_smtp_command -n "AUTH DIGEST-MD5"
	read_smtp_return
	digest_md5_return=$smtp_return
		
	if [ "${digest_md5_return:0:4}" = "334 " ];then
		    
	    challenge="${digest_md5_return:4}"
	    mm_digest=`clm_digest_md5 "$challenge" $smtp_user $smtp_auth_passwd $mail_server`
		
	    write_smtp_command -n "$mm_digest"
	    read_smtp_return
	    digest_md5_return=$smtp_return
	    if [ "${digest_md5_return:0:4}" = "334 " ];then
		write_smtp_command -n
		read_smtp_return
	    fi
	    smtp_auth_return=$smtp_return
	fi
    fi

    if [ $auth_type != none ];then

	if [ "${smtp_auth_return:0:4}" != "235 " ];then
	    
	    if [ "$smtp_auth_return" ];then
		smtp_auth_v=33
	    else
		smtp_auth_v=32
	    fi

	    quit_error 1 $smtp_auth_v
	    return $return_v
	else
	    return_v=0
	fi
    else
	return_v=0
    fi

############### MAIL FROM ###############
    if [ $mbody_encode_type = 8BIT ];then
	if [ $ok_8bit = 1 ];then
	    from_mail_addr_mime="<$from_mail_addr> BODY=8BITMIME"
	else
	    echo $clsmtpc_version-: "***** THIS SERVER CAN'T GET 8BIT *****"
	    quit_error 1 40
	    return $return_v
	fi
    else
	from_mail_addr_mime="<$from_mail_addr>"
    fi

    write_smtp_command -n "MAIL FROM: $from_mail_addr_mime"
    read_smtp_return;rsr_v=$?
    if [ $rsr_v = 0 ];then
	if [ ${smtp_return:0:1} = 2 ];then
	    return_v=0
	else
	    quit_error 1 25
	    return $return_v
	fi
    else
	quit_error 1 24
	return $return_v
    fi

############### RCPT TO command ################
    write_smtp_command -n "RCPT TO: <$mail_to_addr>"
    read_smtp_return;rsr_v=$?
    if [ $rsr_v = 0 ];then
	if [ ${smtp_return:0:1} = 2 ];then
	    return_v=0
	else
	    quit_error 1 17
	    return $return_v
	fi
    else
	quit_error 1 16
	return $return_v
    fi

################ DATA ##################
    write_smtp_command -n "DATA"
    read_smtp_return;rsr_v=$?
    if [ $rsr_v = 0 ];then
	if [ ${smtp_return:0:3} = 354 ];then
	    return_v=0
	else
	    quit_error 1 9
	    return $return_v
	fi
    else
	quit_error 1 8
	return $return_v
    fi

    data="$clm_header"$'\r'$'\n'$'\r'$'\n'"$clm_mbody"$'\r'$'\n'"."
    write_smtp_command -n "$data" -d
    read_smtp_return;rsr_v=$?
    if [ $rsr_v = 0 ];then
	if [ ${smtp_return:0:1} = 2 ];then
	    return_v=0
	else
	    quit_error 1 9
	    return $return_v
	fi
    else
	quit_error 1 8
	return $return_v
    fi

################# QUIT command ##################
    quit_error 1 0
    return $return_v
}

header_enc() {
    if [ "$1" ];then
	sub=$1
    else
	echo -n ""
	return 0
    fi

    enc_method=$2
    enc_charset=$3

    check_ascii_v=0
    sub_c=`echo -n ${sub} |wc -c`
    if [ $(($sub_c%2)) = 1 ];then
	sub_c=$(($sub_c+1))
    fi

    echo -n "$sub" |check_ascii 0 $sub_c -m >/dev/null ;check_ascii_v=$?

    if [ $check_ascii_v -lt 2 ];then
	for t in `echo -n "$sub" |od -h -A n ` ;do
	    h1=${t:0:2}
	    h2=${t:2:2}
	    hex_str_pre=$hex_str
	    hex_str=$hex_str$h2$h1
	done

	if [ $h1 = 00 ];then
	    hex_str=$hex_str_pre$h2
	fi

	hex_str_c=${#hex_str}
	i=0
	n=0
	t=00
	ss=
	while [ loop ];do
	    t_pre=$t
	    t=${hex_str:$(($i*2)):2}
	    if [ $t = 20 ];then
		if [ $n -gt 75 ];then
		    ss="$ss_pre\r\n\x20"
		    n=0
		    i=$max_i
		    continue
		else
		    ss_pre=$ss
		    ss="$ss\x20"
		    max_i=$i
		fi
	    else
		ss="$ss\x$t"
	    fi

	    i=$(($i+1))
	    n=$(($n+1))
	    if [ $hex_str_c = $(($i*2)) ];then
		break
	    fi
	done
	ss=`echo -ne $ss` 
    else
	if [ $enc_method = BASE64 ];then
	    type_method=B
	    enc_method_ex=base64_header_enc
	elif [ $enc_method = QP ];then
	    type_method=Q
	    enc_method_ex=qp_enc
	else
	    type_method=B
	    enc_method_ex=base64_header_enc
	fi

	if [ $charset_opt_v != 1 ];then
	    enc_charset=UTF-8
	fi

	enc_sub=`echo -n "$sub" |$enc_method_ex -w $enc_charset`
	ss=""
	for s in $enc_sub ;do
	    s1=${s%$'\n'}
	    s1=${s1%$'\r'}
	    if [ "$ss" ];then
		ss=$ss$'\r'$'\n'" =?${enc_charset}?$type_method?${s1}?="
	    else
    		ss="=?${enc_charset}?$type_method?${s1}?=" 
	    fi
	done
    fi

    echo -n $'\r'$'\n'" $ss"
    return 0
}

mk_header() {
    if [ "$1" = "${1/<*>/}" ];then
	header_from_name=$1
	header_from_addr=$2
    else
	header_from_addr=${1#*<}
	header_from_addr=${header_from_addr%>*}
	header_from_name=${1/<*>/}
	while [ "$header_from_name" != "${header_from_name% }" ];do
	    header_from_name="${header_from_name% }"
	done
    fi

    if [ "$3" = "${3/<*>/}" ];then
	header_to_name=$3
	header_to_addr=$4
    else
	header_to_addr=${3#*<}
	header_to_addr=${header_to_addr%>*}
	header_to_name=${3/<*>/}
	while [ "$header_to_name" != "${header_to_name% }" ];do
	    header_to_name="${header_to_name% }"
	done
    fi
#    header_to_name=$3
#    header_to_addr=$4

    header_subject=$5
    header_content_type=$6
    header_charset=$7
    header_transfer_encoding=$8
    header_multi_part=$9

    if [ "$header_multi_part" ];then
	b=0
	mbody_content_type=$header_content_type
	header_content_type="multipart/mixed"
	header_boundary=boundary_aPk3${RANDOM}8iTFqw$b
	while [ loop ];do
	    ok_multipart=1
	    for t in `echo -n $header_multi_part |tr ',' ' '` ;do
		check_file=`cat ${t%%:*}`
		if [ "$check_file" = "${check_file%${header_boundary}*}" ];then
		    continue
		else
		    ok_multipart=0
		    break
		fi
	    done

	    if [ $ok_multipart = 0 ];then
		b=$(($b+1))
		header_boundary=boundary_aYk3${RANDOM}8iTFqw$b
	    else
		break
	    fi 
	done

	content_type_multipart="boundary=$header_boundary"
    else
	content_type_multipart="charset=$header_charset"
    fi
 
    if [ $header_transfer_encoding = BASE64 ];then
	header_encoding_method=BASE64
	HCTE=BASE64
    elif [ $header_transfer_encoding = QP ];then
   	header_encoding_method=QP
	HCTE=QUOTED-PRINTABLE
    elif [ $header_transfer_encoding = 7BIT ];then
	header_charset_2022=${header_charset%-*}
	if [ ${header_charset_2022:0:8} = ISO-2022 ];then
   	    header_encoding_method=BASE64
	else
	    header_encoding_method=none
	fi
	HCTE=7BIT
    else
	header_encoding_method=none
	HCTE=8BIT
    fi

    header_from_name_enc=`header_enc "$header_from_name" $header_encoding_method $header_charset`
    header_subject_enc=`header_enc "$header_subject" $header_encoding_method $header_charset`
    header_to_name_enc=`header_enc "$header_to_name" $header_encoding_method $header_charset`

    header_from_name_enc_final=$header_from_name_enc
    header_subject_enc_final=$header_subject_enc
    header_to_name_enc_final=$header_to_name_enc

    uptime_v=`cat /proc/uptime`
    running_tv=${uptime_v% *}
    mess_ID=`date '+%Y%m%d%H%M%S'`$running_tv

    send_date=`LANG=C date '+%a, %_d %b %Y %H:%M:%S %z (%Z)'`

    mk_header_final="From: $header_from_name_enc_final"$'\r'$'\n'\
" <${header_from_addr}>"$'\r'$'\n'\
"To: $header_to_name_enc_final"$'\r'$'\n'\
" <$header_to_addr>"$'\r'$'\n'\
"Subject: $header_subject_enc_final"$'\r'$'\n'\
"X-Mailer: $ex_name-$version"$'\r'$'\n'\
"Mime-Version: 1.0"$'\r'$'\n'\
"Date: $send_date"$'\r'$'\n'\
"Message-Id: <$mess_ID@$from_host>"$'\r'$'\n'\
"Content-Type: $header_content_type; $content_type_multipart"$'\r'$'\n'\
"Content-Transfer-Encoding: $HCTE"

    if [ "$header_multi_part" ];then
	mk_header_mp_final=$'\r'$'\n'$'\r'$'\n'\
"--$header_boundary"$'\r'$'\n'\
"Content-Type: $mbody_content_type; charset=$header_charset"$'\r'$'\n'\
"Content-Transfer-Encoding: $HCTE"
	mk_header_final=$mk_header_final$mk_header_mp_final
    fi
    clm_header_mk="$mk_header_final"
    return 0
}

mk_multipart() {
    header_boundary_mp=$2
    fn_LANG=${3:-$LANG}
    for t in `echo -n $1 |tr ',' ' '` ;do

	if [ ${t%\:*} = $t ];then
	    file_path=$t
	    file_name=${file_path##*/}
	    file_data=`file -i $file_path`
	    file_data_t=${file_data#${file_path}:}
	    file_mime=`echo -n ${file_data_t%;*}`
	    file_charset=`echo -n ${file_data_t#*;}`
	    file_charset=${file_charset#*=}
	else
	    file_opt_data_1_2=${t%:*}

	    file_path=${file_opt_data_1_2%:*}
	    file_name=${file_path##*/}

	    file_opt_mime=${file_opt_data_1_2##*:}
	    file_opt_charset=${t##*:}
	    if [ "$file_opt_mime" ];then
		file_mime=$file_opt_mime
	    else
		file_data=`file -i $file_path`
		file_data_t=${file_data#${file_path}:}
		file_mime=`echo -n ${file_data_t%;*}`
	    fi

	    if [ "$file_opt_charset" ];then
		file_charset=$file_opt_charset
	    else
		file_data=`file -i $file_path`
		file_data_t=${file_data#${file_path}:}
		file_charset=`echo -n ${file_data_t#*;}`
		file_charset=${file_charset#*=}
	    fi
	fi

	if [ $file_mime = $file_charset ];then
	    file_charset=unknown
	else
	    file_charset=`low2up $file_charset`
	fi

	enc_type_multipart=BASE64

	file_name_param=`echo -n "$file_name" |param_enc "${fn_LANG}" filename`
	file_name_enc=""
	for t1 in $file_name_param ;do
	    if [ "$file_name_enc" ];then
		file_name_enc="$file_name_enc
 $t1"
	    else
		file_name_enc=$t1
	    fi
	done

	header_multipart=$'\r'$'\n'\
"--${header_boundary_mp}"$'\r'$'\n'\
"Content-Type: $file_mime; charset=$file_charset"$'\r'$'\n'\
"Content-Disposition: attachment;"$'\r'$'\n'\
" $file_name_enc"$'\r'$'\n'\
"Content-Transfer-Encoding: ${enc_type_multipart}"$'\r'$'\n'\
$'\r'$'\n'

	multipart_body_enc=`cat $file_path |$base64_command`
	chk_NL=`echo -n "$multipart_body_enc" |check_NL`
	multipart_body=`echo -n "${multipart_body_enc}" |trans_NL $chk_NL`
	multipart_body_final=$header_multipart$multipart_body

	if [ "$multipart_body_all" ];then
	    multipart_body_all=$multipart_body_all$'\r'$'\n'$multipart_body_final
	else
	    multipart_body_all="$multipart_body_final"
	fi
    done

    header_multipart_end=$'\r'$'\n'--${header_boundary_mp}--$'\r'$'\n'
    echo -n "$multipart_body_all$header_multipart_end"
    return 0
}

clm_getpass() {
    pass_user=$1
    pass_server=$2
    check_id="$pass_user:$pass_server:"
    pass_file=$3

    if [ -r $pass_file ];then
	check_id_l=${#check_id}
	while read s ;do
	    if [ "${s:0:$check_id_l}" = "${check_id}" ];then
		smtp_pass=${s#${check_id}}
	    fi
	done < $pass_file
	
	if [ "$smtp_pass" ];then
	    echo -n $smtp_pass
	    return 0
	else
	    echo "$ex_name:$FUNCNAME: $pass_user on $pass_server is not found in $pass_file." >&2
	    return 1
	fi
    else
	echo $ex_name:$FUNCNAME: $pass_file, permission denied. >&2
	return 1
    fi
}

############################# main ################################

clm_hostname=${HOSTNAME:-localhost}
smtp_client=$clm_hostname
from_host=$smtp_client
#smtp_user=$USER
from_mail_name=""
mail_to_name=""
use_helo=0

auth_type=none
clmpasswd=/etc/$ex_name/clmpasswd

mail_server=localhost
mail_port=25

mbody_type_file=0
open_stdin=0
no_header=0
raw_nl=0

clsmtpc_subject=""
clsmtpc_mbody=""

term_charset=${LANG##*.}
case "$term_charset" in
    "C" | "POSIX" | "en_US" | "" ) term_charset="US-ASCII" ;;
esac
org_charset=${term_charset}

mbody_charset=UTF-8
charset_opt_v=0

term_NL_hex=`echo |od -h -A n`
if [ "$term_NL_hex" = 000a ];then
    term_NL=n
elif [ "$term_NL_hex" = 0a0d ];then
    term_NL=rn
elif [ "$term_NL_hex" = 000d ];then
    term_NL=r
else
    term_NL=n
fi

content_type="text/plain"

encrypt_mode=none

filename_env=$LANG
silence=0
verify_server=0
ok_nodata=0
debug=0
tls_mode=0

default_conf=/etc/$ex_name/$ex_name.conf
default_user_conf=$HOME/.$ex_name/$ex_name.conf
if [ -f $default_conf ];then
    . $default_conf
fi

if [ -f $default_user_conf ];then
    . $default_user_conf
fi

if [ "$conf_file" ];then
    if [ -f $conf_file ];then
	. $conf_file
    fi
fi

if [ "$mail_from" ];then
    from_mail_addr=$mail_from
    mail_from=""
fi

if [ "$rcpt_to" ];then
    mail_to_addr=$rcpt_to
    rcpt_to=""
fi

if [ "$passwd_file" ];then
    clmpasswd=$passwd_file
    passwd_file=""
fi

if [ "$new_line" ];then
    mbody_NL_opt=$new_line
    new_line=""
fi

check_base64=`type -p base64`
if [ -x "$check_base64" ];then
    base64_command=$check_base64
else
    base64_command=base64_internal
fi

while [ "$1" ];do
    clm_opt=$1
    case ${1%%=*} in
	"-H" | "--from_host" )
	    shift
	    if [ "$1" ];then
		from_host=$1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1
	    fi
	    ;;
	"-T" | "--rcpt_to" )
	    shift
	    if [ "$1" ];then
		mail_to_addr=$1
		rcpt_to=$mail_to_addr
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-M" | "--mail_server" )
	    shift
	    if [ "$1" ];then
		mail_server=$1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-P" | "--mail_port" )
	    shift
	    if [ "$1" ];then
		check_v=$1
		check_v_test=$(($check_v))
		if [ $check_v = $check_v_test ];then
		    mail_port=$1
		    mail_port_opt=$mail_port
		    shift
		else
		    echo $ex_name: $clm_opt: $1, not a decimal value >&2
		    exit 1
		fi
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-W" | "--time_wait" )
	    shift
	    if [ "$1" ];then
		check_v=$1
		check_v_test=$(($check_v))
		if [ $check_v = $check_v_test ];then
		    time_wait_opt=$1
		    time_wait=$time_wait_opt
		    shift
		else
		    echo $ex_name: $clm_opt: $1, not a decimal value >&2
		    exit 1
		fi
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;

	"-r" | "--raw_passwd" )
	    shift
	    if [ "$1" ];then
		raw_passwd=$1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-p" | "--passwd_file" )
	    shift
	    if [ "$1" ];then
		if [ -f $1 ];then
		    clmpasswd=$1
		    shift
		else
		    echo $ex_name: $clm_opt: $1 is not found. >&2
		    exit 3
		fi
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-A" | "--auth_type" )
	    shift
	    if [ "$1" ];then
		case $1 in
		    login ) auth_type=login;;
		    plain ) auth_type=plain;;
		    cram_md5 | cram-md5 ) auth_type=cram_md5;;
		    digest_md5 | digest-md5 ) auth_type=digest_md5;;
		    pop_b_smtp ) auth_type=pop_b_smtp;;
		    auto ) auth_type=auto;;
		    * ) echo  $ex_name: $clm_opt: invalid SMTP AUTH type, $1 >&2
			exit 1
			;;
		esac 
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1
	    fi
	    ;;
	"-N" | "--from_mail_name" )
	    shift
	    if [ "$1" ];then
		from_mail_name=$1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-n" | "--mail_to_name" )
	    shift
	    if [ "$1" ];then
		mail_to_name=$1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-U" | "--smtp_user" )
	    shift
	    if [ "$1" ];then
		smtp_user=$1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-F" | "--mail_from" )
	    shift
	    if [ "$1" ];then
		from_mail_addr=$1
		mail_from=$from_mail_addr
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-L" | "--new_line" )
	    shift
	    if [ "$1" ];then
		case $1 in
		    n ) mbody_NL_opt=n ;;
		    rn ) mbody_NL_opt=rn ;;
		    r ) mbody_NL_opt=r ;;
		    * ) echo $ex_name: $clm_opt: invalid value, $1 >&2
			exit 1
			;;
		esac
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-s" | "--subject" )
	    shift
	    if [ "$1" ];then
		clsmtpc_subject=$1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1 
	    fi
	    ;;
	"-m" | "--mail_data" )
	    shift
	    if [ "$1" ];then

		if [ -f $1 ];then
		    clsmtpc_mbody=`cat $1 ;echo -n .`
		    clsmtpc_mbody=${clsmtpc_mbody%.}
		    mbody_type_file=1
		elif [ "$1" = "-" ];then
		    open_stdin=1
		else
		    echo $ex_name: $clm_opt: $1 is not found. >&2
		    exit 3
		fi
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1
	    fi
	    ;;
	"-t" | "--content_type" )
	    shift
	    if [ "$1" ];then
		content_type=$1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1
	    fi
	    ;;
	"-C" | "--mbody_charset" )
	    shift
	    if [ "$1" ];then
		mbody_charset=`low2up $1`
		charset_opt_v=1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1
	    fi
	    ;;
	"-c" | "--input_charset" )
	    shift
	    if [ "$1" ];then
		input_charset=`low2up $1`
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1
	    fi
	    ;;
	"-f" | "--conf_file" )
	    shift
	    if [ "$1" ];then
		clsmtpc_conf_opt=$1
		if [ ! -f $1 ];then
		    echo $ex_name: $clm_opt: $1 is not found. >&2
		    exit 3
		else
		    . $clsmtpc_conf_opt
		fi
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1
	    fi
	    ;;
	"-e" | "--encode_type" )
	    shift
	    Ce=`low2up $1`
	    case $Ce in
		"8" | "8BIT" | "8-BIT" ) encode_type=8BIT ;;
		"7" | "7BIT" | "7-BIT" ) encode_type=7BIT ;;
	        "BASE64" | "B" | "64" ) encode_type=BASE64 ;;
		"QUOTED-PRINTABLE" | "QP" | "Q" ) encode_type=QP ;;
		* ) echo $ex_name: $clm_opt: unknown encode type, $1 >&2
		    exit 1
		    ;;
	    esac
	    shift
	    ;;
	"-a" | "--attached_files" )
	    shift
	    attached_files_num=0
	    if [ "$1" ];then

		for t in `echo -n $1 |tr ',' ' '` ;do
		    i=0
		    colon_num=0
		    char_num=${#t}
		    while [ loop ];do
			check_char=${t:$i:1}
			if [ ! "$check_char" ];then
			    break
			elif [ $check_char = ":" ];then
			    colon_num=$(($colon_num+1))
			fi
			i=$(($i+1))
			if [ $i = $char_num ];then
			    break
			fi
		    done

		    if [ $colon_num != 0 ]&&[ $colon_num != 2 ];then
			echo $ex_name: $clm_opt: $t is invalid \"':'\" separation.  >&2
			exit 1
		    fi

		    check_file=${t%%:*}
		    if [ -f ${check_file} ];then
			if [ "$attached_files_name" ];then
			    attached_files_name="$attached_files_name $check_file"
			    attached_files_num=$(($attached_files_num+1))
			else
			    attached_files_name=$check_file
			    attached_files_num=1
			fi
		    else
			echo $ex_name: $clm_opt: $check_file is not found. >&2
			exit 3
		    fi
		done
		attached_files_opt=$1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1
	    fi
	    ;;
	"-x" | "--filename_env" )
	    shift
	    if [ "$1" ];then
		filename_env=$1
		shift
	    else
		echo $ex_name: $clm_opt: no value >&2
		exit 1
	    fi
	    ;;
	"-Z" | "--encrypt_mode" )
	    shift
	    encrypt_str=$1
	    case "$1" in
		smtps ) encrypt_mode=smtps ;;
		TLS ) encrypt_mode=TLS ;;
		tls | tls0 )  encrypt_mode=none ;tls_mode=1 ;;
		tls1 )  encrypt_mode=none ;tls_mode=2 ;;
		none ) encrypt_mode=none ;;
		* ) echo $ex_name: $clm_opt: invalid value, $1 >&2
		    exit 1
		;;
	    esac

	    check_openssl=`type -p openssl`
	    if [ ! -x "$check_openssl" ];then
		echo $ex_name: $clm_opt: $1 is not available without openssl. >&2
		exit 5
	    fi
	    shift
	    ;;
	"-O" | "--use_helo" )
	    shift
	    use_helo=1
	    ;;
	"-R" | "--raw_nl" )
	    shift
	    raw_nl=1
	    ;;
	"-d" | "--no_header" )
	    shift
	    no_header=1
	    ;;
	"-S" | "--silence" )
	    shift
	    silence=1
	    ;;
	"-z" | "--ok_nodata" )
	    shift
	    ok_nodata=1
	    ;;
	"-y" | "--sup_op_attached_files" )
	    shift
	    sup_op_attached_files=1
	    ;;
	"-v" | "--verify_server" )
	    shift
	    verify_server=1
	    ;;
	"-V" | "--version" )
	    echo $ex_name: $version, $dev_date
	    echo $clsmtpc_info
	    exit 0
	    ;;
	"-h" | "--help" )
	    help_info
	    exit 0
	    ;;
	"-B" | "--base64_internal_on" )
	    shift
	    base64_internal_on=1
	    ;;
	"--pop3_server" )
	    shift
	    pop3_server=$1
	    shift
	    ;;
	"-D" | "--debug" )
	    shift
	    debug=1
	    ;;
	"-i" | "--input_mail_body" )
	    shift
	    clsmtpc_mbody="$1"
	    shift
	    ;;
	* )
	    if [ ! "$2" ];then
		clsmtpc_mbody="$1"
		mbody_type_file=0
		break
	    else
		echo $ex_name: $clm_opt is not implemented. >&2
		exit 5
	    fi
	    ;;
    esac
done

### command line setting finish ###
if [ "$base64_internal_on" = 1 ];then
    base64_command=base64_internal
fi

if [ $debug = 1 ]||[ $verify_server = 1 ];then
    ok_nodata=1
fi

if [ $encrypt_mode = smtps ];then
    if [ ! "$mail_port_opt" ];then
	mail_port=465
    fi
fi

if [ ! "$clsmtpc_subject" ]&&[ "$subject" ];then
    clsmtpc_subject=$subject
fi

if [ $verify_server != 1 ];then
    org_charset=${input_charset:-$org_charset}
    org_charset=`low2up "$org_charset"`
    mbody_charset=`low2up "$mbody_charset"`

    if [ ! "$clsmtpc_mbody" ]&&[ ! "$mail_data" ];then
	if [ $ok_nodata != 1 ]||[ $open_stdin = 1 ];then
	    clsmtpc_mbody=`cat ;echo -n .`
	    clsmtpc_mbody=${clsmtpc_mbody%.}
	fi
    else
	if [ ! "$clsmtpc_mbody" ];then
	    clsmtpc_mbody=`cat $mail_data ;echo -n .`
	    clsmtpc_mbody=${clsmtpc_mbody%.}
	fi
    fi

    if [ "$encode_type" ];then
	mbody_encode_type=`low2up "$encode_type"`
    elif [ ${mbody_charset} = US-ASCII ];then
	mbody_encode_type=7BIT
    elif [ ${mbody_charset:0:8} = ISO-2022 ];then
	mbody_encode_type=7BIT
    elif [ ${mbody_charset:0:8} = ISO-8859 ];then
	mbody_encode_type=QP
    else

	echo $clsmtpc_mbody |check_ascii 0 255 -a >/dev/null ;check_ascii_v=$?
	if [ $check_ascii_v -lt 2 ];then
	    mbody_encode_type=7BIT
	    if [ $charset_opt_v != 1 ];then
		mbody_charset=US-ASCII
	    fi
	else
	    mbody_encode_type=BASE64
	fi
    fi

    err_iconv=`echo a |iconv -f $org_charset -t $mbody_charset 2>&1`;iv=$?
    if [ $iv != 0 ];then
	echo $ex_name: $err_iconv >&2
	exit $iconv_err_v
    fi

    if [ "$mail_from" ];then
	from_mail_addr=$mail_from
    else 
	if [ ! "$from_mail_addr" ];then
	    echo $ex_name: YOUR MAIL ADDRESS is not set. >&2
	    exit 1
	fi
    fi

    smtp_user=${smtp_user:-${from_mail_addr%%@*}}

    if [ "$rcpt_to" ];then
	mail_to_addr=$rcpt_to
    else
	if [ ! "$mail_to_addr" ];then
	    echo $ex_name: MAIL RECIPIENT ADDRESS is not set. >&2
	    exit 1
	fi
    fi
else
    mbody_encode_type=7BIT
    auth_type=none
    no_header=1
fi

if [ ! "$mbody_NL_opt" ];then
    if [ ! "$clsmtpc_mbody" ];then
	mbody_NL=$term_NL
    else
	check_NL_res=`echo -n "$clsmtpc_mbody" |check_NL`
	if [ $check_NL_res = 0 ]||[ $check_NL_res = u ];then
	    mbody_NL=$term_NL
	elif [ $check_NL_res = r ];then
	    mbody_NL=r
	elif [ $check_NL_res = rn ];then
	    mbody_NL=rn
	else
	    mbody_NL=n
	fi
    fi
else
    mbody_NL=$mbody_NL_opt
fi

if [ "$attached_files_opt" ];then
    attached_files=$attached_files_opt
else
    if [ "$attached_files" ];then

	for t in `echo -n $attached_files |tr ',' ' '` ;do
	    i=0
	    colon_num=0
	    char_num=${#t}
	    while [ loop ];do
		check_char=${t:$i:1}
		if [ $check_char = ":" ];then
		    colon_num=$(($colon_num+1))
		fi
		i=$(($i+1))
		if [ $i = $char_num ];then
		    break
		fi
	    done
	    
	    if [ $colon_num != 0 ]&&[ $colon_num != 2 ];then
		echo $ex_name:conf_file: $t is invalid \"':'\" separation.  >&2
		exit 1
	    fi

	    check_file=${t%%:*}
	    attached_files_name=""
	    attached_files_num=0
	    if [ -f ${check_file} ];then
		if [ "$attached_files_name" ];then
		    attached_files_name="$attached_files_name $check_file"
		    attached_files_num=$(($attached_files_num+1))
		else
		    attached_files_name=$check_file
		    attached_files_num=1
		fi
	    fi
	done
    fi
fi

uri_LANG=${uri_LANG_opt:-$LANG}

if [ "$silence" = 1 ];then
    exec 1>/dev/null
fi

if [ "$debug" = 1 ];then
    echo
    echo mail_server=$mail_server:mail_to_addr=$mail_to_addr:from_host=$from_host
    echo from_mail_addr=${from_mail_addr:-$from_mail_name@$from_host}
    echo from_mail_name=$from_mail_name
    echo smtp_user=$smtp_user:smtp_client=$smtp_client:auth_type=$auth_type
    echo term_NL=$term_NL:mbody_NL_opt=$mbody_NL_opt:mbody_NL=$mbody_NL
    echo mbody_charset=$mbody_charset: mbody_encode_type=$mbody_encode_type
    echo send_mail \"$clsmtpc_subject\" \""$clsmtpc_mbody"\"
fi

#    header_from_name=$1
#    header_from_addr=$2
#    header_to_addr=$3
#    header_subject=$4
#    header_content_type=$5
#    header_charset=$6
#    header_transfer_encoding=$7
#    header_to_name=$8

if [ $no_header != 1 ];then
    if [ $debug = 1 ];then
	echo
	echo mk_header \"$from_mail_name\" $from_mail_addr $mail_to_addr \"$clsmtpc_subject\" text/plain $mbody_charset $mbody_encode_type \"$mail_to_name\"
	mk_header "$from_mail_name" $from_mail_addr "$mail_to_name" $mail_to_addr "$clsmtpc_subject" $content_type $mbody_charset $mbody_encode_type $attached_files
	echo
    else
	mk_header "$from_mail_name" $from_mail_addr "$mail_to_name" $mail_to_addr  "$clsmtpc_subject" $content_type $mbody_charset $mbody_encode_type $attached_files
    fi
else
    clm_header_mk=""
fi

if [ $debug = 1 ];then
    echo "***************** DEBUG MODE *********************"
    exit 0
fi

if [ $raw_nl = 1 ];then
    clsmtpc_mbody_NL=$clsmtpc_mbody
else
    if [ $mbody_NL = n ];then
	clsmtpc_mbody_NL=${clsmtpc_mbody//$'\n'/$'\r'$'\n'}
    elif [ $mbody_NL = rn ];then
	clsmtpc_mbody_NL=$clsmtpc_mbody
    elif [ $mbody_NL = r ];then
	clsmtpc_mbody_NL=${clsmtpc_mbody//$'\r'/$'\r'$'\n'}
    else
	clsmtpc_mbody_NL=${clsmtpc_mbody//$'\n'/$'\r'$'\n'}
    fi
fi

if [ $term_charset = $mbody_charset ];then
    if [ $mbody_encode_type = BASE64 ];then
	clsmtpc_mbody_enc=`echo -n "$clsmtpc_mbody_NL" |$base64_command`
    elif [ $mbody_encode_type = QP ];then
	clsmtpc_mbody_enc=`echo -n "$clsmtpc_mbody_NL" |qp_enc $mbody_charset;echo -n .`
	clsmtpc_mbody_enc=${clsmtpc_mbody_enc%.}
    else
	clsmtpc_mbody_enc="$clsmtpc_mbody_NL"
    fi
else
    clsmtpc_mbody_iconv=`echo -n "$clsmtpc_mbody_NL" |iconv -f $org_charset -t $mbody_charset 2>/dev/null;siv=$?;echo -n .;exit $siv`;iv=$?
    clsmtpc_mbody_iconv=${clsmtpc_mbody_iconv%.}
    if [ $iv != 0 ];then
	err_iconv=`echo -n "$clsmtpc_mbody_NL" |iconv -f $org_charset -t $mbody_charset 2>&1 >/dev/null`
	echo $ex_name:from $org_charset to $mbody_charset :$err_iconv >&2
	exit $iconv_err_v
    fi

    if [ $mbody_encode_type = BASE64 ];then
	clsmtpc_mbody_enc=`echo -n "$clsmtpc_mbody_iconv" |$base64_command`
    elif [ $mbody_encode_type = QP ];then
	clsmtpc_mbody_enc=`echo -n "$clsmtpc_mbody_iconv" |qp_enc $mbody_charset ;echo -n .`
	clsmtpc_mbody_enc=${clsmtpc_mbody_enc%.}
    else
	clsmtpc_mbody_enc="$clsmtpc_mbody_iconv"
    fi
fi

mbody_final=${clsmtpc_mbody_enc//$'\r'$'\n'./$'\r'$'\n'..}

if [ "$attached_files" ];then

    if [ "$sup_op_attached_files" = 1 ];then
	if [ $attached_files_num -gt 1 ];then
	    pl_s=s
	else
	    pl_s=""
	fi
	p_mbody_final="$mbody_final

--- attached file$pl_s ---
$attached_files"
	else
	p_mbody_final=""
    fi
    mbody_final="$mbody_final`mk_multipart "$attached_files" $header_boundary $filename_env`"
fi

send_mail "$clm_header_mk" "$mbody_final";send_mail_v=$?

if [ $encrypt_mode != none ];then
    rm $input $output $stderr 2>/dev/null
fi

exit $send_mail_v
