| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Copyright (c) 2008 Klaas Freitag <freitag@suse.de>, Novell Inc. |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program (see the file COPYING); if not, write to the |
| 16 |
# Free Software Foundation, Inc., |
| 17 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 18 |
# |
| 19 |
################################################################ |
| 20 |
# Contributors: |
| 21 |
# Klaas Freitag <freitag@suse.de> |
| 22 |
# |
| 23 |
# This a command line tool to schedule messages in hermes |
| 24 |
# |
| 25 |
|
| 26 |
use strict qw 'vars'; |
| 27 |
use Getopt::Std; |
| 28 |
use Hermes::MessageSender; |
| 29 |
use Hermes::Message; |
| 30 |
|
| 31 |
use vars qw ($opt_t $opt_h $opt_d); |
| 32 |
|
| 33 |
# -d : delay-string |
| 34 |
# -t : message type |
| 35 |
|
| 36 |
# SendMail Interface to hermes. |
| 37 |
|
| 38 |
my $subject; |
| 39 |
my $text; |
| 40 |
my %receiver; |
| 41 |
|
| 42 |
|
| 43 |
sub help() |
| 44 |
{ |
| 45 |
print<<END |
| 46 |
|
| 47 |
NAME |
| 48 |
sendhermes.pl - commandline tool to hand over a message to hermes. |
| 49 |
|
| 50 |
SYNOPSIS |
| 51 |
|
| 52 |
sendhermes [option...] < message |
| 53 |
|
| 54 |
DESCRIPTION |
| 55 |
|
| 56 |
Handing over messages that look like ordinary mail messages to the |
| 57 |
hermes system. The messages that are fed in have to follow a simplified |
| 58 |
rfc 822 format which describes internet email. |
| 59 |
|
| 60 |
The message contains a header and a body, separated by two empty lines. |
| 61 |
The header has some header fields. These header fields are recognised by |
| 62 |
hermes: |
| 63 |
|
| 64 |
to: Receivers mail address |
| 65 |
cc: Carbon copy receivers |
| 66 |
bcc: blind carbon copy receivers |
| 67 |
subject: A descriptive one line subject |
| 68 |
from: the sender address |
| 69 |
replyto: a reply to address |
| 70 |
type: a hermes message type |
| 71 |
|
| 72 |
The message body is simply the text. |
| 73 |
|
| 74 |
Options: |
| 75 |
-d: The delay string, like SEND_NOW, SEND_HOURLY, SEND_DAILY, SEND_WEEKLY |
| 76 |
or SEND_MONTHLY |
| 77 |
-t: the message type, overwrites a type set in the incoming message |
| 78 |
-h: this help message |
| 79 |
|
| 80 |
EXAMPLE: |
| 81 |
|
| 82 |
Send a message stored in /tmp/foo.mail: |
| 83 |
|
| 84 |
sendhermes.pl -d SEND_NOW < /tmp/foo.mail |
| 85 |
|
| 86 |
END |
| 87 |
; |
| 88 |
|
| 89 |
exit; |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
sub error(;$) |
| 94 |
{ |
| 95 |
my ($msg) = @_; |
| 96 |
|
| 97 |
print "ERROR: $msg\n"; |
| 98 |
exit(1); |
| 99 |
} |
| 100 |
|
| 101 |
getopts('d:t:h' ); |
| 102 |
|
| 103 |
help() if( $opt_h ); |
| 104 |
|
| 105 |
my $mail = join( '', <> ); |
| 106 |
my ($header, $body); |
| 107 |
|
| 108 |
if( $mail =~ /(.+?)(\n{3})(.+)/s ) { |
| 109 |
$header = $1; |
| 110 |
$body = $3; |
| 111 |
} else { |
| 112 |
error( "Could not parse message" ); |
| 113 |
} |
| 114 |
|
| 115 |
my @headerLines = split( /\n/, $header ); |
| 116 |
|
| 117 |
my @headertags = ( 'to', 'cc', 'bcc', 'from', 'replyto' ); |
| 118 |
my $hregexp = join( '|', @headertags ); |
| 119 |
my $type; |
| 120 |
|
| 121 |
foreach my $l ( @headerLines ) { |
| 122 |
if( $l =~ /^\s*($hregexp)\s*:\s*(\S+)\s*/i ) { |
| 123 |
print "###<$l>\n"; |
| 124 |
my $tag = lc $1; |
| 125 |
my $addressee = $2; |
| 126 |
$receiver{$tag} = () unless( $receiver{$tag} ); |
| 127 |
push @{$receiver{$tag} }, $addressee; |
| 128 |
} elsif( $l =~ /^\s*subject\s*:\s*(.+)\s*$/i ) { |
| 129 |
$subject = $1; |
| 130 |
} elsif( $l =~ /^\s*type\s*:\s*(\w+)\s*$/i ) { |
| 131 |
$type = $1; |
| 132 |
} else { |
| 133 |
print "Unrecognised header line <$l>\n"; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
print "==============================================================================\n$body\n"; |
| 138 |
|
| 139 |
$type = $opt_t if( $opt_t ); |
| 140 |
|
| 141 |
error( "No type given, use either command option or in text!" ) unless( $type ); |
| 142 |
|
| 143 |
my $delayID = delayStringToValue( $opt_d ); |
| 144 |
|
| 145 |
print "Type: <$type> and delay <$delayID>\n"; |
| 146 |
error( "No message type specified, use -t to do so!" ) unless( $type ); |
| 147 |
|
| 148 |
my $id = newMessage( $subject, $body, $type, $delayID, @{$receiver{'to'}}, @{$receiver{'cc'}}, |
| 149 |
@{$receiver{'bcc'}}, |
| 150 |
shift @{$receiver{'from'}}, shift @{$receiver{'replyto'}} ); |
| 151 |
print "Message added with id=<$id>\n"; |