summaryrefslogtreecommitdiff
path: root/xml-rpc.el
blob: 1ccb53ddb1fce97fc78054c6cc3f88964ff02783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
;;; xml-rpc.el --- An elisp implementation of clientside XML-RPC

;; Copyright (C) 2001 CodeFactory AB.
;; Copyright (C) 2001 Daniel Lundin.
;; Parts Copyright (C) 2002-2005 Mark A. Hershberger

;; Copyright (C) 2006 Shun-ichi Goto
;;   Modified for non-ASCII character handling.

;; Author: Daniel Lundin <daniel@codefactory.se>
;; Maintainer: Mark A. Hershberger <mah@everybody.org>
;; Version: 1.6.4.1
;; Created: May 13 2001
;; Keywords: xml rpc network
;; URL: http://emacswiki.org/emacs/xml-rpc.el

;; This file is NOT (yet) part of GNU Emacs.

;; This is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This software is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.


;;; Commentary:

;; This is an XML-RPC client implementation in elisp, capable of both
;; synchronous and asynchronous method calls (using the url package's async
;; retrieval functionality).
;; XML-RPC is remote procedure calls over HTTP using XML to describe the
;; function call and return values.

;; xml-rpc.el represents XML-RPC datatypes as lisp values, automatically
;; converting to and from the XML datastructures as needed, both for method
;; parameters and return values, making using XML-RPC methods fairly
;; transparent to the lisp code.

;; Requirements
;; ------------

;; xml-rpc.el uses the url package for http handling and xml.el for
;; XML parsing. url is a part of the W3 browser package.  The url
;; package that is part of Emacs 22+ works great.
;;
;; xml.el is a part of GNU Emacs 21, but can also be downloaded from
;; here: <URL:ftp://ftp.codefactory.se/pub/people/daniel/elisp/xml.el>


;; XML-RPC datatypes are represented as follows
;; --------------------------------------------

;;          int:  42
;; float/double:  42.0
;;       string:  "foo"
;;        array:  '(1 2 3 4)   '(1 2 3 (4.1 4.2))
;;       struct:  '(("name" . "daniel") ("height" . 6.1))


;; Examples
;; ========
;; Here follows some examples demonstrating the use of xml-rpc.el

;; Normal synchronous operation
;; ----------------------------

;; (xml-rpc-method-call "http://localhost:80/RPC" 'foo-method foo bar zoo)

;; Asynchronous example (cb-foo will be called when the methods returns)
;; ---------------------------------------------------------------------

;; (defun cb-foo (foo)
;;   (print (format "%s" foo)))

;; (xml-rpc-method-call-async 'cb-foo "http://localhost:80/RPC"
;;                            'foo-method foo bar zoo)


;; Some real world working examples for fun and play
;; -------------------------------------------------

;; Check the temperature (celsius) outside jonas@codefactory.se's apartment

;; (xml-rpc-method-call
;;      "http://flint.bengburken.net:80/xmlrpc/onewire_temp.php"
;;      'onewire.getTemp)


;; Fetch the latest NetBSD news the past 5 days from O'reillynet

;; (xml-rpc-method-call "http://www.oreillynet.com/meerkat/xml-rpc/server.php"
;;  		     'meerkat.getItems
;;  		     '(("channel" . 1024)
;;  		       ("search" . "/NetBSD/")
;;  		       ("time_period" . "5DAY")
;;  		       ("ids" . 0)
;;  		       ("descriptions" . 200)
;;  		       ("categories" . 0)
;;  		       ("channels" . 0)
;;  		       ("dates" . 0)
;;  		       ("num_items" . 5)))


;;; History:

;; 1.6.4.1 - Updated to work with both Emacs22 and Emacs23.

;; 1.6.2.2 - Modified to allow non-ASCII string again.
;;           It can handle non-ASCII page name and comment
;;           on Emacs 21 also.

;; 1.6.2.1 - Modified to allow non-ASCII string.
;;           If xml-rpc-allow-unicode-string is non-nil,
;;           make 'value' object instead of 'base64' object.
;;           This is good for WikiRPC.

;; 1.6.2 - Fix whitespace issues to work better with new xml.el
;;         Fix bug in string handling.
;;         Add support for gzip-encoding when needed.

;; 1.6.1 - base64 support added.
;;         url-insert-entities-in-string done on string types now.

;; 1.6 - Fixed dependencies (remove w3, add cl).
;;       Move string-to-boolean and boolean-to-string into xml-rpc namespace.
;;       Fix bug in xml-rpc-xml-to-response where non-existent var was.
;;       More tweaking of "Connection: close" header.
;;       Fix bug in xml-rpc-request-process-buffer so that this works with
;;         different mixes of the url.el code.

;; 1.5.1 - Added Andrew J Cosgriff's patch to make the
;;         xml-rpc-clean-string function work in XEmacs.

;; 1.5 - Added headers to the outgoing url-retreive-synchronously
;;       so that it would close connections immediately on completion.

;; 1.4 - Added conditional debugging code.  Added version tag.

;; 1.2 - Better error handling.  The documentation didn't match
;;       the code.  That was changed so that an error was
;;       signaled.  Also, better handling of various and
;;       different combinations of xml.el and url.el.

;; 1.1 - Added support for boolean types.  If the type of a
;;       returned value is not specified, string is assumed

;; 1.0 - First version


;;; Code:

(require 'custom)
(require 'xml)
(require 'url)
(eval-when-compile
  (require 'cl))

(defcustom xml-rpc-load-hook nil
  "*Hook run after loading xml-rpc."
  :type 'hook :group 'xml-rpc)

(defcustom xml-rpc-allow-unicode-string t
  "If non-nil, non-ASCII data is composed as 'value' instead of 'base64'.
And this option overrides `xml-rpc-base64-encode-unicode' and
`xml-rpc-base64-decode-unicode' if set as non-nil."
  :type 'boolean :group 'xml-rpc)

(defcustom xml-rpc-base64-encode-unicode t
  "If non-nil, then strings with non-ascii characters will be turned
into Base64."
  :type 'boolean :group 'xml-rpc)

(defcustom xml-rpc-base64-decode-unicode t
  "If non-nil, then base64 strings will be decoded using the
utf-8 coding system."
  :type 'boolean :group 'xml-rpc)

(defcustom xml-rpc-debug 0
  "Set this to 1 or greater to avoid killing temporary buffers.
Set it higher to get some info in the *Messages* buffer")

(defconst xml-rpc-version "1.6"
  "Current Version of xml-rpc.el")

;;
;; Value type handling functions
;;

(defun xml-rpc-value-intp (value)
  "Return t if VALUE is an integer."
  (integerp value))

(defun xml-rpc-value-doublep (value)
  "Return t if VALUE is a double precision number."
  (floatp value))

(defun xml-rpc-value-stringp (value)
  "Return t if VALUE is a string."
  (stringp value))

;; An XML-RPC struct is a list where every car is cons or a list of
;; length 1 or 2 and has a string for car.
(defsubst xml-rpc-value-structp (value)
  "Return t if VALUE is an XML-RPC struct."
  (and (listp value)
       (let ((vals value)
	     (result t)
	     curval)
	 (while (and vals result)
	   (setq result (and
                         (setq curval (car-safe vals))
                         (consp curval)
                         (stringp (car-safe curval))))
	   (setq vals (cdr-safe vals)))
	 result)))

;; A somewhat lazy predicate for arrays
(defsubst xml-rpc-value-arrayp (value)
  "Return t if VALUE is an XML-RPC struct."
  (and (listp value)
       (not (xml-rpc-value-structp value))))

(defun xml-rpc-value-booleanp (value)
  "Return t if VALUE is a boolean."
  (or (eq value nil)
      (eq value t)))

(defun xml-rpc-value-datetimep (value)
  "Return t if VALUE is a datetime.  For Emacs XML-RPC
implementation, you must put time keyword :datetime before the
time, or it will be confused for a list."
  (and (listp value)
       (eq (car value) :datetime)))

(defun xml-rpc-string-to-boolean (value)
  "Return t if VALUE is a boolean"
  (or (string-equal value "true") (string-equal value "1")))

(defun xml-rpc-caddar-safe (list)
  (car-safe (cdr-safe (cdr-safe (car-safe list)))))

(defun xml-rpc-xml-list-to-value (xml-list)
  "Convert an XML-RPC structure in an xml.el style XML-LIST to an elisp list, \
interpreting and simplifying it while retaining its structure."
  (cond 
   ((and (xml-rpc-caddar-safe xml-list)
	 (listp (car-safe (cdr-safe (cdr-safe (car-safe xml-list))))))

    (setq valtype (car (caddar xml-list))
	  valvalue (caddr (caddar xml-list)))
    (cond
     ;; Base64
     ((eq valtype 'base64)
      (if xml-rpc-base64-decode-unicode
	  (decode-coding-string (base64-decode-string valvalue) 'utf-8)
	(base64-decode-string valvalue)))
     ;; Boolean
     ((eq valtype 'boolean)
      (xml-rpc-string-to-boolean valvalue))
     ;; String
     ((eq valtype 'string)
      valvalue)
     ;; Integer
     ((or (eq valtype 'int) (eq valtype 'i4))
      (string-to-number valvalue))
     ;; Double/float
     ((eq valtype 'double)
      (string-to-number valvalue))
     ;; Struct
     ((eq valtype 'struct)
      (mapcar (lambda (member)
		(let ((membername (cadr (cdaddr member)))
		      (membervalue (xml-rpc-xml-list-to-value (cdddr member))))
		  (cons membername membervalue)))
	      (cddr (caddar xml-list))))
     ;; Fault
     ((eq valtype 'fault)
      (let* ((struct (xml-rpc-xml-list-to-value (list valvalue)))
		   (fault-string (cdr (assoc "faultString" struct)))
		   (fault-code (cdr (assoc "faultCode" struct))))
	      (list 'fault fault-code fault-string)))
     ;; DateTime
     ((eq valtype 'dateTime\.iso8601)
      (list :datetime (date-to-time valvalue)))
     ((eq valtype 'dateTime)
      (list :datetime (date-to-time valvalue)))
     ;; Array
     ((eq valtype 'array)
      (mapcar (lambda (arrval)
		(xml-rpc-xml-list-to-value (list arrval)))
	      (cddr valvalue)))))
   ((xml-rpc-caddar-safe xml-list))))

(defun xml-rpc-boolean-to-string (value)
  "Convert a boolean value to a string"
  (if value
      "1"
    "0"))

(defun xml-rpc-datetime-to-string (value)
  "Convert a date time to a valid XML-RPC date"
  (format-time-string "%Y%m%dT%H%M%S%z" value))

(defun xml-rpc-value-to-xml-list (value)
  "Return XML representation of VALUE properly formatted for use with the  \
functions in xml.el."
  (cond
					;   ((not value)
					;    nil)
   ((xml-rpc-value-booleanp value)
    `((value nil (boolean nil ,(xml-rpc-boolean-to-string value)))))
   ;; Date
   ((xml-rpc-value-datetimep value)
    `((value nil (dateTime nil ,(xml-rpc-datetime-to-string value)))))
   ;; list
   ((xml-rpc-value-arrayp value)
    (let ((result nil)
	  (xmlval nil))
      (while (setq xmlval (xml-rpc-value-to-xml-list (car value))
                   result (if result (append result xmlval)
                            xmlval)
                   value (cdr value)))
      `((value nil (array nil ,(append '(data nil) result))))))
    ;; struct
    ((xml-rpc-value-structp value)
     (let ((result nil)
           (xmlval nil))
       (while (setq xmlval `((member nil (name nil ,(caar value))
                                     ,(car (xml-rpc-value-to-xml-list
                                            (cdar value)))))
                    result (append result xmlval)
                    value (cdr value)))
       `((evalue nil ,(append '(struct nil) result)))))
   ;; Value is a scalar
   ((xml-rpc-value-intp value)
    `((value nil (int nil ,(int-to-string value)))))
   ((xml-rpc-value-stringp value)
    (let ((charset-list (find-charset-string value)))
      (if (or xml-rpc-allow-unicode-string
	      (and (eq 1 (length charset-list))
		   (eq 'ascii (car charset-list)))
	      (not xml-rpc-base64-encode-unicode))
	  `((value nil (string nil ,value)))
	`((value nil (base64 nil ,(base64-encode-string
				   (encode-coding-string value 'utf-8))))))))
   ((xml-rpc-value-doublep value)
    `((value nil (double nil ,(number-to-string value)))))
   (t
    `((value nil (base64 nil ,(base64-encode-string value)))))))

(defun xml-rpc-xml-to-string (xml)
  "Return a string representation of the XML tree as valid XML markup."
  (let ((tree (xml-node-children xml))
	(result (concat "<" (symbol-name (xml-node-name xml)) ">")))
    (while tree
      (cond
       ((listp (car tree))
	(setq result (concat result (xml-rpc-xml-to-string (car tree)))))
       ((stringp (car tree))
	(setq result (concat result (car tree))))
       (t
	(error "Invalid XML tree")))
      (setq tree (cdr tree)))
    (setq result (concat result "</" (symbol-name (xml-node-name xml)) ">"))
    result))

;;
;; Response handling
;;

(defsubst xml-rpc-response-errorp (response)
  "An 'xml-rpc-method-call'  result value is always a list, where the first \
element in RESPONSE is either nil or if an error occured, a cons pair \
according to (errnum .  \"Error string\"),"
  (eq 'fault (car-safe (caddar response))))

(defsubst xml-rpc-response-error-code (response)
  "Return the error code from RESPONSE."
  (and (xml-rpc-response-errorp response)
       (nth 1 (xml-rpc-xml-list-to-value response))))

(defsubst xml-rpc-response-error-string (response)
  "Return the error code from RESPONSE."
  (and (xml-rpc-response-errorp response)
       (nth 2 (xml-rpc-xml-list-to-value response))))

(defun xml-rpc-xml-to-response (xml)
  "Convert an XML list to a method response list.  An error is
signaled if there is a fault or if the response does not appear
to be an XML-RPC response (i.e. no methodResponse).  Otherwise,
the parsed XML response is returned."
  ;; Check if we have a methodResponse
  (cond
   ((not (eq (car-safe (car-safe xml)) 'methodResponse))
    (error "No methodResponse found"))

   ;; Did we get a fault response
   ((xml-rpc-response-errorp xml)
    (let ((resp (xml-rpc-xml-list-to-value xml)))
      (setq xml-rpc-fault-string (nth 2 resp))
      (setq xml-rpc-fault-code   (nth 1 resp))
      (error "XML-RPC fault `%s'" xml-rpc-fault-string)))
 
   ;; Interpret the XML list and produce a more useful data structure
   (t
    (let ((valpart (cdr (cdaddr (caddar xml)))))
      (xml-rpc-xml-list-to-value valpart)))))

;;
;; Misc
;;

(defun xml-rpc-get-temp-buffer-name ()
  "Get a working buffer name such as ` *XML-RPC-<i>*' without a live process \
and empty it"
  (let ((num 1)
	name buf)
    (while (progn (setq name (format " *XML-RPC-%d*" num)
			buf (get-buffer name))
		  (and buf (or (get-buffer-process buf)
			       (save-excursion (set-buffer buf)
					       (> (point-max) 1)))))
      (setq num (1+ num)))
    name))



;;
;; Method handling
;;

(defun xml-rpc-request (server-url xml &optional async-callback-function)
  "Perform http post request to SERVER-URL using XML.

If ASYNC-CALLBACK-FUNCTION is non-nil, the request will be performed
asynchronously and ASYNC-CALLBACK-FUNCTION should be a callback function to
be called when the reuest is finished.  ASYNC-CALLBACK-FUNCTION is called with
a single argument being an xml.el style XML list.

It returns an XML list containing the method response from the XML-RPC server,
or nil if called with ASYNC-CALLBACK-FUNCTION."
  (unwind-protect
      (save-excursion
	(let ((url-request-method "POST")
	      (url-package-name "xml-rpc.el")
	      (url-package-version xml-rpc-version)
	      (url-request-data (concat "<?xml version=\"1.0\""
                                        " encoding=\"UTF-8\"?>\n"
					(with-temp-buffer
					  (xml-print xml)
					  (when xml-rpc-allow-unicode-string
					    (encode-coding-region
					     (point-min) (point-max) 'utf-8))
					  (buffer-string))
					"\n"))
	      (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
	      (url-request-coding-system 'utf-8)
	      (url-http-attempt-keepalives t)
	      (url-request-extra-headers (list 
                                          (cons "Connection" "keep-alive")
					  (cons "Content-Type"
                                                "text/xml; charset=utf-8"))))
	  (if (> xml-rpc-debug 1)
	      (print url-request-data (create-file-buffer "request-data")))

	  (cond ((boundp 'url-be-asynchronous) ; Sniff for w3 lib capability
		 (if async-callback-function
		     (setq url-be-asynchronous t
			   url-current-callback-data (list
						      async-callback-function
						      (current-buffer))	
			   url-current-callback-func
                           'xml-rpc-request-callback-handler)
		   (setq url-be-asynchronous nil))
		 (url-retrieve server-url t)

		 (if url-be-asynchronous
		     nil
		   (let ((result (xml-rpc-request-process-buffer
				  url-working-buffer)))
		     (if (> xml-rpc-debug 1) 
			 (save-excursion
			   (set-buffer (create-file-buffer "result-data"))
			   (insert result)))
		     result)))
		(t			; Post emacs20 w3-el
		 (if async-callback-function
		     (url-retrieve server-url async-callback-function)
		   (let ((buffer (url-retrieve-synchronously server-url))
			 result)
		     (with-current-buffer buffer
		       (if (not (numberp url-http-response-status))
			   ;; this error may occur when keep-alive bug
			   ;; of url-http.el is not cleared.
			   (error "Why? url-http-response-status is %s"
				  url-http-response-status))
		       (if (> url-http-response-status 299)
			   (error "Error during request: %s"
				  url-http-response-status)))
		     (xml-rpc-request-process-buffer buffer)))))))))


(defun xml-rpc-clean-string (s)
  (if (string-match "\\`[ \t\n\r]*\\'" s)
					;"^[ \t\n]*$" s)
      nil
    s))

(defun xml-rpc-clean (l)
  (cond
   ((listp l)
    (let ((remain l)
	  elem
	  (result nil))
      (while l
					; iterate
	(setq elem (car l)
	      l (cdr l))
					; test the head
	(cond
					; a string, so clean it.
	 ((stringp elem)
	  (let ((tmp (xml-rpc-clean-string elem)))
	    (if (and tmp xml-rpc-allow-unicode-string)
		(setq tmp (decode-coding-string tmp 'utf-8)))
	    (if tmp
		(setq result (append result (list tmp)))
	      result)))
					; a list, so recurse.
	 ((listp elem)
	  (setq result (append result (list (xml-rpc-clean elem)))))

					; everthing else, as is.
	 (t
	  (setq result (append result (list elem))))))
      result))

   ((stringp l)			  ; will returning nil be acceptable ?
    elem)

   (t
    l)))

(defun xml-rpc-request-process-buffer (xml-buffer)
  "Process buffer XML-BUFFER."
  (unwind-protect
      (save-excursion
	(set-buffer xml-buffer)
	(when (fboundp 'url-uncompress)
	  (url-uncompress))
	(goto-char (point-min))
	(search-forward-regexp "<\\?xml" nil t)
	(move-to-column 0)
	;; Gather the results
	(let* ((status url-http-response-status)
	       (result (cond
			;; A probable XML response
			((looking-at "<\\?xml ")
			 (xml-rpc-clean (xml-parse-region (point-min) (point-max))))
			  
			;; No HTTP status returned
			((not status)
			 (let ((errstart
				(search-forward "\n---- Error was: ----\n")))
			   (and errstart
				(buffer-substring errstart (point-max)))))

			;; Maybe they just gave us an the XML w/o PI?
			((search-forward "<methodResponse>" nil t)
			 (xml-rpc-clean (xml-parse-region (match-beginning 0)
							  (point-max))))

			;; Valid HTTP status
			(t
			 (int-to-string status)))))
	  result))))


(defun xml-rpc-request-callback-handler (callback-fun xml-buffer)
  "Marshall a callback function request to CALLBACK-FUN with the results \
handled from XML-BUFFER."
  (let ((xml-response (xml-rpc-request-process-buffer xml-buffer)))
    (if (< xml-rpc-debug 1)
	(kill-buffer xml-buffer))
    (funcall callback-fun (xml-rpc-xml-to-response xml-response))))
  

(defun xml-rpc-method-call-async (async-callback-func server-url method
						      &rest params)
  "Call an XML-RPC method asynchronously at SERVER-URL named METHOD with \
PARAMS as parameters. When the method returns, ASYNC-CALLBACK-FUNC will be \
called with the result as parameter."
  (let* ((m-name (if (stringp method)
		     method
		   (symbol-name method)))
	 (m-params (mapcar '(lambda (p)
			      `(param nil ,(car (xml-rpc-value-to-xml-list
						 p))))
			   (if async-callback-func
			       params
			     (car-safe params))))
	 (m-func-call `((methodCall nil (methodName nil ,m-name)
				    ,(append '(params nil) m-params)))))
    (if (> xml-rpc-debug 1)
	(print m-func-call (create-file-buffer "func-call")))
    (xml-rpc-request server-url m-func-call async-callback-func)))

(defun xml-rpc-method-call (server-url method &rest params)
  "Call an XML-RPC method at SERVER-URL named METHOD with PARAMS as \
parameters."
  (let ((response
	 (xml-rpc-method-call-async nil server-url method params)))
    (cond ((stringp response)
	   (list (cons nil (concat "URL/HTTP Error: " response))))
	  (t
	   (xml-rpc-xml-to-response response)))))

(eval-when-compile
  (unless (fboundp 'xml-escape-string)
    (defun xml-debug-print (xml &optional indent-string)
      "Outputs the XML in the current buffer.
XML can be a tree or a list of nodes.
The first line is indented with the optional INDENT-STRING."
      (setq indent-string (or indent-string ""))
      (dolist (node xml)
        (xml-debug-print-internal node indent-string)))

    (defalias 'xml-print 'xml-debug-print)

    (defun xml-escape-string (string)
      "Return the string with entity substitutions made from
xml-entity-alist."
      (mapconcat (lambda (byte)
                   (let ((char (char-to-string byte)))
                     (if (rassoc char xml-entity-alist)
                         (concat "&" (car (rassoc char xml-entity-alist)) ";")
                       char)))
                 ;; This differs from the non-unicode branch.  Just
                 ;; grabbing the string works here.
                 string ""))

    (defun xml-debug-print-internal (xml indent-string)
      "Outputs the XML tree in the current buffer.
The first line is indented with INDENT-STRING."
      (let ((tree xml)
            attlist)
        (insert indent-string ?< (symbol-name (xml-node-name tree)))

        ;;  output the attribute list
        (setq attlist (xml-node-attributes tree))
        (while attlist
          (insert ?\  (symbol-name (caar attlist)) "=\""
                  (xml-escape-string (cdar attlist)) ?\")
          (setq attlist (cdr attlist)))

        (setq tree (xml-node-children tree))

        (if (null tree)
            (insert ?/ ?>)
          (insert ?>)

          ;;  output the children
          (dolist (node tree)
            (cond
             ((listp node)
              (insert ?\n)
              (xml-debug-print-internal node (concat indent-string "  ")))
             ((stringp node)
              (insert (xml-escape-string node)))
             (t
              (error "Invalid XML tree"))))

          (when (not (and (null (cdr tree))
                          (stringp (car tree))))
            (insert ?\n indent-string))
          (insert ?< ?/ (symbol-name (xml-node-name xml)) ?>))))))

(eval-when-compile
  (defun timezone-parse-date (date)
    "Parse DATE and return a vector [YEAR MONTH DAY TIME TIMEZONE].
Two-digit dates are `windowed'.  Those <69 have 2000 added; otherwise 1900
is added.  Three-digit dates have 1900 added.
TIMEZONE is nil for DATEs without a zone field.

Understands the following styles:
 (1) 14 Apr 89 03:20[:12] [GMT]
 (2) Fri, 17 Mar 89 4:01[:33] [GMT]
 (3) Mon Jan 16 16:12[:37] [GMT] 1989
 (4) 6 May 1992 1641-JST (Wednesday)
 (5) 22-AUG-1993 10:59:12.82
 (6) Thu, 11 Apr 16:17:12 91 [MET]
 (7) Mon, 6  Jul 16:47:20 T 1992 [MET]
 (8) 1996-06-24 21:13:12 [GMT]
 (9) 1996-06-24 21:13-ZONE"
    ;; Get rid of any text properties.
    (and (stringp date)
         (or (text-properties-at 0 date)
             (next-property-change 0 date))
         (setq date (copy-sequence date))
         (set-text-properties 0 (length date) nil date))
    (let ((date (or date ""))
          (year nil)
          (month nil)
          (day nil)
          (time nil)
          (zone nil))			;This may be nil.
      (cond ((string-match
              "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([-+a-zA-Z0-9]+\\)" date)
             ;; Styles: (1) and (2) with timezone and buggy timezone
             ;; This is most common in mail and news,
             ;; so it is worth trying first.
             (setq year 3 month 2 day 1 time 4 zone 5))
            ((string-match
              "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\'" date)
             ;; Styles: (1) and (2) without timezone
             (setq year 3 month 2 day 1 time 4 zone nil))
            ((string-match
              "\\([^ \t,]+\\),[ \t]+\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\(T[ \t]+\\|\\)\\([0-9]+\\)[ \t]*\\'" date)
             ;; Styles: (6) and (7) without timezone
             (setq year 6 month 3 day 2 time 4 zone nil))
            ((string-match
              "\\([^ \t,]+\\),[ \t]+\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\(T[ \t]+\\|\\)\\([0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
             ;; Styles: (6) and (7) with timezone and buggy timezone
             (setq year 6 month 3 day 2 time 4 zone 7))
            ((string-match
              "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([0-9]+\\)" date)
             ;; Styles: (3) without timezone
             (setq year 4 month 1 day 2 time 3 zone nil))
            ((string-match
              "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([-+a-zA-Z0-9]+\\)[ \t]+\\([0-9]+\\)" date)
             ;; Styles: (3) with timezone
             (setq year 5 month 1 day 2 time 3 zone 4))
            ((string-match
              "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
             ;; Styles: (4) with timezone
             (setq year 3 month 2 day 1 time 4 zone 5))
            ((string-match
              "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)?[ \t]+\\([-+a-zA-Z0-9]+\\)" date)
             ;; Styles: (5) with timezone.
             (setq year 3 month 2 day 1 time 4 zone 6))
            ((string-match
              "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)?" date)
             ;; Styles: (5) without timezone.
             (setq year 3 month 2 day 1 time 4 zone nil))
            ((string-match
              "\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+\\([-+a-zA-Z0-9]+\\)" date)
             ;; Styles: (8) with timezone.
             (setq year 1 month 2 day 3 time 4 zone 5))
            ((string-match
              "\\([0-9]\\{4\\}\\)-?\\([0-9]\\{0,2\\}\\)-?\\([0-9]\\{0,2\\}\\)[T \t]+\\([0-9]\\{0,2\\}:?[0-9]\\{0,2\\}:?[0-9]\\{0,2\\}\\)[ \t]*\\([-+a-zA-Z]+[0-9:]*\\)" date)
             ;; Styles: (8) with timezone with a colon in it.
             (setq year 1 month 2 day 3 time 4 zone 5))
            ((string-match
              "\\([0-9]\\{4\\}\\)-?\\([0-9]\\{0,2\\}\\)-?\\([0-9]\\{0,2\\}\\)[T \t]+\\([0-9]+:?[0-9]+:?[0-9]+\\)" date)
             ;; Styles: (8) without timezone.
             (setq year 1 month 2 day 3 time 4 zone nil)))

      (when year
        (setq year (match-string year date))
        ;; Guess ambiguous years.  Assume years < 69 don't predate the
        ;; Unix Epoch, so are 2000+.  Three-digit years are assumed to
        ;; be relative to 1900.
        (if (< (length year) 4)
            (let ((y (string-to-number year)))
              (if (< y 69)
                  (setq y (+ y 100)))
              (setq year (int-to-string (+ 1900 y)))))
        (setq month
              (if (or (= (aref date (+ (match-beginning month) 2)) ?-)
                      (let ((n (string-to-number
                                (char-to-string
                                 (aref date (+ (match-beginning month) 2))))))
                        (= (aref (number-to-string n) 0)
                           (aref date (+ (match-beginning month) 2)))))
                  ;; Handle numeric months, spanning exactly two digits.
                  (substring date
                             (match-beginning month)
                             (+ (match-beginning month) 2))
                (let* ((string (substring date
                                          (match-beginning month)
                                          (+ (match-beginning month) 3)))
                       (monthnum
                        (cdr (assoc (upcase string) timezone-months-assoc))))
                  (if monthnum
                      (int-to-string monthnum)))))
        (setq day (match-string day date))
        (setq time (match-string time date)))
      (if zone (setq zone (match-string zone date)))
      ;; Return a vector.
      (if (and year month)
          (vector year month day time zone)
        (vector "0" "0" "0" "0" nil)))))

(provide 'xml-rpc)

;;; xml-rpc.el ends here