All pastes #767017 Raw Edit

wKK

public text v1 · immutable
·published 2007-11-09 11:57 UTC
rendered paste body
##################### post-la.sh ###############################
#!/bin/bash
#
# Written by Flavio do Carmo Junior a.k.a waKKu
# mail: carmo.flavio _AT_ gmail.com
# Original project: http://post-la.kibibit.com.br
# Original project by Henrique Bueno <henriquebueno _AT_ gmail.com>

# Carregando as confs
if [ -r /etc/post-la/post-la.conf ]; then
   . /etc/post-la/post-la.conf
else
   echo "Nao foi possivel carregar as configuracoes de /etc/post-la/post-la.conf"
   exit 1
fi

DATE="$(date -I)"
DATAFILE="$STORDIR/reports/$DATE.log"
RESUMO="$STORDIR/stats/$DATE.log"

# Definindo arquivos temporarios
TMPDIR=${TMPDIR:-/tmp}
LOG="$TMPDIR/postlog$$.tmp"
IDSIZE="$TMPDIR/postidsize$$.tmp"

function relatorio()
{
      # Consigo ler o log ?
      if [ ! -r $LOGFILE ]; then
              echo "Arquivo de log incorreto - $LOGFILE"
              exit 2
      fi

      # Limpando o log
      sed "/\(delivered\ to\ m\|queue\ active\|$TAG$\)/!d" $LOGFILE > $LOG

      # Criando um arquivo com os ID's das mensagens e respectivos tamanhos
      awk '$8~/^size=/{SIZE=gensub(/[^0-9]/,"","G",$8)/1024; printf "%s%.2f\n", $6,SIZE}' $LOG | sort -u > $IDSIZE

      # Filtrando o log
      sed "/$TAG$/!d" $LOG > $LOG.tmp
      mv $LOG.tmp $LOG

      # Apagando o arquivo de hoje, para evitar duplicacao
      rm -f $DATAFILE

      # Gerando o arquivo de dados
      while IFS=$: read ID SIZE; do
         #awk -v SIZE="${SIZE//,/.}Kb" '{print DT=$2"/"$1"|TM="$3"|SB="gensub(/.*Subject: (.*) from.*from.*$/,"\\1",1)"|FR="gensub(/^.*from=<([[:alnum:]@._-]+)>.*$/,"\\1",1)"|TO="gensub(/^.*to=<([[:alnum:]@._-]+)>.*$/,"\\1",1)"|SZ="SIZE}' <(sed -n "/$ID.*$TAG/{p;q}" $LOG)

         # Teste com sed
         #LINHA="$(sed -n "/$ID.*$TAG$/{p;Q}" $LOG)"
         LINHA="$(ed -s $LOG <<<"/${ID}.*${TAG}$/"$'p\nd\nw\nq' 2>&-)"
         LINHA2="$(sed -n "s/\(...\)\ \([0-9]\{1,2\}\)\ \([0-9:]\+\).*Subject:\(.*\)\ from\ .*from=<\([0-9A-Za-z.@=\+\*_-]*\)>.*$/DT=\2\/\1\|TM=\3\|SB=\4\|FR=\5\|TO=/p" <<< $LINHA)"
         END="${LINHA2}$(sed -n "s/^.*to=<\([0-9A-Za-z._=-@]*\)>.*$/\1\|SZ=${SIZE}Kb/p" <<< $LINHA)"
         [ ! -z "$END" ] && echo "$END" >> $DATAFILE.tmp

      done < $IDSIZE
      rm -f $LOG $IDSIZE

      # Removendo os caracteres malucos do relatorio se houver PHP
      if type -a php >/dev/null 2>&1; then
         php -f /usr/libexec/post-la/convert.php $DATAFILE.tmp > $DATAFILE
         rm -f $DATAFILE.tmp
      else
         mv $DATAFILE.tmp $DATAFILE
      fi

      sed '/^DT=/!d;s/\%/porcento/g' $DATAFILE > $DATAFILE.tmp
      mv $DATAFILE.tmp $DATAFILE

      # Acertando as permissoes para que o apache possa ler o arquivo
      chown $WWWUSER.root $DATAFILE
      chmod 0400 $DATAFILE

      echo "Reports generated successfully."
}

function resumo()
{
      CONTASTMP="$TMPDIR/postcontas$$.tmp"
      # pega contas que enviaram e-mails
      DOMINIOS="@$(paste -d# -s /etc/post-la/domains)"
      DOMINIOS="$(sed 's/#/|@/g' <<< $DOMINIOS)"
      awk -F'|' '{print gensub(/fr=/,"",1,tolower($4))}' $DATAFILE >> $CONTASTMP
      awk -F'|' '{print gensub(/to=/,"",1,tolower($5))}' $DATAFILE >> $CONTASTMP
      grep -E "$DOMINIOS" $CONTASTMP | sort -bfu > $CONTASTMP.tmp
      mv $CONTASTMP.tmp $CONTASTMP

      rm -f $RESUMO
      while read EMAIL; do
         TMRECEBIDOS="$(awk -v M="TO=${EMAIL}" -F'|' 'BEGIN {SOMA=0; COUNT=0;} $5==M {SOMA=SOMA+gensub(/[^0-9.,]/,"","g",$6); COUNT++} END {print COUNT"|"SOMA}' $DATAFILE )"
         TMENVIADOS="$(awk -v M="FR=${EMAIL}" -F'|' 'BEGIN {SOMA=0; COUNT=0;} $4==M {SOMA=SOMA+gensub(/[^0-9.,]/,"","g",$6); COUNT++} END {print COUNT"|"SOMA}' $DATAFILE )"
         echo "$EMAIL|$TMENVIADOS|$TMRECEBIDOS" >> $RESUMO
      done < $CONTASTMP
      rm -f $CONTASTMP

      # Acertando as permissoes para que o apache possa ler o arquivo
      chown ${WWWUSER}.root $RESUMO
      chmod 0400 $RESUMO

      echo "Statistics generated successfully."
}

case "$1" in
      resumo)
         echo "Generating statistics"
         resumo
         ;;
      relatorio)
         echo "Generating reports"
         relatorio
         ;;
      *)
         echo "Generating reports and statistics"
         relatorio
         resumo
         ;;
esac

# vim: ts=3
##################### post-la.sh ###############################

##################### convert.php ##############################
<?php

if ($argc != 2) {
        print "Usage: php -f $argv[0] <file>\n";
        die;
}

if ($f = fopen($argv[1], 'r')) do {
        $line = fgets($f);
        $elements = imap_mime_header_decode($line);
        for ($i=0; $i<count($elements); $i++) {
                echo "{$elements[$i]->text}";
        }

} while (!feof($f));

fclose($f);

?>
#################### convert.php #######################