door.go 5.6 KB

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