123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- package door
- import (
- "bufio"
- "container/list"
- "flag"
- "fmt"
- "os"
- "strconv"
- "strings"
- "syscall"
- "time"
- )
- const CRNL = "\r\n"
- var Reset string = Color(0)
- var Unicode bool
- var CP437 bool
- var Full_CP437 bool
- var Height int
- var Width int
- type DropfileConfig struct {
- comm_type int
- comm_handle int
- baudrate int
- BBSID string
- user_number int
- real_name string
- handle string
- security_level int
- time_left int
- emulation int
- node_number int
- }
- type Door struct {
- config DropfileConfig
- READFD int
- WRITEFD int
- TimeOut time.Time
- pushback *list.List
- }
- func (d *Door) TimeLeft() time.Duration {
- return d.TimeOut.Sub(time.Now())
- }
- func (d *Door) ReadDropfile(filename string) {
- file, err := os.Open(filename)
- if err != nil {
- fmt.Printf("Open(%s): %s\n", filename, err)
- os.Exit(2)
- }
- defer file.Close()
- var lines []string
-
-
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- line := scanner.Text()
- lines = append(lines, line)
- }
- d.config.comm_type, err = strconv.Atoi(lines[0])
- d.config.comm_handle, err = strconv.Atoi(lines[1])
- d.config.baudrate, err = strconv.Atoi(lines[2])
- d.config.BBSID = lines[3]
- d.config.user_number, err = strconv.Atoi(lines[4])
- d.config.real_name = lines[5]
- d.config.handle = lines[6]
- d.config.security_level, err = strconv.Atoi(lines[7])
- d.config.time_left, err = strconv.Atoi(lines[8])
- d.config.emulation, err = strconv.Atoi(lines[9])
- d.config.node_number, err = strconv.Atoi(lines[10])
- d.READFD = d.config.comm_handle
- d.WRITEFD = d.config.comm_handle
-
- d.TimeOut = time.Now().Add(time.Duration(d.config.time_left) * time.Minute)
- }
- func (d *Door) detect() {
- d.Write("\x1b[0;30;40m\x1b[2J\x1b[H")
- d.Write("\x03\x04\x1b[6n")
- d.Write(CRNL + "\u2615\x1b[6n")
- d.Write("\x1b[999C\x1b[999B\x1b[6n" + Reset + "\x1b[2J\x1b[H")
-
- time.Sleep(250 * time.Millisecond)
-
-
- var results string
- if d.HasKey() {
- buffer := make([]byte, 100)
- r, err := syscall.Read(d.READFD, buffer)
- results = string(buffer[:r])
- output := strings.Replace(results, "\x1b", "^[", -1)
- fmt.Println("DETECT:", r, err, output)
- } else {
-
- fmt.Println("DETECT: Nothing received.")
- return
- }
- if ((strings.Index(results, "1;1R") != -1) ||
- (strings.Index(results, "1;3R") != -1)) &&
- ((strings.Index(results, "2:2R") != -1) ||
- (strings.Index(results, "2;3R") != -1)) {
- Unicode = true
- } else {
- Unicode = false
- CP437 = true
- }
- if strings.Index(results, "1;3R") != -1 {
- Full_CP437 = true
- }
-
- var err error
- pos := strings.LastIndex(results, "\x1b")
- if pos != -1 {
- pos++
- if results[pos] == '[' {
- pos++
- results = results[pos:]
- pos = strings.Index(results, ";")
- if pos != -1 {
- height := results[:pos]
- Height, err = strconv.Atoi(height)
- pos++
- results = results[pos:]
- pos = strings.Index(results, "R")
- if pos != -1 {
- width := results[:pos]
- Width, err = strconv.Atoi(width)
- fmt.Printf("Width: %s, %d, %v\n", results, Width, err)
- }
- } else {
- Height = 0
- Width = 0
- }
- }
- }
- fmt.Printf("Unicode %v Screen: %d X %d\n", Unicode, Width, Height)
- }
- func (d *Door) Init() {
- var dropfile string
- d.pushback = list.New()
- flag.StringVar(&dropfile, "d", "", "Path to dropfile")
- flag.Parse()
- if len(dropfile) == 0 {
- flag.PrintDefaults()
- os.Exit(2)
- }
- fmt.Printf("Loading: %s\n", dropfile)
- d.ReadDropfile(dropfile)
- 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)
- d.detect()
- }
- func (d *Door) Write(output string) {
- buffer := []byte(output)
- n, err := syscall.Write(d.WRITEFD, buffer)
- if err != nil {
- fmt.Println("Write error/HANGUP?", n)
- }
-
- if n != len(buffer) {
- fmt.Printf("Write fail: %d != %d\n", len(buffer), n)
- }
- }
|