Changes between Version 2 and Version 3 of Scripts/EmailNotifier


Ignore:
Timestamp:
Jan 2, 2011, 11:15:49 PM (11 years ago)
Author:
charles
Comment:

updated as requested by gaa in ticket #3869

Legend:

Unmodified
Added
Removed
Modified
  • Scripts/EmailNotifier

    v2 v3  
    77== Script ==
    88{{{
    9 #!sh
    109#!/bin/sh
    1110
    1211# Script that checks for finished downloads in Transmission and
    1312# sends email to a specified user.
    14 # This code placed into public domain
     13# Author: Alexander Galanin <al@galanin.nnov.ru>
     14# This code placed into public domain.
     15# You can download latest version at
     16# http://galanin.nnov.ru/~al/hg/hgwebdir.cgi/utils/file/tip/checkFinishedTransmissionDownloads.sh
    1517
    1618# Requires:
     
    1921#   transmission-cli
    2022
    21 # History:
    22 #----------------------------------------------------------------------------
    23 # Date        | Author <EMail>                  | Description               |
    24 #----------------------------------------------------------------------------
    25 # 04 May 2009 | A.Galanin <gaa.nnov AT mail.ru> | Creation                  |
    26 # 04 May 2009 | A.Galanin <gaa.nnov AT mail.ru> | Usage moved before locking|
    27 #----------------------------------------------------------------------------
    28 
     23# $Id: checkFinishedTransmissionDownloads.sh,v aa96b17920a4 2010/03/01 12:16:55 al $
     24
     25############################################################################
    2926# default configuration options
    3027# put it into config file ~/.checkFinishedTransmissionDownloads/config
     28############################################################################
     29
    3130HOST=localhost
    3231PORT=9091
     
    3938FROM=torrent-checker
    4039MAIL_CONTENT="Downloading of \"%s\" has been finished.\nGo to %s to make an approriate action.\n"
    41 
    42 #------------------------------------------------------------------------------
    43 
    44 # files
    45 FILEPATH="$HOME/.checkFinishedTransmissionDownloads"
    46 CONFIG_FILE="$FILEPATH/config"
    47 NOTIFY_FILE="$FILEPATH/notified"
     40ERROR_CONTENT="Error detected in torrent \"%s\": %s.\nGo to %s to make an approriate action.\n"
     41
     42############################################################################
     43# CONSTANTS
     44############################################################################
     45
    4846ALL_FILE="/tmp/checkFinishedTransmissionDownloads.all"
     47
    4948TMP_FILE="/tmp/checkFinishedTransmissionDownloads.tmp"
     49TMP_FILE2="/tmp/checkFinishedTransmissionDownloads.tmp2"
     50LIST_FILE="/tmp/checkFinishedTransmissionDownloads.list"
    5051LOCK_FILE="/tmp/checkFinishedTransmissionDownloads"
    5152
    52 [ -f "$CONFIG_FILE" ] && . "$CONFIG_FILE"
    53 
    54 #------------------------------------------------------------------------------
     53############################################################################
     54# FUNCTIONS
     55############################################################################
     56
     57# Show usage information and exit
     58usage() {
     59    echo "$0: check for finished downloads in Transmission"
     60    echo "USAGE: $0 [-C <config-dir>]"
     61    echo "    -C <config-dir>   Change config directory (default: \$HOME/.checkFinishedTransmissionDownloads)"
     62    echo "    -h                Show help"
     63    exit 1
     64}
    5565
    5666# Call transmission-remote with corresponding parameters
     
    6272        transmission-remote "$HOST":"$PORT" -N "$TMP_FILE" "$@"
    6373    fi
     74    # Since transmission 1.70 exit code can be used to detect errors
     75    if [ $? -ne 0 ]
     76    then
     77        echo "Error while accessing Transmission daemon" >&2
     78        exitAndClean 3
     79    fi
    6480}
    6581
    6682# Remove lock and temporary files, exit with code $1
    6783exitAndClean () {
    68     kill "$LOCK_PID"
     84    [ -z "$LOCK_PID" ] || kill "$LOCK_PID"
    6985    lockfile-remove "$LOCK_FILE"
    70     rm -f "$TMP_FILE" "$ALL_FILE"
     86    rm -f "$TMP_FILE" "$ALL_FILE" "$LIST_FILE" "$TMP_FILE2" "$ERROR_FILE.new"
    7187
    7288    exit "$1"
    7389}
    7490
    75 # initialization
    76 if [ $# != 0 ]
     91# Remove torrents that deleted from downloads list from specified list file
     92# $1 -- list name
     93removeDeletedTorrents() {
     94    # remove deleted torrents from sent notifications list
     95    sort "$1" > "$TMP_FILE"
     96    mv "$TMP_FILE" "$TMP_FILE2"
     97
     98    sort "$ALL_FILE" > "$TMP_FILE"
     99    mv "$TMP_FILE" "$ALL_FILE"
     100
     101    comm -1 -2 "$TMP_FILE2" "$ALL_FILE" > "$TMP_FILE"
     102    mv "$TMP_FILE" "$1"
     103}
     104
     105############################################################################
     106# MAIN
     107############################################################################
     108
     109CONFIGPATH="$HOME/.checkFinishedTransmissionDownloads"
     110
     111while getopts C:h opt
     112do
     113    case $opt in
     114        C)
     115            CONFIGPATH="$OPTARG"
     116        ;;
     117        h)
     118            usage
     119        ;;
     120        \?)
     121            usage
     122        ;;
     123    esac
     124    shift `expr $OPTIND - 1`
     125done
     126
     127if [ $# -ne 0 ]
    77128then
    78     echo "$0: check for finished downloads in Transmission"
    79     echo "USAGE: $0"
    80     exit 1
     129    usage
    81130fi
    82131
    83 lockfile-create "$LOCK_FILE" || (echo "Unable to lock lockfile!"; exitAndClean 2)
     132# files
     133CONFIG_FILE="$CONFIGPATH/config"
     134NOTIFY_FILE="$CONFIGPATH/notified"
     135ERROR_FILE="$CONFIGPATH/error"
     136
     137if [ -f "$CONFIG_FILE" ]
     138then
     139    . "$CONFIG_FILE"
     140else
     141    echo "Configuration file \`$CONFIG_FILE' does not exists"
     142    exit 4
     143fi
     144
     145umask 077
     146
     147lockfile-create "$LOCK_FILE" || (echo "Unable to lock lockfile!" >&2; exitAndClean 2)
    84148lockfile-touch "$LOCK_FILE" &
    85149LOCK_PID="$!"
    86150
     151MAIN_PID="$$"
     152
    87153trap "exitAndClean 1" HUP INT QUIT KILL
    88154
    89 mkdir -p "$FILEPATH"
    90 touch "$NOTIFY_FILE" "$TMP_FILE"
    91 echo -n > "$ALL_FILE"
    92 chmod 600 "$TMP_FILE" "$ALL_FILE"
     155touch "$NOTIFY_FILE" "$ERROR_FILE" "$ERROR_FILE.new" "$TMP_FILE"
     156printf "" > "$ALL_FILE"
    93157# generate netrc file for RPC authorisation
    94158printf "machine %s\nlogin %s\npassword %s\n" "$HOST" "$USER" "$PASS" > "$TMP_FILE"
    95159
    96160# main
    97 callTransmission -l | gawk '{
     161callTransmission -l > "$TMP_FILE2" || exitAndClean 1
     162gawk '{
    98163    if ($1 != "Sum:" && $1 != "ID") {
    99164        print $1,$2
    100165    }
    101 }' | while read id percent
     166}' "$TMP_FILE2" > "$LIST_FILE"
     167
     168while read id percent
    102169do
    103     reply="`callTransmission -t "$id" -i | grep -E '^  Name|^  Hash'`"
     170    id="`echo "$id" | sed 's/\*//'`"
     171    reply="`callTransmission -t "$id" -i | grep -E '^  [A-Z]'`"
     172    [ $? -eq 0 ] || exitAndClean 1
    104173
    105174    name="`echo "$reply" | grep '^  Name'  | cut -c 9-`"
    106175    hash="`echo "$reply" | grep '^  Hash'  | cut -c 9-`"
     176    error="`echo "$reply" | grep '^  Error'  | cut -c 10-`"
     177    trackerError="`echo "$reply" | grep '^  Tracker gave an error'  | cut -c 26-`"
    107178
    108179    # check that notification is not yet sent
     
    114185        echo "$hash" >> "$NOTIFY_FILE"
    115186    fi
     187
     188    # check that error exists and not yet reported
     189    if [ -n "$error" -o -n "$trackerError" ]
     190    then
     191        if ! grep -q "$hash" "$ERROR_FILE"
     192        then
     193            #print error message
     194            if [ -n "$error" ]
     195            then
     196                error="$error, "
     197            fi
     198            error="${error}${trackerError}"
     199            printf "$ERROR_CONTENT" "$name" "$error" "http://$HOST:$PORT/" | \
     200                mailx $MAILTO -s "Torrents info: $name" -a "From: $FROM"
     201        fi
     202        echo "$hash" >> "$ERROR_FILE.new"
     203    fi
    116204    echo "$hash" >> "$ALL_FILE"
    117 done
    118 
    119 # remove deleted torrents from sent notifications list
    120 sort "$NOTIFY_FILE" > "$TMP_FILE"
    121 mv "$TMP_FILE" "$NOTIFY_FILE"
    122 
    123 sort "$ALL_FILE" > "$TMP_FILE"
    124 mv "$TMP_FILE" "$ALL_FILE"
    125 
    126 comm -1 -2 "$NOTIFY_FILE" "$ALL_FILE" > "$TMP_FILE"
    127 mv "$TMP_FILE" "$NOTIFY_FILE"
     205done < "$LIST_FILE"
     206
     207removeDeletedTorrents "$NOTIFY_FILE"
     208mv "$ERROR_FILE.new" "$ERROR_FILE"
    128209
    129210exitAndClean 0