Updated hack to avoid recursive loops.
[python-purple] / nullclient.py
1 import purple
2 import ecore
3 import getpass
4 import sys
5 from xml.dom import minidom
6
7
8 class ClientModel:
9     def __init__(self):
10         self.purple = purple.Purple(debug_enabled=True)
11         self.purple.purple_init()
12         self.account = purple.Account()
13
14     def add_account(self, acc):
15         ''' @param acc: {'username': "foo@gmail.com", 'protocol': "XMPP"} '''
16
17         new_acc = self.account.new(acc['username'], acc['protocol'])
18
19         return new_acc
20
21     def get_protocols(self):
22         return self.account.protocol.get_all()
23
24     def get_protocol_options(self, protocol_id):
25         return self.account.protocol.get_options(protocol_id)
26
27     def set_account_info(self, acc, info):
28         ''' @param info: {protocol: 'prpl-jabber', alias: '', password: ' ', server: 'new server', port: '' } ||
29                     {server: 'new server'} '''
30
31         if info.has_key('protocol'):
32             self.account.set_protocol_id(acc, info['protocol'])
33         if info.has_key('alias'):
34             self.account.set_alias(acc, info['alias'])
35         if info.has_key('password'):
36             self.account.set_password(acc, info['password'])
37
38         self.account.protocol.set_options(acc, info)
39
40     def get_account_info(self, acc):
41         info = {}
42         po = self.account.protocol.get_options(acc[1], acc[0])
43         info['protocol'] = self.account.get_protocol_id(acc)
44         info['alias'] = self.account.get_alias(acc)
45         info['password'] = self.account.get_password(acc)
46         info['connect_server'] = po['connect_server'][1]
47         info['port'] = po['port'][1]
48
49         return info
50
51     def set_account_proxy(self, acc, info):
52         self.account.proxy.set_info(acc, info)
53
54     def account_connect(self, acc):
55         self.account.set_enabled(acc, "carman-purple-python", True)
56         # self.account.connect(acc)
57         self.purple.connect()
58
59     def account_disconnect(self, acc):
60         self.account.disconnect(acc)
61
62    
63 class ClientCtrl:
64     def __init__(self):
65         self.clientmodel = ClientModel()
66         new_acc = {}
67         acc_info = {}
68         new_acc['username'] = self.getuser()
69         new_acc['protocol'] = 'prpl-jabber'
70
71         acc = self.clientmodel.add_account(new_acc)
72
73         acc_info = self.clientmodel.get_account_info(acc)
74         acc_info['password'] = self.getpassword()
75         acc_info['connect_server'] = 'talk.google.com'
76         acc_info['port'] = 443
77         acc_info['old_ssl'] = True
78         self.clientmodel.set_account_info(acc, acc_info)
79
80         acc_proxy = {}
81         acc_proxy['type'] = 'HTTP' 
82         acc_proxy['host'] = '172.18.216.211'
83         acc_proxy['port'] = 8080
84         self.clientmodel.set_account_proxy(acc, acc_proxy)
85
86         self.clientmodel.account_connect(acc)
87
88     def getuser(self):
89         sys.stdout.write("GTalk account: ")
90         username = sys.stdin.readline()
91         return username[:-1]
92
93     def getpassword(self):
94         return getpass.getpass()
95
96 if __name__ == '__main__':
97
98     ctrl = ClientCtrl()
99
100     ecore.main_loop_begin()