OSDN Git Service

为好友列表增加菜单
[chatyl/chatyl.git] / LoginGui.py
1 #!/usr/bin/python
2 # encoding: utf-8
3 import os
4 import wx
5 from wx.lib.pubsub import setupkwargs
6 from wx.lib.pubsub import pub
7 import sys
8 import ssl
9 import FriendList
10 from Crypto.Cipher import AES
11 from binascii import b2a_hex, a2b_hex
12 #import MySQLdb
13 import urllib2
14 import urllib
15 import simplejson as json
16 #from gi.repository import Notify
17 import wx.lib.agw.toasterbox as TB
18 import thread
19 import gettext
20 import time
21 import locale
22 if locale.getdefaultlocale()[0] == 'zh_CN':
23     gettext.install('messages', './locale', unicode=False)
24     gettext.translation('messages', './locale', languages=['cn']).install(True)
25 class prpcrypt():
26     def __init__(self, key):
27         self.key = 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)
35         length = 32
36         count = len(text)
37         add = length - (count % length)
38         text = text + ('\0' * add)
39         self.ciphertext = cryptor.encrypt(text)
40         return b2a_hex(self.ciphertext)
41
42 class LoginFrame(wx.Frame):
43     def __init__(self, parent, id, title, size):
44         wx.Frame.__init__(self, parent, id, title)
45         self.SetSize(size)
46         self.Center()
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')
58         self.Show()   
59     def __Friend_list(self, data):
60        #print 'Object', message.data, 'is added'
61        #print data
62        self.Hide()
63        time.sleep(0.1)
64        frame = FriendList.MyFrame(None, id=-1, title=_("Friend List"),user=data,un=self.userName.GetValue())
65        frame.Show(True)
66     def login_thread(self,event):
67             thread.start_new_thread(self.login, ())
68             self.loginButton.Disable()
69     def login(self):
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)      
78          else:
79            if self.cb.IsChecked()==False:
80               try:
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')
88                  try:
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)
93                  time.sleep(0.1)
94                  wx.CallAfter(pub.sendMessage,'list.show', data=json.loads(data))
95               else:
96                 wx.CallAfter(wx.MessageBox,_('Your Password is wrong'), _('Try it again'), 
97                 wx.OK | wx.ICON_ERROR)
98                 wx.CallAfter(self.loginButton.Enable) 
99            else:
100              try:
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)
105              if not cont:   
106                try:
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)
116              else:
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')
121     app = wx.App()
122     LoginFrame(None, -1, title = _("Login"), size = (280, 210))
123     app.MainLoop()