<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<?xml-stylesheet type="text/css" href="http://www.theoldmonk.net/blog/styles/feed.css"?>
<title type="html">hacks</title>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog" />
<link rel="self" type="application/atom+xml" href="http://www.theoldmonk.net/blog/archives/hacks/index-atom.xml" />
<updated>2008-09-27T19:11:09+05:30</updated>
<author>
<name>gera</name>
<uri>http://www.theoldmonk.net/blog</uri>
</author>
<id>http://www.theoldmonk.net/blog/</id>
<generator uri="http://nanoblogger.sourceforge.net" version="3.3">NanoBlogger</generator>
<entry>
<title type="html">Trac, MySQL and authentication</title>
<author>
<name>gera</name>
</author>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog/archives/2008/09/27/trac_mysql_and_authentication/" />
<id>http://www.theoldmonk.net/blog/archives/2008/09/27/trac_mysql_and_authentication/</id>
<published>2008-09-27T19:06:10+05:30</published>
<updated>2008-09-27T19:06:10+05:30</updated>
<category term="tricks" />
<category term="technology" />
<category term="hacks" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[What we needed at <a href="http://www.chaupaati.in">work</a> :
<ul>
 <li> A wiki</li>
 <li> VPN</li>
 <li> A bug/issue tracker</li>
</ul>
<a href="http://trac.edgewall.org/">Trac</a> gave us the first and the last, and <a href="http://openvpn.net/">OpenVPN</a> gave us the middle. The newer versions of Trac can use MySQL as a database instead of SQLite and have a nice <a href="http://trac-hacks.org/wiki/AccountManagerPlugin">AccountManager</a> plugin.
<br /><br />
And it all comes together with <a href="http://pam-mysql.sourceforge.net/">pam_mysql</a>. The only problem was that Trac likes to store passwords hashed as HTTP-Digest style hashes (not the most secure, I know) with an empty realm, and pam_mysql doesn't work for that. The solution was a simple patch to pam_mysql which adds support for such hashes. I'll publish that soon - I'm making it more general by adding support for realms rather than assuming that realm would be empty.
<br /><br />
OpenVPN (and apache - via mod_auth_pam) etc. can authenticate via <a href="http://www.kernel.org/pub/linux/libs/pam/">PAM</a> and adding authentication to anything is a simple matter of placing the right .htaccess file. And this is a *common* password across all services, which users can change on the wiki, in their browsers, with a nice polished interface. No unix shells for people who can't deal with them.
<br /><br />
This is no <a href="http://en.wikipedia.org/wiki/Kerberos_(protocol)">Kerberos</a>, but is infinitely simpler - which means a lot for a small startup.
<br /><br />
There are a few gotchas though :
<ul>
<li> Apache doesn't like empty realms. This can be worked around by switching to Basic authentication where passwords are passed on to PAM and we ignore realm. Yes, Basic is worse, but there's always SSL. Thus, one can supply _any_ value of realm (the AuthName in .htaccess). The situation might improve if my pam_sql patch accepts realms and we manage to modify the Trac AuthManager plugin to use the same realm.</li>
<li> SSH doesn't like to authenticate users which don't have an account on the machine. It does a getpwent() call, and if it fails, it sets the password to '^H ^M INCORRECT' (the ^H and ^M represent the control chars here, but this is what you see in your logs if you debug PAM). One (ugly, in my opinion) way out is to LD_PRELOAD your own getpwent() method for sshd. The other is to grant ssh access to people on a machine by machine basis. This doesn't scale beyond a point but is good enough for small setups and the most secure. We use this. The cleaner solution of course, is to use something like <a href="http://savannah.nongnu.org/projects/nss-mysql">nss_mysql</a>. This might be our next step, but not in the immediate future.</li>
</ul>
Patch to come Real Soon Now.]]>
</div>
</content>
</entry>
<entry>
<title type="html">Location aware GTD trick</title>
<author>
<name>gera</name>
</author>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog/archives/2008/07/05/location_aware_gtd_trick/" />
<id>http://www.theoldmonk.net/blog/archives/2008/07/05/location_aware_gtd_trick/</id>
<published>2008-07-05T12:20:17+05:30</published>
<updated>2008-07-05T12:20:17+05:30</updated>
<category term="tricks" />
<category term="hacks" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[I have my <a href="http://www.theoldmonk.net/blog/archives/2008/01/28/commandline_gtd_with_gtdo/">gtdo</a> to manage the TODOs, and it shows me the list every time I open a terminal (via a 't ls' in my .bashrc). But what to do when my '@work' TODO list grows long and is shown to me on every terminal spawn, even at home?
<br /><br />
what I also have is my <a href="http://www.theoldmonk.net/blog/archives/2008/03/04/network_profiles_in_ubuntu/">network profiles setup</a>. Till today, my office profile was called "office" - which I changed to "work". This makes my profile name the same as my context name ('work' and '@work' - we can manage the @ in the context). Do I need to say more? :)
<br /><br />
Every time I do a 'NETSCHEME="work" sudo ifup ath0' and the network comes up, the '/var/run/network/ifstate' contains a line like 'ath0=ath0-home'. Which means, I can get the profile I'm using. Which means, if I name my profiles carefully, I can get the location I'm at. Which then means, if I name my contexts carefully, I can get the relevant context.
<br /><br />
Adding this:
<pre>
LOCATION=`grep -m 1 '-' /var/run/network/ifstate | awk -F '-' '{ print $2 }'`
t ls @$LOCATION
</pre>
to my .bashrc works like magic.
<br /><br />
By the way, I *could* scan for known SSIDs and select a network (and hence a location and a context) automagically, but I don't do that yet - one of the reasons being that I *think* there might be an information leak hidden somewhere. But I haven't thought it through. But I might, and then I might get around to doing it.]]>
</div>
</content>
</entry>
<entry>
<title type="html">Being too smart is annoying</title>
<author>
<name>gera</name>
</author>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog/archives/2008/06/25/being_too_smart_is_annoying/" />
<id>http://www.theoldmonk.net/blog/archives/2008/06/25/being_too_smart_is_annoying/</id>
<published>2008-06-25T19:19:20+05:30</published>
<updated>2008-06-25T19:19:20+05:30</updated>
<category term="tricks" />
<category term="web" />
<category term="hacks" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[Some people have been saying that Firefox tries to emulate other browsers and be Windows-like in general. Some people have been saying that about Linux/Ubuntu as well. I don't know how correct they are and what they're based on, but there's one thing that comes in the category of being too smart and too user friendly which ends up annoying the regulars.
<br /><br />
I was on my machine and wanted to run a remote instance on Firefox, with the display forwarded back to my machine. I had X forwarding over SSH and my DISPLAY enviroment variable all set up. But when I started 'firefox' on the remote commandline, it opened up an instance of firefox *locally*. WTF!!?
<br /><br />
The <a href="http://linux.derkeiler.com/Mailing-Lists/Fedora/2007-10/msg04000.html">fix</a> to this is a 'MOZ_NO_REMOTE' env variable. Duh.]]>
</div>
</content>
</entry>
<entry>
<title type="html">Git-Bugzilla integration</title>
<author>
<name>gera</name>
</author>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog/archives/2008/03/08/git-bugzilla_integration/" />
<id>http://www.theoldmonk.net/blog/archives/2008/03/08/git-bugzilla_integration/</id>
<published>2008-03-08T19:47:35+05:30</published>
<updated>2008-03-08T19:47:35+05:30</updated>
<category term="tricks" />
<category term="perl" />
<category term="code" />
<category term="hacks" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p>There's always <a href="http://www.mkgnu.net/?q=scmbug">SCMBug</a>, but you can cook your own Git-Bugzilla integration very easily. SCMBug's fine, but it's a lot of code if all you want is simple cross-linking between Git, Bugzilla and <a href="http://trac.edgewall.org/">Trac</a> (btw - <a href="http://trac-hacks.org/wiki/GitPlugin">GitPlugin</a> for Trac would throw up an error unless you have at least *two* commits in your repository - talk about undocumented easter bugs!).</p>

<p>Anyhoo - here's what I want : <br />
1. Git should disallow any commit where the commit message does not have a bug number. <br />
2. Git should add a comment to the corresponding bug on a commit, mentioning the author, the Trac changeset link, the commit message and the list of files which changed.</p>

<p>And here's the code to do it. First, the post-receive hook :</p>
<pre>
#!/usr/bin/perl -w
use strict;

# A hook script which integrates with bugzilla. It looks for bug IDs in
# commit messages and adds the commit message as well as a link to the
# changeset as a comment on the bug.

# This program is released under the terms of the GNU General Public License
# version 2. A copy of the license may be obtained by emailing the author,
# or at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#
# The absolute lack of warranty and other disclaimers as per the license
# apply.
#
# Copyright 2008, Devendra Gera. All rights reserved.
#
# Author : Devendra Gera <gera@theoldmonk.net>

### user configurable section

# The bugzilla has contains the server, username and password for the targeted
# bugzilla installation. There's NO 'http://' in the server line.
my $bugzilla = {
        server          => "10.147.251.90/bugzilla",
        user            => "demonuser",
        password        => "daemonuserspassword",
};

# __PATH__ and __REVISION__ are replaced in $browser->{changeset} and
# $browser->{revision} to get the changeset and revision URLs respectively. 
my $browser = {
        changeset       =>
        "http://10.147.251.90/projname/changeset/__REVISION__",
        revision        =>
        "http://10.147.251.90/projname/browser/__PATH__?rev=__REVISION__",
};

# The bug_regex should extract the bug id from the commit message and place
# it in $1
my $bug_regex = 'bug #(\d+)';


##### End user configurable section

use WWW::Bugzilla;

my $input = <>;
chomp $input;

my ($oldrev, $newrev, $refname) = split /\s+/, $input;
my $commit_msg = `git-whatchanged $oldrev..$newrev`;

# prepare the changeset URL
my $changeset_url = $browser->{ changeset };
$changeset_url =~ s/__REVISION__/$newrev/g;

# author
my ($author) = ( $commit_msg =~ /^Author:\s+(.*)$/m );

# files
my @filelist = grep ( /^:/, split( /\n/, $commit_msg ) );

# prepare comment
$commit_msg =~ s/^.*?Date://s;  # eat everything till the Date: heder
$commit_msg =~ s/^.*?\n//m;     # eat the date line completely
$commit_msg =~ s/^:.*?$//mg;    # eat the file list from the msg.
chomp $commit_msg;
my ($bug_number) = ( $commit_msg =~ /$bug_regex/ );

my $comment = &lt;&lt;END_COMMENT;

------------------------------------
changeset $newrev [ $changeset_url ]
    by $author :

$commit_msg
------------------------------------

Files changed :
END_COMMENT

$comment .= join("", @filelist) . "\n";

my $bz = WWW::Bugzilla->new(
        server          => $bugzilla->{ server },
        email           => $bugzilla->{ user },
        password        => $bugzilla->{ password },
        bug_number      => $bug_number
);

die "cannot connect to bugzilla" unless defined $bz;

$bz->additional_comments( $comment );

$bz->commit;
</pre>

<p>And here's the update hook :</p>
<pre>
#!/usr/bin/perl -w
use strict;

my $refname = shift;
my $oldrev = shift;
my $newrev = shift;

my $commit_msg = `git-whatchanged $oldrev..$newrev`;

# check if the commit message contains a bug number
if($commit_msg !~ /bug #\d+/) {
        exit -1;
}

exit 0;
</pre>

<p>The TODO : <br />
1. Push every configurable thing to git's config file and access it via git-config. <br />
2. Code cleanups. One definition of the bug regex (which is hardcoded in the update for now).</p>

<p><em>edit</em> : the here doc in the code was causing a problem with formatting, eating up some of the code. Fixed now.</p>]]>
</div>
</content>
</entry>
<entry>
<title type="html">WWW::Bugzilla fix</title>
<author>
<name>gera</name>
</author>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog/archives/2008/03/08/wwwbugzilla_fix/" />
<id>http://www.theoldmonk.net/blog/archives/2008/03/08/wwwbugzilla_fix/</id>
<published>2008-03-08T18:45:35+05:30</published>
<updated>2008-03-08T18:45:35+05:30</updated>
<category term="perl" />
<category term="hacks" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<a href="http://search.cpan.org/~bmc/WWW-Bugzilla-1.3/WWW/Bugzilla.pm">WWW::Bugzilla</a> was a great help in setting up git-bugzilla integration, but there's a small fix which needs to be applied before it would work with my Bugzilla 3 install.
<br /><br />
The problem is that WWW::Mechanize selects the first form on a page by default, and WWW::Bugzilla fails in WWW::Mechanize while setting any field to be updated (with a 'no such field' or 'no field called comment' etc. messages). On my Bugzilla install, there's a small bug search form in the header which makes that happen.
<br /><br />
The fix is a single line and has been emailed to the author. Here it is :
<pre>
@@ -614,6 +614,7 @@
     my $mech = $self->{mech};
 
     if ($self->{bug_number}) {
+        $mech->form_name( "changeform" );
         foreach my $field ( keys %update_field_map ) {
             $mech->field( $update_field_map{$field}, $self->{$field} ) if defined($self->{$field});
             # handle special cases
</pre>]]>
</div>
</content>
</entry>
<entry>
<title type="html">Network profiles in Ubuntu</title>
<author>
<name>gera</name>
</author>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog/archives/2008/03/04/network_profiles_in_ubuntu/" />
<id>http://www.theoldmonk.net/blog/archives/2008/03/04/network_profiles_in_ubuntu/</id>
<published>2008-03-04T22:03:31+05:30</published>
<updated>2008-03-04T22:03:31+05:30</updated>
<category term="tricks" />
<category term="technology" />
<category term="perl" />
<category term="hacks" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[<p>There are various ways of managing multiple network profiles in Ubuntu, but I've never been a fan of NetworkManager. Commandlines work for me very well, and even there - multiple solutions exist with the help of packages like resolvconf etc. Here's my setup which is very Debian-ish and depends on this nice package called <a href="http://packages.ubuntu.com/gutsy/ifupdown">ifupdown</a>.</p>

<p>First, there's the /etc/network/interfaces file :</p>
<pre>
# we always want the loopback
auto lo
iface lo inet loopback

# mappings
mapping eth0
  script /etc/network/map-scheme
  map dhcp eth0-dhcp
  map emergency eth0-emergency

mapping ath0
  script /etc/network/map-scheme
  map office ath0-office
  map home ath0-home

iface eth0-dhcp inet dhcp
  up iptables -F
  up lokkit -n -q --high --dhcp
  up /etc/init.d/lokkit restart

iface ath0-office inet dhcp
  wpa-driver madwifi
  wpa-conf /etc/wpa_supplicant/office.conf
  up iptables -F
  up lokkit -n -q --high --dhcp
  up /etc/init.d/lokkit restart

iface eth0-emergency inet static
  address 10.9.5.201
  gateway 10.9.4.1
  netmask 255.255.254.0
  up iptables -F
  up lokkit -q --high
  up echo nameserver 172.31.6.5 > /etc/resolv.conf
  up echo nameserver 203.197.12.30 >> /etc/resolv.conf

iface ath0-home inet dhcp
  wpa-driver madwifi
  wpa-conf /etc/wpa_supplicant/home.conf
  up iptables -F
  up lokkit -n -q --high --dhcp
  up /etc/init.d/lokkit restart
</pre>

<p>Notice the mappings section (and see 'man interfaces') - that allows me to say :</p>
<pre>
NETSCHEME="home" sudo ifup ath0
</pre>

<p>or</p>
<pre>
NETSCHEME="office" sudo ifup ath0
</pre>

<p>because the specified script (/etc/network/map-scheme) just looks up the NETSCHEME environment variable and spit out the correct mapping to go to. This thing, by the way, could be rigged to do arbitrarily complex tasks (look in /usr/share/doc/ifupdown/examples/ for sample scripts, including one which tries to ping some known IPs, and decides its location/profile based on successful pings - you could write one which looks for all known wireless SSIDs and then decide which profile to switch to). Here's my trivial script :</p>
<pre>
#!/usr/bin/perl -w
use strict;

my $scheme = $ENV{NETSCHEME} || "home";

while(<>) {
        if ( s/$scheme\s+// ) {
                print;
        }
}
</pre>

<p>The conf files in /etc/wpa_supplicant/* are of course wpa_supplicant configuration files. See 'man wpa_supplicant.conf' for details.</p>]]>
</div>
</content>
</entry>
<entry>
<title type="html">Eee PC!</title>
<author>
<name>gera</name>
</author>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog/archives/2008/02/06/eee_pc/" />
<id>http://www.theoldmonk.net/blog/archives/2008/02/06/eee_pc/</id>
<published>2008-02-06T23:29:09+05:30</published>
<updated>2008-02-06T23:29:09+05:30</updated>
<category term="tricks" />
<category term="hacks" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[My Eee PC finally arrived. I played with the original Xandros/Asus interface for a day, and then moved to Ubuntu. My <a href="http://www.theoldmonk.net/blog/archives/2007/12/01/encrypted_home_setup/">encrypted $HOME howto</a> worked, but I kept /real_home on a SD card and added "noatime" to every FS mount.
<br /><br />
One fallout is that I can't suspend - the SD card is a USB device and gets reset on suspend/resume. Also, the /var/log, /var/run, /var/tmp and /tmp are tmpfs filesystems. /var/cache/apt/ is a bind mount from the SD card to save the SSD from excessive writes.
<br /><br />
<a href="http://www.flickr.com/photos/gera/2245645565/" title="Eee! by Gera, on Flickr"><img src="http://farm3.static.flickr.com/2305/2245645565_f1174d9d9a_m.jpg" width="240" height="157" alt="Eee!" /></a>
<br /><br />
Processing RAW images from my camera was a problem till I swapped the RAM with one of the modules from my Vostro 1400. GIMP works pretty smoothly now.]]>
</div>
</content>
</entry>
<entry>
<title type="html">Commandline GTD with gtdo</title>
<author>
<name>gera</name>
</author>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog/archives/2008/01/28/commandline_gtd_with_gtdo/" />
<id>http://www.theoldmonk.net/blog/archives/2008/01/28/commandline_gtd_with_gtdo/</id>
<published>2008-01-28T15:17:57+05:30</published>
<updated>2008-01-28T15:17:57+05:30</updated>
<category term="tricks" />
<category term="perl" />
<category term="code" />
<category term="hacks" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[I was introduced to <a href="http://en.wikipedia.org/wiki/Getting_Things_Done">GTD</a> around a year ago, but wanted to manage mine using a commandline. I discovered <a href="http://www.todotxt.com/">todotxt.com</a> and modified a <a href="http://www.todotxt.com/download.php?p=%2Flibrary%2Ftodo.sh%2F&f=todo.py">todo.py</a> to suit my needs (removed priorities, added some commands, bash-completion etc).
<br /><br />
But I stopped doing it.
<br /><br />
Because I had nothing to manage my "projects" (tasks with multiple action items). I maintained a list of projects out-of-band, as a directory structure, which required me to identify the next task for a project and move it to my todo list. Of course it didn't work!
<br /><br />
So I wrote gtdo. gtdo has a similar interface - todo.py was the inspiration - but in addition to the contexts (marked by a '@'), it supports groups (or "projects" - marked by a '/'). So, you might have a project about starting using GNUcash. Of course, the first step is to install/set-up GNUcash. Adding your portfolio comes later. If you have both these items in your todo list, the second one does nothing but adds noise and makes the list longer. The answer is to add them in a group /gnucash. If there are multiple tasks in a group, only the first one is displayed - till its marked done. By default, new tasks are added at the end of the queue in a group, but may be added at a specific position.
<br /><br />
An example would be better than the ramble above (gtdo being aliased to 't'):
<pre>
$ t add /gnucash @online install gnucash
$ t add /frames @errands buy wood
$ t add /email @online set up spamassassin
$ t add /gnucash @online add portfolio to gnucash
<br /><br />
# only the immediate next task is displayed when you 'ls'
$ t ls
1 : /gnucash @online install gnucash
2 : /frames @errands buy wood
3 : /email @online set up spamassassin
<br /><br />
$ t ls @online
1 : /gnucash @online install gnucash
3 : /email @online set up spamassassin
<br /><br />
# except when you specifically ask for a group
$ t ls /gnucash
1 : /gnucash @online install gnucash
4 : /gnucash @online add portfolio to gnucash
<br /><br />
# well, we need to fix F::Q::IndiaMutual before we add our portfolio
# we'll try adding that as the second step in the /gnucash group
$ t add /gnucash.2 fix F::Q::IndiaMutual
<br /><br />
$ t ls /gnucash
1 : /gnucash @online install gnucash
4 : /gnucash fix F::Q::IndiaMutual
5 : /gnucash @online add portfolio to gnucash
</pre>
Comes with standard todo.sh style bash-completion. The Git repository is available at <a href="http://repo.or.cz/w/gtdo.git">http://repo.or.cz/w/gtdo.git</a>. You can obtain a tarball by clicking <a href="http://repo.or.cz/w/gtdo.git?a=snapshot;h=HEAD;sf=tgz">here</a>.]]>
</div>
</content>
</entry>
<entry>
<title type="html">mutt addressbook collector</title>
<author>
<name>gera</name>
</author>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog/archives/2008/01/18/mutt_addressbook_collector/" />
<id>http://www.theoldmonk.net/blog/archives/2008/01/18/mutt_addressbook_collector/</id>
<published>2008-01-18T15:13:02+05:30</published>
<updated>2008-01-18T15:13:02+05:30</updated>
<category term="tricks" />
<category term="perl" />
<category term="code" />
<category term="hacks" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[I have a tiny helper script to collect addresses from my outgoing emails, and add them to my .aliases file. I only care about people who I send out email to, and I've noticed that collecting addresses from incoming emails leads to too much noise. Although I could set it up to *not* pick up addresses from spam and mails which have my firstname (ecommerce stuff which is NOT spam). But that's for later.

<p />
Here's the helper (I use msmtp for sending out email as evindent in my <a href="http://www.theoldmonk.net/blog/archives/2008/01/16/email_setup/">mutt config</a>) :
<p />
<pre>
#!/usr/bin/perl -w
use strict;

my $home = "/home/gera";
my $msmtp = "/usr/bin/msmtp";
my $aliasfile = "/home/gera/.aliases";

open ALIASES, "<$aliasfile" or quit("cannot open $aliasfile : $!");
my %aliases = ();
my %collected = ();
while(<ALIASES>) {
        s/#.*$//;
        chomp;
        next unless $_;
        my ($nick, $name, $email) = ($_ =~ /^alias\t+(\S+)\t+(.+?)\t+<(\S+)>/);
        quit("cannot parse $_") unless ($nick && $name && $email);
        $aliases{$email} = {
                "nick"  => $nick,
                "name"  => $name,
        };
}
close ALIASES;

# now read the email contents and try to figure out the addresses
local $/ = undef;       # slurp mode

my $email = <STDIN>;
my ($to) = ($email =~ /^To:(.*)$/m);
my ($cc) = ($email =~ /^Cc:(.*)$/m);
my @addresses = split( /,/, "$to,$cc");
foreach my $address (@addresses) {
        chomp;
        next unless $address;
        # the address is of the form of "Firstname Lastname <email@address>"
        # or "<email@address>" or "email@address"
        my ($name, undef, $email) = ($address =~ /^(.*?)\s*(<|)([^<>]*?)(>|)$/);
        quit("cannot parse $address") unless $email;
        next if exists $aliases{$email};
        # else, add it to the aliases.
        # See if everything is populated. We'll have to invent a nickname here
        if(! $name) {
                ($name = $email) =~ s/\@.*//;
        }
        my $nick;
        ($nick = lc $name) =~ tr/ //d;
        $collected{$email} = {
                "nick"  => $nick,
                "name"  => $name,
        };
}

if(scalar(keys %collected) != 0) {
        # save to tmp file
        open ALIASES, ">$aliasfile.new.$$" or quit("cannot open $aliasfile.new.$$ : $!");
        foreach my $email (sort keys %aliases) {
                my $nick = $aliases{$email}->{nick};
                my $name = $aliases{$email}->{name};
                print ALIASES "alias    $nick   $name   <$email>\n";
        }
        print ALIASES "\n\n#collected email addresses\n\n";
        foreach my $email (sort keys %collected) {
                my $nick = $collected{$email}->{nick};
                my $name = $collected{$email}->{name};
                print ALIASES "alias    $nick   $name   <$email>\n";
        }
        close ALIASES;

        # move tmp file to $aliasfile
        rename("$aliasfile.new.$$", $aliasfile);
}

# send mail onward
open(MSMTP, "|$msmtp @ARGV") or quit("cannot open pipe to $msmtp @ARGV : $!");
print MSMTP $email;
close(MSMTP) or quit("cannot close pipe to MSMTP : $!");

exit(0);


sub quit
{
        my $msg = shift;
        open LOGFILE, ">$home/.msmtp_helper.log" || die "cannot open log file : $!";
        print LOGFILE "$msg\n";
        close LOGFILE;
        exit(127);
}
</pre>
<p />
Edit : fixed formatting.]]>
</div>
</content>
</entry>
<entry>
<title type="html">Finance::Quote::IndiaMutual bugfix</title>
<author>
<name>gera</name>
</author>
<link rel="alternate" type="text/html" href="http://www.theoldmonk.net/blog/archives/2008/01/17/financequoteindiamutual_bugfix/" />
<id>http://www.theoldmonk.net/blog/archives/2008/01/17/financequoteindiamutual_bugfix/</id>
<published>2008-01-17T22:25:39+05:30</published>
<updated>2008-01-17T22:25:39+05:30</updated>
<category term="perl" />
<category term="code" />
<category term="hacks" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<![CDATA[I used GnuCash for a while and maybe I'll start using it again. A neat thing that one can do with it is to track one's portfolio. Stocks are easy (GnuCash can use Finance::Quote::Yahoo where NSE stocks have symbols ending in '.NS', and BSE stocks are numbers). Mutual funds in India do not have ticker symbols, but prices/NAV etc. can be obtained via the <a href="http://search.cpan.org/~hampton/Finance-Quote-1.13/lib/Finance/Quote/IndiaMutual.pm">Finance::Quote::IndiaMutual</a> module, which fetches them from the <a href="http://amfiindia.com/">AMFI</a> website.
<br /><br />
To use the module, one can grab the code corresponding to the fund from the table at <a href="http://amfiindia.com/downloadnavopen.asp">http://amfiindia.com/downloadnavopen.asp</a> and supply that to the module.
<br /><br />
All that is fine, but there's a tiny regex bug in the module which chokes on many rows. The fix is trivially simple and the diff totals to 
<pre>
74c74
<       my ($symbol, @data) = split /\;/;
---
>       my ($symbol, @data) = split /\s*\;\s*/;
</pre>
I hope that helps someone.]]>
</div>
</content>
</entry>
</feed>
