#!/bin/bash # # Script: rt_queues.sh # Author: Benjamin Schieder # License: Public Domain # Task: This script is meant to be called from procmail. It wil parse # the headers of the e-mail, check if the mail is addressed # to the domain of your Request Tracker installation and route # the mail to the correct queue. # This saves you the work of adding/deleting E-Mail aliases # for each queue. # # # Set up your environment here # #DATABASESERVER="localhost" # your mysql database server #DATABASEUSER="rt3" # a user that may read the Queues table #DATABASEPASS='password' # the user's password #DATABASEDB="rt3" # the database name for RT #TICKETADDRESS="rt.ticket.local" # the domain emails are addressed to (the part after @) #TICKETWEBADDRESS="http://rt.ticket.local/" # the web address your RT can be reached at #RTMAILGATE="/opt/rt3/bin/rt-mailgate" # the full path to the rt-mailgate program #MYSQL="/opt/mysql/bin/mysql" # the full path to the mysql program #MKTEMP="/usr/bin/mktemp" # the full path to the mktemp program #DEBUG=0 # set to 1 to get verbose information on error # # or setup a configuration file # # . /etc/request-tracker/rt_queues.conf ############################################## # NOTHING NEEDS TO BE CHANGED PAST THIS LINE # ############################################## get_queue_by_correspond_address() { local address address="${1}" if [ "${MYSQL/mysql/}" != "${MYSQL}" ] ; then echo "SELECT Name FROM Queues WHERE CorrespondAddress = '${address}';" | "${MYSQL}" -h "${DATABASESERVER}" -u "${DATABASEUSER}" --password="${DATABASEPASS}" --skip-column-names -D "${DATABASEDB}" elif [ "${MYSQL/psql}" != "${MYSQL}" ] ; then echo "SELECT Name FROM Queues WHERE CorrespondAddress = '${address}';" | "${MYSQL}" -h "${DATABASESERVER}" -U "${DATABASEUSER}" --tuples-only "${DATABASEDB}" fi } get_queue_by_comment_address() { local address address="${1}" if [ "${MYSQL/mysql/}" != "${MYSQL}" ] ; then echo "SELECT Name FROM Queues WHERE CommentAddress = '${address}';" | "${MYSQL}" -h "${DATABASESERVER}" -u "${DATABASEUSER}" --password="${DATABASEPASS}" --skip-column-names -D "${DATABASEDB}" elif [ "${MYSQL/psql}" != "${MYSQL}" ] ; then echo "SELECT Name FROM Queues WHERE CommentAddress = '${address}';" | "${MYSQL}" -h "${DATABASESERVER}" -U "${DATABASEUSER}" --tuples-only "${DATABASEDB}" fi } verbose() { local message message="${@}" [ "${DEBUG}" != "1" ] && return echo "${message}" } # Let's see if everything is sane for var in DATABASESERVER DATABASEUSER DATABASEPASS DATABASEDB TICKETADDRESS \ TICKETWEBADDRESS RTMAILGATE MYSQL MKTEMP ; do eval "check=\"\${${var}}\"" if [ -z "${check}" ] ; then echo "ERROR: ${var} is not set!" >&2 exit 1 fi done # # Set up the environment # If a command fails I want this script to commit seppuku # set -k # # clean up on exit # trap 'rm -f "${tempdb}" "${tempmail}"' EXIT # # our temporary files # tempdb="$( ${MKTEMP} )" tempmail="$( ${MKTEMP} )" touch "${tempdb}" "${tempmail}" chmod 0600 "${tempdb}" "${tempmail}" # # We have the complete E-Mail on stdin. Put it into a file # cat - > "${tempmail}" unset ADDRESS TO=0 CC=0 OLDIFS="${IFS}" # # where is this mail addressed or CC'd to? # while [ -z "${ADDRESS}" ] ; do IFS="" read line || break line="$( tr '[[:upper:]]' '[[:lower:]]' <<< "${line}" )" first_char="${line:0:1}" if [ -z "${first_char//[a-z0-9]/}" ] ; then # line is not indented, so it's a new header TO=0 CC=0 fi [ "${line:0:3}" == "to:" ] && TO=1 [ "${line:0:3}" == "cc:" ] && CC=1 if [ ${TO} -eq 1 ] ; then IFS="${OLDIFS}" for address in $( tr -d '<>,' <<< "${line}" ) ; do [ "${address}" == "to:" ] && continue if [ "${address##*@}" == "${TICKETADDRESS}" ] ; then ADDRESS="${address}" break fi done fi if [ ${CC} -eq 1 ] ; then IFS="${OLDIFS}" for address in $( tr -d '<>,' <<< "${line}" ) ; do [ "${address}" == "cc:" ] && continue if [ "${address##*@}" == "${TICKETADDRESS}" ] ; then ADDRESS="${address}" break fi done fi done < "${tempmail}" if [ -z "${ADDRESS}" ] ; then verbose Could not extract an address to host ${TICKETADDRESS} from E-Mail. exit 1 fi # # which queue does this address belong to? # read queue < <( get_queue_by_correspond_address "${ADDRESS}" ) queue="$( tr '[[:upper:]]' '[[:lower:]]' <<< "${queue}" )" if [ -n "${queue}" ] ; then "${RTMAILGATE}" --queue "${queue}" --action correspond --url "${TICKETWEBADDRESS}" < "${tempmail}" exit 0 fi read queue < <( get_queue_by_comment_address "${ADDRESS}" ) queue="$( tr '[[:upper:]]' '[[:lower:]]' <<< "${queue}" )" if [ -n "${queue}" ] ; then "${RTMAILGATE}" --queue "${queue}" --action comment --url "${TICKETWEBADDRESS}" < "${tempmail}" exit 0 fi # # If we're here, we didn't do anything. The mail address is _not_ an RT address # verbose Address ${ADDRESS} is not an RT correspond or comment address. exit 1