+#
+# Lock here to ensure that only one copy of us is processing the incoming
+# mail queue at any point in time.
+#
+sysopen(LOCKFILE, $config{'db_dir'}.'/onak-mail.lck',
+ O_WRONLY|O_CREAT|O_EXCL) or exit;
+print LOCKFILE "$$";
+close(LOCKFILE);
+
+my $file;
+opendir(MAILDIR, $config{'mail_dir'});
+while ($file = readdir(MAILDIR)) {
+ next if $file !~ /\.onak$/;
+
+ $inheader = 1;
+ $subject = $from = $replyto = "";
+ undef %seenby;
+ @body = ();
+
+ open(FILE, '<'.$config{'mail_dir'}.'/'.$file);
+ while (<FILE>) {
+ if ($inheader) {
+ if (/^Subject:\s*(.*)\s*$/i) {
+ $subject = $1;
+ } elsif (/^X-KeyServer-Sent:\s*(.*)\s*$/i) {
+ $seenby{$1} = 1;
+ } elsif (/^From:\s*(.*)\s*$/i) {
+ $from = $1;
+ } elsif (/^Reply-To:\s*(.*)\s*$/i) {
+ $replyto = $1;
+ } elsif (/^$/) {
+ $inheader = 0;
+ }
+ }
+ if (!$inheader) {
+ push @body, $_;
+ }
+ }
+ if (! defined($replyto)) {
+ $replyto = $from;