-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkomparam.py
More file actions
132 lines (110 loc) · 3.89 KB
/
Copy pathkomparam.py
File metadata and controls
132 lines (110 loc) · 3.89 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: iso-8859-1 -*-
# LysKOM Protocol A version 10 client interface for Python
# $Id: komparam.py,v 1.7 2007/05/23 14:30:01 kent Exp $
# (C) 1999 Kent Engström. Released under GPL.
# NOTE: This module is deprecated in favour of komconnect.py
# Handle connection and login in a common way. Parameters are
# searched for in the following order:
# 1) Command line arguments (NOT DONE YET)
# 2) Environment variables
# 3) ~/.komrc file
import sys
import os
import string
import kom
class Parameters:
# Stash away paramers for later us
# (argv should typically be sys.argv[1:])
def __init__(self, argv):
self.argv = argv
self.environ = os.environ
self.file_dict = None
# Connect and log in using parameters stored by the constructor
# (connection_class should be kom.Connection or a subclass)
# Return (connection object, None) on success or
# (None, error string) on failure
def connect_and_login(self, connection_class):
# Connect
server = self.get_server()
port = self.get_port()
if server is None:
return (None, "server not specified")
try:
conn = connection_class(server, port)
except:
return (None, "failed to connect to %s:%d" % (server, port))
# Lookup name
name = self.get_name()
if name is None:
return (None, "name not specified")
persons = conn.lookup_name(name, want_pers = 1, want_confs = 0)
if len(persons) == 0:
return (None, "name doesn't match anybody")
elif len(persons) <> 1:
return (None, "name matches %d persons" % len(persons))
self.person_no = persons[0][0]
# Login
password = self.get_password()
if password is None:
return (None, "password not specified")
try:
kom.ReqLogin(conn, self.person_no, password).response()
except:
return (None, 'failed to log in as "%s"' % persons[0][1])
# Done!
return (conn, None)
# Get remaining arguments (after we remove our special arguments)
def get_arguments(self):
return self.argv[:] # FIXME!
# Get person_no
def get_person_no(self):
return self.person_no
# Semi-public interface
def get_server(self):
data = self.get_parameter("KOMSERVER", "server")
if data is not None:
colon_pos = string.find(data, ":")
if colon_pos <> -1:
data = data[:colon_pos]
return data
def get_port(self):
data = self.get_parameter("KOMSERVER", "server")
if data is not None:
colon_pos = string.find(data, ":")
if colon_pos <> -1:
data = data[colon_pos+1:]
try:
return string.atoi(data)
except:
return 4894
def get_name(self):
return self.get_parameter("KOMNAME", "name")
def get_password(self):
return self.get_parameter("KOMPASSWORD", "password")
# Private
def get_parameter(self, env_name, file_name):
# In environment?
if self.environ.has_key(env_name):
return self.environ[env_name]
# In file?
self.ensure_file_loaded()
if self.file_dict.has_key(file_name):
return self.file_dict[file_name]
return None
def ensure_file_loaded(self):
# Do not load twice
if self.file_dict is not None:
return
# Load file
self.file_dict = {}
try:
f = open("%s/.komrc" % os.environ["HOME"])
lines = f.readlines()
f.close()
for line in lines:
list = map(string.strip,string.split(line,"="))
if len(list) == 2:
self.file_dict[list[0]] = list[1]
pass
except:
pass