summaryrefslogtreecommitdiff
path: root/src/rhimportd/ctrlTelnet.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/rhimportd/ctrlTelnet.go')
-rw-r--r--src/rhimportd/ctrlTelnet.go292
1 files changed, 292 insertions, 0 deletions
diff --git a/src/rhimportd/ctrlTelnet.go b/src/rhimportd/ctrlTelnet.go
new file mode 100644
index 0000000..5d0aebe
--- /dev/null
+++ b/src/rhimportd/ctrlTelnet.go
@@ -0,0 +1,292 @@
+//
+// rhimportd
+//
+// The Radio Helsinki Rivendell Import Daemon
+//
+//
+// Copyright (C) 2015-2016 Christian Pointner <equinox@helsinki.at>
+//
+// 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 <http://www.gnu.org/licenses/>.
+//
+
+package main
+
+import (
+ "fmt"
+ "github.com/spreadspace/telgo"
+ "helsinki.at/rhimport"
+ "net/http"
+ "strconv"
+ "strings"
+)
+
+func telnetQuit(c *telgo.Client, args []string, conf *rhimport.Config, rddb *rhimport.RdDbChan) bool {
+ return true
+}
+
+func telnetHelp(c *telgo.Client, 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 [ <cmd> ]")
+ c.Sayln(" prints command overview or detailed info to <cmd>.")
+ return false
+ case "set":
+ c.Sayln("usage: set <param> <value>")
+ c.Sayln(" this sets the import parameter <param> to <value>.")
+ 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: <cmd> [ [ <arg1> ] ... ]")
+ c.Sayln(" available commands:")
+ c.Sayln(" quit close connection (or use Ctrl-D)")
+ c.Sayln(" help [ <cmd> ] print this, or help for specific command")
+ c.Sayln(" set <param> <value> sets parameter <param> 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 telnetSetInt(c *telgo.Client, 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 telnetSetUint(c *telgo.Client, 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 telnetSetBool(c *telgo.Client, 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 telnetSet(c *telgo.Client, args []string, conf *rhimport.Config, rddb *rhimport.RdDbChan) bool {
+ if len(args) != 3 {
+ c.Sayln("wrong number of arguments")
+ return false
+ }
+
+ var ctx *rhimport.Context
+ if c.UserData == nil {
+ c.UserData = rhimport.NewContext(conf, rddb)
+ ctx = c.UserData.(*rhimport.Context)
+ ctx.Trusted = false
+ } else {
+ ctx = c.UserData.(*rhimport.Context)
+ }
+ 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":
+ telnetSetUint(c, &ctx.ShowId, args[2])
+ case "clearshowcarts":
+ telnetSetBool(c, &ctx.ClearShowCarts, args[2])
+ case "groupname":
+ ctx.GroupName = args[2]
+ case "cart":
+ telnetSetUint(c, &ctx.Cart, args[2])
+ case "clearcart":
+ telnetSetBool(c, &ctx.ClearCart, args[2])
+ case "cut":
+ telnetSetUint(c, &ctx.Cut, args[2])
+ case "channels":
+ telnetSetUint(c, &ctx.Channels, args[2])
+ case "normalizationlevel":
+ telnetSetInt(c, &ctx.NormalizationLevel, args[2])
+ case "autotrimlevel":
+ telnetSetInt(c, &ctx.AutotrimLevel, args[2])
+ case "usemetadata":
+ telnetSetBool(c, &ctx.UseMetaData, args[2])
+ default:
+ c.Sayln("unknown parameter, use 'help set' for a list of available parameters")
+ }
+ return false
+}
+
+func telnetReset(c *telgo.Client, 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 telnetShow(c *telgo.Client, 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.Context)
+ 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 telnetProgressCallback(step int, stepName string, progress float64, userdata interface{}) bool {
+ c := userdata.(*telgo.Client)
+ c.Say("%s: %3.2f%%\r", stepName, progress*100)
+ return true
+}
+
+func telnetRun(c *telgo.Client, 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.Context)
+ if err := ctx.SanityCheck(); err != nil {
+ c.Sayln("sanity check for import context returned: %s", err)
+ return false
+ }
+
+ ctx.ProgressCallBack = telnetProgressCallback
+ 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 string, conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions *rhimport.SessionStoreChan) {
+ cmdlist := make(telgo.CmdList)
+ cmdlist["quit"] = func(c *telgo.Client, args []string) bool { return telnetQuit(c, args, conf, rddb) }
+ cmdlist["help"] = func(c *telgo.Client, args []string) bool { return telnetHelp(c, args, conf, rddb) }
+ cmdlist["set"] = func(c *telgo.Client, args []string) bool { return telnetSet(c, args, conf, rddb) }
+ cmdlist["reset"] = func(c *telgo.Client, args []string) bool { return telnetReset(c, args, conf, rddb) }
+ cmdlist["show"] = func(c *telgo.Client, args []string) bool { return telnetShow(c, args, conf, rddb) }
+ cmdlist["run"] = func(c *telgo.Client, args []string) bool { return telnetRun(c, args, conf, rddb) }
+
+ rhl.Println("telnet-ctrl: listening on", addr)
+ s := telgo.NewServer(addr, "rhimportd> ", cmdlist, nil)
+ if err := s.Run(); err != nil {
+ fmt.Printf("telnet server returned: %s", err)
+ }
+}