Destroying purple.core in nullclient.py
[python-purple] / proxy.pyx
1 #
2 #  Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
3 #
4 #  This file is part of python-purple.
5 #
6 #  python-purple is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  python-purple is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 cimport account
21 cimport proxy
22
23 cdef class ProxyInfoType:
24     cdef proxy.PurpleProxyType c_type
25
26     def __init__(self):
27         self.c_type = proxy.PURPLE_PROXY_NONE
28
29     def get_NONE(self):
30         self.c_type = proxy.PURPLE_PROXY_NONE
31         return self
32     NONE = property(get_NONE)
33
34     def get_USE_GLOBAL(self):
35         self.c_type = proxy.PURPLE_PROXY_USE_GLOBAL
36         return self
37     USE_GLOBAL = property(get_USE_GLOBAL)
38
39     def get_HTTP(self):
40         self.c_type = proxy.PURPLE_PROXY_HTTP
41         return self
42     HTTP = property(get_HTTP)
43
44     def get_SOCKS4(self):
45         self.c_type = proxy.PURPLE_PROXY_SOCKS4
46         return self
47
48     def get_SOCKS5(self):
49         self.c_type = proxy.PURPLE_PROXY_SOCKS5
50         return self
51
52     def get_USE_ENVVAR (self):
53         self.c_type = proxy.PURPLE_PROXY_USE_ENVVAR
54         return self
55     USE_ENVVAR = property(get_USE_ENVVAR)
56
57 cdef class ProxyInfo:
58
59     cdef proxy.PurpleProxyInfo *c_proxyinfo
60     cdef object types
61
62     def __init__(self):
63         self.c_proxyinfo = NULL
64
65         self.types = {"HTTP": proxy.PURPLE_PROXY_HTTP,
66                 "USER_GLOBAL": proxy.PURPLE_PROXY_USE_GLOBAL,
67                 "USE_ENVVAR": proxy.PURPLE_PROXY_USE_ENVVAR,
68                 "SOCKS4": proxy.PURPLE_PROXY_SOCKS4,
69                 "SOCKS5": proxy.PURPLE_PROXY_SOCKS5,
70                 "NONE": proxy.PURPLE_PROXY_NONE}
71
72
73     def set_type(self, ProxyInfoType type):
74         if self.c_proxyinfo:
75             proxy.c_purple_proxy_info_set_type(self.c_proxyinfo, type.c_type)
76
77     def set_host(self, char *host):
78         if self.c_proxyinfo:
79             proxy.c_purple_proxy_info_set_host(self.c_proxyinfo, host)
80
81     def set_port(self, int port):
82         if self.c_proxyinfo:
83             proxy.c_purple_proxy_info_set_port(self.c_proxyinfo, port)
84
85     def set_username(self, char *username):
86         if self.c_proxyinfo:
87             proxy.c_purple_proxy_info_set_username(self.c_proxyinfo, username)
88
89     def set_password(self, char *password):
90         if self.c_proxyinfo:
91             proxy.c_purple_proxy_info_set_password(self.c_proxyinfo, password)
92
93     def get_types(self):
94         return self.types.keys()
95
96     def set_info(self, acc, info):
97         ''' @param acc Tuple (username, protocol id) '''
98         ''' @param info Dictionary {'type': "HTTP", 'port': "1234", '''
99         '''   'host': "1.2.3.4", 'username': "foo", 'passworld': "foo123"} '''
100
101         cdef account.PurpleAccount *c_account
102         cdef proxy.PurpleProxyInfo *c_proxyinfo
103         c_account = account.purple_accounts_find(acc[0], acc[1])
104
105         if c_account == NULL:
106             #FIXME: Message error or call a callback handle to error
107             return False
108
109         c_proxyinfo = account.purple_account_get_proxy_info(c_account)
110         if c_proxyinfo == NULL:
111                 c_proxyinfo = proxy.c_purple_proxy_info_new()
112                 account.purple_account_set_proxy_info(c_account, c_proxyinfo)
113
114         if 'type' in info and info['type']:
115             type = info['type']
116             if not type in self.types:
117                 type = 'HTTP'
118             proxy.c_purple_proxy_info_set_type(c_proxyinfo, self.types[type])
119
120         if 'host' in info and info['host']:
121             host = info['host']
122             proxy.c_purple_proxy_info_set_host(c_proxyinfo, host)
123
124         if 'port' in info and info['port']:
125             port = int(info['port'])
126             proxy.c_purple_proxy_info_set_port(c_proxyinfo, port)
127
128         if 'username' in info and info['username']:
129             username = info['username']
130             proxy.c_purple_proxy_info_set_username(c_proxyinfo, username)
131
132         if 'password' in info and info['password']:
133             password = info['password']
134             proxy.c_purple_proxy_info_set_password(c_proxyinfo, password)
135
136         return True