Project

Profile

Help

How to connect?
Download (2.46 KB) Statistics View on GitHub Reload from mirrored respository
| Branch: | Tag: | Revision:

github / src / client.py @ e1d2d0dc

1
"""This file contains the client that can connect to a MUD.
2

3
It is launched in a new thread, so as not to block the main thread.
4

5
"""
6

    
7
import re
8
from telnetlib import Telnet, WONT, WILL, ECHO
9
import threading
10
import time
11
import wx
12

    
13
try:
14
    from UniversalSpeech import say, braille
15
except ImportError:
16
    say = None
17
    braille = None
18

    
19
from ui.event import FocusEvent, myEVT_FOCUS
20

    
21
# Constants
22
ANSI_ESCAPE = re.compile(r'\x1b[^m]*m')
23

    
24
class Client(threading.Thread):
25

    
26
    """Class to receive data from the MUD."""
27

    
28
    def __init__(self, host, port=4000, timeout=0.1, settings=None):
29
        """Connects to the MUD."""
30
        threading.Thread.__init__(self)
31
        self.client = None
32
        self.timeout = timeout
33
        self.settings = settings
34
        self.running = False
35

    
36
        # Try to connect to the specified host and port
37
        self.client = Telnet(host, port)
38
        self.running = True
39

    
40
    def run(self):
41
        """Run the thread."""
42
        while self.running:
43
            time.sleep(self.timeout)
44
            msg = self.client.read_very_eager()
45
            if msg:
46
                self.handle_message(msg)
47

    
48
    def handle_message(self, msg):
49
        """When the client receives a message."""
50
        pass
51

    
52

    
53
class GUIClient(Client):
54

    
55
    """Client specifically linked to a GUI window.
56

57
    This client proceeds to send the text it receives to the frame.
58

59
    """
60

    
61
    def __init__(self, host, port=4000, timeout=0.1, panel=None,
62
            settings=None):
63
        Client.__init__(self, host, port, timeout, settings)
64
        self.panel = panel
65
        if self.client:
66
            self.client.set_option_negotiation_callback(self.handle_option)
67

    
68
    def handle_message(self, msg):
69
        """When the client receives a message."""
70
        pos = self.panel.output.GetInsertionPoint()
71
        msg = msg.decode("utf-8", "replace")
72
        msg = ANSI_ESCAPE.sub('', msg)
73
        self.panel.output.write(msg)
74
        self.panel.output.SetInsertionPoint(pos)
75
        if self.settings["options.TTS.on"]:
76
            if say and braille:
77
                say(msg, interrupt=False)
78
                braille(msg)
79

    
80
    def handle_option(self, socket, command, option):
81
        """Handle a received option."""
82
        if command == WILL and option == ECHO:
83
            evt = FocusEvent(myEVT_FOCUS, -1, "password")
84
            wx.PostEvent(self.panel, evt)
85
        elif command == WONT and option == ECHO:
86
            evt = FocusEvent(myEVT_FOCUS, -1, "input")
87
            wx.PostEvent(self.panel, evt)
(3-3/6)