/
amsn.py
266 lines (219 loc) · 9.33 KB
/
amsn.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# -*- coding: utf-8 -*-
#
# amsn - a python client for the WLM Network
#
# Copyright (C) 2008 Dario Freddi <drf54321@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from amsn2 import gui
from amsn2 import protocol
from amsn2.backend import aMSNBackendManager
from views import *
from account_manager import *
from contactlist_manager import *
from conversation_manager import *
from oim_manager import *
from theme_manager import *
from personalinfo_manager import *
from event_manager import *
import papyon
import logging
# Top-level loggers
papyon_logger = logging.getLogger("papyon")
logger = logging.getLogger("amsn2")
class aMSNCore(object):
def __init__(self, options):
"""
Create a new aMSN Core. It takes an options class as argument
which has a variable for each option the core is supposed to received.
This is easier done using optparse.
The options supported are :
options.account = the account's username to use
options.password = the account's password to use
options.front_end = the front end's name to use
options.debug = whether or not to enable debug output
"""
self.p2s = {papyon.Presence.ONLINE:"online",
papyon.Presence.BUSY:"busy",
papyon.Presence.IDLE:"idle",
papyon.Presence.AWAY:"away",
papyon.Presence.BE_RIGHT_BACK:"brb",
papyon.Presence.ON_THE_PHONE:"phone",
papyon.Presence.OUT_TO_LUNCH:"lunch",
papyon.Presence.INVISIBLE:"hidden",
papyon.Presence.OFFLINE:"offline"}
self.Presence = papyon.Presence
self._event_manager = aMSNEventManager(self)
self._options = options
self._gui_name = None
self._gui = None
self._loop = None
self._main = None
self._account = None
self.loadUI(self._options.front_end)
self._backend_manager = aMSNBackendManager(self)
self._account_manager = aMSNAccountManager(self, options)
self._theme_manager = aMSNThemeManager(self)
self._contactlist_manager = aMSNContactListManager(self)
self._oim_manager = aMSNOIMManager(self)
self._conversation_manager = aMSNConversationManager(self)
self._personalinfo_manager = aMSNPersonalInfoManager(self)
# TODO: redirect the logs somewhere, something like ctrl-s ctrl-d for amsn-0.9x
logging.basicConfig(level=logging.WARNING)
if self._options.debug_protocol:
papyon_logger.setLevel(logging.DEBUG)
else:
papyon_logger.setLevel(logging.WARNING)
if self._options.debug_amsn2:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.WARNING)
def run(self):
self._main.show()
self._loop.run()
def loadUI(self, ui_name):
"""
@type ui_name: str
@param ui_name: The name of the User Interface
"""
self._gui_name = ui_name
self._gui = gui.GUIManager(self, self._gui_name)
if not self._gui.gui:
print "Unable to load UI %s" %(self._gui_name,)
self.quit()
self._loop = self._gui.gui.aMSNMainLoop(self)
self._main = self._gui.gui.aMSNMainWindow(self)
self._skin_manager = self._gui.gui.SkinManager(self)
def switchToUI(self, ui_name):
"""
@type ui_name: str
@param ui_name: The name of the User Interface
"""
#TODO: unloadUI + stop loops??? + loadUI + run
pass
def mainWindowShown(self):
# TODO : load the accounts from disk and all settings
# then show the login window if autoconnect is disabled
self._main.setTitle("aMSN 2 - Loading")
splash = self._gui.gui.aMSNSplashScreen(self, self._main)
image = ImageView()
image.load("Filename","/path/to/image/here")
splash.setImage(image)
splash.setText("Loading...")
splash.show()
login = self._gui.gui.aMSNLoginWindow(self, self._main)
login.setAccounts(self._account_manager.getAvailableAccountViews())
splash.hide()
self._main.setTitle("aMSN 2 - Login")
login.show()
menu = self.createMainMenuView()
self._main.setMenu(menu)
def getMainWindow(self):
return self._main
def signinToAccount(self, login_window, accountview):
"""
@type login_window: aMSNLoginWindow
@type accountview: AccountView
"""
print "Signing in to account %s" % (accountview.email)
self._account = self._account_manager.signinToAccount(accountview)
self._account.login = login_window
self._account.client = protocol.Client(self, self._account)
self._account.client.connect(accountview.email, accountview.password)
def signOutOfAccount(self):
self._account.client.logout()
self._account.signOut()
def connectionStateChanged(self, account, state):
"""
@type account: aMSNAccount
@type state: L{papyon.event.ClientState}
@param state: New state of the Client.
"""
status_str = \
{
papyon.event.ClientState.CONNECTING : 'Connecting to server...',
papyon.event.ClientState.CONNECTED : 'Connected',
papyon.event.ClientState.AUTHENTICATING : 'Authenticating...',
papyon.event.ClientState.AUTHENTICATED : 'Password accepted',
papyon.event.ClientState.SYNCHRONIZING : 'Please wait while your contact list\nis being downloaded...',
papyon.event.ClientState.SYNCHRONIZED : 'Contact list downloaded successfully.\nHappy Chatting'
}
if state in status_str:
account.login.onConnecting((state + 1)/ 7., status_str[state])
elif state == papyon.event.ClientState.OPEN:
clwin = self._gui.gui.aMSNContactListWindow(self, self._main)
clwin.account = account
account.clwin = clwin
account.login.hide()
self._main.setTitle("aMSN 2")
account.clwin.show()
account.login = None
self._personalinfo_manager.setAccount(account)
self._contactlist_manager.onCLDownloaded(account.client.address_book)
def idlerAdd(self, func):
"""
@type func: function
"""
self._loop.idlerAdd(func)
def timerAdd(self, delay, func):
"""
@type delay: int
@param delay: delay in seconds?
@type func: function
"""
self._loop.timerAdd(delay, func)
def quit(self):
if self._account:
self._account.signOut()
if self._loop:
self._loop.quit()
logging.shutdown()
exit(0)
# TODO: move to UImanager
def addContact(self):
def contactCB(account, invite_msg):
if account:
self._contactlist_manager.addContact(account, self._account.view.email,
invite_msg, [])
self._gui.gui.aMSNContactInputWindow(('Contact to add: ', 'Invite message: '),
contactCB, ())
def removeContact(self):
def contactCB(account):
if account:
try:
papyon_contact = self._contactlist_manager._papyon_addressbook.\
contacts.search_by('account', account)[0]
except IndexError:
self._gui.gui.aMSNErrorWindow('You don\'t have the %s contact!', account)
return
self._contactlist_manager.removeContact(papyon_contact.id)
self._gui.gui.aMSNContactDeleteWindow('Contact to remove: ', contactCB, ())
def createMainMenuView(self):
menu = MenuView()
quitMenuItem = MenuItemView(MenuItemView.COMMAND, label="Quit", command
= self.quit)
logOutMenuItem = MenuItemView(MenuItemView.COMMAND, label="Log out",
command = self.signOutOfAccount)
mainMenu = MenuItemView(MenuItemView.CASCADE_MENU, label="Main")
mainMenu.addItem(logOutMenuItem)
mainMenu.addItem(quitMenuItem)
addContactItem = MenuItemView(MenuItemView.COMMAND, label="Add Contact", command=self.addContact)
removeContact = MenuItemView(MenuItemView.COMMAND, label='Remove contact', command=self.removeContact)
contactsMenu = MenuItemView(MenuItemView.CASCADE_MENU, label="Contacts")
contactsMenu.addItem(addContactItem)
contactsMenu.addItem(removeContact)
menu.addItem(mainMenu)
menu.addItem(contactsMenu)
return menu