forked from ebroder/python-moira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmrclient.pyx
80 lines (69 loc) · 2.18 KB
/
mrclient.pyx
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
cdef extern from "moira/mrclient.h":
int mrcl_connect(char * server, char * client, int version, int auth)
char * mrcl_krb_user()
char * c_partial_canonicalize_hostname "partial_canonicalize_hostname" (char * s)
char * mrcl_get_message()
void mrcl_com_err(char * whoami)
int mrcl_validate_pobox_smtp(char * user, char * address, char ** ret)
enum:
MRCL_SUCCESS
MRCL_FAIL
MRCL_REJECT
MRCL_WARN
MRCL_ENOMEM
MRCL_MOIRA_ERROR
MRCL_AUTH_ERROR
cdef extern from "stdlib.h":
void free(void * ptr)
cdef extern from "string.h":
char * strdup(char * s)
cdef public char * whoami
class MoiraClientException(Exception):
def code(self):
return self.args[0]
code = property(code)
def _error(code, text=None):
if code == MRCL_ENOMEM:
text = "Out of memory"
if code == MRCL_MOIRA_ERROR:
text = "Moira Error"
if code == MRCL_AUTH_ERROR:
text = "Authentication error"
if mrcl_get_message():
if text == None:
text = mrcl_get_message()
else:
text = "%s (%s)" % (text, mrcl_get_message())
raise MoiraClientException(code, text)
def connect(server='', client='mrclient.py', version=-1, auth=1):
status = mrcl_connect(server, client, version, auth)
if status != MRCL_SUCCESS:
_error(status)
whoami = client
def krb_user():
cdef char * c_krb_user
cdef object py_krb_user
c_krb_user = mrcl_krb_user()
if c_krb_user == NULL:
_error(-1, "Could not retrieve principal name")
py_krb_user = c_krb_user
free(c_krb_user)
return py_krb_user
def validate_pobox_smtp(user, address):
cdef char * c_retval
cdef object py_retval
py_retval = address
status = mrcl_validate_pobox_smtp(user, address, &c_retval)
if status != MRCL_SUCCESS:
_error(status)
if c_retval != NULL:
py_retval = c_retval
free(c_retval)
return py_retval
def partial_canonicalize_hostname(host):
cdef object py_hostname
cdef char * c_hostname
c_hostname = c_partial_canonicalize_hostname(strdup(host))
py_hostname = c_hostname
free(c_hostname)
return py_hostname