POP3.pl

From Organic Design wiki
Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, this is only useful for a historic record of work done. You may find a link to the currently used concept or function in this article, if not you can contact the author to find out what has taken the place of this legacy item.
use Net::POP3;

sub popCheck {

	# Login in to POP box
	my( $domain, $user ) = @_;
	my $loggedIn = 0;
	my $pop3 = Net::POP3->new( $domain );
	$pop3->login( $user, $::pwd4 );

	# Loop through all messages
	my $list = $pop3->list();
	my @messages = keys %$list;
	logAdd( ($#messages+1)." messages on $user\@$domain" );
	for ( @messages ) {

		# Get relevent message headers and content
		my $msg = $pop3->top( $_, 4096 );
		chomp @$msg;
		( my $to ) = grep /^to:/i, @$msg;
		if ( $to =~ /([-.&a-z0-9_]+)@([-.a-z0-9_]+)/i ) {

			( my $page, my $domain ) = ( $1, $2 );
			$to = "$page\@$domain";
			$page =~ tr/&/%/;
			( my $from ) = grep /^from:/i, @$msg;
			$from = $1 if $from =~ /([-.&a-z0-9_]+@[-&.a-z0-9_]+)/i;
			( my $subject ) = grep /^subject:/i, @$msg;
			$subject =~ s/^.+?:\s*(.*?)(\r?\n)*/$1/;

			# Couldn't forward an od addr to another od addr at zoneedit
			$page = 'user_talk%3ajewel' if $to =~ /^jewel@/;

			# Append if addressed to a talk page
			if ( $page =~ /^(\w*?_)?talk%3a/i ) {
				$) while shift @$msg;
				my $append = "\n----\n'''Email from [mailto:$from $from]''', ~~";
				$append .= "~~\n\n'''''$subject'''''\n:".join( "\n:", @$msg )."\n";
				$loggedIn = wikiLogin $::wiki, $::peer, $::pwd1 unless $loggedIn;
				wikiPageAppend $::wiki, $page, $append, $subject;
				$pop3->delete($_);
				}

			# Execute if an email command
			if ( $subject =~ /^(.+?):(.+?)\((.*?)\)\s*(.*)$/ and $1 eq $::pwd5 ) {
				spawn $2, split /\s*,\s*/, $3;
				logAdd "Executing $2";
				$pop3->delete($_);
				}

			}
		}
	$pop3->quit();
	logAdd 'Exit';
	}