Tutorial

OfflineIMAP and Byobu hacks

October 18th, 2010  |  Published in FYI, Tutorial

Just a quick post showing a couple of hacks I have done using OfflineIMAP and Byobu on my server. I use OfflineIMAP to download my email from GMail and then use Mutt to read that email. I use Byobu on my server because I run Irssi, Mutt, and a shell, and of course Byobu makes this easy. So here we go.

OfflineIMAP
First, here is my ~/.offlineimaprc configuration:

[general]
metadata = ~/.offlineimap
accounts = GMAIL
maxsyncaccounts = 1
ui = Noninteractive.Quiet
 
[Account GMAIL]
localrepository = LocalGmail
remoterepository = RemoteGmail
 
[Repository LocalGmail]
type = Maildir
localfolders = ~/.maildb/GMAIL
#restoretime = no
 
[Repository RemoteGmail]
type = Gmail
remotehost = imap.gmail.com
remoteuser = your_gmail_login@gmail.com
remotepass = your_gmail_password
ssl = yes
realdelete = no

To fire off OfflineIMAP, I use a cronjob:

*/5 * * * * $HOME/bin/cron-run-offlineimap.sh

And my ~/bin/cron-run-offlineimap.sh looks like this:

#!/bin/sh
ps aux | grep "\/usr\/bin\/offlineimap"
if [ $? -eq "0" ]; then
    logger -i -t offlineimap "Another instance of offlineimap running. Exiting."
    exit 0
else
    logger -i -t offlineimap "Starting offlineimap..."
    chmod +x $HOME/.byobu/bin/1234_OFFLINEIMAP
    offlineimap
    logger -i -t offlineimap "Done offlineimap..."
    chmod -x $HOME/.byobu/bin/1234_OFFLINEIMAP
    exit 0
fi

You can see that this script changes the file mode bits to executable when it runs, and removes the executable bit when it finishes, on the ~/.byobu/bibn/1234_OFFLINEIMAP file which is a Byobu script.

Byobu
Here is what ~/.byobu/bin/1234_OFFLINEIMAP looks like:

#!/bin/sh
printf "\005{= rw}IMAP\005{-}"

So now every time my OfflineIMAP cronjob runs, I will get IMAP in my Byobu bar.

A super simple hack that lets me know when OfflineIMAP is running. Another reason I use this is because sometimes OfflineIMAP hangs, and when it does, I will know this if IMAP stays displayed in Byobu after a minute or so. Then I can check /var/log/syslog to see exactly when OfflineIMAP started. Normally OfflineIMAP runs for about a minute on my server every check. This could all be streamlined into one script as well with Byobu, but I know you don’t want to fire off processes or other things that may cause resource hogging.

Qemu, Qemu-KVM, VirtualBox, play nice!

March 18th, 2010  |  Published in Tutorial

Last night I was working on getting some screenshots of Kubuntu for the Karmic to Lucid upgrade documentation. I typically use Qemu for all of my virtualization needs because: a) it is convenient, b) it is fast, and c) both a and b. One thing I was noticing last night in Qemu was the default wallpaper for KDE SC 4.4 wasn’t blue, but more of a light and ugly brown color. Not Ubuntu brown, or should I say the old Ubuntu brown. What I found a bit weird with this is when I used KSnapShot to take a screenshot, the preview image in KSnapShot showed the correct color of the wallpaper, however when I opened it in Gwenview on my local machine and not in the Qemu guest, the wallpaper was brown and not blue. This drove me nuts, and it still does, because I can’t find any information on such a case. I know I can’t be the only one experiencing this?

Anyways, with the issues I was having, today I decided to go ahead and throw VirtualBox on my machine for screenshots. After getting it installed, rebooting, and doing what you have to, I went to fire up VirtualBox and was presented with the following error message when trying to start a guest OS:

Failed to start the virtual machine Kubuntu

VirtualBox can’t operate in VMX root mode. Please disable the KVM kernel extension, recompile your kernel and reboot (VERR_VMX_IN_VMX_ROOT_MODE).

First off…Really? That is the error you are presenting to the user? VERR_VMX_IN_VMX_ROOT_MODE - oh this really makes me know what the problem is, this should be easy. Did you see what I did there? Yes, I presented you with sarcasm. VirtualBox developers, do me a favor, make your error messages a bit more user friendly. Simply telling the user to “disable extension” and then “recompile kernel” is about as insane of an option for what I consider an awesome end product. FYI people, you don’t need to recompile your kernel, unless of course you compiled your kernel with the KVM module and can’t do anything about it, but I would guess for 99.9% of you, this isn’t the case. And another thing, put that VERR_VMX_IN_VMX_ROOT_MODE garbage under the Details drop down, hide that damn thing.

OK, with that off my chest, easy fix. Fire up your terminal and fire off the following:

Ubuntu or Ubuntu based distro, or distro using Upstart:

sudo stop qemu-kvm

For a non-Ubuntu based distro or distro not using Upstart:

sudo service qemu-kvm stop

And if all else fails:

sudo /etc/init.d/qemu-kvm stop

Now you can fire up VirtualBox and rock-and-roll. If you need to use Qemu again before rebooting or shutting your system down, instead of issuing stop to the examples above, use start instead. This is only a temporary work around. If you reboot your machine after stopping qemu-kvm, when your system starts back up, you will need to stop it again in order to use VirtualBox.

Now back to your regularly scheduled programming consisting of Global Jams, Kubuntu isn’t Ubuntu, To many Chefs, Marrying a coffee pot, and OMG THE BUTTONS ARE ON THE LEFT!

Freenode IRC - Connect And Auth Securely

January 30th, 2010  |  Published in FYI, Tutorial

OK, so today freenode migrated to their new server. It was a bit rough around the edges at first, however they have finally added support for connecting via SSL and using a script in Irssi you can authenticate via SASL. So, I will quickly show you how to get SSL and SASL setup for Irssi and Irssi only, and I am assuming you already have a connection to Freenode already setup.

WARNING: It has been brought to my attention that the Irssi folks get mad when people tell you to edit the config file instead of using the commands, so with that, backup your config file first, and if anything goes wrong, not my fault :)

  1. Let’s install the necessary packages (I think this is all, I already had openssl installed but had to install the libcrypt- packages for the SASL script below):
    sudo apt-get install openssl libcrypt-openssl-bignum-perl libcrypt-dh-perl libcrypt-blowfish-perl
  2. Grab and save the cap_sasl.pl script to ~/.irssi/scripts and setup a link for it to autorun:
    cd ~/.irssi/scripts
    wget http://www.freenode.net/sasl/cap_sasl.pl
    mkdir autorun  ## only if you do not have this directory already
    cd autorun
    ln -s ../cap_sasl.pl .
  3. Fire up Irssi without connecting to anything:
    irssi -!
  4. Once in Irssi, setup your username and password for SASL:
    /sasl set freenode your_nick your_password DH-BLOWFISH
    /sasl save
    /save
  5. Quit Irssi
  6. Using a text editor, edit ~/.irssi/config and in the section that says servers = ( you want to remove the stuff between the { and } for freenode, and then add the following in its place:
    address = "chat.us.freenode.net";
    chatnet = "freenode";
    port = "7000";
    use_ssl = "yes";
    ssl_verify = "yes";
    ssl_capath = "/etc/ssl/certs";
    autoconnect = "yes";
  7. Now under the chatnets = ( section, you want the freenode = part to be changed to:
    freenode = { type = "IRC"; };

If you get a message about your nick being “Juped” or “temporarily unavailable” and get switched to Guestxxxx nick, read the following, otherwise enjoy your new secure connection.

Now you can go ahead and connect to IRC like you are used to. If you have the ENFORCE flag set for your nickname, you may come across some issues with identifying, and the one message I kept getting was:

Nick nixternal is Juped

If you get this, you need to disable the ENFORCE flag on your nick (make sure you are identified with your correct nick first):

/msg nickserv set enforce off

After that, disconnect from IRC, then reconnect to IRC. You shouldn’t be getting that error message now. If you do, go to #freenode and complain accordingly :) If all is well, you can go ahead and set the ENFORCE flag back to on:

/msg nickserv set enforce on

Now all should be well. Enjoy your new secure, SSL and SASL authentication, connection.

Mark E-mails in Mutt as Tasks in Taskwarrior

January 25th, 2010  |  Published in GTD, Tutorial

OK, so I think most people know that I have grown quite fond of Taskwarrior for managing my Getting Things Done stuff. Many of you might also know that Mutt is my e-mail client of choice, for the past 15 years. Recently you saw a post on Planet Ubuntu by the rockstar Bryce Harrington concerning Mark emails in mutt as tasks in gtg. Well, I have had something similar, actually pretty much the same damn thing, just with a different GTD application. So without further ado, here is what you need to do in order to mark an email in mutt as a task in taskwarrior.

Setup Mutt Macro

macro index t "<pipe-message>mutt2task<enter> <save-message>+TODO<enter>"

What this does is set the t key, while in the index of mutt, as a macro. The macro pipes the email message to a script I wrote that strips the header from the message and adds that as a task. The save-message part saves the email to my TODO folder.

Setup Mutt2Task Script

#!/bin/sh
/usr/bin/task add +email E-mail: $(grep 'Subject' $* | awk -F: '{print $2}')

What this does is call the command task add which adds a new task. The +email tags the task, and the E-mail: $(grep ‘Subject’ $* | awk -F: ‘{print $2}’) greps the Subject line and then prints the part after Subject: from the email, therefor just giving me the subject text. Make sure you place this script somewhere in $PATH and make it executable.

Make sure you add a TODO message folder in your mutt configuration so you can see the TODO messages.

Byobu shows me next meeting

December 30th, 2009  |  Published in Tutorial

Have I ever told you all how much I love byobu? I have always used screen, though I really never tweaked it all crazy like many did. Recently I typed screen at the command line and I was presented with this thing called byobu. I went ahead and gave it a shot, and at first I will say I was rather annoyed with the bar at the bottom of my screen, and my scrollwheel didn’t work with byobu the way it did with screen. I went ahead and changed my workflow in order to get used to byobu. A couple of weeks ago, I got nosey, and wanted to know how byobu was doing its thing. After a while of messing around, and seeing everything it could display, I wanted more! And since quite a few of you on IRC wanted it, well here it is.

See, I use the cli more than I do the desktop, which is weird seeing as I am an avid KDE lover and hacker. Here is my current workflow via the command line:

  • IRC - irssi
  • Email - Mutt
  • Feedreader - Newsbeuter
  • Calendar - gcalcli
  • GTD/Todo - task

With byobu, I have it set up to automatically create 5 windows (the 4 above, plus a regular zsh shell). Since I use the command line so much, I tend to forget about meetings from time-to-time as I don’t get any warning of them, until it is either to late or I have totally missed it. So I thought, since I use the command line so much, how can I have something simple to show me the next meeting. Then I thought: I could use gcalcli to read the Fridge meeting calendar, and then have the next meeting output to the bar in byobu, so I will always see the next meeting. Currently with byobu, it isn’t the easiest thing in the world to add custom items such as this, but I have been told they are coming soon! Awesome!

So, here is what my new addition looks like:

To get this, I had to do the following:
Step 1: Create crontab task
Create a crontab task to create a file in my home directory that contains a list of meetings:

*/5  * * * * gcalcli --nc --ignore-started agenda "`date`" > $HOME/.gcal_agenda.txt

Step 2: Create a script for byobu
Create the following script (/usr/lib/byobu/gcal_agenda) and then make it executable:

#!/bin/sh
if [ "$1" = "--detail" ]; then
        head -2 /home/nixternal/.gcal_agenda.txt | tail -1
        exit 0
fi
GCAL=$(head -2 /home/nixternal/.gcal_agenda.txt | tail -1)
printf "\005{+b }%s\005{-} " "$GCAL"

Step 3: Add a tick to the common profile
In /usr/share/byobu/profiles/common, you need to add the following:

backtick 200    67      67              byobu-status gcal_agenda

Add this line right after the last backtick line you see.

Step 4: Add output to the hardstatus string
We need to add the number 200 that represents our backtick in the previous step to the hardstatus string line in $HOME/.byobu/profile. profile is a symbolic link to the current color profile you are using in bybobu, so if you ever change your theme, you will lose this setting until you add it to the next theme. Here is what my hardstatus string line looks like in $HOME/.byobu/profile:

hardstatus string '%99`%{= kw} %100`%112`%= %102`%101`%200`%127`%114`%115`%108`%128`%125`%126`%113`%119`%117`%116`%106`%104`%103`%105`%107`%123`%120`%121`'

That is all on one line. See the %200 in that line, that is our gcalcli output. That is the one we need to add in there.

Step 5: Make byobu use it
For some reason, byobu didn’t automatically pick up my new script in /usr/lib/byobu, even after reloading (F5), so I added gcal_agenda=1 to $HOME/byobu/status, did a reload (F5), and it was there.

I think this is everything, hopefully I didn’t forget anything. If you try it, and it doesn’t work, let me know.

EDIT: I am a moron, not /var/lib, but /usr/lib. I have made the changes to the post already. Also make sure you have gcalcli up and running with your calendars first. To add the fridge calendar, you subscribe to it from your Google calendar.

EDIT: I redid the last 3 steps because I totally forgot them originally. Thanks to Chris Johnston to pointing this one out!

RE: SSH Tab Complete

December 5th, 2009  |  Published in Tutorial

This is a response to SSH Tab Complete by Michael Lustfield.

Create a ~/.ssh/config file and populate it with configurations. Doing this is the only step you need to do, and you don’t need to add anything to your ~/.bashrc. Example ~/.ssh/config:

# foobar.com
Host foobar
    Hostname foobar.com
    User xxxxxxxxxxxxx
 
# Home server (internal)
Host iserver
    Hostname 10.0.0.2
    User xxxxxxxxxxxx
    Port ####

Host is a simple word that will be used with ssh like ssh foobar. Hostname is the actual IP address of domain name of the server. User is your username for that machine. Port is the ssh port number, if it isn’t the default port of 22.

So, when I want to ssh into my home server, I just do ssh is, press tab, then enter. There are many more options to add to the config file as well, and a simple Google search will provide more. Also man ssh_config will give you pretty much everything you need as well.