|
0
|
1 |
# Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
|
|
|
2 |
#
|
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
|
6 |
# (at your option) any later version.
|
|
|
7 |
#
|
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
# GNU General Public License for more details.
|
|
|
12 |
#
|
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
16 |
|
|
|
17 |
import config
|
|
|
18 |
|
|
|
19 |
ok = 0
|
|
|
20 |
bad = 1
|
|
|
21 |
err = 2
|
|
|
22 |
econn = 3
|
|
|
23 |
|
|
|
24 |
class Bad(Exception): pass
|
|
|
25 |
class Error(Exception): pass
|
|
|
26 |
class Econn(Exception): pass
|
|
|
27 |
|
|
|
28 |
def encode_int(i):
|
|
|
29 |
return chr((i/256)%256) + chr(i%256)
|
|
|
30 |
|
|
2
|
31 |
def encode_str(s):
|
|
|
32 |
s = str(s)
|
|
|
33 |
return encode_int(len(s)) + s
|
|
0
|
34 |
|
|
|
35 |
class Command:
|
|
|
36 |
def __init__(self, name, args):
|
|
|
37 |
self.name = name
|
|
|
38 |
self.args = args
|
|
|
39 |
|
|
|
40 |
def encode(self):
|
|
|
41 |
argcount = len(self.args)
|
|
|
42 |
cmd = chr(argcount) + encode_str(self.name)
|
|
|
43 |
for i in range(argcount):
|
|
|
44 |
cmd = cmd + encode_str(self.args[i])
|
|
|
45 |
cmd = chr(2) + encode_int(len(cmd)) + cmd
|
|
|
46 |
return cmd
|
|
|
47 |
|
|
|
48 |
class Daemon:
|
|
|
49 |
def __init__(self):
|
|
|
50 |
self.socket_file = config.read('socket-file', '/tmp/.vmailmgrd')
|
|
|
51 |
self.socket = None
|
|
|
52 |
|
|
|
53 |
def connect(self):
|
|
|
54 |
import socket
|
|
|
55 |
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
56 |
self.socket.connect(self.socket_file)
|
|
|
57 |
|
|
|
58 |
def read_response_noraise(self):
|
|
|
59 |
try:
|
|
|
60 |
reply = self.socket.recv(3)
|
|
|
61 |
code = ord(reply[0])
|
|
|
62 |
msglen = ord(reply[1])*256 + ord(reply[2])
|
|
2
|
63 |
if msglen:
|
|
|
64 |
message = self.socket.recv(msglen)
|
|
|
65 |
else:
|
|
|
66 |
message = ""
|
|
0
|
67 |
except:
|
|
|
68 |
return (econn, 'Server aborted the connection')
|
|
|
69 |
return (code, message)
|
|
|
70 |
|
|
|
71 |
def read_response(self):
|
|
|
72 |
(code, message) = self.read_response_noraise()
|
|
|
73 |
if code == err:
|
|
|
74 |
raise Error, message
|
|
|
75 |
if code == bad:
|
|
|
76 |
raise Bad, message
|
|
|
77 |
if code == econn:
|
|
|
78 |
raise Econn, message
|
|
|
79 |
return message
|
|
|
80 |
|
|
|
81 |
def execute(self, command):
|
|
|
82 |
try:
|
|
|
83 |
self.connect()
|
|
|
84 |
except:
|
|
|
85 |
raise Econn, "Unable to connect to the server"
|
|
|
86 |
data = command.encode()
|
|
|
87 |
try:
|
|
|
88 |
self.socket.send(data)
|
|
|
89 |
except:
|
|
|
90 |
raise Econn, "Server aborted the connection"
|
|
|
91 |
# Don't close the socket here -- it will be closed
|
|
|
92 |
# as soon as it gets garbage collected, and remains
|
|
|
93 |
# usable for other commands like listdomain.
|
|
|
94 |
return self.read_response()
|
|
|
95 |
|
|
|
96 |
def execute(command, args):
|
|
|
97 |
return Daemon().execute(Command(command, args))
|