SHARE
TWEET

Untitled

a guest Feb 11th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import logging
  2. from miniboa import TelnetServer
  3. from plugins import callsign as call
  4.  
  5. IDLE_TIMEOUT = 300
  6. CLIENT_LIST = []
  7. SERVER_RUN = True
  8.  
  9. nodecall = 'WB5OD'
  10. menu_data = [['\n(1) Call "wb5od"', '(2) Weather "Zip"', '(3) Tweet "to" "message"'], ['\n(4) Development', '(5) Random thing', '(6) Getting tired'],
  11.                 ['\n(7) Testing', '(8) Sysop Menu', '(9) List Users']]
  12.  
  13.  
  14. #
  15. ## Setup functions that an amateur operator may want like... aprs messaging, etc.
  16. #
  17.  
  18.  
  19. def on_connect(client):
  20.     """
  21.    Sample on_connect function.
  22.    Handles new connections.
  23.    """
  24.     logging.info("Opened connection to {}".format(client.addrport()))
  25.     #broadcast("{} joins the conversation.\n".format(client.addrport()))
  26.     CLIENT_LIST.append(client)
  27.     client.send("\r\nWelcome to the " + nodecall + " python telnet server for ham radio packet.\n")
  28.  
  29.     col_width = max(len(word) for row in menu_data for word in row) + 2  # padding
  30.     for row in menu_data:
  31.         client.send("".join(word.ljust(col_width) for word in row))
  32.     client.send("\n\rIf you need to see this again just type in 'menu'")
  33.  
  34.  
  35. def on_disconnect(client):
  36.     """
  37.    Sample on_disconnect function.
  38.    Handles lost connections.
  39.    """
  40.     logging.info("Lost connection to {}".format(client.addrport()))
  41.     CLIENT_LIST.remove(client)
  42.     #broadcast("{} leaves the conversation.\n".format(client.addrport()))
  43.  
  44.  
  45. def kick_idle():
  46.     """
  47.    Looks for idle clients and disconnects them by setting active to False.
  48.    """
  49.     # Who hasn't been typing?
  50.     for client in CLIENT_LIST:
  51.         if client.idle() > IDLE_TIMEOUT:
  52.             logging.info("Kicking idle lobby client from {}".format(client.addrport()))
  53.             client.active = False
  54.  
  55.  
  56. def process_clients():
  57.     """
  58.    Check each client, if client.cmd_ready == True then there is a line of
  59.    input available via client.get_command().
  60.    """
  61.     for client in CLIENT_LIST:
  62.         if client.active and client.cmd_ready:
  63.             # If the client sends input echo it to the chat room
  64.             commands(client)
  65.  
  66.  
  67. def broadcast(msg):
  68.     """
  69.    Send msg to every client.
  70.    """
  71.     for client in CLIENT_LIST:
  72.         client.send(msg)
  73.  
  74.  
  75. def commands(client):
  76.     """
  77.    Echo whatever client types to everyone.
  78.    """
  79.     global SERVER_RUN
  80.     msg = client.get_command()
  81.     logging.info("{} says '{}'".format(client.addrport(), msg))
  82.  
  83. #    for guest in CLIENT_LIST:
  84. #        if guest != client:
  85. #            guest.send("{} says '{}'\n".format(client.addrport(), msg))
  86. #        else:
  87. #            guest.send("You say '{}'\n".format(msg))
  88.  
  89.     cmd = msg.lower()
  90.     # bye = disconnect
  91.     if cmd == 'bye':
  92.         client.active = False
  93.     # shutdown == stop the server
  94.     elif cmd == 'hello':
  95.         client.send('Hello you could make this do anything. type bye to leave')
  96.     elif cmd.startswith('call'):
  97.         try:
  98.             split = cmd.split(' ')
  99.             client.send(call.callsign_start(split[1]))
  100.         except:
  101.             client.send('\r\nError')
  102.     elif cmd == 'menu':
  103.         col_width = max(len(word) for row in menu_data for word in row) + 2  # padding
  104.         for row in menu_data:
  105.             client.send("".join(word.ljust(col_width) for word in row))
  106.     elif cmd == 'shutdown':
  107.         SERVER_RUN = False
  108.  
  109.  
  110. if __name__ == '__main__':
  111.  
  112.     # Simple chat server to demonstrate connection handling via the
  113.     # async and telnet modules.
  114.  
  115.     logging.basicConfig(level=logging.DEBUG)
  116.  
  117.     # Create a telnet server with a port, address,
  118.     # a function to call with new connections
  119.     # and one to call with lost connections.
  120.  
  121.     telnet_server = TelnetServer(
  122.         port=4444,
  123.         address='',
  124.         on_connect=on_connect,
  125.         on_disconnect=on_disconnect,
  126.         timeout = .05
  127.         )
  128.  
  129.     logging.info("Listening for connections on port {}. CTRL-C to break.".format(telnet_server.port))
  130.  
  131.     # Server Loop
  132.     while SERVER_RUN:
  133.         telnet_server.poll()        # Send, Recv, and look for new connections
  134.         kick_idle()                 # Check for idle clients
  135.         process_clients()           # Check for client input
  136.  
  137.     logging.info("Server shutdown.")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top