diff --git a/pojkomalerter b/pojkomalerter new file mode 100755 index 0000000000000000000000000000000000000000..2ade142642b35bb598b39b829087d9f657e01e16 --- /dev/null +++ b/pojkomalerter @@ -0,0 +1,116 @@ +#!/usr/bin/env python +# -*- python -*- -*- coding: iso-8859-1 -*- +# LysKOM message writer and article creator +# pojkomalerter + +# Written by Per Jonsson, poj@lysator.liu.se +# Based on stuff in komimportmail: +# (C) 1999 Kent Engstr�m. Released under GPL. + +# To get it to work set the variables: +# * KOMSERVER +# * KOMPORT +# * KOMPERSON (to the person number of the person to log in as) +# * KOMPASSWORD (password of the person above) +# +# Requires kom.py and komauxitems.py from python-lyskom + +import kom +import sys +import getopt + +KOMSERVER = "kom.lysator.liu.se" +KOMPORT = 4894 +#KOMPERSON = 4711 +#KOMPASSWORD = "" + +#ALERTCONF = 11324 +#MSGCONF = 11324 +ALERTCONF = 7990 +MSGCONF = 9791 + +revision = "0.1" + +# Exit codes from sendmail-8.9.3/src/sysexits.c: +EX_OK = 0 +EX_USAGE = 64 +EX_DATAERR = 65 +EX_NOUSER = 67 +EX_UNAVAILABLE = 69 # Catchall +EX_TEMPFAIL = 75 # This is the key to making sendmail requeue a mail! + +# Error reporting: send message to stderr and logfile, exit with right code +def error_exit(str, exit_code): + line = "FATAL: " + str + sys.stderr.write(line + "\n") + sys.exit(exit_code) + +try: + send_msg = False + subject = "" + + try: + options, arguments = getopt.getopt(sys.argv[1:], + "", + ["send-msg", + "subject="]) + except getopt.error, reason: + error_exit("Usage error (%s)" % reason, EX_USAGE) + + # Check for arguments + + for (opt, optarg) in options: + if opt == "--send-msg": + send_msg = True + elif opt == "--subject": + subject = optarg + else: + error_exit("Bad option '%s'" % opt, EX_USAGE) + + # Read message + msg = sys.stdin.read() + + # Connect and log in + + try: + c = kom.CachedConnection(KOMSERVER,KOMPORT) + try: + kom.ReqLogin(c, KOMPERSON, KOMPASSWORD, invisible=1).response() + except: + # Change to EX_TEMPFAIL if you consider this transient... + error_exit("Failed to login to LysKOM server", EX_UNAVAILABLE) + except: + error_exit("Failed to connect to LysKOM server", EX_TEMPFAIL) + + # + # Send message + # + if send_msg: + try: + kom.ReqSendMessage(c, MSGCONF, msg).response() + except kom.MessageNotSent: + #error_exit("Failed to send message, recipient not present", EX_TEMPFAIL) + pass + except kom.Error: + error_exit("Failed to send message", EX_TEMPFAIL) + + msg = subject + "\n" + msg + + # + # Create article + # + aux_items = [] + ai = kom.AuxItem(kom.AI_CREATING_SOFTWARE) + ai.data = "pojkomalerter %s" % revision + aux_items.append(ai) + + misc_info = kom.CookedMiscInfo() + rec = kom.MIRecipient(kom.MIR_TO, ALERTCONF) + misc_info.recipient_list.append(rec) + + try: + text_no = kom.ReqCreateText(c, msg, misc_info, aux_items).response() + except kom.Error: + error_exit("Failed to create text", EX_TEMPFAIL) +except: + error_exit("General Error", -1)