#! /usr/bin/perl
#
# mutt_query.pl - provides the addresses of your palm for mutt(1)
# $Id: mutt_query.pl,v 1.5 2004/03/21 12:35:07 hotti Exp $
#
# This script reads the output from the addresses command (out of the
# pilot-link package) and converts the records with an "E-mail"-field
# to a format readable by mutt(1). Called without any argument, the
# script writes an alias-line for each record. The output can directly
# be sourced in your .muttrc. Called with a regexp, the output only
# consists of the matching records and is in the suitable format for
# mutt's query function.
#
# If you didn't understand all of the above, just add following lines
# to your .muttrc:
#
#   set query_command = "~/bin/mutt_query.pl '%s'"
#   source "$HOME/bin/mutt_query.pl|"
#   set alias_file=~/.aliases	# for new aliases
#   source "$HOME/.aliases"
#
# Then type "Q" in index mode or C-T in address input mode.
#
# Tip: Define a field in your address-db as "Nickname". The query will
#   apply to the nicknames then.
#
# Copyright (c) Daniel Hottinger <hodaniel@student.ethz.ch>
# Released under the terms of the GNU General Public License v2 or later
# 
# Created: Wed Oct 03 21:17:42 CEST 2001
# Last modified: Tue Mar 16 18:29:11 CET 2004
# TODO:
#   - Query: g=ETH -> all in group ETH
#   - Use LDAP?
#

use strict;
use warnings;

# ======================================================================
# Config section
# ======================================================================

my %cfg;
$cfg{addresses}   = $ENV{HOME} . "/include/addresses";

# ======================================================================
# Read config
# ======================================================================

my @data = ();
my %record = ();

open(ADDR, "<$cfg{addresses}") || die $!;
while(<ADDR>)
{
  chomp;
  next if /^\s*$/;

  if ( /^Category:/ ) {
    push(@data, { %record }) if %record and defined $record{"E-mail"};
    %record = ();
  }

  my ($key, $val) = m/([^:]+):\s+(.*)/;
  if ((defined $key) and (defined $val)) {
    if ($key !~ /E-mail/) {
      $record{$key} = $val if (! defined $record{$key});
    }
    else {
      $record{$key} = [] unless defined $record{$key};
      push @{$record{$key}}, $val;
    }
  }
}
close(ADDR);

# ======================================================================
# Do a query if $ARGV[0] is given; print out aliases else
# ======================================================================

if ( defined $ARGV[0] ) {
  print "Searching for " . $ARGV[0] . " in " . $#data . " records...\n";
}

for ( @data ) {

  my $name = "";
  $name = $_->{Vorname} if defined $_->{Vorname};
  $name .= " " . $_->{Nachname} if defined $_->{Nachname};
  $name = $_->{Firma} unless defined $_->{Vorname} or defined $_->{Nachname};
  $name =~ s/^\s+//;

  my $nick = $_->{Nickname} || $name;
  $nick =~ s/\s+/_/g;

  my $email = "";
  my @mails = @{$_->{"E-mail"}};
  for ( @mails ) {
    $email = $_;
    $nick =~ s/_.*$//;
    $nick .= "_" . ($email =~ m/.*@(.*)/)[0] if @mails > 1;
    if ( defined $ARGV[0] ) {
      print $email, "\t", $name, "\t", $nick, "\n" 
	if $nick =~ m/$ARGV[0]/io || $name =~ m/$ARGV[0]/io;
    } else {
      print "alias $nick $name <$email>\n";
    }
  }
}
 
