// // 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 ( "fmt" "github.com/spreadspace/telgo" "helsinki.at/rhimport" "net/http" "strconv" "strings" ) func telnet_quit(c *telgo.TelnetClient, args []string, conf *rhimport.Config, rddb *rhimport.RdDbChan) bool { return true } func telnet_help(c *telgo.TelnetClient, args []string, conf *rhimport.Config, rddb *rhimport.RdDbChan) bool { switch len(args) { case 2: switch args[1] { case "quit": c.Sayln("usage: quit") c.Sayln(" terminates the client connection. You may also use Ctrl-D to do this.") return false case "help": c.Sayln("usage: help [ ]") c.Sayln(" prints command overview or detailed info to .") return false case "set": c.Sayln("usage: set ") c.Sayln(" this sets the import parameter to .") c.Sayln("") c.Sayln(" available parameters:") c.Sayln(" UserName string username to use for rdxport interface") c.Sayln(" Password string password to use for rdxport interface") c.Sayln(" SourceUri string uri to the file to import") c.Sayln(" ShowId uint the RHRD show id to import to") c.Sayln(" ClearShowCarts bool clear all show-carts before importing?") c.Sayln(" GroupName string name of music-pool group to import to") c.Sayln(" Cart uint cart to import to") c.Sayln(" ClearCart bool remove/add cart before import") c.Sayln(" Cut uint cut to import to") c.Sayln(" Channels uint number of audio channels (default: %v)", conf.ImportParamDefaults.Channels) c.Sayln(" NormalizationLevel int normalization level in dB (default: %v)", conf.ImportParamDefaults.NormalizationLevel) c.Sayln(" AutotrimLevel int autotrim level in dB (default: %v)", conf.ImportParamDefaults.AutotrimLevel) c.Sayln(" UseMetaData bool extract meta data from file (default: %v)", conf.ImportParamDefaults.UseMetaData) c.Sayln("") c.Sayln(" UserName, Password and SourceUri are mandatory parameters.") c.Sayln("") c.Sayln(" If ShowId is supplied GroupName, Channels, NomalizationLevel, AutorimLevel,") c.Sayln(" UseMetaData and Cut will be ignored. The values from the shows' dropbox will") c.Sayln(" be used instead. Cart may be specified but must point to an empty cart within") c.Sayln(" that show. If ClearCut is true the specified cart will get deleted before") c.Sayln(" importing. If Cart is 0 the next free cart in the show will be used. Show") c.Sayln(" carts will always be imported into cut 1.") c.Sayln("") c.Sayln(" If GroupName is supplied Channels, NomalizationLevel, AutorimLevel,") c.Sayln(" UseMetaData, Cut, Cart and ClearCart will be ignored. The values from") c.Sayln(" the music pools' dropbox will be used instead. The file will always be") c.Sayln(" imported into cut 1 of the first free cart within the music pool.") c.Sayln("") c.Sayln(" If ShowId and GroupName are omitted a Cart must be specified. Cut may be") c.Sayln(" supplied in which case both cart and cut must already exist. The import will") c.Sayln(" then replace the contents of the current data stored in Cart/Cut. If only Cart") c.Sayln(" and no Cut is supplied and ClearCut is false the file will either get imported") c.Sayln(" into the next cut of an existing cart or the cart will be created and the file") c.Sayln(" will be imported into cut 1 of this cart.") c.Sayln("") c.Sayln(" In case of an error carts/cuts which might got created will be removed. Carts") c.Sayln(" which got deleted because of ClearShowCarts or ClearCart are however gone for") c.Sayln(" good.") return false case "show": c.Sayln("usage: show") c.Sayln(" this prints the current values of all import parameters.") return false case "reset": c.Sayln("usage: reset") c.Sayln(" this resets all import parameters to default values.") return false case "run": c.Sayln("usage: run") c.Sayln(" this starts the fetch/import process according to the current") c.Sayln(" import parameters.") return false } fallthrough default: c.Sayln("usage: [ [ ] ... ]") c.Sayln(" available commands:") c.Sayln(" quit close connection (or use Ctrl-D)") c.Sayln(" help [ ] print this, or help for specific command") c.Sayln(" set sets parameter on current import context") c.Sayln(" show shows current import context") c.Sayln(" reset resets current import context") c.Sayln(" run runs fetch/import using current import context") } return false } func telnet_set_int(c *telgo.TelnetClient, param *int, val string) { if vint, err := strconv.ParseInt(val, 10, 32); err != nil { c.Sayln("invalid value (must be an integer)") } else { *param = int(vint) } } func telnet_set_uint(c *telgo.TelnetClient, param *uint, val string) { if vuint, err := strconv.ParseUint(val, 10, 32); err != nil { c.Sayln("invalid value (must be a positive integer)") } else { *param = uint(vuint) } } func telnet_set_bool(c *telgo.TelnetClient, param *bool, val string) { if vbool, err := strconv.ParseBool(val); err != nil { c.Sayln("invalid value (must be true or false)") } else { *param = vbool } } func telnet_set(c *telgo.TelnetClient, args []string, conf *rhimport.Config, rddb *rhimport.RdDbChan) bool { if len(args) != 3 { c.Sayln("wrong number of arguments") return false } var ctx *rhimport.ImportContext if c.UserData == nil { c.UserData = rhimport.NewImportContext(conf, rddb, "") ctx = c.UserData.(*rhimport.ImportContext) ctx.Trusted = false } else { ctx = c.UserData.(*rhimport.ImportContext) } switch strings.ToLower(args[1]) { case "username": ctx.UserName = args[2] case "password": ctx.Password = args[2] case "sourceuri": ctx.SourceUri = args[2] case "showid": telnet_set_uint(c, &ctx.ShowId, args[2]) case "clearshowcarts": telnet_set_bool(c, &ctx.ClearShowCarts, args[2]) case "groupname": ctx.GroupName = args[2] case "cart": telnet_set_uint(c, &ctx.Cart, args[2]) case "clearcart": telnet_set_bool(c, &ctx.ClearCart, args[2]) case "cut": telnet_set_uint(c, &ctx.Cut, args[2]) case "channels": telnet_set_uint(c, &ctx.Channels, args[2]) case "normalizationlevel": telnet_set_int(c, &ctx.NormalizationLevel, args[2]) case "autotrimlevel": telnet_set_int(c, &ctx.AutotrimLevel, args[2]) case "usemetadata": telnet_set_bool(c, &ctx.UseMetaData, args[2]) default: c.Sayln("unknown parameter, use 'help set' for a list of available parameters") } return false } func telnet_reset(c *telgo.TelnetClient, args []string, conf *rhimport.Config, rddb *rhimport.RdDbChan) bool { if len(args) > 1 { c.Sayln("too many arguments") return false } c.UserData = nil return false } func telnet_show(c *telgo.TelnetClient, args []string, conf *rhimport.Config, rddb *rhimport.RdDbChan) bool { if len(args) > 1 { c.Sayln("too many arguments") return false } if c.UserData != nil { ctx := c.UserData.(*rhimport.ImportContext) c.Sayln(" UserName: %q", ctx.UserName) c.Sayln(" Password: %q", ctx.Password) c.Sayln(" SourceUri: %q", ctx.SourceUri) c.Sayln(" ShowId: %v", ctx.ShowId) c.Sayln(" ClearShowCarts: %v", ctx.ClearShowCarts) c.Sayln(" GroupName: %q", ctx.GroupName) c.Sayln(" Cart: %v", ctx.Cart) c.Sayln(" ClearCart: %v", ctx.ClearCart) c.Sayln(" Cut: %v", ctx.Cut) c.Sayln(" Channels: %v", ctx.Channels) c.Sayln(" NormalizationLevel: %v", ctx.NormalizationLevel) c.Sayln(" AutotrimLevel: %v", ctx.AutotrimLevel) c.Sayln(" UseMetaData: %v", ctx.UseMetaData) } else { c.Sayln("context is empty") } return false } func telnet_progress_callback(step int, step_name string, progress float64, userdata interface{}) bool { c := userdata.(*telgo.TelnetClient) c.Say("%s: %3.2f%%\r", step_name, progress*100) return true } func telnet_run(c *telgo.TelnetClient, args []string, conf *rhimport.Config, rddb *rhimport.RdDbChan) bool { if c.UserData == nil { c.Sayln("context is empty please set at least one option") return false } ctx := c.UserData.(*rhimport.ImportContext) if err := ctx.SanityCheck(); err != nil { c.Sayln("sanity check for import context returned: %s", err) return false } ctx.ProgressCallBack = telnet_progress_callback ctx.ProgressCallBackData = c ctx.Cancel = c.Cancel c.Sayln("fetching file from '%s'", ctx.SourceUri) if res, err := rhimport.FetchFile(ctx); err != nil { c.Sayln("fetch file error: %s", err) return false } else if res.ResponseCode != http.StatusOK { c.Sayln("fetch file error: %s", res.ErrorString) return false } c.Sayln("") c.Sayln("importing file '%s'", ctx.SourceFile) if res, err := rhimport.ImportFile(ctx); err != nil { c.Sayln("") c.Sayln("import file error: %s", err) } else { c.Sayln("") if res.ResponseCode == http.StatusOK { c.Sayln("File got succesfully imported into Cart/Cut %d/%d", res.Cart, res.Cut) } else { c.Sayln("Fileimport has failed (Cart/Cut %d/%d): %s", res.Cart, res.Cut, res.ErrorString) } } return false } func StartControlTelnet(addr_s string, conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions *rhimport.SessionStoreChan) { cmdlist := make(telgo.TelgoCmdList) cmdlist["quit"] = func(c *telgo.TelnetClient, args []string) bool { return telnet_quit(c, args, conf, rddb) } cmdlist["help"] = func(c *telgo.TelnetClient, args []string) bool { return telnet_help(c, args, conf, rddb) } cmdlist["set"] = func(c *telgo.TelnetClient, args []string) bool { return telnet_set(c, args, conf, rddb) } cmdlist["reset"] = func(c *telgo.TelnetClient, args []string) bool { return telnet_reset(c, args, conf, rddb) } cmdlist["show"] = func(c *telgo.TelnetClient, args []string) bool { return telnet_show(c, args, conf, rddb) } cmdlist["run"] = func(c *telgo.TelnetClient, args []string) bool { return telnet_run(c, args, conf, rddb) } rhl.Println("telnet-ctrl: listening on", addr_s) s := telgo.NewTelnetServer(addr_s, "rhimportd> ", cmdlist, nil) if err := s.Run(); err != nil { fmt.Printf("telnet server returned: %s", err) } }