milter quota for Maildir

This is a backend cgi that works with Maildir, and complements the previous milter quota posting.

Be careful! This is only a proof-of-concept. It does not do any sort of caching, nor does it use UDP.

Apparently, the standard Maildir++ specification dictates that everyone must update the maildirsize file when doing any operation in Maildirs. So we don’t have to do heavy filesystem operations to get current mailbox size, we can read it from this file..

#!/usr/bin/perl
# This is quota.cgi for Maildir # http://wiki.dovecot.org/Quota/Maildir

use CGI qw(:standard);

$username = param("u");
exit if ($username eq "");

$home = "/var/mail";

$file=$home . "/" . $username . "/Maildir/maildirsize";
open(FILE, "< $file") or die "OK $file does not exist.";
@lines = <FILE>;
close(FILE);

($quota, $msgquota) = split(" ", $lines[0]);
$quota=int($quota); $msgquota=int($msgquota);
$line=0; $size=0; $msgs=0;
do {
 $line++;
 ($msgsize, $msgcount) = split(" ", $lines[$line]);
 $size+=$msgsize; $msgs+=$msgcount;
} while ($line< =$#lines);

if (($size>$quota) || (($msgs>$msgquota) && ($msgquota!=0))) {
    print "OVERQUOTA $username $size $quota $msgs $msgquota\n";
} else {
    print "OK $username $size $quota $msgs $msgquota\n";
}

(Ref)