// // rhimportd // // The Radio Helsinki Rivendell Import Daemon // // // Copyright (C) 2015 Christian Pointner // // 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 . // 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() } }