door.go 7.6 KB

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