123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package door
- import (
- "bufio"
- "flag"
- "fmt"
- "os"
- "strconv"
- "strings"
- "syscall"
- "time"
- )
- const CRNL = "\r\n"
- const Clrscr = "\x1b[0m\x1b[2J\x1b[H"
- const HideCursor = "\x1b[?25l"
- const ShowCursor = "\x1b[?25h"
- var Reset string = Color(0)
- var Unicode bool
- var CP437 bool
- var Full_CP437 bool
- var Height int
- var Width int
- var Inactivity int64 = 120
- type LIFOBuffer struct {
- data []int
- index int
- }
- func (b *LIFOBuffer) Empty() bool {
- return b.index == 0
- }
- func (b *LIFOBuffer) Push(value int) {
- if b.index+1 > len(b.data) {
- b.data = append(b.data, value)
- b.index++
- } else {
- b.data[b.index] = value
- b.index++
- }
- }
- func (b *LIFOBuffer) Pop() int {
- if b.index == 0 {
- panic("Attempting to Pop from empty LIFOBuffer.")
- }
- b.index--
- return b.data[b.index]
- }
- 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 int
- }
- type Door struct {
- Config DropfileConfig
- READFD int
- WRITEFD int
- Disconnected bool
- TimeOut time.Time
- StartTime time.Time
- Pushback LIFOBuffer
- }
- func (d *Door) TimeLeft() time.Duration {
- return d.TimeOut.Sub(time.Now())
- }
- func (d *Door) TimeUsed() time.Duration {
- return time.Now().Sub(d.StartTime)
- }
- func (d *Door) ReadDropfile(filename string) {
- file, err := os.Open(filename)
- if err != nil {
- panic(fmt.Sprintf("Open(%s): %s\n", filename, err))
-
- }
- 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, err = strconv.Atoi(lines[10])
- d.READFD = d.Config.Comm_handle
- d.WRITEFD = d.Config.Comm_handle
-
- d.StartTime = time.Now()
- 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 = ""
- for {
- r := d.getch()
- if r == -1 {
- break
- }
- results += string(byte(r))
- }
- if len(results) > 0 {
- output := strings.Replace(results, "\x1b", "^[", -1)
- fmt.Println("DETECT:", 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
- 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)
- readerChannel = make(chan byte)
- go Reader(syscall.Handle(d.Config.Comm_handle))
- d.detect()
- if Unicode {
- BOXES = BOXES_UNICODE
- BARS = BARS_UNICODE
- } else {
- BOXES = BOXES_CP437
- BARS = BARS_CP437
- }
- }
- func Goto(x int, y int) string {
- return fmt.Sprintf("\x1b[%d;%dH", y, x)
- }
|