door.go 8.2 KB

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