-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkommsg
More file actions
executable file
·99 lines (79 loc) · 2.06 KB
/
Copy pathkommsg
File metadata and controls
executable file
·99 lines (79 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
# LysKOM Protocol A version 10 client interface for Python
# $Id: kommsg,v 1.2 2009/02/03 07:22:15 forsberg Exp $
# (C) 1999 Kent Engström. Released under GPL.
import kom
import komparam
import sys
import getopt
import string
# Get revision number from RCS/CVS
vc_revision = "$Revision: 1.2 $"
revision = vc_revision[11:-2]
# Error/sucess reporting
exit_code = 0
def error(str, code = 2):
global exit_code
exit_code = max(exit_code, code)
sys.stderr.write("ERROR: " + str + "\n")
def success(str):
sys.stderr.write("OK: " + str + "\n")
def exit():
sys.exit(exit_code)
# MAIN
# Connect and log in
param = komparam.Parameters(sys.argv[1:])
(conn, conn_error) = param.connect_and_login(kom.CachedConnection)
if conn is None:
sys.stderr.write("%s: %s\n" % (sys.argv[0], conn_error))
sys.exit(1)
#
# CHECK FOR OPTIONS
#
to = None
options, arguments = getopt.getopt(param.get_arguments(),
"",
[
"to=",
"all",
])
for (opt, optarg) in options:
if opt == "--to":
to = optarg
elif opt == "--all":
to = 0
else:
error("Option %s not handled (internal error)" % opt)
# Recipient
if to == 0:
conf_no = 0
elif to is not None:
matches = conn.lookup_name(to, 1, 1)
if len(matches) == 0:
error("%s -- recipient not found" % to)
elif len(matches) <> 1:
error("%s -- ambiguous recipient" % to)
else:
conf_no = matches[0][0]
else:
error("recipient not specified")
#
# READ MESSAGE TEXT
#
# Errors already?
if exit_code <> 0:
exit()
text = sys.stdin.read()
#
# SEND MESSAGE
#
# Send!
try:
kom.ReqSendMessage(conn, conf_no, text).response()
success("Message sent.")
except kom.MessageNotSent:
error("Recipient not present (%s)" % sys.exc_info()[0:1], code = 1)
except kom.Error:
error("Failed to send message (%s)" % sys.exc_info()[0:1])
exit()