123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- /*
- Package door: a go implementation of a BBS door for linux and Windows
- that uses door32.sys, understand CP437 and unicode (if available),
- detects screen size, and supports TheDraw Fonts.
- import (
- "door"
- )
- int main() {
- d = door.Door{}
- d.Init() // Process commandline switches, initialize door, detect screen size.
- d.Write("Welcome to my awesome door, written in "+door.ColorText("BLINK BOLD WHITE")+"go"+door.Reset+"."+door.CRNL)
- d.Write("Press a key...")
- d.Key()
- d.Write(door.CRNL)
- }
- */
- package door
- import (
- "bufio"
- "flag"
- "fmt"
- "log"
- "os"
- "path/filepath"
- "runtime/debug"
- "strconv"
- "strings"
- "sync"
- "time"
- )
- const SavePos = "\x1b[s" // Save Cursor Position
- const RestorePos = "\x1b[u" // Restore Cursor Position
- const CRNL = "\r\n" // BBS Line Ending
- const Clrscr = "\x1b[0m\x1b[2J\x1b[H" // Clear screen, home cursor
- const HideCursor = "\x1b[?25l" // Hide Cursor
- const ShowCursor = "\x1b[?25h" // Show Cursor
- var Reset string = Color(0) // ANSI Color Reset
- var Unicode bool // Unicode support detected
- var CP437 bool // CP437 support detected
- var Full_CP437 bool // Full CP437 support detected (handles control codes properly)
- var Height int // Screen height detected
- var Width int // Screen width detected
- var Inactivity int64 = 120 // Inactivity timeout
- type CursorPos struct {
- X, Y int
- }
- type Mouse struct {
- Button int8
- X int8
- Y int8
- }
- /*
- door32.sys:
- 0 Line 1 : Comm type (0=local, 1=serial, 2=telnet)
- 0 Line 2 : Comm or socket handle
- 38400 Line 3 : Baud rate
- Mystic 1.07 Line 4 : BBSID (software name and version)
- 1 Line 5 : User record position (1-based)
- James Coyle Line 6 : User's real name
- g00r00 Line 7 : User's handle/alias
- 255 Line 8 : User's security level
- 58 Line 9 : User's time left (in minutes)
- 1 Line 10: Emulation *See Below
- 1 Line 11: Current node number
- */
- // Door32 information
- type DropfileConfig struct {
- Comm_type int // (not used)
- Comm_handle int // Handle to use to talk to the user
- Baudrate int // (not used)
- BBSID string // BBS Software name
- User_number int // User number
- Real_name string // User's Real Name
- Handle string // User's Handle/Nick
- Security_level int // Security Level (if given)
- Time_left int // Time Left (minutes)
- Emulation int // (not used)
- Node int // BBS Node number
- }
- type Door struct {
- Config DropfileConfig
- READFD int
- WRITEFD int
- Disconnected bool // int32 // atomic bool // Has User disconnected/Hung up?
- TimeOut time.Time // Fixed point in time, when time expires
- StartTime time.Time // Time when User started door
- Pushback FIFOBuffer // Key buffer
- LastColor []int // Track the last color sent for restore color
- ReaderClosed bool // Reader close
- readerChannel chan byte // Reading from the User
- ReaderCanClose bool // We can close the reader (in tests)
- WriterClosed bool // Writer closed
- writerChannel chan string // Writing to the User
- writerMutex sync.RWMutex
- LastMouse []Mouse // Store Mouse information
- LastCursor []CursorPos // Store Cursor pos information
- mcMutex sync.Mutex // Lock for LastMouse, LastCursor
- wg sync.WaitGroup
- }
- func (d *Door) SafeWriterClose() {
- d.writerMutex.Lock()
- defer d.writerMutex.Unlock()
- if !d.WriterClosed {
- d.WriterClosed = true
- close(d.writerChannel)
- }
- }
- func (d *Door) WriterIsClosed() bool {
- d.writerMutex.RLock()
- defer d.writerMutex.RUnlock()
- return d.WriterClosed
- }
- func (d *Door) AddMouse(mouse Mouse) {
- d.mcMutex.Lock()
- defer d.mcMutex.Unlock()
- d.LastMouse = append(d.LastMouse, mouse)
- }
- func (d *Door) GetMouse() (Mouse, bool) {
- d.mcMutex.Lock()
- defer d.mcMutex.Unlock()
- return ArrayDelete(&d.LastMouse, 0)
- }
- func (d *Door) GetCursorPos() (CursorPos, bool) {
- d.mcMutex.Lock()
- defer d.mcMutex.Unlock()
- return ArrayDelete(&d.LastCursor, 0)
- }
- func (d *Door) ClearMouseCursor() {
- d.mcMutex.Lock()
- defer d.mcMutex.Unlock()
- d.LastMouse = make([]Mouse, 0, 2)
- d.LastCursor = make([]CursorPos, 0, 3)
- }
- // Return the amount of time left as time.Duration
- func (d *Door) TimeLeft() time.Duration {
- return time.Until(d.TimeOut)
- }
- func (d *Door) TimeUsed() time.Duration {
- return time.Since(d.StartTime)
- }
- func (d *Door) Disconnect() bool {
- return d.Disconnected // atomic.LoadInt32(&d.Disconnected) != 0
- }
- // Read the BBS door file. We only support door32.sys.
- func (d *Door) ReadDropfile(filename string) {
- var file *os.File
- var err error
- file, err = os.Open(filename)
- if err != nil {
- log.Panicf("Open(%s): %s\n", filename, err)
- }
- defer file.Close()
- var lines []string
- // read line by line
- // The scanner handles DOS and linux file endings.
- var scanner *bufio.Scanner = bufio.NewScanner(file)
- for scanner.Scan() {
- line := scanner.Text()
- lines = append(lines, line)
- }
- d.Config.Comm_type, err = strconv.Atoi(lines[0])
- if err != nil {
- log.Panicf("Door32 Comm Type (expected integer): %s\n", err)
- }
- d.Config.Comm_handle, err = strconv.Atoi(lines[1])
- if err != nil {
- log.Panicf("Door32 Comm Handle (expected integer): %s\n", err)
- }
- d.Config.Baudrate, err = strconv.Atoi(lines[2])
- if err != nil {
- log.Panicf("Door32 Baudrate (expected integer): %s\n", err)
- }
- d.Config.BBSID = lines[3]
- d.Config.User_number, err = strconv.Atoi(lines[4])
- if err != nil {
- log.Panicf("Door32 User Number (expected integer): %s\n", err)
- }
- d.Config.Real_name = lines[5]
- d.Config.Handle = lines[6]
- d.Config.Security_level, err = strconv.Atoi(lines[7])
- if err != nil {
- log.Panicf("Door32 Security Level (expected integer): %s\n", err)
- }
- d.Config.Time_left, err = strconv.Atoi(lines[8])
- if err != nil {
- log.Panicf("Door32 Time Left (expected integer): %s\n", err)
- }
- d.Config.Emulation, err = strconv.Atoi(lines[9])
- if err != nil {
- log.Panicf("Door32 Emulation (expected integer): %s\n", err)
- }
- d.Config.Node, err = strconv.Atoi(lines[10])
- if err != nil {
- log.Panicf("Door32 Node Number (expected integer): %s\n", err)
- }
- d.READFD = d.Config.Comm_handle
- d.WRITEFD = d.Config.Comm_handle
- d.StartTime = time.Now()
- // Calculate when time expires.
- d.TimeOut = time.Now().Add(time.Duration(d.Config.Time_left) * time.Minute)
- }
- // Detect client terminal capabilities, Unicode, CP437, Full_CP437,
- // screen Height and Width.
- // Full_CP437 means the terminal understands control codes as CP437.
- func (d *Door) detect() {
- d.Write("\x1b[0;30;40m\x1b[2J\x1b[H") // black on black, clrscr, go home
- d.Write("\x03\x04\x1b[6n") // hearts and diamonds does CP437 work?
- d.Write(CRNL + "\u2615\x1b[6n") // hot beverage
- d.Write("\x1b[999C\x1b[999B\x1b[6n" + Reset + "\x1b[2J\x1b[H") // goto end of screen + cursor pos
- // Yuck!
- time.Sleep(250 * time.Millisecond)
- // read everything
- var results string
- for {
- var r int = d.getch()
- if r < 0 {
- break
- }
- results += string(byte(r))
- }
- if len(results) > 0 {
- output := strings.Replace(results, "\x1b", "^[", -1)
- log.Println("DETECT:", output)
- } else {
- log.Println("DETECT: Nothing received.")
- return
- }
- if (strings.Contains(results, "1;1R") ||
- strings.Contains(results, "1;3R")) &&
- (strings.Contains(results, "2:2R") ||
- strings.Contains(results, "2;3R")) {
- Unicode = true
- } else {
- Unicode = false
- CP437 = true
- }
- if strings.Contains(results, "1;3R") {
- Full_CP437 = true
- }
- // get screen size
- var pos int = strings.LastIndex(results, "\x1b")
- if pos != -1 {
- pos++
- if results[pos] == '[' {
- pos++
- results = results[pos:]
- pos = strings.Index(results, ";")
- if pos != -1 {
- var height string = results[:pos]
- Height, _ = strconv.Atoi(height)
- pos++
- results = results[pos:]
- pos = strings.Index(results, "R")
- if pos != -1 {
- width := results[:pos]
- Width, _ = strconv.Atoi(width)
- }
- } else {
- Height = 0
- Width = 0
- }
- }
- }
- log.Printf("Unicode %v Screen: %d X %d\n", Unicode, Width, Height)
- }
- // Initialize door framework. Parse commandline, read dropfile,
- // detect terminal capabilities.
- func (d *Door) Init(doorname string) {
- var dropfile string
- d.Pushback = NewFIFOBuffer(5)
- // Get path to binary, and chdir to it.
- var binaryPath string
- binaryPath, _ = os.Executable()
- binaryPath = filepath.Dir(binaryPath)
- _ = os.Chdir(binaryPath)
- flag.StringVar(&dropfile, "d", "", "Path to dropfile")
- flag.Parse()
- if len(dropfile) == 0 {
- flag.PrintDefaults()
- os.Exit(2)
- }
- d.ReadDropfile(dropfile)
- // Logfile will be doorname - node #
- var logfilename string = fmt.Sprintf("%s-%d.log", doorname, d.Config.Node)
- var logf *os.File
- var err error
- logf, err = os.OpenFile(logfilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
- if err != nil {
- log.Panicf("Error creating log file %s: %v", logfilename, err)
- }
- log.SetOutput(logf)
- log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
- log.Printf("Loading dropfile %s\n", dropfile)
- log.Printf("BBS %s, User %s / Handle %s / File %d\n", d.Config.BBSID, d.Config.Real_name, d.Config.Handle, d.Config.Comm_handle)
- d.readerChannel = make(chan byte, 8)
- d.writerChannel = make(chan string) // unbuffered
- // changing this to unbound/sync hangs tests.
- // d.closeChannel = make(chan struct{}, 2) // reader & door.Close
- d.setupChannels()
- d.detect()
- if Unicode {
- BOXES = BOXES_UNICODE
- BARS = BARS_UNICODE
- } else {
- BOXES = BOXES_CP437
- BARS = BARS_CP437
- }
- }
- func (d *Door) Close() {
- defer func() {
- if err := recover(); err != nil {
- log.Println("door.Close FAILURE:", err)
- // This displays stack trace stderr
- debug.PrintStack()
- }
- }()
- log.Println("Closing...")
- // d.closeChannel <- struct{}{}
- if !d.WriterClosed {
- d.writerChannel <- ""
- }
- // CloseReader(d.Config.Comm_handle)
- log.Println("wg.Wait()")
- d.wg.Wait()
- log.Println("Closed.")
- }
- // Goto X, Y - Position the cursor using ANSI Escape Codes
- //
- // Example:
- //
- // d.Write(door.Goto(1, 5) + "Now at X=1, Y=5.")
- func Goto(x int, y int) string {
- return fmt.Sprintf("\x1b[%d;%dH", y, x)
- }
|