Project

General

Profile

Problem calling rdm-mailhandler from bash script - tracker names with spaces/escaping problem

Added by Chris Platts about 14 years ago

Hi,

I'm setting up a script to feed incoming emails into Redmine using rdm-mailhandler.rb. The idea is that received email is fed into a bash script which looks at the headers to decide what project the mail should go to. The script consults a 'parameters' file which holds the mailhandler parameters to be used with each inbound email address.

I've hit a problem where I can't escape the --tracker= parameter correctly for trackers with spaces in their names. When executing rdm-mailhandler.rb from a bash prompt, the following works just fine:

--project=helpdesk --tracker="Incoming - rdm-support" --unknown-user=create --no-permission-check

However, the (apparently) same string used within a bash script fails. Looking at production.log, I get:

Processing MailHandlerController#index (for 1.2.3.4 at 2010-01-08 10:54:38) [POST]
Parameters: {"no_permission_check"=>"1", "unknown_user"=>"create", "allow_override"=>"", "issue"=>{"project"=>"helpdesk", "tracker"=>"\"Incoming"} [...etc...]

I'm sure I'm simply failing to get my head around how bash is expanding variables... Could anyone lend me a hand in getting this to work? :)

Below is the source of the script, with comments:


# get some support functions
source /home/redmine-gw/common

log "----- redmine email gateway starting -----" 
log "Running as `whoami`" 
log "Received email from ${1}" 
log "addressed to ${2}" 
log "ccd to ${3}" 

# parse the To and CC addresses to identify the destination
getTarget "$2" "$3" 

# definitions for what to do for each receiving email alias
# are stored in a .INI-style file.  The following commands
# extract the parameter values for the current destination.

PROJECT=`parseini.pl $TARGET project`
TRACKER=`parseini.pl $TARGET tracker`
UNKNOWN_USER_ACTION=`parseini.pl $TARGET unknown-user`
NO_PERMISSION_CHECK=`parseini.pl $TARGET no-permission-check`

CMD="" 

if [ "$PROJECT" == "" ]; then
        iniParamError "project" 
else
        CMD="--project="$PROJECT"" 
fi

if [ "$TRACKER" == "" ]; then
        iniParamError "tracker" 
else
        CMD="${CMD} --tracker=\""$TRACKER"\"" 
fi

if [ -n "$UNKNOWN_USER_ACTION" ]; then
        CMD="${CMD} --unknown-user="$UNKNOWN_USER_ACTION"" 
fi

if [ "$NO_PERMISSION_CHECK" == "1" ]; then
        CMD="${CMD} --no-permission-check" 
fi

echo $CMD

cat /dev/stdin | /home/rosetta-gw/rdm-mailhandler.rb --verbose --url http://1.2.3.4:88 --key xXxXxXxXx1234xXxX $CMD  2>&1 $LOGFILE

function iniParamError () {
        log "aborting - config section for $TARGET does not specify a $1" 
        exit
}

The 'echo' in the above script prints out the parameters it's using with rdm-mailhandler. It currently outputs (for example):

--project=helpdesk --tracker="Incoming - rdm-support" --unknown-user=create --no-permission-check

... which looks absolutely fine, but leads to the problem shown in the production.log extract above.

Can anyone explain the magic I need in the script to allow this to be parsed correctly by rdm-mailhandler?

Many thanks,
Chris


Replies (4)

RE: Problem calling rdm-mailhandler from bash script - tracker names with spaces/escaping problem - Added by Felix Schäfer about 14 years ago

That seems overly complex for what I understand it tries to do. Why not just create multiple mail entry points for the different projects (say creates new tickets in project1, in project2, and so on) in addition to the listener on the mail address redmine uses for sending mails (so that if your redmine sends mails as , the comments to an issue getting replied to get indeed inserted as comments in redmine).

Back to the script, echo is not the be-all-end-all tool to get the string out of the script, because it does some munging of its own, but I think if you change the middle part of the script to something like that:

if [ "${PROJECT}" == "" ]; then
        iniParamError "project" 
else
        CMD="--project=${PROJECT}" 
fi

if [ "${TRACKER}" == "" ]; then
        iniParamError "tracker" 
else
        CMD="${CMD} --tracker=\"${TRACKER}\"" 
fi

if [ -n "${UNKNOWN_USER_ACTION}" ]; then
        CMD="${CMD} --unknown-user=${UNKNOWN_USER_ACTION}" 
fi

if [ "${NO_PERMISSION_CHECK}" == "1" ]; then
        CMD="${CMD} --no-permission-check" 
fi

RE: Problem calling rdm-mailhandler from bash script - tracker names with spaces/escaping problem - Added by Chris Platts about 14 years ago

Thanks for the reply, Felix.

I've set it up this way in an attempt to make it easier to manage the incoming addresses used for projects. We have a constantly-changing number of third-parties dealing with us, so I wanted to avoid configuring each incoming mailbox. Plus, I wanted other people in my department to be able to perform this configuration, without having to take them through the basics of postfix, procmail and rdm-mailhandler.rb :)

Thanks for the help with the script. I've got it all up and running now, and it's working quite well. The setup is:

  • Email server with Webmin interface
  • This server has a single 'redmine-gateway' mailbox
  • All other per-project email addresses are simple aliases to this mailbox, added/removed via a Webmin module.
  • Internal Redmine server runs fetchmail to pick up emails from the mailserver, delivering to a local 'redmine-gateway' address.
  • Procmail sends all incoming email on that address to a script.
  • Script looks up the original destination address in 'destinations.ini' - a simple '.ini'-style file which describes the rdm-mailhandler options to use for that destination.
  • Script pipes the message through to rdm-mailhandler

So, adding an email endpoint involves simply adding the alias on the mailserver and editing the destinations.ini file (all via webmin). The destinations.ini file has a comment header explaining what all the options mean, which is handy for some of my colleagues to refer to :)

I suppose there was some method to my madness!

Cheers,
Chris

RE: Problem calling rdm-mailhandler from bash script - tracker names with spaces/escaping problem - Added by Ahammad Fekri almost 12 years ago

Hi Chris,
your written bulleted points are matched exactly what I have wanted to do but I couldn't found destinations.ini file and I don't understand how configure it. I’m using Ubuntu 10.04 and the system is creating issues from mail but more configuration is needed like yours.

Your little participation would be pleasure for me ....
Thanx in advance

    (1-4/4)