git » msnlib » master » tree

[master] / utils / msntk

  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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python

import sys
import time
import string
import socket
import select
from Tkinter import *
import tkMessageBox
import tkSimpleDialog

import msnlib
import msncb

"""
MSN Tk Client

This is a beta msn client based on msnlib. As you see, it's GUI based on the
Tk bindings, which provide an abstraction to create graphical interfaces; it
works both under linux, windows and probably others too.

For further information refer to the documentation or the source (which is
always preferred).
Please direct any comments to albertito@blitiri.com.ar.
You can find more information, and the package itself, at
http://blitiri.com.ar/p/msnlib/
"""


# main msnlib classes
m = msnlib.msnd()
m.cb = msncb.cb()

# void debug output
#def void(s): pass
#msnlib.debug = msncb.debug = void



#
# useful functions
#

#sys.setdefaultencoding("iso-8859-15")
encoding = 'iso-8859-1'

def encode(s):
	try:
		return s.decode(encoding).encode('utf-8')
	except:
		return s

def decode(s):
	try:
		return s.decode('utf-8').encode(encoding)
	except:
		return s

def nick2email(nick):
	"Returns an email according to the given nick, or None if noone matches"
	for email in m.users.keys():
		if str(m.users[email].nick) == str(nick):
			return email
	if nick in m.users.keys():
		return nick
	return None

def email2nick(email):
	"Returns a nick accoriding to the given email, or None if noone matches"
	if email in m.users.keys():
		return m.users[email].nick
	else:
		return None

def now():
	"Returns the current time in format HH:MM:SSTT"
	return time.strftime('%I:%M:%S%p', time.localtime(time.time()) )

def quit():
	"Cleans up and quits everything"
	try:
		m.disconnect()
	except:
		pass
	root.quit()
	sys.exit(0)



#
# GUI classes
#

class userlist(Frame):
	"The user list"
	def __init__(self, master):
		Frame.__init__(self, master)
		self.scrollbar = Scrollbar(self, orient = VERTICAL)
		self.list = Listbox(self, 
				yscrollcommand = self.scrollbar.set)
		self.list.config(font = "Courier")
		self.scrollbar.config(command = self.list.yview)
		self.scrollbar.pack(side = RIGHT, fill = Y)
		self.list.pack(side = LEFT, fill = BOTH, expand = 1)
		
		self.list.bind("<Double-Button-1>", self.create_chat)
			
	def create_chat(self, evt = None):
		"Creates a chat window"
		if m.status == 'HDN':
			tkMessageBox.showwarning("Warning", 
				"You can't open chats when you're invisible")
			return
		nick = self.list.get(self.list.curselection())[4:]
		email = nick2email(nick)
		if email in emwin.keys():
			emwin[email].lift()
		elif m.users[email].status == 'FLN':
			tkMessageBox.showwarning("Warning",
				"The user is offline")
		else:
			emwin[email] = chatwindow(root, email)
	

class mainmenu(Menu):
	"Main menu used in the main window"
	def __init__(self, master):
		Menu.__init__(self, master)
		self.status_menu = Menu(self, tearoff = 0)
		self.add_cascade(label = "Status", menu = self.status_menu)
		self.status_menu.add_command(label = "Online",
			command = self.chst_online)
		self.status_menu.add_command(label = "Away",
			command = self.chst_away)
		self.status_menu.add_command(label = "Busy",
			command = self.chst_busy)
		self.status_menu.add_command(label = "Be Right Back",
			command = self.chst_brb)
		self.status_menu.add_command(label = "Lunch",
			command = self.chst_lunch)
		self.status_menu.add_command(label = "Phone", 
			command = self.chst_phone)
		self.status_menu.add_command(label = "Invisible", 
			command = self.chst_invisible)
			
		self.add_command(label = 'Info', command = self.show_info)
	
	def show_info(self, evt = None):
		csel = mainlist.list.curselection()
		if not csel:
			return
		nick = mainlist.list.get(csel)[4:]
		email = nick2email(nick)
		infowindow(root, email)

	# status change callbacks
	def clear_heads(self):
		for i in emwin.keys():
			emwin[i].head.config(text = '')
	
	def chst_online(self):
		self.clear_heads()
		m.change_status('online')
	def chst_away(self):
		self.clear_heads()
		m.change_status('away')
	def chst_busy(self):
		self.clear_heads()
		m.change_status('busy')
	def chst_brb(self):
		self.clear_heads()
		m.change_status('brb')
	def chst_lunch(self):
		self.clear_heads()
		m.change_status('lunch')
	def chst_phone(self):
		self.clear_heads()
		m.change_status('phone')
	def chst_invisible(self):
		warn = "Warning: as you are invisible, it is possible that\n"
		warn += "the messages you type here never get to the user."
		for i in emwin.keys():
			emwin[i].head.config(text = warn)
		m.change_status('invisible')


class chatwindow(Toplevel):
	"Represents a chat window"
	def __init__(self, master, email):
		Toplevel.__init__(self, master)
		self.email = email
		self.protocol("WM_DELETE_WINDOW", self.destroy_window)
		nick = email2nick(email)
		# FIXME: update the title with status change
		status = msnlib.reverse_status[m.users[email].status]
		if nick:
			self.wm_title(nick + ' (' + status + ')')
		else:
			self.wm_title(email + ' (' + status + ')')

		# head label
		self.head = Label(self)
		self.head.pack(side = TOP, fill = X, expand = 0)
		self.head.config(justify = LEFT)
		self.head.config(text = "")

		# text box (with scrollbar), where the message goes
		self.frame = Frame(self)
		self.scrollbar = Scrollbar(self.frame, orient = VERTICAL)
		self.text = Text(self.frame, 
				yscrollcommand = self.scrollbar.set)
		self.scrollbar.config(command = self.text.yview)
		self.scrollbar.pack(side = RIGHT, fill = Y)
		self.text.pack(side = TOP, fill = BOTH, expand = 1)
		self.frame.pack(side = TOP, fill = BOTH, expand = 1)
		
		self.text.config(state = DISABLED)
		self.text.tag_config('from', foreground = 'blue')
		self.text.tag_config('to', foreground = 'red')
		self.text.tag_config('typing', foreground = 'lightblue')
		
		# entry, where the user types
		self.entry = Entry(self)
		self.entry.pack(side = BOTTOM, fill = X, expand = 0)
		self.entry.bind('<Return>', self.send_line)
	
	def append(self, s, direction, scroll = 1):
		"Adds text to the window's text box"
		self.text.config(state = NORMAL)
		self.text.insert(END, s, direction)
		self.text.yview(SCROLL, scroll, UNITS)
		self.text.config(state = DISABLED)
	
	def send_line(self, evt = None):
		"Sends the current entry as a message"
		msg = self.entry.get()
		lines = msg.split('\n')
		if len(lines) == 1:
			s = now() + ' >>> ' + msg + '\n'
		else:
			s = now() + ' >>>\n\t'
			s += string.join(lines, '\n\t')
			s = s[:-1]
		self.append(s, 'to', scroll = len(lines))
		
		# we need to encode it before sending because msg is already
		# an unicode string; so use utf-8
		msg = msg.encode('utf-8')

		m.sendmsg(self.email, msg)
		self.entry.delete(0, END)
	
	def destroy_window(self, evt = None):
		"Clean up when the window is closed"
		del(emwin[self.email])
		self.destroy()


class infowindow(Toplevel):
	"Represents a window with user information"
	def __init__(self, master, email):
		Toplevel.__init__(self, master)
		self.email = email
		self.wm_title('Info on ' + email)
		u = m.users[email]
		out = ''
		out += 'Information for user ' + email + '\n\n'
		out += 'Nick: ' + u.nick + '\n'
		out += 'Status: ' + msnlib.reverse_status[u.status] + '\n'
		if 'B' in u.lists:
			out += 'Mode: ' + 'blocked' + '\n'
		if u.gid != None:
			out += 'Group: ' + m.groups[u.gid] + '\n'
		if u.realnick:
			out += 'Real Nick: ' + u.realnick + '\n'
		if u.homep:
			out += 'Home phone: ' + u.homep + '\n'
		if u.workp:
			out += 'Work phone: ' + u.workp + '\n'
		if u.mobilep:
			out += 'Mobile phone: ' + u.mobilep + '\n'

		self.label = Label(self)
		self.label.pack(side = TOP, fill = BOTH, expand = 1)
		self.label.config(justify = LEFT)
		self.label.config(text = out)


def redraw_main():
	"Redraws the main screen"
	# sync the user list - FIXME: instead of redrawing, use the callbacks
	# for status change notifications
	nicks = []
	for i in m.users.keys():
		if m.users[i].status == 'FLN':
			s = '[X] '
		elif m.users[i].status in ('NLN', 'IDL'):
			s = '[ ] '
		else:
			s = '[-] '
		if 'B' in m.users[i].lists:
			s = '[!] '
		
		s += m.users[i].nick
		nicks.append(s)
	nicks.sort()
	mainlist.list.delete(0, END)
	for i in nicks:
		mainlist.list.insert(END, i)
	
	# update status
	s = msnlib.reverse_status[m.status]
	status.config(text = s)



#
# callbacks
#

def cb_msg(md, type, tid, params, sbd):
	"Gets a message"
	t = tid.split(' ')
	email = t[0]

	# parse
	lines = params.split('\n')
	headers = {} 
	eoh = 0
	for i in lines:
		# end of headers
		if i == '\r':
			break
		tv = i.split(':', 1)
		type = tv[0]
		value = tv[1].strip()
		headers[type] = value
		eoh += 1
	eoh +=1

	# ignore hotmail messages
	if email == 'Hotmail':
		return
	
	if email not in emwin.keys():
		emwin[email] = chatwindow(root, email)
		
	# typing notifications
	if (headers.has_key('Content-Type') and 
			headers['Content-Type'] == 'text/x-msmsgscontrol'):
		if not m.users[email].priv.has_key('typing'):
			m.users[email].priv['typing'] = 1
			msg = now() + ' --- is typing\n'
			emwin[email].append(msg, 'typing')
			
	# normal message
	else:
		if len(lines[eoh:]) > 1:
			msg = now() + ' <<<\n\t'
			msg += string.join(lines[eoh:], '\n\t')
			msg = msg.replace('\r', '')
		else:
			msg = now() + ' <<< ' + lines[eoh] + '\n'
			
		if m.users[email].priv.has_key('typing'):
			del(m.users[email].priv['typing'])
			
		emwin[email].append(msg, 'from')
		root.bell()

	msncb.cb_msg(md, type, tid, params, sbd)
m.cb.msg = cb_msg



#
# main
#

# email - chatwindow dictionary
emwin = {}

# gui init
root = Tk()
root.wm_title('msnlib')

mainlist = userlist(root)
mainlist.pack(side = TOP, fill = BOTH, expand = 1)

status = Label(root, text = "logging in...", bd=1, relief = SUNKEN, anchor = W)
status.pack(side = BOTTOM, fill = X, expand = 0)

menu = mainmenu(root)
root.config(menu = menu)

# initial update, to display at least something while we log in
root.update()

# ask for username and password if not given in the command line
if len(sys.argv) < 3:
	m.email = tkSimpleDialog.askstring("Username",
		"Please insert your email")
	if not m.email:
		quit()
	
	m.pwd = tkSimpleDialog.askstring("Password",
		"Please insert your password")
	if not m.pwd:
		quit()
else:
	m.email = sys.argv[1]
	m.pwd = sys.argv[2]

m.email = m.email.strip()
m.pwd = m.pwd.strip()

# the encoding is utf-8 because the text class uses unicode directly
m.encoding = 'utf-8'

root.update()

# login
try:
	m.login()
	m.sync()
except msnlib.AuthError:
	tkMessageBox.showerror("Login", "Error logging in: wrong password")
	quit()

# start as invisible
m.change_status('invisible')


# main loop
while 1:
	fds = m.pollable()
	infd = fds[0]
	outfd = fds[1]
	
	try:
		# both network and gui checks
		fds = select.select(infd, outfd, [], 0)
		root.update()
	except KeyboardInterrupt:
		quit()
	except TclError:
		quit()

	for i in fds[0] + fds[1]:
		try:
			m.read(i)
		except (msnlib.SocketError, socket.error), err:
			if i != m:
				m.close(i)
			else:
				tkMessageBox.showwarning("Warning",
					"Server disconnected us - you " +
					"probably logged in somewhere else")
				quit()
		
		# always redraw after a network event
		redraw_main()
	
	# sleep a bit so we don't take over the cpu
	time.sleep(0.05)