]> code.delx.au - gnu-emacs-elpa/blob - packages/debbugs/debbugs.el
Merge commit 'ef509502cdd228c8ce0a562bbf411e5f98beaaf1'
[gnu-emacs-elpa] / packages / debbugs / debbugs.el
1 ;;; debbugs.el --- SOAP library to access debbugs servers
2
3 ;; Copyright (C) 2011-2016 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, hypermedia
7 ;; Package: debbugs
8 ;; Version: 0.9.1
9 ;; Package-Requires: ((async "1.6"))
10
11 ;; This file is not part of GNU Emacs.
12
13 ;; This program is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; This package provides basic functions to access a Debbugs SOAP
29 ;; server (see <http://wiki.debian.org/DebbugsSoapInterface>).
30
31 ;; The function "get_versions" is not implemented (yet). "search_est"
32 ;; is an extension on <http://debbugs.gnu.org>.
33
34 ;;; Code:
35
36 ;(setq soap-debug t message-log-max t)
37 (require 'soap-client)
38 (eval-when-compile (require 'cl))
39
40 (declare-function soap-invoke-async "soap-client")
41 (declare-function async-start "async")
42 (declare-function async-get "async")
43
44 (defgroup debbugs nil
45 "Debbugs library"
46 :group 'hypermedia)
47
48 (defcustom debbugs-servers
49 '(("gnu.org"
50 :wsdl "http://debbugs.gnu.org/cgi/soap.cgi?WSDL"
51 :bugreport-url "http://debbugs.gnu.org/cgi/bugreport.cgi")
52 ("debian.org"
53 :wsdl "http://bugs.debian.org/cgi-bin/soap.cgi?WSDL"
54 :bugreport-url "http://bugs.debian.org/cgi-bin/bugreport.cgi"))
55 "*List of Debbugs server specifiers.
56 Each entry is a list that contains a string identifying the port
57 name and the server parameters in keyword-value form. Allowed
58 keywords are:
59
60 `:wsdl' -- Location of WSDL. The value is a string with URL that
61 should return the WSDL specification of Debbugs/SOAP service.
62
63 `:bugreport-url' -- URL of the server script that returns mboxes
64 with bug logs.
65
66 The list initially contains two predefined and configured Debbugs
67 servers: \"gnu.org\" and \"debian.org\"."
68 :group 'debbugs
69 :link '(custom-manual "(debbugs)Debbugs server specifiers")
70 :type '(choice
71 (const nil)
72 (repeat
73 (cons :tag "Server"
74 (string :tag "Port name")
75 (checklist :tag "Options" :greedy t
76 (group :inline t
77 (const :format "" :value :wsdl)
78 (string :tag "WSDL"))
79 (group :inline t
80 (const :format "" :value :bugreport-url)
81 (string :tag "Bugreport URL")))))))
82
83 (defcustom debbugs-port "gnu.org"
84 "The port instance to be applied from `debbugs-wsdl'.
85 This corresponds to the Debbugs server to be accessed, either
86 \"gnu.org\", or \"debian.org\", or user defined port name."
87 ;; Maybe we should create an own group?
88 :group 'debbugs
89 :type '(choice :tag "Debbugs server" (const "gnu.org") (const "debian.org")
90 (string :tag "user defined port name")))
91
92 ;; It would be nice if we could retrieve it from the debbugs server.
93 ;; Not supported yet.
94 (defconst debbugs-wsdl
95 (soap-load-wsdl
96 (expand-file-name
97 "Debbugs.wsdl"
98 (if load-in-progress
99 (file-name-directory load-file-name)
100 default-directory)))
101 "The WSDL object to be used describing the SOAP interface.")
102
103 ;; Please do not increase this value, otherwise we would run into
104 ;; performance problems on the server. Maybe we need to change this a
105 ;; server specific value.
106 (defconst debbugs-max-hits-per-request 500
107 "The max number of bugs or results per soap invocation.")
108
109 (defvar debbugs-cache-data
110 (make-hash-table :test 'equal :size debbugs-max-hits-per-request)
111 "Hash table of retrieved bugs.")
112
113 (defcustom debbugs-cache-expiry (* 60 60)
114 "How many seconds debbugs query results are cached.
115 `t' or 0 disables caching, `nil' disables expiring."
116 :group 'debbugs
117 :type '(choice (const :tag "Always" t)
118 (const :tag "Never" nil)
119 (integer :tag "Seconds")))
120
121 (defvar debbugs-soap-invoke-async-object nil
122 "The object manipulated by `debbugs-soap-invoke-async'.")
123
124 (defun debbugs-soap-invoke-async (operation-name &rest parameters)
125 "Invoke the SOAP connection asynchronously.
126 If possible, it uses `soap-invoke-async' from soapclient 3.0.
127 Otherwise, `async-start' from the async package is used."
128 (if (fboundp 'soap-invoke-async)
129 ;; This is soap-client 3.0.
130 (apply
131 'soap-invoke-async
132 (lambda (response &rest args)
133 (setq debbugs-soap-invoke-async-object
134 (append debbugs-soap-invoke-async-object (car response))))
135 nil
136 debbugs-wsdl debbugs-port operation-name parameters)
137 ;; Fallback with async.
138 (async-start
139 `(lambda ()
140 (load ,(locate-library "soap-client"))
141 (apply
142 'soap-invoke
143 (soap-load-wsdl
144 ,(expand-file-name
145 "Debbugs.wsdl"
146 (file-name-directory (locate-library "debbugs"))))
147 ,debbugs-port ,operation-name ',parameters)))))
148
149 (defun debbugs-get-bugs (&rest query)
150 "Return a list of bug numbers which match QUERY.
151
152 QUERY is a sequence of keyword-value pairs where the values are
153 strings, i.e. :KEYWORD \"VALUE\" [:KEYWORD \"VALUE\"]*
154
155 The keyword-value pair is a subquery. The keywords are allowed to
156 have multiple occurrence within the query at any place. The
157 subqueries with the same keyword form the logical subquery, which
158 returns the union of bugs of every subquery it contains.
159
160 The result of the QUERY is an intersection of results of all
161 subqueries.
162
163 Valid keywords are:
164
165 :package -- The value is the name of the package a bug belongs
166 to, like \"emacs\", \"coreutils\", \"gnus\", or \"tramp\".
167
168 :src -- This is used to retrieve bugs that belong to source
169 with given name.
170
171 :severity -- This is the severity of the bug. The exact set of
172 allowed values depends on the Debbugs port. Examples are
173 \"normal\", \"minor\", \"wishlist\" etc.
174
175 :tag -- An arbitrary string the bug is annotated with.
176 Usually, this is used to mark the status of the bug, like
177 \"fixed\", \"moreinfo\", \"notabug\", \"patch\",
178 \"unreproducible\" or \"wontfix\". The exact set of tags
179 depends on the Debbugs port.
180
181 :owner -- This is used to identify bugs by the owner's email
182 address. The special email address \"me\" is used as pattern,
183 replaced with `user-mail-address'.
184
185 :submitter -- With this keyword it is possible to filter bugs
186 by the submitter's email address. The special email address
187 \"me\" is used as pattern, replaced with `user-mail-address'.
188
189 :maint -- This is used to find bugs of the packages which are
190 maintained by the person with the given email address. The
191 special email address \"me\" is used as pattern, replaced with
192 `user-mail-address'.
193
194 :correspondent -- This allows to find bug reports where the
195 person with the given email address has participated. The
196 special email address \"me\" is used as pattern, replaced with
197 `user-mail-address'.
198
199 :affects -- With this keyword it is possible to find bugs which
200 affect the package with the given name. The bugs are chosen by
201 the value of field `affects' in bug's status. The returned bugs
202 do not necessary belong to this package.
203
204 :status -- Status of bug. Valid values are \"done\",
205 \"forwarded\" and \"open\".
206
207 :archive -- A keyword to filter for bugs which are already
208 archived, or not. Valid values are \"0\" (not archived),
209 \"1\" (archived) or \"both\". If this keyword is not given in
210 the query, `:archive \"0\"' is assumed by default.
211
212 Example. Get all opened and forwarded release critical bugs for
213 the packages which are maintained by \"me\" and which have a
214 patch:
215
216 \(debbugs-get-bugs :maint \"me\" :tag \"patch\"
217 :severity \"critical\"
218 :status \"open\"
219 :severity \"grave\"
220 :status \"forwarded\"
221 :severity \"serious\")"
222
223 (let (vec kw key val)
224 ;; Check query.
225 (while (and (consp query) (<= 2 (length query)))
226 (setq kw (pop query)
227 val (pop query))
228 (unless (and (keywordp kw) (stringp val))
229 (error "Wrong query: %s %s" kw val))
230 (setq key (substring (symbol-name kw) 1))
231 (case kw
232 ((:package :severity :tag :src :affects)
233 ;; Value shall be one word.
234 (if (string-match "\\`\\S-+\\'" val)
235 (setq vec (vconcat vec (list key val)))
236 (error "Wrong %s: %s" key val)))
237 ((:owner :submitter :maint :correspondent)
238 ;; Value is an email address.
239 (if (string-match "\\`\\S-+\\'" val)
240 (progn
241 (when (string-equal "me" val)
242 (setq val user-mail-address))
243 (when (string-match "<\\(.+\\)>" val)
244 (setq val (match-string 1 val)))
245 (setq vec (vconcat vec (list key val))))
246 (error "Wrong %s: %s" key val)))
247 (:status
248 ;; Possible values: "done", "forwarded" and "open"
249 (if (string-match "\\`\\(done\\|forwarded\\|open\\)\\'" val)
250 (setq vec (vconcat vec (list key val)))
251 (error "Wrong %s: %s" key val)))
252 (:archive
253 ;; Value is `0' or `1' or `both'.
254 (if (string-match "\\`\\(0\\|1\\|both\\)\\'" val)
255 (setq vec (vconcat vec (list key val)))
256 (error "Wrong %s: %s" key val)))
257 (t (error "Unknown key: %s" kw))))
258
259 (unless (null query)
260 (error "Unknown key: %s" (car query)))
261 (sort (car (soap-invoke debbugs-wsdl debbugs-port "get_bugs" vec)) '<)))
262
263 (defun debbugs-newest-bugs (amount)
264 "Return the list of bug numbers, according to AMOUNT (a number) latest bugs."
265 (sort (car (soap-invoke debbugs-wsdl debbugs-port "newest_bugs" amount)) '<))
266
267 (defun debbugs-convert-soap-value-to-string (string-value)
268 "If STRING-VALUE is unibyte, decode its contents as a UTF-8 string.
269 If STRING-VALUE is a multibyte string, then `soap-client'
270 received an xsd:string for this value, and will have decoded it
271 already.
272
273 If STRING-VALUE is a unibyte string, then `soap-client' received
274 an xsd:base64Binary, and ran `base64-decode-string' on it to
275 produce a unibyte string of bytes.
276
277 For some reason, the Debbugs server code base64-encodes strings
278 that contain UTF-8 characters, and returns them as
279 xsd:base64Binary, instead of just returning them as xsd:string.
280 Therefore, when STRING-VALUE is a unibyte string, we assume its
281 bytes represent a UTF-8 string and decode them accordingly."
282 (if (stringp string-value)
283 (if (not (multibyte-string-p string-value))
284 (decode-coding-string string-value 'utf-8)
285 string-value)
286 (error "Invalid string value")))
287
288 (defun debbugs-get-status (&rest bug-numbers)
289 "Return a list of status entries for the bugs identified by BUG-NUMBERS.
290
291 Every returned entry is an association list with the following attributes:
292
293 `bug_num': The bug number.
294
295 `package': A list of package names the bug belongs to.
296
297 `severity': The severity of the bug report. This can be
298 \"critical\", \"grave\", \"serious\", \"important\",
299 \"normal\", \"minor\" or \"wishlist\".
300
301 `tags': The status of the bug report, a list of strings. This
302 can be \"fixed\", \"notabug\", \"wontfix\", \"unreproducible\",
303 \"moreinfo\" or \"patch\".
304
305 `pending': The string \"pending\", \"forwarded\" or \"done\".
306
307 `subject': Subject/Title of the bugreport.
308
309 `originator': Submitter of the bugreport.
310
311 `mergedwith': A list of bug numbers this bug was merged with.
312 If it is a single bug, then this attribute contains just a
313 number.
314
315 `source': Source package name of the bug report.
316
317 `date': Date of bug creation.
318
319 `log_modified', `last_modified': Date of last update.
320
321 `found_date', `fixed_date': Date of bug report / bug fix
322 \(empty for now).
323
324 `done': The email address of the worker who has closed the bug (if done).
325
326 `archived': `t' if the bug is archived, `nil' otherwise.
327
328 `unarchived': The date the bug has been unarchived, if ever.
329
330 `found_versions', `fixed_versions': List of version strings.
331
332 `forwarded': A URL or an email address.
333
334 `blocks': A list of bug numbers this bug blocks.
335
336 `blockedby': A list of bug numbers this bug is blocked by.
337
338 `msgid': The message id of the initial bug report.
339
340 `owner': Who is responsible for fixing.
341
342 `location': Always the string \"db-h\" or \"archive\".
343
344 `affects': A list of package names.
345
346 `summary': Arbitrary text.
347
348 Example:
349
350 \(debbugs-get-status 10)
351
352 => ;; Attributes with empty values are not shown
353 \(\(\(bug_num . 10)
354 \(source . \"unknown\")
355 \(date . 1203606305.0)
356 \(msgid . \"<87zltuz7eh.fsf@freemail.hu>\")
357 \(severity . \"wishlist\")
358 \(owner . \"Magnus Henoch <mange@freemail.hu>\")
359 \(log_modified . 1261079402.0)
360 \(location . \"db-h\")
361 \(subject . \"url-gw should support HTTP CONNECT proxies\")
362 \(originator . \"Magnus Henoch <mange@freemail.hu>\")
363 \(last_modified . 1271200046.0)
364 \(pending . \"pending\")
365 \(package \"emacs\")))"
366 (let (cached-bugs)
367 ;; Check for cached bugs.
368 (setq bug-numbers (delete-dups bug-numbers)
369 bug-numbers
370 (delete
371 nil
372 (mapcar
373 (lambda (bug)
374 (let ((status (gethash bug debbugs-cache-data)))
375 (if (and
376 status
377 (or
378 (null debbugs-cache-expiry)
379 (and
380 (natnump debbugs-cache-expiry)
381 (> (cdr (assoc 'cache_time status))
382 (- (float-time)) debbugs-cache-expiry))))
383 (progn
384 (setq cached-bugs (append cached-bugs (list status)))
385 nil)
386 bug)))
387 bug-numbers)))
388
389 ;; Retrieve the data.
390 (setq debbugs-soap-invoke-async-object nil)
391 (when bug-numbers
392 ;; Retrieve bugs asynchronously.
393 (let ((bug-ids bug-numbers)
394 results)
395 (while bug-ids
396 (setq results
397 (append
398 results
399 (list
400 (debbugs-soap-invoke-async
401 "get_status"
402 (apply
403 'vector
404 (butlast
405 bug-ids (- (length bug-ids)
406 debbugs-max-hits-per-request))))))
407
408 bug-ids
409 (last bug-ids (- (length bug-ids)
410 debbugs-max-hits-per-request))))
411
412 (dolist (res results)
413 (if (bufferp res)
414 ;; This is soap-client 3.0.
415 (while (buffer-live-p res)
416 (accept-process-output (get-buffer-process res) 0.1))
417 ;; Fallback with async.
418 (dolist (status (async-get res))
419 (setq debbugs-soap-invoke-async-object
420 (append debbugs-soap-invoke-async-object status)))))))
421
422 (append
423 cached-bugs
424 ;; Massage results.
425 (mapcar
426 (lambda (x)
427 (let (y)
428 ;; "archived" is the number 1 or 0.
429 (setq y (assoc 'archived (cdr (assoc 'value x))))
430 (setcdr y (= (cdr y) 1))
431 ;; "found_versions" and "fixed_versions" are lists,
432 ;; containing strings or numbers.
433 (dolist (attribute '(found_versions fixed_versions))
434 (setq y (assoc attribute (cdr (assoc 'value x))))
435 (setcdr y (mapcar
436 (lambda (z) (if (numberp z) (number-to-string z) z))
437 (cdr y))))
438 ;; "mergedwith", "blocks" and "blockedby are strings,
439 ;; containing blank separated bug numbers.
440 (dolist (attribute '(mergedwith blocks blockedby))
441 (setq y (assoc attribute (cdr (assoc 'value x))))
442 (when (stringp (cdr y))
443 (setcdr y (mapcar
444 'string-to-number (split-string (cdr y) " " t)))))
445 ;; "subject", "originator", "owner" and "summary" may be an
446 ;; xsd:base64Binary value containing a UTF-8-encoded string.
447 (dolist (attribute '(subject originator owner summary))
448 (setq y (assoc attribute (cdr (assoc 'value x))))
449 (when (stringp (cdr y))
450 (setcdr y (debbugs-convert-soap-value-to-string (cdr y)))))
451 ;; "package" is a string, containing comma separated
452 ;; package names. "keywords" and "tags" are strings,
453 ;; containing blank separated package names.
454 (dolist (attribute '(package keywords tags))
455 (setq y (assoc attribute (cdr (assoc 'value x))))
456 (when (stringp (cdr y))
457 (setcdr y (split-string (cdr y) ",\\| " t))))
458 ;; Cache the result, and return.
459 (if (and debbugs-cache-expiry (natnump debbugs-cache-expiry))
460 (puthash
461 (cdr (assoc 'key x))
462 ;; Put also a time stamp.
463 (cons (cons 'cache_time (floor (float-time)))
464 (cdr (assoc 'value x)))
465 debbugs-cache-data)
466 ;; Don't cache.
467 (cdr (assoc 'value x)))))
468 debbugs-soap-invoke-async-object))))
469
470 (defun debbugs-get-usertag (&rest query)
471 "Return a list of bug numbers which match QUERY.
472
473 QUERY is a sequence of keyword-value pairs where the values are
474 strings, i.e. :KEYWORD \"VALUE\" [:KEYWORD \"VALUE\"]*
475
476 Valid keywords are:
477
478 :user -- The value is the name of the package a bug belongs to,
479 like \"emacs\", \"coreutils\", \"gnus\", or \"tramp\". It can
480 also be an email address of a user who has applied a user tag.
481 The special email address \"me\" is used as pattern, replaced
482 with `user-mail-address'. There must be at least one such
483 entry; it is recommended to have exactly one.
484
485 :tag -- A string applied as user tag. Often, it is a
486 subproduct identification, like \"cedet\" or \"tramp\" for the
487 package \"emacs\".
488
489 If there is no :tag entry, no bug numbers will be returned but a list of
490 existing user tags for :user.
491
492 Example:
493
494 \(debbugs-get-usertag :user \"emacs\")
495
496 => (\"www\" \"solaris\" \"ls-lisp\" \"cygwin\")
497
498 \(debbugs-get-usertag :user \"emacs\" :tag \"www\" :tag \"cygwin\")
499
500 => (807 1223 5637)"
501
502 (let (user tags kw key val object result)
503 ;; Check query.
504 (while (and (consp query) (<= 2 (length query)))
505 (setq kw (pop query)
506 val (pop query))
507 (unless (and (keywordp kw) (stringp val))
508 (error "Wrong query: %s %s" kw val))
509 (setq key (substring (symbol-name kw) 1))
510 (case kw
511 ((:user)
512 ;; Value shall be one word. Extract email address, if existing.
513 (if (string-match "\\`\\S-+\\'" val)
514 (progn
515 (when (string-equal "me" val)
516 (setq val user-mail-address))
517 (when (string-match "<\\(.+\\)>" val)
518 (setq val (match-string 1 val)))
519 (pushnew val user :test #'equal))
520 (error "Wrong %s: %s" key val)))
521 ((:tag)
522 ;; Value shall be one word.
523 (if (string-match "\\`\\S-+\\'" val)
524 (pushnew val tags :test #'equal)
525 (error "Wrong %s: %s" key val)))
526 (t (error "Unknown key: %s" kw))))
527
528 (unless (null query)
529 (error "Unknown key: %s" (car query)))
530 (unless (= (length user) 1)
531 (error "There must be exactly one :user entry"))
532
533 (setq
534 object
535 (car (soap-invoke debbugs-wsdl debbugs-port "get_usertag" (car user))))
536
537 (if (null tags)
538 ;; Return the list of existing tags.
539 (mapcar (lambda (x) (symbol-name (car x))) object)
540
541 ;; Return bug numbers.
542 (dolist (elt object result)
543 (when (member (symbol-name (car elt)) tags)
544 (setq result (append (cdr elt) result)))))))
545
546 (defun debbugs-get-bug-log (bug-number)
547 "Return a list of messages related to BUG-NUMBER.
548
549 Every message is an association list with the following attributes:
550
551 `msg_num': The number of the message inside the bug log. The
552 numbers are ascending, newer messages have a higher number.
553
554 `header': The message header lines, as arrived at the bug tracker.
555
556 `body': The message body.
557
558 `attachments' A list of possible attachments, or `nil'. Not
559 implemented yet server side."
560 (car (soap-invoke debbugs-wsdl debbugs-port "get_bug_log" bug-number)))
561
562 (defun debbugs-search-est (&rest query)
563 "Return the result of a full text search according to QUERY.
564
565 QUERY is a sequence of lists of keyword-value pairs where the
566 values are strings or numbers, i.e. :KEYWORD \"VALUE\" [:KEYWORD
567 VALUE]*
568
569 Every sublist of the QUERY forms a hyperestraier condition. A
570 detailed description of hyperestraier conditions can be found at
571 URL `http://fallabs.com/hyperestraier/uguide-en.html#searchcond'.
572
573 The following conditions are possible:
574
575 \[:phrase SEARCH-PHRASE :skip NUMBER :max NUMBER\]
576
577 The string SEARCH-PHRASE forms the search on the database. It
578 contains words to be searched for, combined by operators like
579 AND, ANDNOT and OR. If there is no operator between the words,
580 AND is used by default. The phrase keyword and value can also
581 be omitted, this is useful in combination with other conditions.
582
583 :skip and :max are optional. They specify, how many hits are
584 skipped, and how many maximal hits are returned. This can be
585 used for paged results. Per default, :skip is 0 and all
586 possible hits are returned.
587
588 There must be exactly one such condition.
589
590 \[ATTRIBUTE VALUE+ :operation OPERATION :order ORDER\]
591
592 ATTRIBUTE is one of the following keywords:
593
594 :status -- Status of bug. Valid values are \"done\",
595 \"forwarded\" and \"open\".
596
597 :subject, :@title -- The subject of a message or the title of
598 the bug, a string.
599
600 :date, :@cdate -- The submission or modification dates of a
601 message, a number.
602
603 :submitter, :@author -- The email address of the submitter of a
604 bug or the author of a message belonging to this bug, a string.
605 The special email address \"me\" is used as pattern, replaced
606 with `user-mail-address'.
607
608 :package -- The value is the name of the package a bug belongs
609 to, like \"emacs\", \"coreutils\", \"gnus\", or \"tramp\".
610
611 :tags -- An arbitrary string the bug is annotated with.
612
613 :severity -- This is the severity of the bug. The exact set of
614 allowed values depends on the Debbugs port. Examples are
615 \"normal\", \"minor\", \"wishlist\" etc.
616
617 :operator defines the comparison operator to be applied to
618 ATTRIBUTE. For string attributes this could be \"STREQ\" \(is
619 equal to the string), \"STRNE\" \(is not equal to the string),
620 \"STRINC\" \(includes the string), \"STRBW\" \(begins with the
621 string), \"STREW\" \(ends with the string), \"STRAND\"
622 \(includes all tokens in the string), \"STROR\" \(includes at
623 least one token in the string), \"STROREQ\" \(is equal to at
624 least one token in the string) or \"STRRX\" \(matches regular
625 expressions of the string). For operators with tokens, several
626 values for ATTRIBUTE shall be used.
627
628 Numbers can be compared by the operators \"NUMEQ\" \(is equal
629 to the number), \"NUMNE\" \(is not equal to the number),
630 \"NUMGT\" \(is greater than the number), \"NUMGE\" \(is greater
631 than or equal to the number), \"NUMLT\" \(is less than the
632 number), \"NUMLE\" \(is less than or equal to the number) or
633 \"NUMBT\" \(is between the two numbers). In the last case,
634 there must be two values for ATTRIBUTE.
635
636 If an operator is leaded by \"!\", the meaning is inverted. If
637 a string operator is leaded by \"I\", the case of the value is
638 ignored.
639
640 The optional :order can be specified only in one condition. It
641 means, that ATTRIBUTE is used for sorting the results. The
642 following order operators exist: \"STRA\" \(ascending by
643 string), \"STRD\" \(descending by string), \"NUMA\" \(ascending
644 by number) or \"NUMD\" \(descending by number).
645
646 A special case is an :order, where there is no corresponding
647 attribute value and no operator. In this case, ATTRIBUTE is
648 not used for the search.
649
650 The result of the QUERY is a list of association lists with the
651 same attributes as in the conditions. Additional attributes are
652
653 `id': The bug number.
654
655 `msg_num': The number of the message inside the bug log.
656
657 `snippet': The surrounding text found by the search. For the
658 syntax of the snippet, consult the hyperestraier user guide.
659
660 Examples:
661
662 \(debbugs-search-est
663 '\(:phrase \"armstrong AND debbugs\" :skip 10 :max 2)
664 '\(:severity \"normal\" :operator \"STRINC\")
665 '\(:date :order \"NUMA\"))
666
667 => \(\(\(msg_num . 21)
668 \(date . 1229208302)
669 \(@author . \"Glenn Morris <rgm@gnu.org>\")
670 \(@title . \"Re: bug#1567: Mailing an archived bug\")
671 \(id . 1567)
672 \(severity . \"normal\")
673 \(@cdate . \"Wed, 17 Dec 2008 14:34:50 -0500\")
674 \(snippet . \"...\")
675 \(subject . \"Mailing an archived bug\")
676 \(package . \"debbugs.gnu.org\"))
677 ...)
678
679 ;; Show all messages from me between 2011-08-01 and 2011-08-31.
680 \(debbugs-search-est
681 '\(:max 20)
682 '\(:@author \"me\" :operator \"ISTRINC\")
683 `\(:date
684 ,\(floor \(float-time \(encode-time 0 0 0 1 8 2011)))
685 ,\(floor \(float-time \(encode-time 0 0 0 31 8 2011)))
686 :operator \"NUMBT\"))"
687
688 (let ((phrase (assoc :phrase query))
689 args result)
690 (if (and phrase (not (member :skip phrase)) (not (member :skip phrase)))
691 ;; We loop, until we have all results.
692 (let ((skip 0)
693 (query (delete phrase query))
694 result1)
695 (while skip
696 (setq result1
697 (apply
698 'debbugs-search-est
699 (append
700 (list
701 (append
702 phrase `(:skip ,skip)
703 `(:max ,debbugs-max-hits-per-request)))
704 query))
705 skip (and (= (length result1) debbugs-max-hits-per-request)
706 (+ skip debbugs-max-hits-per-request))
707 result (append result result1)))
708 result)
709
710 ;; Compile search arguments.
711 (dolist (elt query)
712 (let (vec kw key val
713 phrase-cond attr-cond)
714
715 ;; Phrase is mandatory, even if empty.
716 (when (and (or (member :skip elt) (member :max elt))
717 (not (member :phrase elt)))
718 (setq vec (vector "phrase" "")))
719
720 ;; Parse condition.
721 (while (consp elt)
722 (setq kw (pop elt))
723 (unless (keywordp kw)
724 (error "Wrong keyword: %s" kw))
725 (setq key (substring (symbol-name kw) 1))
726 (cl-case kw
727 ;; Phrase condition.
728 (:phrase
729 ;; It shouldn't happen in an attribute condition.
730 (if attr-cond
731 (error "Wrong keyword: %s" kw))
732 (setq phrase-cond t val (pop elt))
733 ;; Value is a string.
734 (if (stringp val)
735 (setq vec (vconcat vec (list key val)))
736 (error "Wrong %s: %s" key val)))
737
738 ((:skip :max)
739 ;; It shouldn't happen in an attribute condition.
740 (if attr-cond
741 (error "Wrong keyword: %s" kw))
742 (setq phrase-cond t val (pop elt))
743 ;; Value is a number.
744 (if (numberp val)
745 (setq vec (vconcat vec (list key (number-to-string val))))
746 (error "Wrong %s: %s" key val)))
747
748 ;; Attribute condition.
749 ((:submitter :@author)
750 ;; It shouldn't happen in a phrase condition.
751 (if phrase-cond
752 (error "Wrong keyword: %s" kw))
753 (if (not (stringp (car elt)))
754 (setq vec (vconcat vec (list key "")))
755 ;; Value is an email address.
756 (while (and (stringp (car elt))
757 (string-match "\\`\\S-+\\'" (car elt)))
758 (when (string-equal "me" (car elt))
759 (setcar elt user-mail-address))
760 (when (string-match "<\\(.+\\)>" (car elt))
761 (setcar elt (match-string 1 (car elt))))
762 (let ((x (pop elt)))
763 (unless (member x val)
764 (setq val (append val (list x))))))
765 (setq vec
766 (vconcat vec (list key (mapconcat 'identity val " "))))))
767
768 (:status
769 ;; It shouldn't happen in a phrase condition.
770 (if phrase-cond
771 (error "Wrong keyword: %s" kw))
772 (setq attr-cond t)
773 (if (not (stringp (car elt)))
774 (setq vec (vconcat vec (list key "")))
775 ;; Possible values: "done", "forwarded" and "open"
776 (while (and (stringp (car elt))
777 (string-match
778 "\\`\\(done\\|forwarded\\|open\\)\\'" (car elt)))
779 (let ((x (pop elt)))
780 (unless (member x val)
781 (setq val (append val (list x))))))
782 (setq vec
783 (vconcat vec (list key (mapconcat 'identity val " "))))))
784
785 ((:subject :package :tags :severity :@title)
786 ;; It shouldn't happen in a phrase condition.
787 (if phrase-cond
788 (error "Wrong keyword: %s" kw))
789 (setq attr-cond t)
790 (if (not (stringp (car elt)))
791 (setq vec (vconcat vec (list key "")))
792 ;; Just a string.
793 (while (stringp (car elt))
794 (let ((x (pop elt)))
795 (unless (member x val)
796 (setq val (append val (list x))))))
797 (setq vec
798 (vconcat vec (list key (mapconcat 'identity val " "))))))
799
800 ((:date :@cdate)
801 ;; It shouldn't happen in a phrase condition.
802 (if phrase-cond
803 (error "Wrong keyword: %s" kw))
804 (setq attr-cond t)
805 (if (not (numberp (car elt)))
806 (setq vec (vconcat vec (list key "")))
807 ;; Just a number.
808 (while (numberp (car elt))
809 (let ((x (pop elt)))
810 (unless (member x val)
811 (setq val (append val (list x))))))
812 (setq vec
813 (vconcat
814 vec (list key (mapconcat 'number-to-string val " "))))))
815
816 ((:operator :order)
817 ;; It shouldn't happen in a phrase condition.
818 (if phrase-cond
819 (error "Wrong keyword: %s" kw))
820 (setq attr-cond t val (pop elt))
821 ;; Value is a number.
822 (if (stringp val)
823 (setq vec (vconcat vec (list key val)))
824 (error "Wrong %s: %s" key val)))
825
826 (t (error "Unknown key: %s" kw))))
827
828 (setq args (vconcat args (list vec)))))
829
830 (setq result
831 (car (soap-invoke debbugs-wsdl debbugs-port "search_est" args)))
832 ;; The result contains lists (key value). We transform it into
833 ;; cons cells (key . value).
834 (dolist (elt1 result result)
835 (dolist (elt2 elt1)
836 (setcdr elt2 (cadr elt2)))))))
837
838 (defun debbugs-get-attribute (bug-or-message attribute)
839 "Return the value of key ATTRIBUTE.
840
841 BUG-OR-MESSAGE must be list element returned by either
842 `debbugs-get-status' or `debbugs-get-bug-log'.
843
844 Example: Return the originator of last submitted bug.
845
846 \(debbugs-get-attribute
847 \(car \(apply 'debbugs-get-status \(debbugs-newest-bugs 1))) 'originator)"
848 (cdr (assoc attribute bug-or-message)))
849
850 (defun debbugs-get-message-numbers (messages)
851 "Return the message numbers of MESSAGES.
852 MESSAGES must be the result of a `debbugs-get-bug-log' call."
853 (mapcar (lambda (x) (debbugs-get-attribute x 'msg_num)) messages))
854
855 (defun debbugs-get-message (messages message-number)
856 "Return the message MESSAGE-NUMBER of MESSAGES.
857 MESSAGES must be the result of a `debbugs-get-bug-log' call.
858
859 The returned message is a list of strings. The first element are
860 the header lines of the message, the second element is the body
861 of the message. Further elements of the list, if any, are
862 attachments of the message.
863
864 If there is no message with MESSAGE-NUMBER, the function returns `nil'.
865
866 Example: Return the first message of last submitted bug.
867
868 \(let \(\(messages \(apply 'debbugs-get-bug-log \(debbugs-newest-bugs 1))))
869 \(debbugs-get-message messages
870 \(car \(debbugs-get-message-numbers messages))))"
871 (while (and messages
872 (/= (debbugs-get-attribute (car messages) 'msg_num)
873 message-number))
874 (setq messages (cdr messages)))
875 (when messages
876 (append (list (debbugs-get-attribute (car messages) 'header)
877 (debbugs-get-attribute (car messages) 'body))
878 (debbugs-get-attribute (car messages) 'attachments))))
879
880 (defun debbugs-get-mbox (bug-number mbox-type &optional filename)
881 "Download mbox with messages of bug BUG-NUMBER from Debbugs server.
882 BUG-NUMBER is a number of bug. It must be of integer type.
883
884 MBOX-TYPE specifies a type of mbox and can be one of the
885 following symbols:
886
887 `mboxfolder': Download mbox folder.
888
889 `mboxmaint': Download maintainer's mbox.
890
891 `mboxstat', `mboxstatus': Download status mbox. The use of
892 either symbol depends on actual Debbugs server configuration.
893 For gnu.org, use the former; for debian.org - the latter.
894
895 FILENAME, if non-`nil', is the name of file to store mbox. If
896 FILENAME is `nil', the downloaded mbox is inserted into the
897 current buffer."
898 (let (url (mt "") bn)
899 (unless (setq url (plist-get
900 (cdr (assoc debbugs-port debbugs-servers))
901 :bugreport-url))
902 (error "URL of bugreport script for port %s is not specified"
903 debbugs-port))
904 (setq bn (format "bug=%s;" (number-to-string bug-number)))
905 (unless (eq mbox-type 'mboxfolder)
906 (if (memq mbox-type '(mboxmaint mboxstat mboxstatus))
907 (setq mt (concat (symbol-name mbox-type) "=yes;"))
908 (error "Unknown mbox type: %s" mbox-type)))
909 (setq url (concat url (format "?%s%smbox=yes" bn mt)))
910 (if filename
911 (url-copy-file url filename t)
912 (url-insert-file-contents url))))
913
914 (provide 'debbugs)
915
916 ;;; TODO:
917
918 ;; * SOAP interface extensions (wishlist).
919 ;; - Server-side sorting.
920 ;; - Regexp and/or wildcards search.
921 ;; - Returning message attachments.
922
923 ;;; debbugs.el ends here