door.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. Disconnected bool
  54. TimeOut time.Time // Fixed point in time, when time expires
  55. pushback *list.List
  56. }
  57. // Return the amount of time left as time.Duration
  58. func (d *Door) TimeLeft() time.Duration {
  59. return d.TimeOut.Sub(time.Now())
  60. }
  61. // Read the BBS door file. We only support door32.sys.
  62. func (d *Door) ReadDropfile(filename string) {
  63. file, err := os.Open(filename)
  64. if err != nil {
  65. fmt.Printf("Open(%s): %s\n", filename, err)
  66. os.Exit(2)
  67. }
  68. defer file.Close()
  69. var lines []string
  70. // read line by line
  71. // The scanner handles DOS and linux file endings.
  72. scanner := bufio.NewScanner(file)
  73. for scanner.Scan() {
  74. line := scanner.Text()
  75. lines = append(lines, line)
  76. }
  77. d.config.comm_type, err = strconv.Atoi(lines[0])
  78. d.config.comm_handle, err = strconv.Atoi(lines[1])
  79. d.config.baudrate, err = strconv.Atoi(lines[2])
  80. d.config.BBSID = lines[3]
  81. d.config.user_number, err = strconv.Atoi(lines[4])
  82. d.config.real_name = lines[5]
  83. d.config.handle = lines[6]
  84. d.config.security_level, err = strconv.Atoi(lines[7])
  85. d.config.time_left, err = strconv.Atoi(lines[8])
  86. d.config.emulation, err = strconv.Atoi(lines[9])
  87. d.config.node_number, err = strconv.Atoi(lines[10])
  88. d.READFD = d.config.comm_handle
  89. d.WRITEFD = d.config.comm_handle
  90. // Calculate the time when time expires.
  91. d.TimeOut = time.Now().Add(time.Duration(d.config.time_left) * time.Minute)
  92. }
  93. // Detect client terminal capabilities, Unicode, CP437, Full_CP437,
  94. // screen Height and Width.
  95. func (d *Door) detect() {
  96. d.Write("\x1b[0;30;40m\x1b[2J\x1b[H") // black on black, clrscr, go home
  97. d.Write("\x03\x04\x1b[6n") // hearts and diamonds does CP437 work?
  98. d.Write(CRNL + "\u2615\x1b[6n") // hot beverage
  99. d.Write("\x1b[999C\x1b[999B\x1b[6n" + Reset + "\x1b[2J\x1b[H") // goto end of screen + cursor pos
  100. // time.Sleep(50 * time.Millisecond)
  101. time.Sleep(250 * time.Millisecond)
  102. // read everything
  103. // telnet term isn't in RAW mode, so keys are buffer until <CR>
  104. var results string
  105. if d.HasKey() {
  106. buffer := make([]byte, 100)
  107. r, err := syscall.Read(d.READFD, buffer)
  108. results = string(buffer[:r])
  109. output := strings.Replace(results, "\x1b", "^[", -1)
  110. fmt.Println("DETECT:", r, err, output)
  111. } else {
  112. // local telnet echos the reply :()
  113. fmt.Println("DETECT: Nothing received.")
  114. return
  115. }
  116. if ((strings.Index(results, "1;1R") != -1) ||
  117. (strings.Index(results, "1;3R") != -1)) &&
  118. ((strings.Index(results, "2:2R") != -1) ||
  119. (strings.Index(results, "2;3R") != -1)) {
  120. Unicode = true
  121. } else {
  122. Unicode = false
  123. CP437 = true
  124. }
  125. if strings.Index(results, "1;3R") != -1 {
  126. Full_CP437 = true
  127. }
  128. // get screen size
  129. var err error
  130. pos := strings.LastIndex(results, "\x1b")
  131. if pos != -1 {
  132. pos++
  133. if results[pos] == '[' {
  134. pos++
  135. results = results[pos:]
  136. pos = strings.Index(results, ";")
  137. if pos != -1 {
  138. height := results[:pos]
  139. Height, err = strconv.Atoi(height)
  140. pos++
  141. results = results[pos:]
  142. pos = strings.Index(results, "R")
  143. if pos != -1 {
  144. width := results[:pos]
  145. Width, err = strconv.Atoi(width)
  146. fmt.Printf("Width: %s, %d, %v\n", results, Width, err)
  147. }
  148. } else {
  149. Height = 0
  150. Width = 0
  151. }
  152. }
  153. }
  154. fmt.Printf("Unicode %v Screen: %d X %d\n", Unicode, Width, Height)
  155. }
  156. // Initialize door framework. Parse commandline, read dropfile,
  157. // detect terminal capabilities.
  158. func (d *Door) Init() {
  159. var dropfile string
  160. d.pushback = list.New()
  161. flag.StringVar(&dropfile, "d", "", "Path to dropfile")
  162. flag.Parse()
  163. if len(dropfile) == 0 {
  164. flag.PrintDefaults()
  165. os.Exit(2)
  166. }
  167. fmt.Printf("Loading: %s\n", dropfile)
  168. d.ReadDropfile(dropfile)
  169. 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)
  170. d.detect()
  171. }
  172. // Write string to client.
  173. func (d *Door) Write(output string) {
  174. if d.Disconnected {
  175. return
  176. }
  177. buffer := []byte(output)
  178. n, err := syscall.Write(d.WRITEFD, buffer)
  179. if err != nil {
  180. fmt.Println("Write error/HANGUP?", n)
  181. d.Disconnected = true
  182. }
  183. // No, this isn't it. The # of bytes in buffer == bytes written.
  184. if n != len(buffer) {
  185. fmt.Printf("Write fail: %d != %d\n", len(buffer), n)
  186. }
  187. }
  188. func Goto(x int, y int) string {
  189. return fmt.Sprintf("\x1b[%d;%dH", y, x)
  190. }