Advertisement
Azelphur

Untitled

Feb 18th, 2012
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. ###
  2. # Copyright (c) 2012, Alfie "Azelphur" Day
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. #   * Redistributions of source code must retain the above copyright notice,
  9. #     this list of conditions, and the following disclaimer.
  10. #   * Redistributions in binary form must reproduce the above copyright notice,
  11. #     this list of conditions, and the following disclaimer in the
  12. #     documentation and/or other materials provided with the distribution.
  13. #   * Neither the name of the author of this software nor the name of
  14. #     contributors to this software may be used to endorse or promote products
  15. #     derived from this software without specific prior written consent.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28.  
  29. ###
  30.  
  31. import supybot.utils as utils
  32. from supybot.commands import *
  33. import supybot.plugins as plugins
  34. import supybot.ircutils as ircutils
  35. import supybot.callbacks as callbacks
  36. import urllib2, socket
  37.  
  38. LOGIN_URL = 'https://login.minecraft.net/?user=&password='
  39.  
  40. SESSION_URL = 'http://session.minecraft.net/game/joinserver.jsp'
  41.  
  42. MINECRAFT_ADDRESS = 'Minecraft.azelphur.com'
  43. MINECRAFT_PORT = 25567
  44.  
  45. class Minecraft(callbacks.Plugin):
  46.     """Add the help for "@plugin help Minecraft" here
  47.    This should describe *how* to use this plugin."""
  48.     threaded = True
  49.     def mcstatus(self, irc, msg, args):
  50.         login = 'Online'
  51.         session = 'Online'
  52.         server = 'Online'
  53.  
  54.         try:
  55.             u = urllib2.urlopen(LOGIN_URL)
  56.             if u.read() != 'Bad login':
  57.                 login = 'Behaving oddly'
  58.         except urllib2.HTTPError, code:
  59.             login = 'Offline (HTTP Code %d)' % (code)
  60.         except urllib2.URLError, code
  61.             login = 'Offline (Error %d, %s)' % (code[0], code[1])
  62.  
  63.         try:
  64.             u = urllib2.urlopen(SESSION_URL)
  65.             if u.read() != 'Bad login':
  66.                 session = 'Behaving oddly'
  67.         except urllib2.HTTPError, code:
  68.             session = 'Offline (HTTP Code %d)' % (code)
  69.         except urllib2.URLError, code
  70.             session = 'Offline (Error %d, %s)' % (code[0], code[1])
  71.        
  72.         try:
  73.             #Set up our socket
  74.             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  75.             s.connect((MINECRAFT_ADDRESS, MINECRAFT_PORT))
  76.  
  77.             #Send 0xFE: Server list ping
  78.             s.send('\xfe')
  79.  
  80.             #Read as much data as we can (max packet size: 241 bytes)
  81.             d = s.recv(256)
  82.             s.close()
  83.  
  84.             #Check we've got a 0xFF Disconnect
  85.             assert d[0] == '\xff'
  86.  
  87.             #Remove the packet ident (0xFF) and the short containing the length of the string
  88.             #Decode UCS-2 string
  89.             #Split into list
  90.             d = d[3:].decode('utf-16be').split(u'\xa7')
  91.             server = 'Online, %d / %d' % (int(d[1]), int(d[2]))
  92.         except socket.error:
  93.             server = 'Offline'
  94.  
  95.         irc.reply("Login: %s, Session: %s, Our server: %s" % (login, session, server))
  96.  
  97. Class = Minecraft
  98.  
  99.  
  100. # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement