// // 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" "net/http" "strconv" "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) { switch len(args) { case 1: switch args[0] { case "quit": c.say("usage: quit") c.say(" terminates the client connection.") return case "help": c.say("usage: help [ ]") c.say(" prints command overview or detailed info to .") return case "set": c.say("usage: set ") c.say(" this sets the import parameter to .") c.say("") c.say(" available parameters:") c.say(" UserName string username to use for requests to rivendell rdxport interface") c.say(" Password string password to use for requests to rivendell rdxport interface") c.say(" SourceUri string uri to the file to import") c.say(" ShowId uint the RHRD show id to import to") c.say(" ClearShowCarts bool tells the importer to clear all show-carts before importing to a show") c.say(" GroupName string name of music-pool group to import to") c.say(" Cart uint cart to import to") c.say(" ClearCart bool remove all existing cuts from cart before importing?") c.say(" Cut uint cut to import to") c.say(" Channels uint number of audio channels (defaults to: %v)", c.conf.ImportParamDefaults.Channels) c.say(" NormalizationLevel int normalization level in dB (defaults to: %v)", c.conf.ImportParamDefaults.NormalizationLevel) c.say(" AutotrimLevel int autotrim level in dB (defaults to: %v)", c.conf.ImportParamDefaults.AutotrimLevel) c.say(" UseMetaData bool extract meta data from source file (defaults to: %v)", c.conf.ImportParamDefaults.UseMetaData) c.say("") c.say(" UserName, Password and SourceUri are mandatory parameters.") c.say("") c.say(" If ShowId is supplied GroupName, Channels, NomalizationLevel, AutorimLevel, UseMetaData") c.say(" and Cut will be ignored. The values from the shows' dropbox will be used instead. Cart may") c.say(" be specified but must point to an empty cart within that show. If ClearCut is true the") c.say(" specified cart will get deleted before importing. If Cart is 0 the next free cart in the") c.say(" show will be used. Show carts will always be imported into cut 1.") c.say("") c.say(" If GroupName is supplied Channels, NomalizationLevel, AutorimLevel, UseMetaData, Cut and") c.say(" Cart will be ignored. The values from the music pools' dropbox will be used instead. The") c.say(" file will always be imported into cut 1 of the first free cart within the music pool.") c.say("") c.say(" If ShowId and GroupName are omitted a Cart must be specified. Cut may be supplied in which") c.say(" case both cart and cut must already exist. The import will then replace the contents of the") c.say(" current data stored in Cart/Cut. If only Cart and no Cut is supplied and ClearCut is false the") c.say(" file will either get imported into the next cut of an existing cart or the cart will be created") c.say(" and the file will be imported into cut 1 of this cart.") c.say("") c.say(" In case of an error carts/cuts which might got created will be removed. Carts which got") c.say(" deleted because of ClearShowCarts or ClearCart are however gone for good.") return case "show": c.say("usage: show") c.say(" this prints the current values of all import parameters.") return case "reset": c.say("usage: reset") c.say(" this resets all import parameters to default values.") return case "run": c.say("usage: run") c.say(" this starts the fetch/import process according to the current import parameters.") return } fallthrough default: c.say("usage: [ [ ] ... ]") c.say(" available commands:") c.say(" quit close connection") c.say(" help [ ] print this, or help for specific command") c.say(" set sets parameter on current import context") c.say(" show shows current import context") c.say(" reset resets current import context") c.say(" run runs fetch/import using current import context") } } func (c *TelnetClient) handle_cmd_set(args []string) { if len(args) != 2 { c.say("wrong number of arguments") return } if c.ctx == nil { c.ctx = rhimport.NewImportContext(c.conf, c.rddb, "") c.ctx.Trusted = true // TODO: set this back to false as soon as debugging is done } switch strings.ToLower(args[0]) { case "username": c.ctx.UserName = args[1] case "password": c.ctx.Password = args[1] case "sourceuri": c.ctx.SourceUri = args[1] case "showid": if id, err := strconv.ParseUint(args[1], 10, 32); err != nil { c.say("invalid value (must be an positive integer)") } else { c.ctx.ShowId = uint(id) } case "clearshowcarts": if val, err := strconv.ParseBool(args[1]); err != nil { c.say("invalid value (must be true or false)") } else { c.ctx.ClearShowCarts = val } case "groupname": c.ctx.GroupName = args[1] case "cart": if cart, err := strconv.ParseUint(args[1], 10, 32); err != nil { c.say("invalid value (must be an positive integer)") } else { c.ctx.Cart = uint(cart) } case "clearcart": if val, err := strconv.ParseBool(args[1]); err != nil { c.say("invalid value (must be true or false)") } else { c.ctx.ClearCart = val } case "cut": if cut, err := strconv.ParseUint(args[1], 10, 32); err != nil { c.say("invalid value (must be an positive integer)") } else { c.ctx.Cut = uint(cut) } case "channels": if channels, err := strconv.ParseUint(args[1], 10, 32); err != nil { c.say("invalid value (must be an positive integer)") } else { c.ctx.Channels = uint(channels) } case "normalizationlevel": if normalizationlevel, err := strconv.ParseInt(args[1], 10, 32); err != nil { c.say("invalid value (must be an positive integer)") } else { c.ctx.NormalizationLevel = int(normalizationlevel) } case "autotrimlevel": if autotrimlevel, err := strconv.ParseInt(args[1], 10, 32); err != nil { c.say("invalid value (must be an positive integer)") } else { c.ctx.AutotrimLevel = int(autotrimlevel) } case "usemetadata": if val, err := strconv.ParseBool(args[1]); err != nil { c.say("invalid value (must be true or false)") } else { c.ctx.UseMetaData = val } default: c.say("unknown parameter, use 'help set' for a list of available parameters") } } 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(" SourceUri: %v", c.ctx.SourceUri) 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) } else { c.say("context is empty") } } func (c *TelnetClient) handle_cmd_run(args []string) { if c.ctx == nil { c.say("context is empty please set at least one option") return } ctx := *c.ctx if err := ctx.SanityCheck(); err != nil { c.say("sanity check for import context returned: %s", err) return } c.say("fetching file from '%s'", ctx.SourceUri) if err := rhimport.FetchFile(&ctx); err != nil { c.say("fetch file error: %s", err) return } c.say("importing file '%s'", ctx.SourceFile) if result, err := rhimport.ImportFile(&ctx); err != nil { c.say("import file error: %s", err) return } else { if result.ResponseCode == http.StatusOK { c.say("File got succesfully imported into Cart/Cut %d/%d", result.Cart, result.Cut) rhl.Printf("File got succesfully imported into Cart/Cut %d/%d", result.Cart, result.Cut) } else { c.say("Fileimport has failed (Cart/Cut %d/%d): %s", result.Cart, result.Cut, result.ErrorString) rhl.Printf("Fileimport has failed (Cart/Cut %d/%d): %s", result.Cart, result.Cut, result.ErrorString) } } } 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() } }