door.go 6.0 KB

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