door.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. Package door: a golang implementation of a BBS door for linux that
  3. support door32.sys.
  4. import (
  5. "door"
  6. )
  7. int main() {
  8. d = door.Door{}
  9. d.Init() // Process commandline switches, initialize door, detect screen size.
  10. d.Write("Welcome to my awesome door, written in "+door.ColorText("BLINK BOLD WHITE")+"golang"+door.Reset+"."+door.CRNL)
  11. d.Write("Press a key...")
  12. d.Key()
  13. d.Write(door.CRNL)
  14. }
  15. */
  16. package door
  17. import (
  18. "bufio"
  19. "flag"
  20. "fmt"
  21. "os"
  22. "strconv"
  23. "strings"
  24. "syscall"
  25. "time"
  26. )
  27. const CRNL = "\r\n" // BBS Line Ending
  28. const Clrscr = "\x1b[0m\x1b[2J\x1b[H" // Clear screen, home cursor
  29. const HideCursor = "\x1b[?25l" // Hide Cursor
  30. const ShowCursor = "\x1b[?25h" // Show Cursor
  31. var Reset string = Color(0) // ANSI Color Reset
  32. var Unicode bool // Unicode support detected
  33. var CP437 bool // CP437 support detected
  34. var Full_CP437 bool // Full CP437 support detected (handles control codes properly)
  35. var Height int // Screen height detected
  36. var Width int // Screen width detected
  37. var Inactivity int64 = 120 // Inactivity timeout
  38. type LIFOBuffer struct {
  39. data []int
  40. index int
  41. }
  42. func (b *LIFOBuffer) Empty() bool {
  43. return b.index == 0
  44. }
  45. func (b *LIFOBuffer) Push(value int) {
  46. if b.index+1 > len(b.data) {
  47. b.data = append(b.data, value)
  48. b.index++
  49. } else {
  50. b.data[b.index] = value
  51. b.index++
  52. }
  53. }
  54. func (b *LIFOBuffer) Pop() int {
  55. if b.index == 0 {
  56. panic("Attempting to Pop from empty LIFOBuffer.")
  57. }
  58. b.index--
  59. return b.data[b.index]
  60. }
  61. /*
  62. door32.sys:
  63. 0 Line 1 : Comm type (0=local, 1=serial, 2=telnet)
  64. 0 Line 2 : Comm or socket handle
  65. 38400 Line 3 : Baud rate
  66. Mystic 1.07 Line 4 : BBSID (software name and version)
  67. 1 Line 5 : User record position (1-based)
  68. James Coyle Line 6 : User's real name
  69. g00r00 Line 7 : User's handle/alias
  70. 255 Line 8 : User's security level
  71. 58 Line 9 : User's time left (in minutes)
  72. 1 Line 10: Emulation *See Below
  73. 1 Line 11: Current node number
  74. */
  75. type DropfileConfig struct {
  76. Comm_type int
  77. Comm_handle int
  78. Baudrate int
  79. BBSID string
  80. User_number int
  81. Real_name string
  82. Handle string
  83. Security_level int
  84. Time_left int
  85. Emulation int
  86. Node int
  87. }
  88. type Door struct {
  89. Config DropfileConfig
  90. READFD int
  91. WRITEFD int
  92. Disconnected bool
  93. TimeOut time.Time // Fixed point in time, when time expires
  94. StartTime time.Time
  95. Pushback LIFOBuffer
  96. }
  97. // Return the amount of time left as time.Duration
  98. func (d *Door) TimeLeft() time.Duration {
  99. return d.TimeOut.Sub(time.Now())
  100. }
  101. func (d *Door) TimeUsed() time.Duration {
  102. return time.Now().Sub(d.StartTime)
  103. }
  104. // Read the BBS door file. We only support door32.sys.
  105. func (d *Door) ReadDropfile(filename string) {
  106. file, err := os.Open(filename)
  107. if err != nil {
  108. panic(fmt.Sprintf("Open(%s): %s\n", filename, err))
  109. // os.Exit(2)
  110. }
  111. defer file.Close()
  112. var lines []string
  113. // read line by line
  114. // The scanner handles DOS and linux file endings.
  115. scanner := bufio.NewScanner(file)
  116. for scanner.Scan() {
  117. line := scanner.Text()
  118. lines = append(lines, line)
  119. }
  120. d.Config.Comm_type, err = strconv.Atoi(lines[0])
  121. d.Config.Comm_handle, err = strconv.Atoi(lines[1])
  122. d.Config.Baudrate, err = strconv.Atoi(lines[2])
  123. d.Config.BBSID = lines[3]
  124. d.Config.User_number, err = strconv.Atoi(lines[4])
  125. d.Config.Real_name = lines[5]
  126. d.Config.Handle = lines[6]
  127. d.Config.Security_level, err = strconv.Atoi(lines[7])
  128. d.Config.Time_left, err = strconv.Atoi(lines[8])
  129. d.Config.Emulation, err = strconv.Atoi(lines[9])
  130. d.Config.Node, err = strconv.Atoi(lines[10])
  131. d.READFD = d.Config.Comm_handle
  132. d.WRITEFD = d.Config.Comm_handle
  133. // Calculate the time when time expires.
  134. d.StartTime = time.Now()
  135. d.TimeOut = time.Now().Add(time.Duration(d.Config.Time_left) * time.Minute)
  136. }
  137. // Detect client terminal capabilities, Unicode, CP437, Full_CP437,
  138. // screen Height and Width.
  139. func (d *Door) detect() {
  140. d.Write("\x1b[0;30;40m\x1b[2J\x1b[H") // black on black, clrscr, go home
  141. d.Write("\x03\x04\x1b[6n") // hearts and diamonds does CP437 work?
  142. d.Write(CRNL + "\u2615\x1b[6n") // hot beverage
  143. d.Write("\x1b[999C\x1b[999B\x1b[6n" + Reset + "\x1b[2J\x1b[H") // goto end of screen + cursor pos
  144. // time.Sleep(50 * time.Millisecond)
  145. time.Sleep(250 * time.Millisecond)
  146. // read everything
  147. // telnet term isn't in RAW mode, so keys are buffer until <CR>
  148. var results string
  149. if d.HasKey() {
  150. buffer := make([]byte, 100)
  151. r, err := syscall.Read(d.READFD, buffer)
  152. results = string(buffer[:r])
  153. output := strings.Replace(results, "\x1b", "^[", -1)
  154. fmt.Println("DETECT:", r, err, output)
  155. } else {
  156. // local telnet echos the reply :()
  157. fmt.Println("DETECT: Nothing received.")
  158. return
  159. }
  160. if ((strings.Index(results, "1;1R") != -1) ||
  161. (strings.Index(results, "1;3R") != -1)) &&
  162. ((strings.Index(results, "2:2R") != -1) ||
  163. (strings.Index(results, "2;3R") != -1)) {
  164. Unicode = true
  165. } else {
  166. Unicode = false
  167. CP437 = true
  168. }
  169. if strings.Index(results, "1;3R") != -1 {
  170. Full_CP437 = true
  171. }
  172. // get screen size
  173. var err error
  174. pos := strings.LastIndex(results, "\x1b")
  175. if pos != -1 {
  176. pos++
  177. if results[pos] == '[' {
  178. pos++
  179. results = results[pos:]
  180. pos = strings.Index(results, ";")
  181. if pos != -1 {
  182. height := results[:pos]
  183. Height, err = strconv.Atoi(height)
  184. pos++
  185. results = results[pos:]
  186. pos = strings.Index(results, "R")
  187. if pos != -1 {
  188. width := results[:pos]
  189. Width, err = strconv.Atoi(width)
  190. fmt.Printf("Width: %s, %d, %v\n", results, Width, err)
  191. }
  192. } else {
  193. Height = 0
  194. Width = 0
  195. }
  196. }
  197. }
  198. fmt.Printf("Unicode %v Screen: %d X %d\n", Unicode, Width, Height)
  199. }
  200. // Initialize door framework. Parse commandline, read dropfile,
  201. // detect terminal capabilities.
  202. func (d *Door) Init() {
  203. var dropfile string
  204. flag.StringVar(&dropfile, "d", "", "Path to dropfile")
  205. flag.Parse()
  206. if len(dropfile) == 0 {
  207. flag.PrintDefaults()
  208. os.Exit(2)
  209. }
  210. fmt.Printf("Loading: %s\n", dropfile)
  211. d.ReadDropfile(dropfile)
  212. 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)
  213. d.detect()
  214. if Unicode {
  215. BOXES = BOXES_UNICODE
  216. BARS = BARS_UNICODE
  217. } else {
  218. BOXES = BOXES_CP437
  219. BARS = BARS_CP437
  220. }
  221. }
  222. // Write string to client.
  223. func (d *Door) Write(output string) {
  224. if d.Disconnected {
  225. return
  226. }
  227. buffer := []byte(output)
  228. n, err := syscall.Write(d.WRITEFD, buffer)
  229. if err != nil {
  230. fmt.Println("Write error/HANGUP?", n)
  231. d.Disconnected = true
  232. }
  233. // No, this isn't it. The # of bytes in buffer == bytes written.
  234. if n != len(buffer) {
  235. fmt.Printf("Write fail: %d != %d\n", len(buffer), n)
  236. }
  237. }
  238. func Goto(x int, y int) string {
  239. return fmt.Sprintf("\x1b[%d;%dH", y, x)
  240. }