#!/bin/sh

# Qmail - mailquotacheck
#
# Japanized by Tatsuya Nonogaki. http://www.asahi-net.or.jp/~aa4t-nngk/
#
# Original source:
# Author: Paul Gregg <pgregg@tibus.net>
# Url: http://www.tibus.net/pgregg/projects/qmail/
# Run this program ala: |mailquotacheck   before your ./Maildir/ or ./Mailbox
# entry in the .qmail file. e.g:
# |/usr/local/bin/mailquotacheck
# ./Maildir/

# Default quota is set to 3000Kb per user, this can be changed below

# You can also install per user quotas which will override the defaults
# by creating a .quota file in the same directory as the .qmail file, e.g:
# echo 10240 > .quota
# this will give that user a 10Mb mail quota

# Individual per message quotas can also be used by creating a file telling
# mailquotacheck that maximum permitted size per email - this is useful
# when you want to allow someone, say, a 20Mb limit but want to prevent emails
# larger than 5Mb. e.g:
# echo 5120 > .maxmsgsize

# Program location defs:
cat="/bin/cat"
expr="/usr/bin/expr"
wc="/usr/bin/wc"
du="/usr/bin/du"
bc="/usr/bin/bc"
cut="/bin/cut"
awk="/bin/awk"
echo="/bin/echo"
nkf="/usr/bin/nkf -E -j"

# Program defaults
# quota is the default user quota if the user does not have a .quota file
quota=10240
# hardquotabuffer is the 'extra' space allowed for small (<1Kb) messages.
hardquotabuffer=100

# -------------------------------------------------------------------------
# You should not need to change anything below here
# -------------------------------------------------------------------------


# Find out how big the email is in Kb - We don't care about < 1Kb messages.
msgbytes=`$cat - | $wc -c`
ERROR=$?
if [ ${ERROR} -ne 0 ]; then
  # If this fails then you are in trouble ;) - Check program defs at the top.
  $echo | $nkf <<EOM
クォータチェックエラー:
メールボックス容量チェックプログラムは、あなたの送信したメールのサイズを判断できませんでした。できれば、受信者の利用しているメールサーバの管理者にお知らせください。
EOM
  exit 100
fi
msgkb=`$expr $msgbytes / 1024`
# or you can use:
# msgkb=`$echo $msgbytes / 1024 | $bc`


# Get the users 'home' directory - where there .qmail file is
dir="$HOME"


# Figure out a users mail quota - default is 3000Kb (see above)
# If there is a file '.quota' in their dir then use that value instead.
if [ -f "$dir/.quota" ]; then
  quota=`$cat $dir/.quota 2>/dev/null`
  ERROR=$?
  if [ ${ERROR} -ne 0 ]; then
    $echo | $nkf <<EOM
宛先のメールボックス容量制限の読み取りでエラーが起こりました。後ほど再配送を試みます。
EOM
    exit 111
  fi
fi


# Impose a maximum 'per message' email size.  Use the users quota as standard
# but if there is a file '.maxmsgsize' then use that value.
maxmsgsize=$quota
if [ -f "$dir/.maxmsgsize" ]; then
  maxmsgsize=`$cat $dir/.maxmsgsize`
  ERROR=$?
  if [ ${ERROR} -ne 0 ]; then
    $echo | $nkf <<EOM
宛先の受取り可能メールサイズの読み取りでエラーが起こりました。後ほど再配送を試みます。
EOM
    exit 111
  fi
fi

absquota=`$expr $quota + $hardquotabuffer`

# What is the maildir's current disk usage
du=`$du -sk $dir | $awk {'print $1'}`
ERROR=$?
if [ ${ERROR} -ne 0 ]; then
  $echo | $nkf <<EOM
宛先のメールボックス使用量の読み取りでエラーが起こりました。後ほど再配送を試みます。
EOM
  exit 111
fi

duwould=`$expr $du + $msgkb`

#debug - mail all these vars to me.
#set | mail pgregg@tibus.net

# Refuse the email if it is too big
if [ $msgkb -gt $maxmsgsize ]; then
  $echo | $nkf <<EOM
申し訳ありませんが あなたの送信されたメールは、受信者の受取れる最大メールサイズを超えています。
あなたの送信したメール: $msgkb Kバイト
受信者の受信可能サイズ: $maxmsgsize Kバイト
EOM
  exit 100
fi

# Check if the user would be above the absolute quota
if [ $duwould -gt $absquota ]; then
  # Ok, we aren't going to deliver this message, lets try and give the sender
  # a decent error message
  if [ $du -gt $quota ]; then
    $echo | $nkf <<EOM
メールボックス容量オーバー:
申し訳ありませんが、受信者のメールボックスが容量オーバーのため、メールは配送できませんでした。
EOM
  else
    $echo | $nkf <<EOM
申し訳ありませんが、あなたの送信されたメールは、受信者のメールボックス残量に収まらないため、配送できませんでした。
EOM
  fi
  if [ $du -lt $absquota ]; then
    $echo | $nkf <<EOM
このことを受信者に通知されたければ、ごく小さなメール（1Kバイト以下）ならば受信可能です。
EOM
  fi
  exit 100
fi

# If the email would put the user over quota, then refuse it (accept < 1Kb)
if [ $msgbytes -gt 1024 ]; then
  if [ $duwould -gt $quota ]; then

    $echo | $nkf <<EOM
メールボックス容量オーバー:
申し訳ありませんが、宛先のユーザーは、メールボックスにメールが溜まりすぎていて受取りができません。
あなたのメッセージの合計サイズ: $msgkb Kバイト（$msgbytes バイト）
このことを受信者に通知されたければ、ごく小さなメール（1Kバイト以下）ならば受信可能です。
EOM
    exit 100

  fi
fi

exit 0
