html/mailcheck_pop3_kimmo.pl


   1 # Provides /mail command for POP3 mail checking
   2 # for irssi 0.7.98 (tested on CVS) by Kimmo Lehto
   3 #
   4 # Requires Net::POP3 module
   5 # If you don't have it, you can install it using:
   6 #
   7 # perl -e shell -MCPAN;
   8 # >install Net::POP3
   9 #
  10 
  11 use Irssi;
  12 use Net::POP3;
  13 use vars qw($VERSION %IRSSI);
  14 
  15 $VERSION = '0.5';
  16 %IRSSI = (
  17     authors     => 'Kimmo Lehto',
  18     contact     => 'kimmo@a-men.org' ,
  19     name        => 'Mailcheck-POP3',
  20     description => 'POP3 new mail notification and listing of mailbox contents. Use "/mail help" for instructions. Requires Net::POP3.',
  21     license     => 'Public Domain',
  22     changed	=> 'Sun Apr 7 00:10 EET 2002'
  23 );
  24 
  25 
  26 
  27 
  28 
  29 sub cmd_checkmail
  30 {
  31 	my $args = shift;
  32 	my ($user, $pass, $host) = split(/\;/, $args);
  33 	my $i, $from, $subject, $head;
  34 	my $POP3TIMEOUT = Irssi::settings_get_int("pop3_timeout");
  35         my $pop = Net::POP3->new( $host, Timeout => $POP3TIMEOUT );
  36 	$count = $pop->login($user, $pass);
  37 
  38 	if (!$count || !$pop)
  39 	{
  40 		Irssi::print("Invalid POP3 user, pass or host.", MSGLEVEL_CLIENTERROR);
  41 		if (!$_mailcount{"$user\@$host"})
  42 		{
  43 			Irssi::timeout_remove($_mailchecktimer{"$user\@$host"});
  44 			delete $_mailchecktimer{"$user\@$host"};
  45 		}
  46 		$pop->quit();
  47 		return undef;
  48 	}
  49 	if (!$_mailcount{"$user\@$host"}) { $_mailcount{"$user\@$host"} = $count; $pop->quit(); return 1; }
  50 	if ($_mailcount{"$user\@$host"} < $count)
  51 	{
  52 		Irssi::print("%R>>%n New Mail for $user\@$host:"); 
  53 		
  54   		for( $i = $_mailcount{"$user\@$host"} + 1; $i <= $count; $i++ ) 
  55 		{
  56 			foreach $head (@{$pop->top($i)})
  57 			{
  58 				if ($head =~ /^From:\s+(.*)$/i) { $from = $1; chomp($from);}
  59 				elsif ($head =~ /^Subject:\s+(.*)$/i) { $subject = $1; chomp($subject);}
  60 			}
  61 			Irssi::print("From   : %W$from%n\nSubject: %W$subject%n");
  62   		}
  63 	}
  64   
  65 	$_mailcount{"$user\@$host"} = $count;
  66 	$pop->quit();
  67 	return 1;
  68 }
  69 sub start_check
  70 {
  71 	my ($userhost, $pass) = @_;
  72 	my ($user, $host) = split(/\@/, $userhost);
  73 	my $INTERVAL = Irssi::settings_get_int("pop3_interval");
  74 	if (cmd_checkmail("$user;$pass;$host"))
  75 	{
  76 		$_mailchecktimer{"$user\@$host"} = Irssi::timeout_add($INTERVAL * 1000, 'cmd_checkmail', "$user;$pass;$host");
  77 		Irssi::print("Account $user\@$host is now being monitored for new mail.");
  78 	}
  79 }
  80 
  81 sub cmd_mail
  82 {
  83 	my $args = shift;
  84 	my (@arg) = split(/\s+/, $args);
  85 
  86 	if ((@arg[0] eq "add") && @arg[1] && @arg[2])
  87 	{
  88 		if ($_mailchecktimer{@arg[1]})
  89 		{
  90 			Irssi::print("Account " . @arg[1] . " is already being monitored.");
  91 		}
  92 		else
  93 		{
  94 			start_check(@arg[1], @arg[2]);
  95 		}
  96 	}
  97 	elsif (@arg[0] eq "list")
  98 	{
  99 		Irssi::print("Active POP3 Accounts Being Monitored:");
 100 		foreach (keys %_mailchecktimer)
 101 		{
 102 			Irssi::print(" %W-%n $_ ($_mailcount{$_} Mail message(s))");
 103 		}
 104 		Irssi::print("End of /mail list");
 105 	}
 106 	else
 107 	{
 108 		Irssi::print("%Wmailcheck.pl%n $VERSION - By KimmoKe\%W@%nircnet\n");
 109 		Irssi::print("Usage:");
 110 		Irssi::print("/mail add <user\@host> <password> - add account to be monitored.");
 111 		Irssi::print("/mail remove <user\@host> - stop monitoring account");
 112 		Irssi::print("/mail list - list monitored accounts");
 113 		Irssi::print("/mail list <user\@host> - list ALL messages in mailbox");
 114 		Irssi::print("\n%WNote:%n Passwords are kept in irssi's memory in %Wplain text%n, and the password will also remain in the command history. The POP3 authorization is currently also plain text.\n");
 115 		Irssi::print("Check interval and POP3 login timeout are controlled with %W/set pop3_interval%n (default: 60 seconds) and %Wpop3_timeout%n (default: 30 seconds).");
 116 	}
 117 
 118 
 119 	
 120 }
 121 
 122 
 123 Irssi::settings_add_int("misc","pop3_timeout",30);
 124 Irssi::settings_add_str("misc","pop3_interval","60");	
 125 Irssi::command_bind('mail', 'cmd_mail');