// // 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 conf *rhimport.Config rddb *rhimport.RdDb ctx *rhimport.ImportContext } 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) say(text string) (err error) { return c.write_string(text + "\n") } func (c *TelnetClient) handle_cmd_help(args []string) { c.say(fmt.Sprintf("usage: tba...(args: %v)", args)) } func (c *TelnetClient) handle_cmd_set(args []string) { c.say(fmt.Sprintf("setting args: %v", args)) if c.ctx == nil { c.ctx = &rhimport.ImportContext{Config: c.conf, RdDb: c.rddb} c.ctx.UserName = "hugo" } } func (c *TelnetClient) handle_cmd_clear(args []string) { if len(args) >= 0 { c.say("too many arguments") } c.ctx = nil } func (c *TelnetClient) handle_cmd_show(args []string) { c.say(fmt.Sprintf("%+v", c.ctx)) } func (c *TelnetClient) handle_cmd_run(args []string) { if c.ctx != nil { c.say(fmt.Sprintf("running args: %v", args)) c.ctx = nil } else { c.say(fmt.Sprintf("context is empty please set at least one option")) } } func (c *TelnetClient) handle_cmd(cmdstr string) bool { cmdslice := strings.Fields(cmdstr) if len(cmdslice) == 0 || cmdslice[0] == "" { return false } cmd := cmdslice[0] args := cmdslice[1:] if cmd == "quit" { return true } else if cmd == "help" { c.handle_cmd_help(args) } else if cmd == "set" { c.handle_cmd_set(args) } else if cmd == "clear" { c.handle_cmd_clear(args) } else if cmd == "show" { c.handle_cmd_show(args) } else if cmd == "run" { c.handle_cmd_run(args) } else { c.say("unknown command") } 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_cmd(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, conf *rhimport.Config, rddb *rhimport.RdDb) (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) c.conf = conf c.rddb = rddb c.ctx = nil 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, conf, rddb) go c.handle() } }