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