door.go 7.9 KB

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