2012-04-05 04:56:21 +02:00
import urllib
import urllib2
import getpass
import sys
2012-07-18 17:01:10 +02:00
from networking import PacketSenderManager , NetworkManager
import NoGUIstuff
2012-04-06 01:04:51 +02:00
import time
2012-04-05 04:56:21 +02:00
import threading
2012-09-08 00:07:17 +02:00
wxImportError = False
2012-05-31 14:50:01 +02:00
try :
import wx
2012-09-08 00:07:17 +02:00
import GUI
2012-07-17 23:42:42 +02:00
wx = wx
2012-05-31 14:50:01 +02:00
except ImportError :
print " wxPython not found, falling back to no gui version "
2012-09-08 00:07:17 +02:00
wxImportError = True
2012-07-17 23:42:42 +02:00
2012-04-05 04:56:21 +02:00
class MinecraftLoginThread ( threading . Thread ) :
2012-04-16 22:09:43 +02:00
def __init__ ( self , window , rotationthread , username , password ) :
2012-04-05 04:56:21 +02:00
threading . Thread . __init__ ( self )
self . username = username
self . password = password
2012-04-16 22:09:43 +02:00
self . window = window
self . rotationthread = rotationthread
2012-04-05 04:56:21 +02:00
def run ( self ) :
url = ' https://login.minecraft.net '
header = { ' Content-Type ' : ' application/x-www-form-urlencoded ' }
data = { ' user ' : self . username ,
' password ' : self . password ,
' version ' : ' 13 ' }
2012-04-06 12:25:30 +02:00
try :
data = urllib . urlencode ( data )
req = urllib2 . Request ( url , data , header )
opener = urllib2 . build_opener ( )
response = opener . open ( req )
response = response . read ( )
except urllib2 . URLError :
2012-04-16 22:09:43 +02:00
self . rotationthread . Kill = True
self . window . Status . SetForegroundColour ( wx . RED )
self . window . Status . SetLabel ( ' Connection to minecraft.net failed ' )
2012-04-06 12:25:30 +02:00
return
2012-04-05 04:56:21 +02:00
if ( response == " Bad login " ) :
2012-04-16 22:09:43 +02:00
self . rotationthread . Kill = True
self . window . Status . SetForegroundColour ( wx . RED )
self . window . Status . SetLabel ( ' Incorrect username or password ' )
2012-04-05 04:56:21 +02:00
return
2012-07-18 16:53:40 +02:00
if ( response == " Account migrated, use e-mail as username. " ) :
self . rotationthread . Kill = True
self . window . Status . SetForegroundColour ( wx . RED )
self . window . Status . SetLabel ( ' Account migrated, use e-mail as username. ' )
2012-04-05 04:56:21 +02:00
response = response . split ( " : " )
2012-04-16 22:09:43 +02:00
username = response [ 2 ]
sessionID = response [ 3 ]
2012-05-31 14:50:01 +02:00
#KeepConnectionAlive(self.username, self.password, self.window).start()
self . window . parent . username = username
self . window . parent . sessionID = sessionID
2012-04-16 22:09:43 +02:00
self . window . loggedIn = True
self . window . handlePostLogin ( self . rotationthread )
2012-04-06 01:04:51 +02:00
class KeepConnectionAlive ( threading . Thread ) :
2012-05-31 14:50:01 +02:00
def __init__ ( self , username , password , window ) :
2012-04-06 01:04:51 +02:00
threading . Thread . __init__ ( self )
self . username = username
self . password = password
2012-05-31 14:50:01 +02:00
self . window = window
2012-04-06 01:04:51 +02:00
def run ( self ) :
2012-04-07 16:02:46 +02:00
while True :
time . sleep ( 300 )
2012-04-06 01:04:51 +02:00
url = ' https://login.minecraft.net '
header = { ' Content-Type ' : ' application/x-www-form-urlencoded ' }
data = { ' user ' : self . username ,
' password ' : self . password ,
' version ' : ' 13 ' }
2012-04-06 12:25:30 +02:00
try :
data = urllib . urlencode ( data )
req = urllib2 . Request ( url , data , header )
opener = urllib2 . build_opener ( )
2012-05-31 14:50:01 +02:00
response = opener . open ( req )
response = response . read ( )
2012-04-06 12:25:30 +02:00
except urllib2 . URLError :
popup = wx . MessageBox ( ' Keep alive to minecraft.net failed ' , ' Warning ' ,
wx . OK | wx . ICON_ERROR )
2012-05-31 14:50:01 +02:00
popup . ShowModal ( )
if ( response == " Bad login " ) :
popup = wx . MessageBox ( ' Keep alive to minecraft.net failed (Incorrect username/password) ' , ' Warning ' ,
wx . OK | wx . ICON_ERROR )
popup . ShowModal ( )
return
response = response . split ( " : " )
sessionID = response [ 3 ]
if ( self . window != None ) :
self . window . parent . sessionID = sessionID
self . window . parent . loggedIn = True
2012-04-30 19:05:06 +02:00
2012-04-05 04:56:21 +02:00
if __name__ == " __main__ " :
2012-09-08 00:07:17 +02:00
noGUI = False
2012-04-11 22:22:58 +02:00
if ( len ( sys . argv ) > 1 ) :
2012-09-11 17:03:36 +02:00
if ( sys . argv . count ( " nogui " ) > 0 ) :
2012-09-08 00:07:17 +02:00
noGUI = True
if ( noGUI or wxImportError ) :
user = raw_input ( " Enter your username: " )
passwd = getpass . getpass ( " Enter your password: " )
derp = NoGUIstuff . loginToMinecraft ( user , passwd )
if ( derp [ ' Response ' ] == " Incorrect username/password " or derp [ ' Response ' ] == " Can ' t connect to minecraft.net " or derp [ ' Response ' ] == " Account migrated, use e-mail as username. " ) :
print derp [ ' Response ' ]
sys . exit ( )
sessionid = derp [ ' SessionID ' ]
print " Logged in as " + derp [ ' Username ' ] + " ! Your session id is: " + sessionid
stuff = raw_input ( " Enter host and port if any: " )
if ' : ' in stuff :
StuffEnteredIntoBox = stuff . split ( " : " )
host = StuffEnteredIntoBox [ 0 ]
port = int ( StuffEnteredIntoBox [ 1 ] )
else :
host = stuff
port = 25565
connection = NetworkManager . ServerConnection ( None , derp [ ' Username ' ] , passwd , sessionid , host , port )
connection . start ( )
2012-09-12 02:11:58 +02:00
while True :
try :
2012-10-09 17:16:37 +02:00
chat_input = raw_input ( )
if ( connection . isConnected ) :
PacketSenderManager . send03 ( connection . grabSocket ( ) , chat_input )
else :
pass
2012-09-12 02:11:58 +02:00
except KeyboardInterrupt , e :
connection . disconnect ( )
sys . exit ( 1 )
2012-04-11 22:22:58 +02:00
else :
2012-04-30 19:05:06 +02:00
app = wx . PySimpleApp ( )
Login = GUI . MainFrame ( None , - 1 , " " )
2012-04-16 22:09:43 +02:00
app . SetTopWindow ( Login )
Login . Show ( )
2012-04-11 22:22:58 +02:00
app . MainLoop ( )
2012-07-17 23:42:42 +02:00
app2 = wx . PySimpleApp ( )
ChatPanel = GUI . ServerChatPanel ( None , - 1 , " pyCraft " )
app2 . SetTopWindow ( ChatPanel )
ChatPanel . Show ( )
app2 . MainLoop ( )