door_windows.go 7.0 KB

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