Archive

Archive for November, 2011

IMAP over SSL Perl/Ruby scripts and how to connect via CLI Linux

2011-11-30 Leave a comment

I wanted to try a few ways of accessing my email using scripts and also needed to test if the connection could be established over SSL/TSL. Here are the results of that.

To get started check to see if you can make a connection to the server over SSL. 993 is the standard IMAP SSL port check to see if the server is using a different port or protocol if it fails. See how google mail server is configured to use SMTP over SSL so the port is 465. This also will change the commands you have to use once connected either SMTP or IMAP commands depending.

openssl s_client -connect smtp.gmail.com:465 // SMTP connections
openssl s_client -connect imap.gmail.com:993 // For IMAP connections
# If you're having issues with the commands after you connected try adding -crlf to change the linefeeds into windows format.

Luckily making connections is much easier with modules/gems in perl and ruby. I use Mail::IMAPClient in perl; in Ruby I used net/imap and tmail. So my perl one does a bit more then the ruby one I just tried to do it in ruby for fun.

Perl

#!/usr/bin/perl
#####
##  Author: Kyle Rizzo
##  Date: 2011-10-24
##  Email: lifeforce0@gmail.com
##
##  Function: This script will create an IMAP connection to a mail server.
##
##  For documentation see: http://search.cpan.org/~plobbes/Mail-IMAPClient-3.28/lib/Mail/IMAPClient.pod
##
##  System Requirements:
##      Term::ReadKey # Used for password input
##      Getopt::Long # Used for command line options
##      Mail::IMAPClient # Used to make an IMAP connection and access folders/messages
##      Authen::NTLM # This allows us to connect to exchange (2010 does not support NTLM with out SP1 installed)
##      MIME::QuotedPrint::Perl # Incase it's formatted.
#####

use strict;
use warnings;
use Term::ReadKey;
use Getopt::Long;
use Mail::IMAPClient;

## Required to access Exchange server and formatting of the emails
#use Authen::NTLM;
#use MIME::QuotedPrint::Perl;

#my $authmech = "NTLM";

## Option variables
my $server = 'mail.server.com';
my $port = '993';
my $debug = '';
my $username = 'DEFAULT_ACCOUNT';
my $password = 'Can have it prompt all the time if you want';
my $msgFolder = 'inbox';
GetOptions ('debug' => \$debug, "user=s" => \$username, "msg=s" => \$msgFolder);

## If the user didn't add the option for a username prompt now
if (! defined $username)
{
    print "Enter account name: ";
    $username = ReadLine 0;
    chomp $username;
}

## If they are using the option to prompt for a password do so now.
if ($username ne "DEFAULT_ACCOUNT" || ! defined $password)
{
    print("Enter your Password: ");
    ReadMode 'noecho';
    $password = ReadLine 0;
    chomp $password;
    ReadMode 'normal';
    print "\n";
}

# If in debug mode print the account and password this can be removed.
print ("Username: $username Password $password\n\n") if  ($debug);

my $endprogram = 0;
do
{
    print("Logging in\n") if ($debug);

    ## Settings for connecting to IMAP server
    my $imap = Mail::IMAPClient->new(
        Server         => $server,
        User           => $username,
        Password       => $password,
        Port           => $port,
        Ssl            => 1,
 #       Authmechanism  => $authmech # only needed for Exchange
    ) or die "Cannot connect through IMAPClient: $@\n";
    
    ## Once authenticated access mail how you want (everything should be in this if statement)
    if ( $imap->IsAuthenticated() )
    {
        ## Check if the folder exists then select it
        $imap->exists($msgFolder) or warn "$msgFolder not found: $@\n";
        $imap->select($msgFolder) or warn('Cannot select mail folder - ', $@);
    
        ## Get the number of messages in the folder if there are any continue
        my $msgcount = $imap->message_count($msgFolder);
        print "Message Count: $msgcount\n" if ($debug);

        ## If there are any messages get the latest one for the status message
        if ($msgcount)
        {
            ## Get all the messages uID with in the folder
            my @msgs = $imap->messages or die "Could not messages: $@\n";

            ## We only care about the last message.
            my $numMsgs = @msgs;
            my $msgNum = $numMsgs - 1;

            ## Get the message ID for the latest email
            my $msgID = $msgs[$msgNum];
            print "\n===========================================================\nmsgID: $msgID\n" if ($debug);

            ## Get the body of the latest
            my $msg_date = $imap->parse_headers($msgID, "Date", "Subject")
                or die "Could not get date: ", $imap->LastError;

            ## Get the body of the latest
            my $body = $imap->body_string($msgID)
                or die "Could not body_string: ", $imap->LastError;

            print "\n===== ORIGINAL BODY =====\n$body\n\n" if ($debug);

            ## Decode the mesages body from Quoted Printable format
            $body = decode_qp($body);

            print "===== QUOTED PRINTABLE DECODED =====\n$body\n\n" if ($debug);

            ## Delete an email
            if (! $debug)
            {
                my @viewed_msgs = $imap->seen;
                scalar(@viewed_msgs) and $imap->delete_message(\@viewed_msgs)
                    or die "Could not delete_message: $@\n";

            ## Commit the changes to the server
                $imap->expunge;
            }
        }
        
        ## If in debug mode exit out of the check and program loop
        if ($debug) { $done = 1; $endprogram = 1; }
    }   
} while (! $endprogram);

## logout
$imap->logout();

Ruby

#####
##  Author: Kyle Rizzo
##  Date: 2011-10-24
##  Email: lifeforce0@gmail.com
##
##  Function: This script will create an IMAP connection to a mail server.
##              It will print out each message in the inbox.
##
##  For documentation see: http://www.ruby-doc.org/stdlib-1.8.7/libdoc/net/imap/rdoc/Net/IMAP.html
##                         http://tmail.rubyforge.org/rdoc/
##
##  System Requirements:
##      gem install highline # used for password input
##      gem install tmail    # Used to parse the RFC822 IMAP data
#####

require 'rubygems'
require 'highline/import'
require 'net/imap'
require 'tmail'

# Destination server connection info.
HOST = 'imap.gmail.com'
PORT = 993
SSL  = true
USER = 'account'
PASS = 'password'

# Create a connection to the server
imap = Net::IMAP.new(HOST, PORT, SSL)

# Login to the server with the correct account
imap.login(USER, PASS)

# Select the folder you want to work in
imap.examine('INBOX')

# Search for a subject
imap.search(["SUBJECT", "Newegg"]).each do |message_id|
    
    # Get the data for the message
    msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
    
    # Have tmail parse it nicely for us
    mail = TMail::Mail.parse(msg)
    
    # Dump everything that is part of the email.
    puts mail
end
Categories: Linux, Mail, Perl, Ruby

Installing Eagle CAD 5.11 on CentOS X64_86

2011-11-05 Leave a comment

I was having issues installing eagle cad with their simple run binary so I decided to post the commands required to install it on CentOS 6 64bit systems.

Run the following commands in a terminal as the root user.

wget ftp://ftp.cadsoft.de/eagle/program/5.11/eagle-lin-5.11.0.run

chmod 555 eagle*.run

sudo yum install libXrandr.i686 libXcursor.i686 libfontenc.i686 expat.i686 fontconfig.i686 libpng.i686 libjpeg.i686 libgcc.i686 libstdc++.i686