forked from la5nta/pat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.go
175 lines (153 loc) · 3.45 KB
/
interactive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2016 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
// Use of this source code is governed by the MIT-license that can be
// found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"log"
"os"
"runtime"
"strings"
"time"
"github.com/la5nta/wl2k-go/transport/ax25"
"github.com/peterh/liner"
)
func Interactive() {
line := liner.NewLiner()
defer line.Close()
for {
str, _ := line.Prompt(getPrompt())
if str == "" {
continue
}
line.AppendHistory(str)
if str[0] == '#' {
continue
}
if quit := execCmd(str); quit {
break
}
}
}
func execCmd(line string) (quit bool) {
cmd, param := parseCommand(line)
switch cmd {
case "connect":
if param == "" {
printInteractiveUsage()
return
}
Connect(param)
case "listen":
Listen(param)
case "unlisten":
Unlisten(param)
case "heard":
PrintHeard()
case "freq":
freq(param)
case "qtc":
PrintQTC()
case "debug":
os.Setenv("ardop_debug", "1")
os.Setenv("winmor_debug", "1")
fmt.Println("Number of goroutines:", runtime.NumGoroutine())
case "q", "quit":
return true
case "":
return
default:
printInteractiveUsage()
}
return
}
func printInteractiveUsage() {
fmt.Println("Uri examples: 'LA3F@5350', 'LA1B-10 v LA5NTA-1', 'LA5NTA:[email protected]:54321'")
methods := []string{
MethodWinmor,
MethodArdop,
MethodAX25,
MethodTelnet,
MethodSerialTNC,
}
fmt.Println("Methods:", strings.Join(methods, ", "))
cmds := []string{
"connect METHOD:[URI] or alias Connect to a remote station.",
"listen METHOD Listen for incoming connections.",
"unlisten METHOD Unregister listener for incoming connections.",
"freq METHOD:FREQ Change rig frequency.",
"heard Display all stations heard over the air.",
"qtc Print pending outbound messages.",
}
fmt.Println("Commands: ")
for _, cmd := range cmds {
fmt.Printf(" %s\n", cmd)
}
}
func getPrompt() string {
var buf bytes.Buffer
status := getStatus()
if len(status.ActiveListeners) > 0 {
fmt.Fprintf(&buf, "L%v", status.ActiveListeners)
}
fmt.Fprint(&buf, "> ")
return buf.String()
}
func PrintHeard() {
pf := func(call string, t time.Time) {
fmt.Printf(" %-10s (%s)\n", call, t.Format(time.RFC1123))
}
fmt.Println("winmor:")
if wmTNC == nil {
fmt.Println(" (not initialized)")
} else if heard := wmTNC.Heard(); len(heard) == 0 {
fmt.Println(" (none)")
} else {
for call, t := range heard {
pf(call, t)
}
}
fmt.Println("ardop:")
if adTNC == nil {
fmt.Println(" (not initialized)")
} else if heard := adTNC.Heard(); len(heard) == 0 {
fmt.Println(" (none)")
} else {
for call, t := range heard {
pf(call, t)
}
}
fmt.Println("ax25:")
if heard, err := ax25.Heard(config.AX25.Port); err != nil {
fmt.Printf(" (%s)\n", err)
} else if len(heard) == 0 {
fmt.Println(" (none)")
} else {
for call, t := range heard {
pf(call, t)
}
}
}
func PrintQTC() {
msgs, err := mbox.Outbox()
if err != nil {
log.Println(err)
return
}
fmt.Printf("QTC: %d.\n", len(msgs))
for _, msg := range msgs {
fmt.Printf(`%-12.12s (%s): %s`, msg.MID(), msg.Subject(), fmt.Sprint(msg.To()))
if msg.Header.Get("X-P2POnly") == "true" {
fmt.Printf(" (P2P only)")
}
fmt.Println("")
}
}
func parseCommand(str string) (mode, param string) {
parts := strings.SplitN(str, " ", 2)
if len(parts) == 1 {
return parts[0], ""
}
return parts[0], parts[1]
}