-
Notifications
You must be signed in to change notification settings - Fork 86
/
interactive.go
173 lines (152 loc) · 3.44 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
// 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"
"context"
"fmt"
"log"
"os"
"runtime"
"strings"
"time"
"github.com/la5nta/wl2k-go/transport/ax25"
"github.com/peterh/liner"
)
func Interactive(ctx context.Context) {
line := liner.NewLiner()
defer line.Close()
done := make(chan struct{})
go func() {
defer close(done)
for {
str, _ := line.Prompt(getPrompt())
if str == "" {
continue
}
line.AppendHistory(str)
if str[0] == '#' {
continue
}
if quit := execCmd(str); quit {
break
}
}
}()
select {
case <-ctx.Done():
case <-done:
}
}
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")
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'")
transports := []string{
MethodArdop,
MethodAX25, MethodAX25AGWPE, MethodAX25Linux, MethodAX25SerialTNC,
MethodPactor,
MethodTelnet,
MethodVaraHF,
MethodVaraFM,
}
fmt.Println("Transports:", strings.Join(transports, ", "))
cmds := []string{
"connect <connect-url or alias> Connect to a remote station.",
"listen <transport> Listen for incoming connections.",
"unlisten <transport> Unregister listener for incoming connections.",
"freq <transport>[:<freq>] Read/set 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("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+linux:")
if heard, err := ax25.Heard(config.AX25Linux.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]
}