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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
//
// 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 (
"code.helsinki.at/rhrd-go/rddb"
"code.helsinki.at/rhrd-go/rhimport"
"fmt"
"github.com/spreadspace/telgo"
"net/http"
"strconv"
"strings"
)
func telnetQuit(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DBChan) bool {
return true
}
func telnetHelp(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DBChan) 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(" SourceFilePolicy enum one of: auto, keep, delete, delete-with-dir")
c.Sayln(" FetchConverter string fetch converter to use")
c.Sayln(" (one of: null, ffmpeg, bs1770, ffmpeg-bs1770)")
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 a cart within that")
c.Sayln(" show which must be either empty or ClearCart must be set to true. The latter")
c.Sayln(" tells rhimportd to delete and recreate the cart before importing. If Cart is")
c.Sayln(" set to 0 the next free cart in the show will be used. Show carts will always")
c.Sayln(" be imported into cut 1.")
c.Sayln("")
c.Sayln(" If GroupName is supplied but ShowId is not, Channels, NomalizationLevel,")
c.Sayln(" AutorimLevel, UseMetaData, Cut, Cart and ClearCart will be ignored. The")
c.Sayln(" values from the music pools' dropbox will be used instead. The file will")
c.Sayln(" always be 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")
c.Sayln(" Cart and no Cut is supplied and ClearCart is false the file will either get")
c.Sayln(" imported into the next free cut, in case the cart exists, or the cart will")
c.Sayln(" be created and the file will be imported into cut 1 of this cart.")
c.Sayln(" If ClearCart is true the cart will be deleted, recreated and the file will")
c.Sayln(" be imported into cut 1 of this cart.")
c.Sayln("")
c.Sayln(" In case of an error carts and cuts which might got created will be removed.")
c.Sayln(" Carts which got deleted because of ClearShowCarts or ClearCart are however")
c.Sayln(" gone for good.")
c.Sayln("")
c.Sayln(" The SourceFilePolicy specifies how to deal with temporary files which might")
c.Sayln(" got fetched from remote sources. In 'auto' mode, which is the default, it")
c.Sayln(" depends on the fetch uri. Remote fetcher such as http://, archiv://, etc.")
c.Sayln(" will create a temporary directory and store the fetched file inside that")
c.Sayln(" directory. After the import, successful or not, the file gets deleted. If")
c.Sayln(" a local fetcher like tmp:// and local:// are used the file will be left")
c.Sayln(" untouched.")
c.Sayln(" The 'keep' mode tells rhimportd to leave the file as it is regardless of it")
c.Sayln(" being created by rhimportd or not.")
c.Sayln(" 'delete' tells rhimportd to delete the file in any case, even if the import")
c.Sayln(" has failed or the file was supplied using local://. A directory which might")
c.Sayln(" got created by rhimportd will be kept.")
c.Sayln(" 'delete-with-dir' also deletes the directory where the source file is in.")
c.Sayln(" This might fail if the directory is not empty.")
c.Sayln(" ")
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 telnetSetFilePolicy(c *telgo.Client, param *rhimport.FilePolicy, val string) {
if err := param.FromString(val); err != nil {
c.Sayln("invalid value (%s)", err)
}
}
func telnetSet(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DBChan) bool {
if len(args) != 3 {
c.Sayln("wrong number of arguments")
return false
}
var ctx *rhimport.Context
if c.UserData == nil {
logname := "telnet-" + c.Conn.RemoteAddr().String()
c.UserData = rhimport.NewContext(conf, db, getStdLog(logname+"-std"), getDbgLog(logname+"-dbg"))
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 "sourcefilepolicy":
telnetSetFilePolicy(c, &ctx.SourceFilePolicy, args[2])
case "fetchconverter":
ctx.FetchConverter = 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, db *rddb.DBChan) 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, db *rddb.DBChan) 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(" SourceFilePolicy: %s", ctx.SourceFilePolicy.String())
c.Sayln(" FetchConverter: %q", ctx.FetchConverter)
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, current, total float64, title string, cart, cut uint, userdata interface{}) bool {
c := userdata.(*telgo.Client)
if cart > 0 {
c.Say("%s: %3.2f%% (Cart/Cut: %d/%d)\r", stepName, (current/total)*100, cart, cut)
} else {
c.Say("%s: %3.2f%%\r", stepName, (current/total)*100)
}
return true
}
func telnetRun(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DBChan) 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("normalizing file '%s'", ctx.SourceFile)
if res, err := rhimport.NormalizeFile(ctx); err != nil {
c.Sayln("normalize file error: %s", err)
return false
} else if res.ResponseCode != http.StatusOK {
c.Sayln("normalize 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, Source: '%s'", res.Cart, res.Cut, res.SourceFile)
} else {
c.Sayln("Fileimport has failed (Cart/Cut %d/%d, Source: '%s'): %s", res.Cart, res.Cut, res.SourceFile, res.ErrorString)
}
}
return false
}
func StartControlTelnet(addr string, conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport.SessionStoreChan) {
cmdlist := make(telgo.CmdList)
cmdlist["quit"] = func(c *telgo.Client, args []string) bool { return telnetQuit(c, args, conf, db) }
cmdlist["help"] = func(c *telgo.Client, args []string) bool { return telnetHelp(c, args, conf, db) }
cmdlist["set"] = func(c *telgo.Client, args []string) bool { return telnetSet(c, args, conf, db) }
cmdlist["reset"] = func(c *telgo.Client, args []string) bool { return telnetReset(c, args, conf, db) }
cmdlist["show"] = func(c *telgo.Client, args []string) bool { return telnetShow(c, args, conf, db) }
cmdlist["run"] = func(c *telgo.Client, args []string) bool { return telnetRun(c, args, conf, db) }
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)
}
}
|