-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
mentor-rpc.el
306 lines (259 loc) · 9.7 KB
/
mentor-rpc.el
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
;;; mentor-rpc.el --- Mentor XML-RPC handling -*- lexical-binding: t -*-
;; Copyright (C) 2016-2023 Stefan Kangas
;; Author: Stefan Kangas <[email protected]>
;; This file is NOT part of GNU Emacs.
;; Mentor 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, or (at your option)
;; any later version.
;;
;; Mentor 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 Mentor. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This library contains functions to speak with rTorrent over XML-RPC.
;;; Code:
(require 'mentor-data)
(defvar mentor-rpc--rtorrent-url nil
"The current rtorrent XML-RPC api URL.")
(defconst mentor-rpc-method-exclusions-regexp "d\\.get_\\(mode\\|custom.*\\|bitfield\\)"
"Do not try methods that makes rtorrent crash.")
(defvar mentor-rpc--rtorrent-methods-cache nil)
(defvar url-http-response-status)
;; Silence compiler warnings
(defvar mentor-current-view)
(declare-function mentor-view-torrent-list-clear "mentor")
(defun mentor-rpc-command (&rest args)
"Run command as an XML-RPC call to rtorrent.
ARGS is a list of strings to run."
(let* ((url-http-response-status 200)
(response (apply 'xml-rpc-method-call mentor-rpc--rtorrent-url args)))
(if (equal response '((nil . "URL/HTTP Error: 200")))
;; Add warning about bug#23606.
;; Remove when Emacs 25 hits Debian stable.
(if (and (string-match "localhost" mentor-rpc--rtorrent-url)
(< emacs-major-version 25))
(error "Unable to connect to %s [try using 127.0.0.1 instead -- see bug#23606]" mentor-rpc--rtorrent-url)
(error "Unable to connect to %s" mentor-rpc--rtorrent-url))
response)))
(defun mentor-rpc-list-methods (&optional regexp)
"Return a list of all available commands.
This uses the RPC method `system.listMethods'.
If REGEXP is specified it only returns the matching functions."
(when (not mentor-rpc--rtorrent-methods-cache)
(let ((methods (mentor-rpc-command "system.listMethods")))
(setq mentor-rpc--rtorrent-methods-cache
(delq nil
(mapcar (lambda (m)
(when (not (string-match mentor-rpc-method-exclusions-regexp m))
m))
methods)))))
(if regexp
(delq nil (mapcar (lambda (m)
(when (string-match regexp m)
m))
mentor-rpc--rtorrent-methods-cache))
mentor-rpc--rtorrent-methods-cache))
;; General RPC commands, prefix c
(defun mentor-rpc-c-load (file &optional stopped)
(let ((cmd (if stopped "load.verbose" "load.start_verbose")))
(mentor-rpc-command cmd "" file)))
(defun mentor-rpc-c-load-raw (file &optional stopped)
(let ((cmd (if stopped "load.raw" "load.raw_start"))
(data (list
:base64
(with-temp-buffer
(set-buffer-multibyte nil)
(setq buffer-file-coding-system 'binary)
(insert-file-contents-literally file)
(buffer-substring-no-properties (point-min) (point-max))))))
(mentor-rpc-command cmd "" data)))
(defun mentor-rpc-c-execute2 (&rest args)
"Run the \"execute2\" command via RPC.
This is an arbitrary shell command."
(apply 'mentor-rpc-command "execute2" "" args))
;; Download RPC commands, prefix d
(defun mentor-rpc-d-directory-set
(hash new) (mentor-rpc-command "d.directory.set" hash new))
(defun mentor-rpc-d-close (hash)
(mentor-rpc-command "d.close" hash))
(defun mentor-rpc-d-erase (hash)
(mentor-rpc-command "d.erase" hash))
(defun mentor-rpc-d-start (hash)
(mentor-rpc-command "d.start" hash))
(defun mentor-rpc-d-stop (hash)
(mentor-rpc-command "d.stop" hash))
(defun mentor-rpc-d.multicall (d-methods t-methods &optional is-init)
"Call \"d.multicall2\" with METHODS.
If optional argument IS-INIT is non-nil, this is initializing."
(let* ((d-methods= (mapcar (lambda (m) (concat m "=")) d-methods))
(t-methods= (list (mentor-rpc-join-t-methods t-methods)))
(all-methods (append d-methods= t-methods=))
(list-of-values (apply 'mentor-rpc-command "d.multicall2"
"" mentor-current-view all-methods)))
(mentor-view-torrent-list-clear)
(let ((d-properties (mentor-rpc-methods-to-properties d-methods))
(t-properties (mentor-rpc-methods-to-properties t-methods)))
(dolist (values list-of-values)
(mentor-data-download-update-from d-properties t-properties values is-init)))))
;;;; Download data
(defun mentor-rpc-methods-to-properties (methods)
(mapcar
(lambda (method)
(intern
(replace-regexp-in-string
"^\\([tp]\\)\\." "\\1_"
(replace-regexp-in-string "^[df]\\.\\(get_\\)?\\|=$" "" method))))
methods))
;; https://rtorrent-docs.readthedocs.io/en/latest/cmd-ref.html#d-commands
(defconst mentor-rpc-d-methods
'("d.hash"
"d.local_id"
;; "d.local_id_html"
"d.base_filename"
"d.base_path"
;; "d.bitfield"
"d.bytes_done"
"d.chunk_size"
;; "d.chunks_hashed"
;; "d.complete"
;; "d.completed_bytes"
;; "d.completed_chunks"
;; "d.connection_current"
;; "d.connection_leech"
;; "d.connection_seed"
;; "d.creation_date"
;; "d.custom"
;; "d.custom1"
;; "d.custom2"
;; "d.custom3"
;; "d.custom4"
;; "d.custom5"
;; "d.custom_throw"
"d.directory"
"d.directory_base"
"d.down.rate"
;; "d.down.total"
;; "d.free_diskspace"
"d.hashing"
"d.hashing_failed"
;; "d.ignore_commands"
;; "d.left_bytes"
;; "d.loaded_file"
;; "d.max_file_size"
;; "d.max_size_pex"
"d.message"
;; "d.mode"
"d.name"
;; "d.peer_exchange"
;; "d.peers_accounted"
;; "d.peers_complete"
;; "d.peers_connected"
;; "d.peers_max"
;; "d.peers_min"
;; "d.peers_not_connected"
"d.priority"
;; "d.priority_str"
;; "d.ratio"
"d.size_bytes"
;; "d.size_chunks"
;; "d.size_files"
;; "d.size_pex"
;; "d.skip.rate"
;; "d.skip.total"
"d.state"
;; "d.state_changed"
;; "d.state_counter"
;; "d.throttle_name"
"d.tied_to_file"
;; "d.tracker_focus"
;; "d.tracker_numwant"
;; "d.tracker_size"
"d.up.rate"
"d.up.total"
;; "d.uploads_max"
"d.is_active"
"d.is_hash_checked"
"d.is_hash_checking"
"d.is_multi_file"
"d.is_open"
"d.is_not_partially_done"
"d.is_pex_active"
"d.is_partially_done"
"d.is_private"
"d.views"))
(defconst mentor-rpc-volatile-d-methods
'("d.local_id" ;; must not be removed
"d.base_path"
"d.bytes_done"
"d.directory"
"d.down.rate"
"d.hashing"
"d.hashing_failed"
"d.message"
"d.priority"
"d.chunk_size"
"d.up.rate"
"d.up.total"
"d.state"
"d.views"
"d.is_active"
"d.is_hash_checked"
"d.is_hash_checking"
"d.is_open"
"d.is_pex_active"))
;;;; File data
(defun mentor-rpc-f.multicall (hash &rest methods)
(apply 'mentor-rpc-command "f.multicall" hash "" methods))
;;;; Tracker data
;; https://rtorrent-docs.readthedocs.io/en/latest/cmd-ref.html#t-commands
(defconst mentor-rpc-t-methods
'("t.url"
"t.is_enabled"))
(defconst mentor-rpc-t-multicall-sep "#"
"Separator used to join calls (and their results) in \"t.multicall\".
This is also used to split the result string, so it should be
something that is unlikely to appear in any of the \"t.*\"
fields.")
(defun mentor-rpc-join-t-methods (methods)
"Construct a quoted \"t.multicall\" call string for a \"d.multicall call\".
Each call in a \"d.multicall\" returns a string, so the entire
result of the \"t.mutlicall\" will be returned as a single string
that we need to split. Thus, insert a separator between every
\"t.*\" call so we can can split on it."
(when methods
;; d.hash must be included: it is the primary key rtorrent uses to identify
;; which torrent the other properties should be looked up for. The trailing
;; separator is necessary because if a torrent has multiple trackers, the
;; t.multicall command will be executed for each of them and the result
;; will be concatenated with no additional separator.
(concat "cat=\"$t.multicall=d.hash=,"
(mapconcat (lambda (m) (concat m "=,cat=" mentor-rpc-t-multicall-sep))
methods ",")
"\"")))
;; sys.multicall -- unused?
;; TODO: Is sys.multicall a possible optimization for later? From wiki:
;; Process an array of calls, and return an array of
;; results. Calls should be structs of the form {'methodName':
;; string, 'params': array}. Each result will either be a
;; single-item array containing the result value, or a struct of the
;; form {'faultCode': int, 'faultString': string}. This is useful
;; when you need to make lots of small calls without lots of round
;; trips. See rTorrent-system_multicall for syntax.
(defun mentor-rpc--multicall-string (method &rest args)
(list (cons "methodName" method) (cons "params" args)))
(defun mentor-rpc-sys-multicall (&rest calls)
"Perform a `system.multicall' with CALLS.
CALLS is a list of lists where the first element is the method
name and all consecutive elements is its arguments."
(mentor-rpc-command
"system.multicall"
(mapcar (lambda (c)
(apply 'mentor--multicall-string
(car c) (cdr c))) calls)))
(provide 'mentor-rpc)
;;; mentor-rpc.el ends here