#!/usr/bin/perl -w
# This script (sophomorix-imp-expand-fromadress) is maintained by Rüdiger Beck
# It is Free Software (License GPLv3)
# If you find errors, contact the author
# jeffbeck@web.de  or  jeffbeck@gmx.de


# Anleitung fuer ML 3.0
# 1) In /etc/horde/horde3/hooks.php folgendes eintragen:

# if (!function_exists('_prefs_hook_from_addr')) {
#     function _prefs_hook_from_addr($name = null)
#     {
#         if (is_null($name)) {
#             $name = Auth::getAuth();
#         }
#         if (!empty($name)) {
#             $cmd='sophomorix-imp-expand-from-address $name';
#             $mail=`$cmd`;
#             return (empty($mail) ? '' : $mail);
#         }
#         return '';
#     }
# }

# 2) Hook einschalten
# Folgende Zeile  zum hook 'from_addr' hinzufügen: 
#
#     'hook' => 'true',
#
# in /etc/horde/horde3/conf.php
#
# Sodass folgendes dasteht:
#
# $_prefs['from_addr'] = array(
#     'value' => '',
#     'hook' => 'true',
#     'locked' => false,
#     'shared' => true,
#    'type' => 'text',
#    'desc' =>  _("Your From: address:")
#);


# 3) Horde Datenbank-Voreinstellungen leeren (nur so wird obiges script genutzt):
# mysql
# mysql> use database horde;

# Voreinstellungen des users 'bz' entfernen
# mysql> DELETE FROM horde_prefs WHERE ((pref_name='identities' OR pref_name='from_addr') AND pref_uid='bz');

# ALLE Voreinstellungen entfernen
# mysql> DELETE FROM horde_prefs WHERE (pref_name='identities' OR pref_name='from_addr');


# Bibliotheken
use strict;

# Einlesen der Konfigurationsdatei
#require "/usr/sbin/sophomorix-lib";

my $username="";
my $file="/etc/aliases";


$username=$ARGV[0];
my $liste=$ARGV[1];


if ($username eq "--help" or
    $username eq "-h"){
   print('
sophomorix-imp-expand-from-address returns the alias name in /etc/aliases 

Options
  -h  / --help

Example:

  sophomorix-imp-expand-fromaddress login

returns i.e. firstname.surname of login from /etc/aliases 
(or login, if there is no alias)


Please see the sophomorix-workstation(8) man pages for full documentation
');
   print "\n";
   exit;

}

if(not defined $username) {
   $username="";
}
if(not defined $liste) {
   $liste="";
}


if ($liste ne "liste") {
   # Einzelwert ausgeben
   my $mail=&get_mail_alias_from($username);
   print "$mail";
} else {
   # Liste ausgeben
   my @mail=&get_mail_alias_from($username, "liste");
   print "@mail";
}





sub get_mail_alias_from {
   my ($username, $param) = @_;
   if (not defined $param) {
       $param="";
   }

   my $begin_automatisch=0;
   my @liste=();
   if ($param eq "liste") {
      # jede Zeile durchsuchen, auch manuell konfigurierte
      $begin_automatisch=1;
   }

   open(ALIASES, "<$file");
      while (<ALIASES>) {
          # Erste ab dieser Zeile beginnen
          if(/^\#\#\# Entries after here are managed by sophomorix-mail/){
              #print "Found sophomorix-mail line\n";
              $begin_automatisch=1
          }

          if ($begin_automatisch==1) {
             # wenn die Automatischen Einträge erreicht sind
             s/\s//g; # Spezialzeichen raus
             if ($_ eq ""){next;} # Wenn Zeile Leer, dann aussteigen
             if(/^\#/){next;} # Bei Kommentarzeichen aussteigen
             if(not /:/){next;} # Bei fehlendem Doppelpunkt aussteigen

             # gültige Zeilen untersuchen
             my($alias, $login)=split(/:/);

             if (not defined $username) {
               # ungültige Zeile/Listenzeile/kein Parameter übergeben
                return "";
                close(ALIASES);
                exit;
             }

             if ($login eq $username) {
                #print "$alias";
                $alias=~tr/A-Z/a-z/; # in Kleinbuchstaben umwandeln
	       if ($param eq "liste") {
                   push @liste, $alias;     
	       } else {
                   return $alias;
                   close(ALIASES);
                   exit; 
	      }
             }
          }

      }

   close(ALIASES);
   if ($param eq "liste") {
        return @liste;
    } else {
#	my $last=pop(@liste);
#	print "Last $last\n";
#        return $last;
        return $username
    }


}






