-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmethods.go
550 lines (462 loc) · 13.7 KB
/
methods.go
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// go-libdeluge v0.5.6 - a native deluge RPC client library
// Copyright (C) 2015~2023 gdm85 - https://github.com/gdm85/go-libdeluge/
// This program 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 2
// of the License, or (at your option) any later version.
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package delugeclient
import (
"fmt"
"github.com/gdm85/go-rencode"
)
// GetFreeSpace returns the available free space; path is optional.
func (c *Client) GetFreeSpace(path string) (int64, error) {
var args rencode.List
args.Add(path)
resp, err := c.rpc("core.get_free_space", args, rencode.Dictionary{})
if err != nil {
return 0, err
}
if resp.IsError() {
return 0, resp.RPCError
}
var freeSpace int64
err = resp.returnValue.Scan(&freeSpace)
if err != nil {
return 0, err
}
return freeSpace, nil
}
// GetLibtorrentVersion returns the libtorrent version.
func (c *Client) GetLibtorrentVersion() (string, error) {
resp, err := c.rpc("core.get_libtorrent_version", rencode.List{}, rencode.Dictionary{})
if err != nil {
return "", err
}
if resp.IsError() {
return "", resp.RPCError
}
var ltVersion string
err = resp.returnValue.Scan(<Version)
if err != nil {
return "", err
}
return ltVersion, nil
}
// AddTorrentMagnet adds a torrent via magnet URI and returns the torrent hash.
func (c *Client) AddTorrentMagnet(magnetURI string, options *Options) (string, error) {
var args rencode.List
args.Add(magnetURI, options.toDictionary(c.v2daemon))
resp, err := c.rpc("core.add_torrent_magnet", args, rencode.Dictionary{})
if err != nil {
return "", err
}
if resp.IsError() {
return "", resp.RPCError
}
// returned hash will be nil if torrent was already added
vals := resp.returnValue.Values()
if len(vals) == 0 {
return "", ErrInvalidReturnValue
}
torrentHash := vals[0]
if torrentHash == nil {
return "", nil
}
return string(torrentHash.([]uint8)), nil
}
// AddTorrentURL adds a torrent via a URL and returns the torrent hash.
func (c *Client) AddTorrentURL(url string, options *Options) (string, error) {
var args rencode.List
args.Add(url, options.toDictionary(c.v2daemon))
resp, err := c.rpc("core.add_torrent_url", args, rencode.Dictionary{})
if err != nil {
return "", err
}
if resp.IsError() {
return "", resp.RPCError
}
// returned hash will be nil if torrent was already added
vals := resp.returnValue.Values()
if len(vals) == 0 {
return "", ErrInvalidReturnValue
}
torrentHash := vals[0]
if torrentHash == nil {
return "", nil
}
return string(torrentHash.([]uint8)), nil
}
// AddTorrentFile adds a torrent via a base64 encoded file and returns the torrent hash.
func (c *Client) AddTorrentFile(fileName, fileContentBase64 string, options *Options) (string, error) {
var args rencode.List
args.Add(fileName, fileContentBase64, options.toDictionary(c.v2daemon))
resp, err := c.rpc("core.add_torrent_file", args, rencode.Dictionary{})
if err != nil {
return "", err
}
if resp.IsError() {
return "", resp.RPCError
}
// returned hash will be nil if torrent was already added
vals := resp.returnValue.Values()
if len(vals) == 0 {
return "", ErrInvalidReturnValue
}
torrentHash := vals[0]
if torrentHash == nil {
return "", nil
}
return string(torrentHash.([]uint8)), nil
}
// TorrentError is a tuple of a torrent id and an error message, returned by
// methods that manipulate many torrents at once.
type TorrentError struct {
// ID is the hash of the torrent that experienced an error
ID string
Message string
}
func (t TorrentError) Error() string {
return fmt.Sprintf("<%s>: '%s'", t.ID, t.Message)
}
// RemoveTorrents tries to remove multiple torrents at once.
// If `rmFiles` is set it also tries to delete all downloaded data for the
// specified torrents.
// If errors were encountered the returned list will be a list of
// TorrentErrors.
// On success an empty list of errors is returned.
//
// The user should not rely on files being removed or torrents being
// removed from the session, just because no errors have been returned,
// as returned errors will primarily indicate that some of the supplied
// torrent hashes were invalid.
func (c *Client) RemoveTorrents(ids []string, rmFiles bool) ([]TorrentError, error) {
var args rencode.List
args.Add(sliceToRencodeList(ids), rmFiles)
resp, err := c.rpc("core.remove_torrents", args, rencode.Dictionary{})
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, resp.RPCError
}
vals := resp.returnValue.Values()
if len(vals) != 1 {
return nil, ErrInvalidReturnValue
}
failedList := vals[0].(rencode.List)
var torrentErrors []TorrentError
// Iterate through the list of errors that have occurred, and
// convert each of them into a more typesafe format.
for _, e := range failedList.Values() {
failedEntry, ok := e.(rencode.List)
if !ok {
// Unexpected response from the API
return torrentErrors, ErrInvalidReturnValue
}
failedTuple := failedEntry.Values()
if len(failedTuple) != 2 {
// return here, as we don't know how to parse the returned
// error structure
return torrentErrors, ErrInvalidReturnValue
}
torrentError := TorrentError{
ID: string(failedTuple[0].([]byte)),
Message: string(failedTuple[1].([]byte)),
}
torrentErrors = append(torrentErrors, torrentError)
}
return torrentErrors, nil
}
// RemoveTorrent removes a single torrent, returning true if successful.
// If `rmFiles` is set it also tries to delete all downloaded data for the
// specified torrent.
func (c *Client) RemoveTorrent(id string, rmFiles bool) (bool, error) {
var args rencode.List
args.Add(id, rmFiles)
resp, err := c.rpc("core.remove_torrent", args, rencode.Dictionary{})
if err != nil {
return false, err
}
if resp.IsError() {
return false, resp.RPCError
}
vals := resp.returnValue.Values()
if len(vals) != 1 {
return false, ErrInvalidReturnValue
}
success := vals[0]
return success.(bool), nil
}
// PauseTorrents pauses a group of torrents with the given IDs.
func (c *Client) PauseTorrents(ids ...string) error {
var args rencode.List
args.Add(sliceToRencodeList(ids))
method := "core.pause_torrents"
if !c.v2daemon {
method = "core.pause_torrent"
}
resp, err := c.rpc(method, args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
return err
}
// ResumeTorrents resumes a group of torrents with the given IDs.
func (c *Client) ResumeTorrents(ids ...string) error {
var args rencode.List
args.Add(sliceToRencodeList(ids))
method := "core.resume_torrents"
if !c.v2daemon {
method = "core.resume_torrent"
}
resp, err := c.rpc(method, args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
return err
}
// MoveStorage will move the storage location of the group of torrents with the given IDs.
func (c *Client) MoveStorage(torrentIDs []string, dest string) error {
var args rencode.List
args.Add(sliceToRencodeList(torrentIDs), dest)
resp, err := c.rpc("core.move_storage", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
return err
}
// SessionState returns the current session state.
func (c *Client) SessionState() ([]string, error) {
return c.rpcWithStringsResult("core.get_session_state")
}
// SetTorrentOptions updates options for the torrent with the given hash.
func (c *Client) SetTorrentOptions(id string, options *Options) error {
var args rencode.List
args.Add(id, options.toDictionary(c.v2daemon))
resp, err := c.rpc("core.set_torrent_options", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
return nil
}
// SetTorrentTracker sets the primary tracker for the torrent with the
// given hash to be `trackerURL`.
func (c *Client) SetTorrentTracker(id, trackerURL string) error {
var tracker rencode.Dictionary
tracker.Add("url", trackerURL)
tracker.Add("tier", 0)
var trackers rencode.List
trackers.Add(tracker)
var args rencode.List
args.Add(id, trackers)
resp, err := c.rpc("core.set_torrent_trackers", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
return nil
}
// KnownAccounts returns all known accounts, including password and
// permission levels.
func (c *ClientV2) KnownAccounts() ([]Account, error) {
resp, err := c.rpc("core.get_known_accounts", rencode.List{}, rencode.Dictionary{})
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, resp.RPCError
}
var users rencode.List
err = resp.returnValue.Scan(&users)
if err != nil {
return nil, err
}
// users is now a list of dictionaries, each containing
// three []byte attributes: username, password and auth level
var accounts []Account
for _, u := range users.Values() {
dict, ok := u.(rencode.Dictionary)
if !ok {
return nil, ErrInvalidDictionaryResponse
}
var a Account
err := a.fromDictionary(dict)
if err != nil {
return nil, err
}
accounts = append(accounts, a)
}
return accounts, nil
}
// CreateAccount creates a new Deluge user with the supplied username,
// password and permission level. The authenticated user must have an
// authLevel of ADMIN to succeed.
func (c *ClientV2) CreateAccount(account Account) (bool, error) {
resp, err := c.rpc("core.create_account", account.toList(), rencode.Dictionary{})
if err != nil {
return false, err
}
if resp.IsError() {
return false, resp.RPCError
}
vals := resp.returnValue.Values()
if len(vals) == 0 {
return false, ErrInvalidReturnValue
}
success := vals[0]
return success.(bool), nil
}
// UpdateAccount sets a new password and permission level for a account.
// The authenticated user must have an authLevel of ADMIN to succeed.
func (c *ClientV2) UpdateAccount(account Account) (bool, error) {
resp, err := c.rpc("core.update_account", account.toList(), rencode.Dictionary{})
if err != nil {
return false, err
}
if resp.IsError() {
return false, resp.RPCError
}
vals := resp.returnValue.Values()
if len(vals) == 0 {
return false, ErrInvalidReturnValue
}
success := vals[0]
return success.(bool), nil
}
// RemoveAccount will delete an existing username.
// The authenticated user must have an authLevel of ADMIN to succeed.
func (c *ClientV2) RemoveAccount(username string) (bool, error) {
var args rencode.List
args.Add(username)
resp, err := c.rpc("core.remove_account", args, rencode.Dictionary{})
if err != nil {
return false, err
}
if resp.IsError() {
return false, resp.RPCError
}
vals := resp.returnValue.Values()
if len(vals) == 0 {
return false, ErrInvalidReturnValue
}
success := vals[0]
return success.(bool), nil
}
// ForceReannounce will reannounce torrent status to associated tracker(s).
func (c *Client) ForceReannounce(ids []string) error {
var args rencode.List
args.Add(sliceToRencodeList(ids))
resp, err := c.rpc("core.force_reannounce", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
return nil
}
// GetEnabledPlugins returns a list of enabled plugins.
func (c *Client) GetEnabledPlugins() ([]string, error) {
return c.rpcWithStringsResult("core.get_enabled_plugins")
}
// GetAvailablePlugins returns a list of available plugins.
func (c *Client) GetAvailablePlugins() ([]string, error) {
return c.rpcWithStringsResult("core.get_available_plugins")
}
// EnablePlugin enables the plugin with the given name.
func (c *Client) EnablePlugin(name string) error {
var args rencode.List
args.Add(name)
resp, err := c.rpc("core.enable_plugin", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
// deluge v2+ returns a boolean, but since it is not available in v1 it is ignored here
return nil
}
// DisablePlugin disables the plugin with the given name.
func (c *Client) DisablePlugin(name string) error {
var args rencode.List
args.Add(name)
resp, err := c.rpc("core.disable_plugin", args, rencode.Dictionary{})
if err != nil {
return err
}
if resp.IsError() {
return resp.RPCError
}
// deluge v2+ returns a boolean, but since it is not available in v1 it is ignored here
return nil
}
func sliceToRencodeList(s []string) rencode.List {
var list rencode.List
for _, v := range s {
list.Add(v)
}
return list
}
// TestListenPort checks if the active port is open.
func (c *Client) TestListenPort() (bool, error) {
resp, err := c.rpc("core.test_listen_port", rencode.List{}, rencode.Dictionary{})
if err != nil {
return false, err
}
if resp.IsError() {
return false, resp.RPCError
}
vals := resp.returnValue.Values()
if len(vals) == 0 {
return false, ErrInvalidReturnValue
}
first := vals[0]
v, ok := first.(bool)
if ok {
return v, nil
}
if c.settings.Logger != nil {
// sometimes a nil or rencode.List is returned, it is a bug in deluge
c.settings.Logger.Printf("TestListenPort returned %v", first)
}
return false, ErrInvalidReturnValue
}
// GetListenPort returns the listen port of the deluge daemon.
func (c *Client) GetListenPort() (uint16, error) {
resp, err := c.rpc("core.get_listen_port", rencode.List{}, rencode.Dictionary{})
if err != nil {
return 0, err
}
if resp.IsError() {
return 0, resp.RPCError
}
var port int32
err = resp.returnValue.Scan(&port)
if err != nil {
return 0, err
}
return uint16(port), nil
}