]> code.delx.au - gnu-emacs-elpa/blob - packages/debbugs/debbugs.el
Merge commit '11fbd70347a8cc62817c6d4ebf2291471ebdd607' from avy
[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
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-get-status (&rest bug-numbers)
268 "Return a list of status entries for the bugs identified by BUG-NUMBERS.
269
270 Every returned entry is an association list with the following attributes:
271
272 `bug_num': The bug number.
273
274 `package': A list of package names the bug belongs to.
275
276 `severity': The severity of the bug report. This can be
277 \"critical\", \"grave\", \"serious\", \"important\",
278 \"normal\", \"minor\" or \"wishlist\".
279
280 `tags': The status of the bug report, a list of strings. This
281 can be \"fixed\", \"notabug\", \"wontfix\", \"unreproducible\",
282 \"moreinfo\" or \"patch\".
283
284 `pending': The string \"pending\", \"forwarded\" or \"done\".
285
286 `subject': Subject/Title of the bugreport.
287
288 `originator': Submitter of the bugreport.
289
290 `mergedwith': A list of bug numbers this bug was merged with.
291 If it is a single bug, then this attribute contains just a
292 number.
293
294 `source': Source package name of the bug report.
295
296 `date': Date of bug creation.
297
298 `log_modified', `last_modified': Date of last update.
299
300 `found_date', `fixed_date': Date of bug report / bug fix
301 \(empty for now).
302
303 `done': The email address of the worker who has closed the bug (if done).
304
305 `archived': `t' if the bug is archived, `nil' otherwise.
306
307 `unarchived': The date the bug has been unarchived, if ever.
308
309 `found_versions', `fixed_versions': List of version strings.
310
311 `forwarded': A URL or an email address.
312
313 `blocks': A list of bug numbers this bug blocks.
314
315 `blockedby': A list of bug numbers this bug is blocked by.
316
317 `msgid': The message id of the initial bug report.
318
319 `owner': Who is responsible for fixing.
320
321 `location': Always the string \"db-h\" or \"archive\".
322
323 `affects': A list of package names.
324
325 `summary': Arbitrary text.
326
327 Example:
328
329 \(debbugs-get-status 10)
330
331 => ;; Attributes with empty values are not shown
332 \(\(\(bug_num . 10)
333 \(source . \"unknown\")
334 \(date . 1203606305.0)
335 \(msgid . \"<87zltuz7eh.fsf@freemail.hu>\")
336 \(severity . \"wishlist\")
337 \(owner . \"Magnus Henoch <mange@freemail.hu>\")
338 \(log_modified . 1261079402.0)
339 \(location . \"db-h\")
340 \(subject . \"url-gw should support HTTP CONNECT proxies\")
341 \(originator . \"Magnus Henoch <mange@freemail.hu>\")
342 \(last_modified . 1271200046.0)
343 \(pending . \"pending\")
344 \(package \"emacs\")))"
345 (let (cached-bugs)
346 ;; Check for cached bugs.
347 (setq bug-numbers (delete-dups bug-numbers)
348 bug-numbers
349 (delete
350 nil
351 (mapcar
352 (lambda (bug)
353 (let ((status (gethash bug debbugs-cache-data)))
354 (if (and
355 status
356 (or
357 (null debbugs-cache-expiry)
358 (and
359 (natnump debbugs-cache-expiry)
360 (> (cdr (assoc 'cache_time status))
361 (- (float-time)) debbugs-cache-expiry))))
362 (progn
363 (setq cached-bugs (append cached-bugs (list status)))
364 nil)
365 bug)))
366 bug-numbers)))
367
368 ;; Retrieve the data.
369 (setq debbugs-soap-invoke-async-object nil)
370 (when bug-numbers
371 ;; Retrieve bugs asynchronously.
372 (let ((bug-ids bug-numbers)
373 results)
374 (while bug-ids
375 (setq results
376 (append
377 results
378 (list
379 (debbugs-soap-invoke-async
380 "get_status"
381 (apply
382 'vector
383 (butlast
384 bug-ids (- (length bug-ids)
385 debbugs-max-hits-per-request))))))
386
387 bug-ids
388 (last bug-ids (- (length bug-ids)
389 debbugs-max-hits-per-request))))
390
391 (dolist (res results)
392 (if (bufferp res)
393 ;; This is soap-client 3.0.
394 (while (buffer-live-p res)
395 (accept-process-output (get-buffer-process res) 0.1))
396 ;; Fallback with async.
397 (dolist (status (async-get res))
398 (setq debbugs-soap-invoke-async-object
399 (append debbugs-soap-invoke-async-object status)))))))
400
401 (append
402 cached-bugs
403 ;; Massage results.
404 (mapcar
405 (lambda (x)
406 (let (y)
407 ;; "archived" is the number 1 or 0.
408 (setq y (assoc 'archived (cdr (assoc 'value x))))
409 (setcdr y (= (cdr y) 1))
410 ;; "found_versions" and "fixed_versions" are lists,
411 ;; containing strings or numbers.
412 (dolist (attribute '(found_versions fixed_versions))
413 (setq y (assoc attribute (cdr (assoc 'value x))))
414 (setcdr y (mapcar
415 (lambda (z) (if (numberp z) (number-to-string z) z))
416 (cdr y))))
417 ;; "mergedwith", "blocks" and "blockedby are strings,
418 ;; containing blank separated bug numbers.
419 (dolist (attribute '(mergedwith blocks blockedby))
420 (setq y (assoc attribute (cdr (assoc 'value x))))
421 (when (stringp (cdr y))
422 (setcdr y (mapcar
423 'string-to-number (split-string (cdr y) " " t)))))
424 ;; "package" is a string, containing comma separated
425 ;; package names. "keywords" and "tags" are strings,
426 ;; containing blank separated package names.
427 (dolist (attribute '(package keywords tags))
428 (setq y (assoc attribute (cdr (assoc 'value x))))
429 (when (stringp (cdr y))
430 (setcdr y (split-string (cdr y) ",\\| " t))))
431 ;; Cache the result, and return.
432 (if (and debbugs-cache-expiry (natnump debbugs-cache-expiry))
433 (puthash
434 (cdr (assoc 'key x))
435 ;; Put also a time stamp.
436 (cons (cons 'cache_time (floor (float-time)))
437 (cdr (assoc 'value x)))
438 debbugs-cache-data)
439 ;; Don't cache.
440 (cdr (assoc 'value x)))))
441 debbugs-soap-invoke-async-object))))
442
443 (defun debbugs-get-usertag (&rest query)
444 "Return a list of bug numbers which match QUERY.
445
446 QUERY is a sequence of keyword-value pairs where the values are
447 strings, i.e. :KEYWORD \"VALUE\" [:KEYWORD \"VALUE\"]*
448
449 Valid keywords are:
450
451 :user -- The value is the name of the package a bug belongs to,
452 like \"emacs\", \"coreutils\", \"gnus\", or \"tramp\". It can
453 also be an email address of a user who has applied a user tag.
454 The special email address \"me\" is used as pattern, replaced
455 with `user-mail-address'. There must be at least one such
456 entry; it is recommended to have exactly one.
457
458 :tag -- A string applied as user tag. Often, it is a
459 subproduct identification, like \"cedet\" or \"tramp\" for the
460 package \"emacs\".
461
462 If there is no :tag entry, no bug numbers will be returned but a list of
463 existing user tags for :user.
464
465 Example:
466
467 \(debbugs-get-usertag :user \"emacs\")
468
469 => (\"www\" \"solaris\" \"ls-lisp\" \"cygwin\")
470
471 \(debbugs-get-usertag :user \"emacs\" :tag \"www\" :tag \"cygwin\")
472
473 => (807 1223 5637)"
474
475 (let (user tags kw key val object result)
476 ;; Check query.
477 (while (and (consp query) (<= 2 (length query)))
478 (setq kw (pop query)
479 val (pop query))
480 (unless (and (keywordp kw) (stringp val))
481 (error "Wrong query: %s %s" kw val))
482 (setq key (substring (symbol-name kw) 1))
483 (case kw
484 ((:user)
485 ;; Value shall be one word. Extract email address, if existing.
486 (if (string-match "\\`\\S-+\\'" val)
487 (progn
488 (when (string-equal "me" val)
489 (setq val user-mail-address))
490 (when (string-match "<\\(.+\\)>" val)
491 (setq val (match-string 1 val)))
492 (pushnew val user :test #'equal))
493 (error "Wrong %s: %s" key val)))
494 ((:tag)
495 ;; Value shall be one word.
496 (if (string-match "\\`\\S-+\\'" val)
497 (pushnew val tags :test #'equal)
498 (error "Wrong %s: %s" key val)))
499 (t (error "Unknown key: %s" kw))))
500
501 (unless (null query)
502 (error "Unknown key: %s" (car query)))
503 (unless (= (length user) 1)
504 (error "There must be exactly one :user entry"))
505
506 (setq
507 object
508 (car (soap-invoke debbugs-wsdl debbugs-port "get_usertag" (car user))))
509
510 (if (null tags)
511 ;; Return the list of existing tags.
512 (mapcar (lambda (x) (symbol-name (car x))) object)
513
514 ;; Return bug numbers.
515 (dolist (elt object result)
516 (when (member (symbol-name (car elt)) tags)
517 (setq result (append (cdr elt) result)))))))
518
519 (defun debbugs-get-bug-log (bug-number)
520 "Return a list of messages related to BUG-NUMBER.
521
522 Every message is an association list with the following attributes:
523
524 `msg_num': The number of the message inside the bug log. The
525 numbers are ascending, newer messages have a higher number.
526
527 `header': The message header lines, as arrived at the bug tracker.
528
529 `body': The message body.
530
531 `attachments' A list of possible attachments, or `nil'. Not
532 implemented yet server side."
533 (car (soap-invoke debbugs-wsdl debbugs-port "get_bug_log" bug-number)))
534
535 (defun debbugs-search-est (&rest query)
536 "Return the result of a full text search according to QUERY.
537
538 QUERY is a sequence of lists of keyword-value pairs where the
539 values are strings or numbers, i.e. :KEYWORD \"VALUE\" [:KEYWORD
540 VALUE]*
541
542 Every sublist of the QUERY forms a hyperestraier condition. A
543 detailed description of hyperestraier conditions can be found at
544 URL `http://fallabs.com/hyperestraier/uguide-en.html#searchcond'.
545
546 The following conditions are possible:
547
548 \[:phrase SEARCH-PHRASE :skip NUMBER :max NUMBER\]
549
550 The string SEARCH-PHRASE forms the search on the database. It
551 contains words to be searched for, combined by operators like
552 AND, ANDNOT and OR. If there is no operator between the words,
553 AND is used by default. The phrase keyword and value can also
554 be omitted, this is useful in combination with other conditions.
555
556 :skip and :max are optional. They specify, how many hits are
557 skipped, and how many maximal hits are returned. This can be
558 used for paged results. Per default, :skip is 0 and all
559 possible hits are returned.
560
561 There must be exactly one such condition.
562
563 \[ATTRIBUTE VALUE+ :operation OPERATION :order ORDER\]
564
565 ATTRIBUTE is one of the following keywords:
566
567 :status -- Status of bug. Valid values are \"done\",
568 \"forwarded\" and \"open\".
569
570 :subject, :@title -- The subject of a message or the title of
571 the bug, a string.
572
573 :date, :@cdate -- The submission or modification dates of a
574 message, a number.
575
576 :submitter, :@author -- The email address of the submitter of a
577 bug or the author of a message belonging to this bug, a string.
578 The special email address \"me\" is used as pattern, replaced
579 with `user-mail-address'.
580
581 :package -- The value is the name of the package a bug belongs
582 to, like \"emacs\", \"coreutils\", \"gnus\", or \"tramp\".
583
584 :tags -- An arbitrary string the bug is annotated with.
585
586 :severity -- This is the severity of the bug. The exact set of
587 allowed values depends on the Debbugs port. Examples are
588 \"normal\", \"minor\", \"wishlist\" etc.
589
590 :operator defines the comparison operator to be applied to
591 ATTRIBUTE. For string attributes this could be \"STREQ\" \(is
592 equal to the string), \"STRNE\" \(is not equal to the string),
593 \"STRINC\" \(includes the string), \"STRBW\" \(begins with the
594 string), \"STREW\" \(ends with the string), \"STRAND\"
595 \(includes all tokens in the string), \"STROR\" \(includes at
596 least one token in the string), \"STROREQ\" \(is equal to at
597 least one token in the string) or \"STRRX\" \(matches regular
598 expressions of the string). For operators with tokens, several
599 values for ATTRIBUTE shall be used.
600
601 Numbers can be compared by the operators \"NUMEQ\" \(is equal
602 to the number), \"NUMNE\" \(is not equal to the number),
603 \"NUMGT\" \(is greater than the number), \"NUMGE\" \(is greater
604 than or equal to the number), \"NUMLT\" \(is less than the
605 number), \"NUMLE\" \(is less than or equal to the number) or
606 \"NUMBT\" \(is between the two numbers). In the last case,
607 there must be two values for ATTRIBUTE.
608
609 If an operator is leaded by \"!\", the meaning is inverted. If
610 a string operator is leaded by \"I\", the case of the value is
611 ignored.
612
613 The optional :order can be specified only in one condition. It
614 means, that ATTRIBUTE is used for sorting the results. The
615 following order operators exist: \"STRA\" \(ascending by
616 string), \"STRD\" \(descending by string), \"NUMA\" \(ascending
617 by number) or \"NUMD\" \(descending by number).
618
619 A special case is an :order, where there is no corresponding
620 attribute value and no operator. In this case, ATTRIBUTE is
621 not used for the search.
622
623 The result of the QUERY is a list of association lists with the
624 same attributes as in the conditions. Additional attributes are
625
626 `id': The bug number.
627
628 `msg_num': The number of the message inside the bug log.
629
630 `snippet': The surrounding text found by the search. For the
631 syntax of the snippet, consult the hyperestraier user guide.
632
633 Examples:
634
635 \(debbugs-search-est
636 '\(:phrase \"armstrong AND debbugs\" :skip 10 :max 2)
637 '\(:severity \"normal\" :operator \"STRINC\")
638 '\(:date :order \"NUMA\"))
639
640 => \(\(\(msg_num . 21)
641 \(date . 1229208302)
642 \(@author . \"Glenn Morris <rgm@gnu.org>\")
643 \(@title . \"Re: bug#1567: Mailing an archived bug\")
644 \(id . 1567)
645 \(severity . \"normal\")
646 \(@cdate . \"Wed, 17 Dec 2008 14:34:50 -0500\")
647 \(snippet . \"...\")
648 \(subject . \"Mailing an archived bug\")
649 \(package . \"debbugs.gnu.org\"))
650 ...)
651
652 ;; Show all messages from me between 2011-08-01 and 2011-08-31.
653 \(debbugs-search-est
654 '\(:max 20)
655 '\(:@author \"me\" :operator \"ISTRINC\")
656 `\(:date
657 ,\(floor \(float-time \(encode-time 0 0 0 1 8 2011)))
658 ,\(floor \(float-time \(encode-time 0 0 0 31 8 2011)))
659 :operator \"NUMBT\"))"
660
661 (let ((phrase (assoc :phrase query))
662 args result)
663 (if (and phrase (not (member :skip phrase)) (not (member :skip phrase)))
664 ;; We loop, until we have all results.
665 (let ((skip 0)
666 (query (delete phrase query))
667 result1)
668 (while skip
669 (setq result1
670 (apply
671 'debbugs-search-est
672 (append
673 (list
674 (append
675 phrase `(:skip ,skip)
676 `(:max ,debbugs-max-hits-per-request)))
677 query))
678 skip (and (= (length result1) debbugs-max-hits-per-request)
679 (+ skip debbugs-max-hits-per-request))
680 result (append result result1)))
681 result)
682
683 ;; Compile search arguments.
684 (dolist (elt query)
685 (let (vec kw key val
686 phrase-cond attr-cond)
687
688 ;; Phrase is mandatory, even if empty.
689 (when (and (or (member :skip elt) (member :max elt))
690 (not (member :phrase elt)))
691 (setq vec (vector "phrase" "")))
692
693 ;; Parse condition.
694 (while (consp elt)
695 (setq kw (pop elt))
696 (unless (keywordp kw)
697 (error "Wrong keyword: %s" kw))
698 (setq key (substring (symbol-name kw) 1))
699 (cl-case kw
700 ;; Phrase condition.
701 (:phrase
702 ;; It shouldn't happen in an attribute condition.
703 (if attr-cond
704 (error "Wrong keyword: %s" kw))
705 (setq phrase-cond t val (pop elt))
706 ;; Value is a string.
707 (if (stringp val)
708 (setq vec (vconcat vec (list key val)))
709 (error "Wrong %s: %s" key val)))
710
711 ((:skip :max)
712 ;; It shouldn't happen in an attribute condition.
713 (if attr-cond
714 (error "Wrong keyword: %s" kw))
715 (setq phrase-cond t val (pop elt))
716 ;; Value is a number.
717 (if (numberp val)
718 (setq vec (vconcat vec (list key (number-to-string val))))
719 (error "Wrong %s: %s" key val)))
720
721 ;; Attribute condition.
722 ((:submitter :@author)
723 ;; It shouldn't happen in a phrase condition.
724 (if phrase-cond
725 (error "Wrong keyword: %s" kw))
726 (if (not (stringp (car elt)))
727 (setq vec (vconcat vec (list key "")))
728 ;; Value is an email address.
729 (while (and (stringp (car elt))
730 (string-match "\\`\\S-+\\'" (car elt)))
731 (when (string-equal "me" (car elt))
732 (setcar elt user-mail-address))
733 (when (string-match "<\\(.+\\)>" (car elt))
734 (setcar elt (match-string 1 (car elt))))
735 (let ((x (pop elt)))
736 (unless (member x val)
737 (setq val (append val (list x))))))
738 (setq vec
739 (vconcat vec (list key (mapconcat 'identity val " "))))))
740
741 (:status
742 ;; It shouldn't happen in a phrase condition.
743 (if phrase-cond
744 (error "Wrong keyword: %s" kw))
745 (setq attr-cond t)
746 (if (not (stringp (car elt)))
747 (setq vec (vconcat vec (list key "")))
748 ;; Possible values: "done", "forwarded" and "open"
749 (while (and (stringp (car elt))
750 (string-match
751 "\\`\\(done\\|forwarded\\|open\\)\\'" (car elt)))
752 (let ((x (pop elt)))
753 (unless (member x val)
754 (setq val (append val (list x))))))
755 (setq vec
756 (vconcat vec (list key (mapconcat 'identity val " "))))))
757
758 ((:subject :package :tags :severity :@title)
759 ;; It shouldn't happen in a phrase condition.
760 (if phrase-cond
761 (error "Wrong keyword: %s" kw))
762 (setq attr-cond t)
763 (if (not (stringp (car elt)))
764 (setq vec (vconcat vec (list key "")))
765 ;; Just a string.
766 (while (stringp (car elt))
767 (let ((x (pop elt)))
768 (unless (member x val)
769 (setq val (append val (list x))))))
770 (setq vec
771 (vconcat vec (list key (mapconcat 'identity val " "))))))
772
773 ((:date :@cdate)
774 ;; It shouldn't happen in a phrase condition.
775 (if phrase-cond
776 (error "Wrong keyword: %s" kw))
777 (setq attr-cond t)
778 (if (not (numberp (car elt)))
779 (setq vec (vconcat vec (list key "")))
780 ;; Just a number.
781 (while (numberp (car elt))
782 (let ((x (pop elt)))
783 (unless (member x val)
784 (setq val (append val (list x))))))
785 (setq vec
786 (vconcat
787 vec (list key (mapconcat 'number-to-string val " "))))))
788
789 ((:operator :order)
790 ;; It shouldn't happen in a phrase condition.
791 (if phrase-cond
792 (error "Wrong keyword: %s" kw))
793 (setq attr-cond t val (pop elt))
794 ;; Value is a number.
795 (if (stringp val)
796 (setq vec (vconcat vec (list key val)))
797 (error "Wrong %s: %s" key val)))
798
799 (t (error "Unknown key: %s" kw))))
800
801 (setq args (vconcat args (list vec)))))
802
803 (setq result
804 (car (soap-invoke debbugs-wsdl debbugs-port "search_est" args)))
805 ;; The result contains lists (key value). We transform it into
806 ;; cons cells (key . value).
807 (dolist (elt1 result result)
808 (dolist (elt2 elt1)
809 (setcdr elt2 (cadr elt2)))))))
810
811 (defun debbugs-get-attribute (bug-or-message attribute)
812 "Return the value of key ATTRIBUTE.
813
814 BUG-OR-MESSAGE must be list element returned by either
815 `debbugs-get-status' or `debbugs-get-bug-log'.
816
817 Example: Return the originator of last submitted bug.
818
819 \(debbugs-get-attribute
820 \(car \(apply 'debbugs-get-status \(debbugs-newest-bugs 1))) 'originator)"
821 (cdr (assoc attribute bug-or-message)))
822
823 (defun debbugs-get-message-numbers (messages)
824 "Return the message numbers of MESSAGES.
825 MESSAGES must be the result of a `debbugs-get-bug-log' call."
826 (mapcar (lambda (x) (debbugs-get-attribute x 'msg_num)) messages))
827
828 (defun debbugs-get-message (messages message-number)
829 "Return the message MESSAGE-NUMBER of MESSAGES.
830 MESSAGES must be the result of a `debbugs-get-bug-log' call.
831
832 The returned message is a list of strings. The first element are
833 the header lines of the message, the second element is the body
834 of the message. Further elements of the list, if any, are
835 attachments of the message.
836
837 If there is no message with MESSAGE-NUMBER, the function returns `nil'.
838
839 Example: Return the first message of last submitted bug.
840
841 \(let \(\(messages \(apply 'debbugs-get-bug-log \(debbugs-newest-bugs 1))))
842 \(debbugs-get-message messages
843 \(car \(debbugs-get-message-numbers messages))))"
844 (while (and messages
845 (/= (debbugs-get-attribute (car messages) 'msg_num)
846 message-number))
847 (setq messages (cdr messages)))
848 (when messages
849 (append (list (debbugs-get-attribute (car messages) 'header)
850 (debbugs-get-attribute (car messages) 'body))
851 (debbugs-get-attribute (car messages) 'attachments))))
852
853 (defun debbugs-get-mbox (bug-number mbox-type &optional filename)
854 "Download mbox with messages of bug BUG-NUMBER from Debbugs server.
855 BUG-NUMBER is a number of bug. It must be of integer type.
856
857 MBOX-TYPE specifies a type of mbox and can be one of the
858 following symbols:
859
860 `mboxfolder': Download mbox folder.
861
862 `mboxmaint': Download maintainer's mbox.
863
864 `mboxstat', `mboxstatus': Download status mbox. The use of
865 either symbol depends on actual Debbugs server configuration.
866 For gnu.org, use the former; for debian.org - the latter.
867
868 FILENAME, if non-`nil', is the name of file to store mbox. If
869 FILENAME is `nil', the downloaded mbox is inserted into the
870 current buffer."
871 (let (url (mt "") bn)
872 (unless (setq url (plist-get
873 (cdr (assoc debbugs-port debbugs-servers))
874 :bugreport-url))
875 (error "URL of bugreport script for port %s is not specified"
876 debbugs-port))
877 (setq bn (format "bug=%s;" (number-to-string bug-number)))
878 (unless (eq mbox-type 'mboxfolder)
879 (if (memq mbox-type '(mboxmaint mboxstat mboxstatus))
880 (setq mt (concat (symbol-name mbox-type) "=yes;"))
881 (error "Unknown mbox type: %s" mbox-type)))
882 (setq url (concat url (format "?%s%smbox=yes" bn mt)))
883 (if filename
884 (url-copy-file url filename t)
885 (url-insert-file-contents url))))
886
887 (provide 'debbugs)
888
889 ;;; TODO:
890
891 ;; * SOAP interface extensions (wishlist).
892 ;; - Server-side sorting.
893 ;; - Regexp and/or wildcards search.
894 ;; - Returning message attachments.
895
896 ;;; debbugs.el ends here