summaryrefslogtreecommitdiff
path: root/src/helsinki.at/rhimportd/ctrlTelnet.go
blob: 5d0aebec6a7b8274cb48dc6bc55dbf7116aa6de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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)
	}
}