POP3 to Twitter script checks your POP3 email account and see if there's a new message (email) that is sent by your remote machine (such as your cell phone).

If there's indeed a new message from a specified address, it would then extract the text of this email, and update your twitter status with it, after encoding the text into UTF-8 encoding.

All the necessary information (namely your account information) must be stored within the source code (this might be going to be outsourced later on).

Please note : All the messages are deleted from your POP3 mail box, regardless of its address. Please use an email account which is used only for this purpose.

Please note 2 : The incoming messages are assumed to be either Japanese or English (since I frequently use Japanese characters on my cell phone). The code would need to be modified, in case the user likes to use different characters.


Usage : perl pop3_to_twitter.pl

This script must be called regularly (e.g., by cron daemon).
No argument is needed. 
#!/usr/local/bin/perl

#    pop3_to_twitter.pl
#          
#    Copyright(C) Since 2006 Akira KAKINOHANA All Rights Reserved
#    Author :  Akira KAKINOHANA <kira@kirameister.net>
#    Distributed at : http://softwares.kirameister.net/
#    
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation version 3.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    For GNU General Public License, see <http://www.gnu.org/licenses/>.

use Net::POP3;
use Net::Twitter;
use utf8;
use Encode;
use Encode::Guess qw/iso-2022-jp/; ## should work with most of the Japanese-coded emails..

###
# POP3 to Twitter
# Written by : Akira K. 
# Version : 0.1
# Description : This script allows you to update your twitter status by sending
# an email to an email address you specify.
# This script must be called regularly (e.g., by cron daemon) in order to check
# if there's a new email on your POP3 mail box.
###

## Email Server and account settings..
my $mailserver   = 'YOUR_BASE_SERVER.com';
my $mailaccount  = 'YOUR_BASE_ACCOUNT';
my $mailpassword = 'YOUR_BASE_PASSWORD';

## Twitter account settings..
my $twitter_username = "Your_Twitter_Account";
my $twitter_password = "Your_Twitter_Password";

## Cell Phone Email address setting..
my $cell_phone_address = "YOUR\@Cell_Phone_Address.com";

## initializing..
my $pop = Net::POP3->new($mailserver, Timeout => 60);
my $twit = Net::Twitter->new(
			     username=>$twitter_username,
			     password=>$twitter_password
			    );

if ($pop->login($mailaccount, $mailpassword) > 0) {
  my $msgnums = $pop->list; # hashref of msgnum => size
  LOOP: foreach my $msgnum (keys %$msgnums) {
    my $msg = $pop->get($msgnum);
    $pop->delete($msgnum);   ## message will be deleted in any case
    foreach my $line (@$msg){
      next LOOP if ($line =~ /^$/); ## go to next message if it's reached to the text..
      if ($line =~ /From:.*$cell_phone_address/){ ## From check done..
	until ($line =~ /^$/){
	  $line = shift(@$msg);
	  chomp($line);
	} ## the pointer is at the beggining of text
	my $msgtext = "";
	while($line = shift(@$msg)){
	  chomp($line);
	  $msgtext .= $line;
	}
	my $enc = guess_encoding ( $msgtext );
	if ( ref $enc ) { $msgtext = decode ( $enc->name , $msgtext ); }
	$msgtext = encode("utf8", $msgtext);

	my $result = $twit->update($msgtext);
	#print "Message : $msgtext\n";
      }
    }
  }
}

$pop->quit;

__END__