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 simplejson as json
15 #from gi.repository import Notify
16 import wx.lib.agw.toasterbox as TB
17 import thread
18 import gettext
19 import time
20 import locale
21 if locale.getdefaultlocale()[0] == 'zh_CN':
22     gettext.install('messages', './locale', unicode=False)
23     gettext.translation('messages', './locale', languages=['cn']).install(True)
24 class prpcrypt():
25     def __init__(self, key):
26         self.key = key
27         self.mode = AES.MODE_CBC    
28     def decrypt(self, text):
29         cryptor = AES.new(self.key, self.mode, self.key)
30         plain_text = cryptor.decrypt(a2b_hex(text))
31         return plain_text.rstrip('\0')
32     def encrypt(self, text):
33         cryptor = AES.new(self.key, self.mode, self.key)
34         length = 32
35         count = len(text)
36         add = length - (count % length)
37         text = text + ('\0' * add)
38         self.ciphertext = cryptor.encrypt(text)
39         return b2a_hex(self.ciphertext)
40
41 class LoginFrame(wx.Frame):
42     def __init__(self, parent, id, title, size):
43         wx.Frame.__init__(self, parent, id, title)
44         self.SetSize(size)
45         self.Center()
46         self.passWordLabel = wx.StaticText(self, label = _("UserName"), pos = (30, 40), size = (120, 25))
47         self.userNameLabel = wx.StaticText(self, label = _("Password"), pos = (30, 90), size = (120, 25))
48         self.userName = wx.TextCtrl(self, pos = (85, 37), size = (150, 25),style=wx.TE_PROCESS_ENTER)
49         self.passWord= wx.TextCtrl(self, pos = (85, 87), size = (150, 25),style=wx.TE_PASSWORD|wx.TE_PROCESS_ENTER)
50         self.userName.Bind(wx.EVT_TEXT_ENTER,self.login_thread)
51         self.passWord.Bind(wx.EVT_TEXT_ENTER,self.login_thread)
52         self.cb = wx.CheckBox(self, pos=(30,120) ,label="I am new user", size=(120,25))
53         self.loginButton = wx.Button(self, label = _('Login'), pos = (80, 145), size = (130, 30))
54         self.loginButton.Bind(wx.EVT_BUTTON,self.login_thread)
55         pub.subscribe(self.__Friend_list, 'list.show')
56         self.Show()   
57     def __Friend_list(self, data):
58        #print 'Object', message.data, 'is added'
59        #print data
60        self.Hide()
61        time.sleep(0.1)
62        frame = FriendList.MyFrame(None, id=-1, title=_("Friend List"),user=data,un=self.userName.GetValue())
63        frame.Show(True)
64     def login_thread(self,event):
65             thread.start_new_thread(self.login, ())
66             self.loginButton.Disable()
67     def login(self):
68          if not self.userName.GetValue():
69                    wx.CallAfter(wx.MessageBox,_('Please enter the username'), _('Error'), 
70                    wx.OK | wx.ICON_ERROR)
71                    wx.CallAfter(self.loginButton.Enable)
72          elif not self.passWord.GetValue():
73                     wx.CallAfter(wx.MessageBox,_('Please enter the password'), _('Error'), 
74                     wx.OK | wx.ICON_ERROR)     
75                     wx.CallAfter(self.loginButton.Enable)      
76          else:
77            if self.cb.IsChecked()==False:
78               try:
79                 passwd = urllib2.urlopen("http://chat-tyl.coding.io/put_db.php?content=PASS&db=USER&where=NAME&where_t=" + self.userName.GetValue()).read()
80               except urllib2.HTTPError,e:
81                 wx.CallAfter(wx.MessageBox,_('Unable to fetch data'),_('Error'), wx.OK | wx.ICON_ERROR)
82                 wx.CallAfter(self.loginButton.Enable)                                   
83               passwd0 = pc.decrypt(passwd)
84               if self.passWord.GetValue()==passwd0: 
85                  #urllib2.urlopen('http://chat-tyl.coding.io/user_log?info=User___'+self.userName.GetValue()+'___Login')
86                  try:
87                    data = urllib2.urlopen("http://chat-tyl.coding.io/put_db.php?content=LIST&db=FRIEND&where=NAME&where_t=" + self.userName.GetValue()).read()
88                  except urllib2.HTTPError,e:
89                    wx.CallAfter(wx.MessageBox,_('Unable to fetch data'),_('Error'), wx.OK | wx.ICON_ERROR)
90                    wx.CallAfter(self.loginButton.Enable)
91                  time.sleep(0.1)
92                  wx.CallAfter(pub.sendMessage,'list.show', data=json.loads(data))
93               else:
94                 wx.CallAfter(wx.MessageBox,_('Your Password is wrong'), _('Try it again'), 
95                 wx.OK | wx.ICON_ERROR)
96                 wx.CallAfter(self.loginButton.Enable) 
97            else:
98              try:
99                cont = urllib2.urlopen("http://chat-tyl.coding.io/put_db.php?content=PASS&db=USER&where=NAME&where_t=" + self.userName.GetValue()).read()
100              except urllib2.HTTPError,e:
101                wx.CallAfter(wx.MessageBox,_('Check Your NetWork,and try it again'),_('Error'), wx.OK | wx.ICON_ERROR)
102                wx.CallAfter(self.loginButton.Enable)
103              if not cont:   
104                try:
105                 passwd = urllib2.urlopen("http://chat-tyl.coding.io/in_db.php?db=USER&first=NAME&seconed=PASS&name=" + self.userName.GetValue() + "&pass=" + pc.encrypt(self.passWord.GetValue())).read()
106                except urllib2.HTTPError,e:
107                  wx.CallAfter(wx.MessageBox,_("We can't register,check your network and try it again"),_('Error'), wx.OK | wx.ICON_ERROR)
108                  wx.CallAfter(self.loginButton.Enable)    
109                try:
110                 list = urllib2.urlopen("http://chat-tyl.coding.io/in_db.php?db=FRIEND&first=NAME&seconed=LIST&name=" + self.userName.GetValue() + "&pass={'item': ['friend'],'friend' : ['tyl','Test']}").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"),_('Error'), wx.OK | wx.ICON_ERROR)
115                wx.CallAfter(self.loginButton.Enable)
116              else:
117                wx.CallAfter(wx.MessageBox,_('You already registered'),_('Error'), wx.OK | wx.ICON_INFORMATION)
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()