Пример #1
0
def main():
    db.connect(config.dbhost, config.dbuser, config.dbpass, config.dbname)
    
    hon_monitor = HoNStatus()
    
    test_count = 1
    while True:
        log.info("Running test #" + str(test_count))

        # Reconfigure the monitor.
        hon_monitor.configure()
        
        login_status, login_reason = hon_monitor.login_test()
        log.info("Login server: %s - %s" % (login_status, login_reason))
        chat_status, chat_reason = hon_monitor.chat_test()
        log.info("Chat Server: %s - %s" % (chat_status, chat_reason))
        
        # MotD data can be checked each test, regardless of the server statuses.
        try:
            hon_monitor.motd_parser()
        except MasterServerError, e:
            if e.code == 108:
                log.error('Could not obtain MotD data from the Master server')

        # Check that all tests returned good, otherwise the test fails and should
        # be re-attempted in 90 seconds
        if login_status is "Up" and chat_status is "Up":
            hon_monitor.logged_in = True
            timer = 0
            while hon_monitor.is_logged_in:
                timer += 1
                if timer >= 300:
                    hon_monitor.disconnect_logout()
                    break
                else:
                    time.sleep(1)

            # Client disconnected, cool down for a moment
            log.debug("Client disconnected, cooling..")
            time.sleep(2)
        else:
            # Start dropping the players online to zero once it's been determined that
            # the servers are down.
            if down_count > 5:
                db.execute("""INSERT INTO players (time, value) VALUES (%s, %s)""", [str(int(time.time())), 0])
            time.sleep(90)

        # Loop increment
        test_count += 1

        # And log back out again
        hon_monitor.logged_in = False
Пример #2
0
    def motd_parser(self):
        """ Retrieves the dictionary of message of the day(s(?)) 
            and replaces S2's colour formatting with html classes.
            Then places any new items into the database.
        """

        colour_map = ["00","1C","38","54","70","8C","A8","C4","E0","FF"]
        s2colours = lambda m: '<span style="color: #' + ''.join([colour_map[int(x)] for x in m.group(1)]) + '">'

        def urlfix(x):
            """ Replaces urls which only contain a 'www' with a 'http://wwww'. """
            if x.group(2) in ['http://', 'www']:
                colour = "y"
                url = x.group(1)
            else:
                colour = x.group(1)
                url = x.group(2)
            
            r = re.compile(r"(?<!http://)www")
            if r.match(url):
                url = 'http://' + ''.join(url)

            return '<a href=' + url + ' class=' + colour + ' target="_blank">' + url + '</a>'
        
        motd_data = self.motd_get()
        motd_list = motd_data['motd_list']

        ## NOTE: This entire thing is fairly broken due to glows, and when retards use rainbows in their text.

        # Iterate over the list in reverse because entries are retrieved in order newest -> oldest
        # and must be entered into the database oldest -> newest.
        for motd in motd_list[::-1]:

            # First find any un-coloured hyperlinks and fill them with html tags.
            # This regex matches any hyperlink which is not preceeded by a ^g formatter for EG.
            # It is not very accurate at the moment as it will match a http which has a ^* at the end.
            # http://gskinner.com/RegExr/?2u79l
            r = re.compile(r"(?<=[^\^a-zA-Z])((http://|(?<!http://)www)[-a-zA-Z0-9@:%_\+.~#?&//=]+)[^\^\*]")
            motd['body'] = r.sub(urlfix, motd['body'])

            # Then find all hyperlinks that contain colour formatting and replace with HTML tags.
            r = re.compile(r"\^([a-zA-Z])((http://|(?<!http://)www)[-a-zA-Z0-9@:%_\+.~#?&//=]+)(\^\*)")
            motd['body'] = r.sub(urlfix, motd['body'])

            # Find all coded colours eg ^428 and replace with inline html styling
            # ''.join([color_map[int(x)] for x in r.search(msg).group(1)])
            r = re.compile(r"\^([0-9]{3})")
            motd['body'] = r.sub(s2colours, motd['body'])

            # Replace the colours with HTML classes
            # Replace ^* with </span>
            motd['body'] = motd['body'].replace("^*", "</span>")

            # Find all basic colour codes eg ^y or ^r or ^o and replace with inline html
            r = re.compile(r"\^([a-z]{1})")
            motd['body'] = r.sub(r"<span class='\1'>", motd['body'])
            
            # Replace \r\n with <br />
            motd['body'] = motd['body'].replace("\r\n", "<br />")

            title_exists = db.query("""SELECT id FROM motds WHERE title = %s AND date = %s""", [motd['title'], motd['date']])
            msg_exists = db.query("""SELECT id, title FROM motds WHERE body = %s AND date = %s""", [motd['body'], motd['date']])

            if not title_exists:
                # Check if the message was simply updated by the staff.
                # If it's been changed then update it in the database automatically.
                # TODO: Is this level of comparison okay?
                if msg_exists:
                    # Title doesn't exist, but message body does, title changed.
                    db.execute("""UPDATE motds SET title=%s, author=%s, date=%s, body=%s WHERE id = %s""", [motd['title'], motd['author'], motd['date'], motd['body'], msg_exists[0][0]])
                    log.info("Updated motd #%s - %s. Title updated to %s" % (msg_exists[0][0], msg_exists[0][1], motd['title']))

            elif title_exists:
                log.debug("Duplicate title for motd id %s with title %s" % (title_exists[0][0], motd['title']))
                # This entry is already here, possibly it could have been updated.
                # Note: Seems they like to change the titles after publishing them.
                if not msg_exists:
                    # Title exists but the msg body doesn't, so it was likely updated.
                    db.execute("""UPDATE motds SET title=%s, author=%s, date=%s, body=%s WHERE id = %s""", [motd['title'], motd['author'], motd['date'], motd['body'], title_exists[0][0]])
                    log.info("Updated motd #%s - %s. Message updated" % (title_exists[0][0], motd['title']))

            if not msg_exists and not title_exists:
                    # Neither the title or message are there, either both are changed or this is a brand new motd
                    # Treat it as new for now.
                    # Add this motd to the database
                    db.execute("""INSERT INTO motds (title, author, date, body) VALUES(%s, %s, %s, %s)""", [motd['title'], motd['author'], motd['date'], motd['body']])
                    log.info("Added new message of the day - %s - %s" % (motd['title'], motd['date']))

        # Get the image from S2 for the motd.
        # Save it to static/img/motd/ if it does not exist.
        image_file = motd_data['image'].split("`")[0]
        image_name = re.search(r'\/([a-f0-9]+.jpg)', image_file).group(1)
        if not os.path.isfile(os.path.join(config.motd_img_dir, image_name)):
            urllib.urlretrieve(image_file, os.path.join(config.motd_img_dir, image_name)) 
            # Set the image name in the database so it can be retrieved.
            db.execute("""UPDATE `motd_extra` SET `value`=%s WHERE `key`=%s""", [image_name, 'image'])
            log.info("New MOTD image.")
Пример #3
0
        log.info("Testing chat server...")
        status = ""
        note = ""

        global down_count
        try:
            response = self._chat_connect()
            status = "Up"
            note = "Chat server is OK."
            down_count = 0
        except ChatServerError, e:
            status = "Down"
            note = e.error
            down_count += 1
        
        db.execute("""UPDATE servers SET status = %s, time = %s, note = %s WHERE id = 2 """, [1 if status == "Up" else 0, time.time(), note])
        return status, note

    def disconnect_logout(self):
        log.info("Disconnecting from chat server")
        self._chat_disconnect()
        
        log.info("Logging out")
        self._logout()

    def motd_parser(self):
        """ Retrieves the dictionary of message of the day(s(?)) 
            and replaces S2's colour formatting with html classes.
            Then places any new items into the database.
        """