door.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package door
  2. import (
  3. "bufio"
  4. "container/list"
  5. "flag"
  6. "fmt"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. "time"
  12. )
  13. const CRNL = "\r\n" // BBS Line Ending
  14. const Clrscr = "\x1b[2J\x1b[H" // Clear screen, home cursor
  15. var Reset string = Color(0) // ANSI Color Reset
  16. var Unicode bool // Unicode support detected
  17. var CP437 bool // CP437 support detected
  18. var Full_CP437 bool // Full CP437 support detected (handles control codes properly)
  19. var Height int // Screen height detected
  20. var Width int // Screen width detected
  21. var Inactivity int64 = 120 // Inactivity timeout
  22. /*
  23. door32.sys:
  24. 0 Line 1 : Comm type (0=local, 1=serial, 2=telnet)
  25. 0 Line 2 : Comm or socket handle
  26. 38400 Line 3 : Baud rate
  27. Mystic 1.07 Line 4 : BBSID (software name and version)
  28. 1 Line 5 : User record position (1-based)
  29. James Coyle Line 6 : User's real name
  30. g00r00 Line 7 : User's handle/alias
  31. 255 Line 8 : User's security level
  32. 58 Line 9 : User's time left (in minutes)
  33. 1 Line 10: Emulation *See Below
  34. 1 Line 11: Current node number
  35. */
  36. type DropfileConfig struct {
  37. comm_type int
  38. comm_handle int
  39. baudrate int
  40. BBSID string
  41. user_number int
  42. real_name string
  43. handle string
  44. security_level int
  45. time_left int
  46. emulation int
  47. node_number int
  48. }
  49. type Door struct {
  50. config DropfileConfig
  51. READFD int
  52. WRITEFD int
  53. TimeOut time.Time // Fixed point in time, when time expires
  54. pushback *list.List
  55. }
  56. // Return the amount of time left as time.Duration
  57. func (d *Door) TimeLeft() time.Duration {
  58. return d.TimeOut.Sub(time.Now())
  59. }
  60. // Read the BBS door file. We only support door32.sys.
  61. func (d *Door) ReadDropfile(filename string) {
  62. file, err := os.Open(filename)
  63. if err != nil {
  64. fmt.Printf("Open(%s): %s\n", filename, err)
  65. os.Exit(2)
  66. }
  67. defer file.Close()
  68. var lines []string
  69. // read line by line
  70. // The scanner handles DOS and linux file endings.
  71. scanner := bufio.NewScanner(file)
  72. for scanner.Scan() {
  73. line := scanner.Text()
  74. lines = append(lines, line)
  75. }
  76. d.config.comm_type, err = strconv.Atoi(lines[0])
  77. d.config.comm_handle, err = strconv.Atoi(lines[1])
  78. d.config.baudrate, err = strconv.Atoi(lines[2])
  79. d.config.BBSID = lines[3]
  80. d.config.user_number, err = strconv.Atoi(lines[4])
  81. d.config.real_name = lines[5]
  82. d.config.handle = lines[6]
  83. d.config.security_level, err = strconv.Atoi(lines[7])
  84. d.config.time_left, err = strconv.Atoi(lines[8])
  85. d.config.emulation, err = strconv.Atoi(lines[9])
  86. d.config.node_number, err = strconv.Atoi(lines[10])
  87. d.READFD = d.config.comm_handle
  88. d.WRITEFD = d.config.comm_handle
  89. // Calculate the time when time expires.
  90. d.TimeOut = time.Now().Add(time.Duration(d.config.time_left) * time.Minute)
  91. }
  92. // Detect client terminal capabilities, Unicode, CP437, Full_CP437,
  93. // screen Height and Width.
  94. func (d *Door) detect() {
  95. d.Write("\x1b[0;30;40m\x1b[2J\x1b[H") // black on black, clrscr, go home
  96. d.Write("\x03\x04\x1b[6n") // hearts and diamonds does CP437 work?
  97. d.Write(CRNL + "\u2615\x1b[6n") // hot beverage
  98. d.Write("\x1b[999C\x1b[999B\x1b[6n" + Reset + "\x1b[2J\x1b[H") // goto end of screen + cursor pos
  99. // time.Sleep(50 * time.Millisecond)
  100. time.Sleep(250 * time.Millisecond)
  101. // read everything
  102. // telnet term isn't in RAW mode, so keys are buffer until <CR>
  103. var results string
  104. if d.HasKey() {
  105. buffer := make([]byte, 100)
  106. r, err := syscall.Read(d.READFD, buffer)
  107. results = string(buffer[:r])
  108. output := strings.Replace(results, "\x1b", "^[", -1)
  109. fmt.Println("DETECT:", r, err, output)
  110. } else {
  111. // local telnet echos the reply :()
  112. fmt.Println("DETECT: Nothing received.")
  113. return
  114. }
  115. if ((strings.Index(results, "1;1R") != -1) ||
  116. (strings.Index(results, "1;3R") != -1)) &&
  117. ((strings.Index(results, "2:2R") != -1) ||
  118. (strings.Index(results, "2;3R") != -1)) {
  119. Unicode = true
  120. } else {
  121. Unicode = false
  122. CP437 = true
  123. }
  124. if strings.Index(results, "1;3R") != -1 {
  125. Full_CP437 = true
  126. }
  127. // get screen size
  128. var err error
  129. pos := strings.LastIndex(results, "\x1b")
  130. if pos != -1 {
  131. pos++
  132. if results[pos] == '[' {
  133. pos++
  134. results = results[pos:]
  135. pos = strings.Index(results, ";")
  136. if pos != -1 {
  137. height := results[:pos]
  138. Height, err = strconv.Atoi(height)
  139. pos++
  140. results = results[pos:]
  141. pos = strings.Index(results, "R")
  142. if pos != -1 {
  143. width := results[:pos]
  144. Width, err = strconv.Atoi(width)
  145. fmt.Printf("Width: %s, %d, %v\n", results, Width, err)
  146. }
  147. } else {
  148. Height = 0
  149. Width = 0
  150. }
  151. }
  152. }
  153. fmt.Printf("Unicode %v Screen: %d X %d\n", Unicode, Width, Height)
  154. }
  155. // Initialize door framework. Parse commandline, read dropfile,
  156. // detect terminal capabilities.
  157. func (d *Door) Init() {
  158. var dropfile string
  159. d.pushback = list.New()
  160. flag.StringVar(&dropfile, "d", "", "Path to dropfile")
  161. flag.Parse()
  162. if len(dropfile) == 0 {
  163. flag.PrintDefaults()
  164. os.Exit(2)
  165. }
  166. fmt.Printf("Loading: %s\n", dropfile)
  167. d.ReadDropfile(dropfile)
  168. fmt.Printf("BBS %s, User %s / Handle %s / File %d\n", d.config.BBSID, d.config.real_name, d.config.handle, d.config.comm_handle)
  169. d.detect()
  170. }
  171. // Write string to client.
  172. func (d *Door) Write(output string) {
  173. buffer := []byte(output)
  174. n, err := syscall.Write(d.WRITEFD, buffer)
  175. if err != nil {
  176. fmt.Println("Write error/HANGUP?", n)
  177. }
  178. // No, this isn't it. The # of bytes in buffer == bytes written.
  179. if n != len(buffer) {
  180. fmt.Printf("Write fail: %d != %d\n", len(buffer), n)
  181. }
  182. }
  183. func Goto(x int, y int) string {
  184. return fmt.Sprintf("\x1b[%d;%dH", y, x)
  185. }