Archive for the ‘Ruby’ Category

Installing RVM (Ruby Version Manager) in Xubuntu 12.04 or Slackware 14

2013-01-23 Leave a comment

When I first started learning Ruby I found out about RVM which was a great project as I could easily switch between ruby versions based on what I wanted. To get RVM installed in Xubuntu (Ubuntu) 12.04 took more work then I’m normally use to with Slackware. I’ve had to install it on multiple systems with a space between each install making my memory fuzzy about what is required to get everything working. So I decided to dump my information here for later reference and who knows the next time I do it and something changes I’ll update this page. With out delay the processes can be copied and pasted below.

All the required dependencies to get it working that don’t get installed with ubuntu. I’ll create a VM where I can test to see which packages aren’t required since I was installing a few other packages I needed at the time all in the same line.

$ sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkg-config

For Slackware you can skip the dependencies since everything we need is already installed. Since packages/libraries are managed the slack way the need to disable the autolibs functionality of rvm is required. More about autolibs can be found here

Installing RVM and ruby, taken from the RVM install documentation found here.
Slackware 14

$ \curl -L | bash -s stable --ruby
$ ~/.rvm/bin/rvm autolibs 0
$ ~/.rvm/bin/rvm install ruby

*buntu 12.04

$ \curl -L | bash -s stable --ruby
$ rvm install ruby
Categories: Computers, Linux, Programming, Ruby

Text file lines manipulation.

2012-04-24 Leave a comment

A few simple little ruby scripts that will do some text file manipulation. If you couldn’t tell I was working on a project that required all these.

Convert all lines in a text file to lowercase (downcase) and save them in a new file, I could have saved them to the same file (see remove line number below) but I wanted to preserve the original file as well.

def fileToLower(inFile, outFile), 'a') do |fout| do |fin|
            fin.each_line do |line|
                fout.puts line.downcase

Remove a specific line number from a text file.

def removeLine(fileName, lineNum)
    lines = File.readlines(fileName)
    count = 1, "w") do |fout|
        lines.each do |line|
            fout.puts(line) unless count == lineNum
            count += 1

Get a single line from a text file.

def getSingleLine(fileName, lineNum) do |file|
      curLine = 1
      file.each_line do |line|
         return line if lineNum == curLine
         curLine += 1
Categories: Programming, Ruby

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 connections
openssl s_client -connect // 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.


##  Author: Kyle Rizzo
##  Date: 2011-10-24
##  Email:
##  Function: This script will create an IMAP connection to a mail server.
##  For documentation see:
##  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 = '';
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;
    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
        ## If in debug mode exit out of the check and program loop
        if ($debug) { $done = 1; $endprogram = 1; }
} while (! $endprogram);

## logout


##  Author: Kyle Rizzo
##  Date: 2011-10-24
##  Email:
##  Function: This script will create an IMAP connection to a mail server.
##              It will print out each message in the inbox.
##  For documentation see:
##  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 = ''
PORT = 993
SSL  = true
USER = 'account'
PASS = 'password'

# Create a connection to the server
imap =, PORT, SSL)

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

# Select the folder you want to work in

# Search for a subject["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
Categories: Linux, Mail, Perl, Ruby