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
|
//
// rhimportd
//
// The Radio Helsinki Rivendell Import Daemon
//
//
// Copyright (C) 2015 Christian Pointner <equinox@helsinki.at>
//
// This file is part of rhimportd.
//
// rhimportd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// rhimportd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with rhimportd. If not, see <http://www.gnu.org/licenses/>.
//
package main
import (
"bufio"
"fmt"
"helsinki.at/rhimport"
"net"
"strings"
)
const (
prompt = "rhimportd> "
)
type TelnetClient struct {
conn net.Conn
scanner *bufio.Scanner
writer *bufio.Writer
}
func (c *TelnetClient) write_string(text string) (err error) {
if _, err = c.writer.WriteString(text); err != nil {
return
}
return c.writer.Flush()
}
func (c *TelnetClient) handle_command(cmd string) bool {
if cmd == "quit" {
return true
} else if strings.HasPrefix(cmd, "help") {
c.write_string(fmt.Sprintf("usage: tba...\n"))
} else {
c.write_string("unknown command\n")
}
return false
}
func (c *TelnetClient) handle() {
defer c.conn.Close()
if err := c.write_string(prompt); err != nil {
return
}
for c.scanner.Scan() {
if exit := c.handle_command(c.scanner.Text()); exit {
return
}
if err := c.write_string(prompt); err != nil {
return
}
}
if err := c.scanner.Err(); err != nil {
rhdl.Printf("telnet-ctrl(%s): read command error: %s", c.conn.RemoteAddr(), err)
} else {
rhdl.Printf("telnet-ctrl(%s): connection closed", c.conn.RemoteAddr())
}
}
func newTelnetClient(conn net.Conn) (c *TelnetClient) {
rhl.Println("telnet-ctrl: new client from:", conn.RemoteAddr())
c = &TelnetClient{}
c.conn = conn
c.scanner = bufio.NewScanner(conn)
c.writer = bufio.NewWriter(conn)
return c
}
func StartControlTelnet(addr_s string, conf *rhimport.Config, rddb *rhimport.RdDb) {
rhl.Println("telnet-ctrl: listening on", addr_s)
server, err := net.Listen("tcp", addr_s)
if err != nil {
rhl.Println("telnet-ctrl: Listen() Error:", err)
return
}
for {
conn, err := server.Accept()
if err != nil {
rhl.Println("telnet-ctrl: Accept() Error:", err)
return
}
c := newTelnetClient(conn)
go c.handle()
}
}
|