// // 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 = "> " ) 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(format string, a ...interface{}) (err error) { return c.write_string(fmt.Sprintf(format, a...) + "\n") } func (c *TelnetClient) handle_cmd_help(args []string) { c.say("usage: tba...(args: %v)", args) } func (c *TelnetClient) handle_cmd_set(args []string) { c.say("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_reset(args []string) { if len(args) > 0 { c.say("too many arguments") return } c.ctx = nil } func (c *TelnetClient) handle_cmd_show(args []string) { if len(args) > 0 { c.say("too many arguments") return } if c.ctx != nil { c.say(" UserName: %v", c.ctx.UserName) c.say(" Password: %v", c.ctx.Password) c.say(" Trusted: %v", c.ctx.Trusted) c.say(" ShowId: %v", c.ctx.ShowId) c.say(" ClearShowCarts: %v", c.ctx.ClearShowCarts) c.say(" GroupName: %v", c.ctx.GroupName) c.say(" Cart: %v", c.ctx.Cart) c.say(" ClearCart: %v", c.ctx.ClearCart) c.say(" Cut: %v", c.ctx.Cut) c.say(" Channels: %v", c.ctx.Channels) c.say(" NormalizationLevel: %v", c.ctx.NormalizationLevel) c.say(" AutotrimLevel: %v", c.ctx.AutotrimLevel) c.say(" UseMetaData: %v", c.ctx.UseMetaData) c.say(" SourceUri: %v", c.ctx.SourceUri) } else { c.say("context is empty") } } func (c *TelnetClient) handle_cmd_run(args []string) { if c.ctx != nil { c.say("running args: %v", args) c.ctx = nil } else { c.say("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 == "reset" { c.handle_cmd_reset(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() } }