5 from wx.lib.pubsub import setupkwargs
6 from wx.lib.pubsub import pub
10 from Crypto.Cipher import AES
11 from binascii import b2a_hex, a2b_hex
15 import simplejson as json
16 #from gi.repository import Notify
17 import wx.lib.agw.toasterbox as TB
22 if locale.getdefaultlocale()[0] == 'zh_CN':
23 gettext.install('messages', './locale', unicode=False)
24 gettext.translation('messages', './locale', languages=['cn']).install(True)
26 def __init__(self, key):
28 self.mode = AES.MODE_CBC
29 def decrypt(self, text):
30 cryptor = AES.new(self.key, self.mode, self.key)
31 plain_text = cryptor.decrypt(a2b_hex(text))
32 return plain_text.rstrip('\0')
33 def encrypt(self, text):
34 cryptor = AES.new(self.key, self.mode, self.key)
37 add = length - (count % length)
38 text = text + ('\0' * add)
39 self.ciphertext = cryptor.encrypt(text)
40 return b2a_hex(self.ciphertext)
42 class LoginFrame(wx.Frame):
43 def __init__(self, parent, id, title, size):
44 wx.Frame.__init__(self, parent, id, title)
47 self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_3DLIGHT ))
48 self.passWordLabel = wx.StaticText(self, label = _("UserName"), pos = (30, 40), size = (120, 25))
49 self.userNameLabel = wx.StaticText(self, label = _("Password"), pos = (30, 90), size = (120, 25))
50 self.userName = wx.TextCtrl(self, pos = (85, 37), size = (150, 25),style=wx.TE_PROCESS_ENTER)
51 self.passWord= wx.TextCtrl(self, pos = (85, 87), size = (150, 25),style=wx.TE_PASSWORD|wx.TE_PROCESS_ENTER)
52 self.userName.Bind(wx.EVT_TEXT_ENTER,self.login_thread)
53 self.passWord.Bind(wx.EVT_TEXT_ENTER,self.login_thread)
54 self.cb = wx.CheckBox(self, pos=(30,120) ,label="I am new user", size=(120,25))
55 self.loginButton = wx.Button(self, label = _('Login'), pos = (80, 145), size = (130, 30))
56 self.loginButton.Bind(wx.EVT_BUTTON,self.login_thread)
57 pub.subscribe(self.__Friend_list, 'list.show')
59 def __Friend_list(self, data):
60 #print 'Object', message.data, 'is added'
64 frame = FriendList.MyFrame(None, id=-1, title=_("Friend List"),user=data,un=self.userName.GetValue())
66 def login_thread(self,event):
67 thread.start_new_thread(self.login, ())
68 self.loginButton.Disable()
70 if not self.userName.GetValue():
71 wx.CallAfter(wx.MessageBox,_('Please enter the username'), _('Error'),
72 wx.OK | wx.ICON_ERROR)
73 wx.CallAfter(self.loginButton.Enable)
74 elif not self.passWord.GetValue():
75 wx.CallAfter(wx.MessageBox,_('Please enter the password'), _('Error'),
76 wx.OK | wx.ICON_ERROR)
77 wx.CallAfter(self.loginButton.Enable)
79 if self.cb.IsChecked()==False:
81 passwd = urllib2.urlopen("http://chat-tyl.coding.io/put_db.php?content=PASS&db=USER&where=NAME&where_t=" + self.userName.GetValue()).read()
82 except urllib2.HTTPError,e:
83 wx.CallAfter(wx.MessageBox,_('Unable to fetch data'),_('Error'), wx.OK | wx.ICON_ERROR)
84 wx.CallAfter(self.loginButton.Enable)
85 passwd0 = pc.decrypt(passwd)
86 if self.passWord.GetValue()==passwd0:
87 #urllib2.urlopen('http://chat-tyl.coding.io/user_log?info=User___'+self.userName.GetValue()+'___Login')
89 data = urllib2.urlopen("http://chat-tyl.coding.io/put_db.php?content=FRIEND&db=USER&where=NAME&where_t=" + self.userName.GetValue()).read()
90 except urllib2.HTTPError,e:
91 wx.CallAfter(wx.MessageBox,_('Unable to fetch data'),_('Error'), wx.OK | wx.ICON_ERROR)
92 wx.CallAfter(self.loginButton.Enable)
94 wx.CallAfter(pub.sendMessage,'list.show', data=json.loads(data))
96 wx.CallAfter(wx.MessageBox,_('Your Password is wrong'), _('Try it again'),
97 wx.OK | wx.ICON_ERROR)
98 wx.CallAfter(self.loginButton.Enable)
101 cont = urllib2.urlopen("http://chat-tyl.coding.io/put_db.php?content=PASS&db=USER&where=NAME&where_t=" + self.userName.GetValue()).read()
102 except urllib2.HTTPError,e:
103 wx.CallAfter(wx.MessageBox,_('Check Your NetWork,and try it again'),_('Error'), wx.OK | wx.ICON_ERROR)
104 wx.CallAfter(self.loginButton.Enable)
107 req = urllib2.Request("http://chat-tyl.coding.io/in_db.php")
108 data = urllib.urlencode({'db':'USER','name':self.userName.GetValue(),'pass':pc.encrypt(self.passWord.GetValue()),'friend':"{\"item\":[\"friend\"],\"friend\":[\"tyl\",\"Test\"]}",'avatar':'default','info':"{\"name\":[\"" + self.userName.GetValue() + "\"]}"})
109 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
110 opener.open(req, data).read()
111 except urllib2.HTTPError,e:
112 wx.CallAfter(wx.MessageBox,_("We can't register,check your network and try it again"),_('Error'), wx.OK | wx.ICON_ERROR)
113 wx.CallAfter(self.loginButton.Enable)
114 wx.CallAfter(wx.MessageBox,_("Register Successful"),_('Information'), wx.OK | wx.ICON_INFORMATION)
115 wx.CallAfter(self.loginButton.Enable)
117 wx.CallAfter(wx.MessageBox,_('You already registered'),_('Error'), wx.OK | wx.ICON_ERROR)
118 wx.CallAfter(self.loginButton.Enable)
119 if __name__ == '__main__':
120 pc = prpcrypt('keyskeyskeyskeys')
122 LoginFrame(None, -1, title = _("Login"), size = (280, 210))