door_linux.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 time.Until(d.TimeOut)
  79. // return d.TimeOut.Sub(time.Now())
  80. }
  81. func (d *Door) TimeUsed() time.Duration {
  82. return time.Since(d.StartTime)
  83. // return time.Now().Sub(d.StartTime)
  84. }
  85. // Read the BBS door file. We only support door32.sys.
  86. func (d *Door) ReadDropfile(filename string) {
  87. file, err := os.Open(filename)
  88. if err != nil {
  89. log.Panicf("Open(%s): %s\n", filename, err)
  90. // os.Exit(2)
  91. }
  92. defer file.Close()
  93. var lines []string
  94. // read line by line
  95. // The scanner handles DOS and linux file endings.
  96. scanner := bufio.NewScanner(file)
  97. for scanner.Scan() {
  98. line := scanner.Text()
  99. lines = append(lines, line)
  100. }
  101. d.Config.Comm_type, err = strconv.Atoi(lines[0])
  102. if err != nil {
  103. log.Panicf("Door32 Comm Type (expected integer): %s\n", err)
  104. }
  105. d.Config.Comm_handle, err = strconv.Atoi(lines[1])
  106. if err != nil {
  107. log.Panicf("Door32 Comm Handle (expected integer): %s\n", err)
  108. }
  109. d.Config.Baudrate, err = strconv.Atoi(lines[2])
  110. if err != nil {
  111. log.Panicf("Door32 Baudrate (expected integer): %s\n", err)
  112. }
  113. d.Config.BBSID = lines[3]
  114. d.Config.User_number, err = strconv.Atoi(lines[4])
  115. if err != nil {
  116. log.Panicf("Door32 User Number (expected integer): %s\n", err)
  117. }
  118. d.Config.Real_name = lines[5]
  119. d.Config.Handle = lines[6]
  120. d.Config.Security_level, err = strconv.Atoi(lines[7])
  121. if err != nil {
  122. log.Panicf("Door32 Security Level (expected integer): %s\n", err)
  123. }
  124. d.Config.Time_left, err = strconv.Atoi(lines[8])
  125. if err != nil {
  126. log.Panicf("Door32 Time Left (expected integer): %s\n", err)
  127. }
  128. d.Config.Emulation, err = strconv.Atoi(lines[9])
  129. if err != nil {
  130. log.Panicf("Door32 Emulation (expected integer): %s\n", err)
  131. }
  132. d.Config.Node, err = strconv.Atoi(lines[10])
  133. if err != nil {
  134. log.Panicf("Door32 Node Number (expected integer): %s\n", err)
  135. }
  136. d.READFD = d.Config.Comm_handle
  137. d.WRITEFD = d.Config.Comm_handle
  138. // Calculate the time when time expires.
  139. d.StartTime = time.Now()
  140. d.TimeOut = time.Now().Add(time.Duration(d.Config.Time_left) * time.Minute)
  141. }
  142. // Detect client terminal capabilities, Unicode, CP437, Full_CP437,
  143. // screen Height and Width.
  144. func (d *Door) detect() {
  145. d.Write("\x1b[0;30;40m\x1b[2J\x1b[H") // black on black, clrscr, go home
  146. d.Write("\x03\x04\x1b[6n") // hearts and diamonds does CP437 work?
  147. d.Write(CRNL + "\u2615\x1b[6n") // hot beverage
  148. d.Write("\x1b[999C\x1b[999B\x1b[6n" + Reset + "\x1b[2J\x1b[H") // goto end of screen + cursor pos
  149. // time.Sleep(50 * time.Millisecond)
  150. time.Sleep(250 * time.Millisecond)
  151. // read everything
  152. // telnet term isn't in RAW mode, so keys are buffer until <CR>
  153. var results string
  154. if d.HasKey() {
  155. buffer := make([]byte, 100)
  156. r, err := syscall.Read(d.READFD, buffer)
  157. if r == -1 {
  158. d.Disconnected = true
  159. return
  160. }
  161. results = string(buffer[:r])
  162. output := strings.Replace(results, "\x1b", "^[", -1)
  163. log.Println("DETECT:", r, err, output)
  164. } else {
  165. // local telnet echos the reply :()
  166. log.Println("DETECT: Nothing received.")
  167. return
  168. }
  169. if (strings.Contains(results, "1;1R") ||
  170. strings.Contains(results, "1;3R")) &&
  171. (strings.Contains(results, "2:2R") ||
  172. strings.Contains(results, "2;3R")) {
  173. Unicode = true
  174. } else {
  175. Unicode = false
  176. CP437 = true
  177. }
  178. if strings.Contains(results, "1;3R") {
  179. Full_CP437 = true
  180. }
  181. // get screen size
  182. pos := strings.LastIndex(results, "\x1b")
  183. if pos != -1 {
  184. pos++
  185. if results[pos] == '[' {
  186. pos++
  187. results = results[pos:]
  188. pos = strings.Index(results, ";")
  189. if pos != -1 {
  190. height := results[:pos]
  191. Height, _ = strconv.Atoi(height)
  192. pos++
  193. results = results[pos:]
  194. pos = strings.Index(results, "R")
  195. if pos != -1 {
  196. width := results[:pos]
  197. Width, _ = strconv.Atoi(width)
  198. // log.Printf("Width: %s, %d, %v\n", results, Width, err)
  199. }
  200. } else {
  201. Height = 0
  202. Width = 0
  203. }
  204. }
  205. }
  206. log.Printf("Unicode %v Screen: %d X %d\n", Unicode, Width, Height)
  207. }
  208. // Initialize door framework. Parse commandline, read dropfile,
  209. // detect terminal capabilities.
  210. func (d *Door) Init(doorname string) {
  211. var dropfile string
  212. d.Pushback = NewFIFOBuffer(5)
  213. // Get path to binary, and chdir to it.
  214. binaryPath, _ := os.Executable()
  215. binaryPath = filepath.Dir(binaryPath)
  216. _ = os.Chdir(binaryPath)
  217. flag.StringVar(&dropfile, "d", "", "Path to dropfile")
  218. flag.Parse()
  219. if len(dropfile) == 0 {
  220. flag.PrintDefaults()
  221. os.Exit(2)
  222. }
  223. d.ReadDropfile(dropfile)
  224. // doorname - node #?
  225. logfilename := fmt.Sprintf("%s-%d.log", doorname, d.Config.Node)
  226. logf, err := os.OpenFile(logfilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
  227. if err != nil {
  228. log.Panicf("Error creating log file %s: %v", logfilename, err)
  229. }
  230. log.SetOutput(logf)
  231. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  232. // log.SetPrefix(doorname + " ")
  233. //= log.New(logf, fmt.Sprintf("%s-%d", doorname, d.Config.Node), 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.detect()
  237. if Unicode {
  238. BOXES = BOXES_UNICODE
  239. BARS = BARS_UNICODE
  240. } else {
  241. BOXES = BOXES_CP437
  242. BARS = BARS_CP437
  243. }
  244. }
  245. func Goto(x int, y int) string {
  246. return fmt.Sprintf("\x1b[%d;%dH", y, x)
  247. }