door.go 5.6 KB

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