User Tools

Site Tools


simple-bpq-telnet-application

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
simple-bpq-telnet-application [2026/01/01 22:42] – [BPQ configuration] kc2ihxsimple-bpq-telnet-application [2026/02/21 20:23] (current) – [Program Code] kc2ihx
Line 34: Line 34:
 from optparse import OptionParser from optparse import OptionParser
  
 +# this sets up the command line options, primarily the file to read and the port to open for BPQ
 parser = OptionParser() parser = OptionParser()
 parser.add_option("-f", "--file", dest="filename", help="which json file with message and welcome", metavar="FILE") parser.add_option("-f", "--file", dest="filename", help="which json file with message and welcome", metavar="FILE")
Line 39: Line 40:
 (options, args) = parser.parse_args() (options, args) = parser.parse_args()
  
 +# read file with messages
 messages = json.load(open(options.filename,'r')) messages = json.load(open(options.filename,'r'))
  
 +# logging method to write to stderr for journalctl to pick up
 def print_log(*a): def print_log(*a):
     print(*a, file=sys.stderr)     print(*a, file=sys.stderr)
  
 +# this is the main function that processes input from user and writes outputs
 async def shell(reader, writer): async def shell(reader, writer):
 +    # first thing provided is user's callsign
     incall = await reader.readline()     incall = await reader.readline()
     print_log("%s connected" % incall)     print_log("%s connected" % incall)
     writer.write("\r\nWelcome %s" % incall)     writer.write("\r\nWelcome %s" % incall)
     writer.write("\r\n%s" % messages["welcome"])     writer.write("\r\n%s" % messages["welcome"])
 +    # make copy of blocks so we can pop 'em
     textblocks = messages["text"].copy()     textblocks = messages["text"].copy()
-    leave = False #inp[0].upper() in ["Q", "B"]+    # don't let them leave until they've been rolled 
 +    leave = False 
     rolled = False     rolled = False
-    inp = await reader.read(1)+    # take first input from user after welcome message 
 +    inp = await reader.readline() 
 +    text_count = 0 
 +    incall = incall.rstrip() 
 +    # loop on inputs until they leave
     while inp and not leave and len(textblocks) > 0:     while inp and not leave and len(textblocks) > 0:
-        # print("writing next block") 
         writer.write(textblocks.pop(0))         writer.write(textblocks.pop(0))
         await writer.drain()         await writer.drain()
         inp = await reader.readline()         inp = await reader.readline()
 +        text_count += 1
         if not rolled:          if not rolled: 
-            # notify that rolling has happened+            # log that rolling has happened
             print_log("%s rickrolled" % incall)             print_log("%s rickrolled" % incall)
             rolled = True             rolled = True
 +        # if they type something starting with Q or B (quit, bye, bye-bye, etc.) let them leave
         leave = inp[0].upper() in ["Q", "B"] and rolled         leave = inp[0].upper() in ["Q", "B"] and rolled
 +        # log their input to journalctl via stderr
 +        print_log("%s (%d): \"%s\"" % (incall, text_count, inp.rstrip()))
     writer.close()     writer.close()
  
 +# sets up the async process to take inputs
 loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
 coro = telnetlib3.create_server(port=options.port, shell=shell) coro = telnetlib3.create_server(port=options.port, shell=shell)
simple-bpq-telnet-application.1767307350.txt.gz · Last modified: by kc2ihx