summaryrefslogtreecommitdiff
path: root/Network/XmlRpc/Internals.hs
blob: af4494608e5f21125c528ccd9d4c56b0222b4305 (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
{-# LANGUAGE CPP #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Network.XmlRpc.Internals
-- Copyright   :  (c) Bjorn Bringert 2003
-- License     :  BSD-style
--
-- Maintainer  :  bjorn@bringert.net
-- Stability   :  experimental
-- Portability :  non-portable (requires extensions and non-portable libraries)
--
-- This module contains the core functionality of the XML-RPC library.
-- Most applications should not need to use this module. Client
-- applications should use "Network.XmlRpc.Client" and server applications should
-- use "Network.XmlRpc.Server".
--
-- The XML-RPC specifcation is available at <http://www.xmlrpc.com/spec>.
--
-----------------------------------------------------------------------------

#if __GLASGOW_HASKELL__ >= 710
#define OVERLAPPABLE_ {-# OVERLAPPABLE #-}
#define OVERLAPPING_  {-# OVERLAPPING #-}
#else
{-# LANGUAGE OverlappingInstances #-}
#define OVERLAPPABLE_
#define OVERLAPPING_
#endif

module Network.XmlRpc.Internals (
-- * Method calls and repsonses
MethodCall(..), MethodResponse(..),
-- * XML-RPC types
Value(..), Type(..), XmlRpcType(..),
-- * Converting from XML
parseResponse, parseCall, getField, getFieldMaybe,
-- * Converting to XML
renderCall, renderResponse,
-- * Converting to and from DTD types
toXRValue, fromXRValue,
toXRMethodCall, fromXRMethodCall,
toXRMethodResponse, fromXRMethodResponse,
toXRParams, fromXRParams,
toXRMember, fromXRMember,
-- * Error monad
Err, maybeToM, handleError, ioErrorToErr
) where

import           Control.Exception
import           Control.Monad
import           Control.Monad.Except
import qualified Control.Monad.Fail as Fail
import           Control.Monad.Fail (MonadFail)
import           Data.Char
import           Data.List
import           Data.Maybe
import           Data.Time.Calendar
import           Data.Time.Calendar.OrdinalDate (toOrdinalDate)
import           Data.Time.Calendar.WeekDate (toWeekDate)
import           Data.Time.Format
import           Data.Time.LocalTime
import           Numeric (showFFloat)
import           Prelude hiding (showString, catch)
import           System.IO.Unsafe (unsafePerformIO)
import           System.Time (CalendarTime(..))

#if ! MIN_VERSION_time(1,5,0)
import           System.Locale (defaultTimeLocale)
#endif

import           Data.Text (Text)
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as BS (ByteString, pack, unpack)
import qualified Data.ByteString.Lazy.Char8 as BSL (ByteString, pack)
import qualified Network.XmlRpc.Base64 as Base64
import qualified Network.XmlRpc.DTD_XMLRPC as XR
import           Network.XmlRpc.Pretty
import           Text.XML.HaXml.XmlContent

--
-- General utilities
--

-- | Replaces all occurances of a sublist in a list with another list.
--   If the list to replace is the empty list, does nothing.
replace :: Eq a =>
        [a] -- ^ The sublist to replace when found
        -> [a] -- ^ The list to replace it with
        -> [a] -- ^ The list to replace in
        -> [a] -- ^ The result
replace [] _ xs = xs
replace _ _ [] = []
replace ys zs xs@(x:xs')
    | isPrefixOf ys xs = zs ++ replace ys zs (drop (length ys) xs)
    | otherwise = x : replace ys zs xs'

-- | Convert a 'Maybe' value to a value in any monad
maybeToM :: MonadFail m =>
                String -- ^ Error message to fail with for 'Nothing'
             -> Maybe a -- ^ The 'Maybe' value.
             -> m a -- ^ The resulting value in the monad.
maybeToM err Nothing = Fail.fail err
maybeToM _ (Just x) = return x

-- | Convert a 'Maybe' value to a value in any monad
eitherToM :: Monad m
          => String -- ^ Error message to fail with for 'Nothing'
          -> Either String a -- ^ The 'Maybe' value.
          -> m a -- ^ The resulting value in the monad.
eitherToM err (Left s)  = fail (err ++ ": " ++ s)
eitherToM   _ (Right x) = return x

-- | The format for \"dateTime.iso8601\"
xmlRpcDateFormat :: String
xmlRpcDateFormat = "%Y%m%dT%H:%M:%S"

--
-- Error monad stuff
--

type Err m a = ExceptT String m a

-- | Evaluate the argument and catch error call exceptions
errorToErr :: (Show e, MonadError e m) => a -> Err m a
errorToErr x = unsafePerformIO (liftM return (evaluate x) `catch` handleErr)
  where handleErr :: Monad m => SomeException -> IO (Err m a)
        handleErr = return . throwError . show

-- | Catch IO errors in the error monad.
ioErrorToErr :: IO a -> Err IO a
ioErrorToErr x = (liftIO x >>= return) `catchError` \e -> throwError (show e)

-- | Handle errors from the error monad.
handleError :: MonadFail m => (String -> m a) -> Err m a -> m a
handleError h m = do
                  Right x <- runExceptT (catchError m (lift . h))
                  return x

errorRead :: (Monad m, Read a) =>
             ReadS a -- ^ Parser
          -> String -- ^ Error message
          -> String -- ^ String to parse
          -> Err m a
errorRead r err s = case [x | (x,t) <- r s, ("","") <- lex t] of
                          [x] -> return x
                          _   -> fail (err ++ ": '" ++ s ++ "'")

--
-- Types for methods calls and responses
--

-- | An XML-RPC method call. Consists of a method name and a list of
--   parameters.
data MethodCall = MethodCall String [Value]
                  deriving (Eq, Show) -- for debugging

-- | An XML-RPC response.
data MethodResponse = Return Value -- ^ A method response returning a value
                    | Fault Int String -- ^ A fault response
                      deriving (Eq, Show) -- for debugging

-- | An XML-RPC value.
data Value =
      ValueInt Int -- ^ int, i4, or i8
    | ValueBool Bool -- ^ bool
    | ValueString String -- ^ string
    | ValueUnwrapped String -- ^ no inner element
    | ValueDouble Double -- ^ double
    | ValueDateTime LocalTime -- ^ dateTime.iso8601
    | ValueBase64 BS.ByteString -- ^ base 64.  NOTE that you should provide the raw data; the haxr library takes care of doing the base-64 encoding.
    | ValueStruct [(String,Value)] -- ^ struct
    | ValueArray [Value]  -- ^ array
      deriving (Eq, Show) -- for debugging

-- | An XML-RPC value. Use for error messages and introspection.
data Type =
          TInt
          | TBool
          | TString
          | TDouble
          | TDateTime
          | TBase64
          | TStruct
          | TArray
          | TUnknown
      deriving (Eq)

instance Show Type where
    show TInt = "int"
    show TBool = "bool"
    show TString = "string"
    show TDouble = "double"
    show TDateTime = "dateTime.iso8601"
    show TBase64 = "base64"
    show TStruct = "struct"
    show TArray = "array"
    show TUnknown = "unknown"

instance Read Type where
    readsPrec _ s = case break isSpace (dropWhile isSpace s) of
                    ("int",r) -> [(TInt,r)]
                    ("bool",r) -> [(TBool,r)]
                    ("string",r) -> [(TString,r)]
                    ("double",r) -> [(TDouble,r)]
                    ("dateTime.iso8601",r) -> [(TDateTime,r)]
                    ("base64",r) -> [(TBase64,r)]
                    ("struct",r) -> [(TStruct,r)]
                    ("array",r) -> [(TArray,r)]

-- | Gets the value of a struct member
structGetValue :: MonadFail m => String -> Value -> Err m Value
structGetValue n (ValueStruct t) =
    maybeToM ("Unknown member '" ++ n ++ "'") (lookup n t)
structGetValue _ _ = fail "Value is not a struct"

-- | Builds a fault struct
faultStruct :: Int -> String -> Value
faultStruct code str = ValueStruct [("faultCode",ValueInt code),
                                    ("faultString",ValueString str)]

-- XML-RPC specification:
-- "The body of the response is a single XML structure, a
-- <methodResponse>, which can contain a single <params> which contains a
-- single <param> which contains a single <value>."
onlyOneResult :: Monad m => [Value] -> Err m Value
onlyOneResult [] = fail "Method returned no result"
onlyOneResult [x] = return x
onlyOneResult _ = fail "Method returned more than one result"

--
-- Converting to and from XML-RPC types
--

-- | A class for mapping Haskell types to XML-RPC types.
class XmlRpcType a where
    -- | Convert from this type to a 'Value'
    toValue :: a -> Value
    -- | Convert from a 'Value' to this type. May fail if
    --   if there is a type error.
    fromValue :: Monad m => Value -> Err m a
    getType :: a -> Type

typeError :: (XmlRpcType a, Monad m) => Value -> Err m a
typeError v = withType $ \t ->
       fail ("Wanted: "
             ++ show (getType t)
             ++ "', got: '"
             ++ showXml False (toXRValue v) ++ "'") `asTypeOf` return t

-- a type hack for use in 'typeError'
withType :: (a -> Err m a) -> Err m a
withType f = f undefined

simpleFromValue :: (Monad m, XmlRpcType a) => (Value -> Maybe a)
                -> Value -> Err m a
simpleFromValue f v =
    maybe (typeError v) return (f v)


-- | Exists to allow explicit type conversions.
instance XmlRpcType Value where
    toValue = id
    fromValue = return . id
    getType _ = TUnknown

-- FIXME: instance for ()?


instance XmlRpcType Int where
    toValue = ValueInt
    fromValue = simpleFromValue f
        where f (ValueInt x) = Just x
              f _ = Nothing
    getType _ = TInt

instance XmlRpcType Bool where
    toValue = ValueBool
    fromValue = simpleFromValue f
        where f (ValueBool x) = Just x
              f _ = Nothing
    getType _ = TBool

instance OVERLAPPING_ XmlRpcType String where
    toValue = ValueString
    fromValue = simpleFromValue f
        where f (ValueString x) = Just x
              f (ValueUnwrapped x) = Just x
              f _ = Nothing
    getType _ = TString

instance XmlRpcType Text where
    toValue = ValueString . T.unpack
    fromValue = (liftM T.pack) . fromValue
    getType _ = TString

instance XmlRpcType BS.ByteString where
    toValue = ValueBase64
    fromValue = simpleFromValue f
        where f (ValueBase64 x) = Just x
              f _ = Nothing
    getType _ = TBase64

instance XmlRpcType Double where
    toValue = ValueDouble
    fromValue = simpleFromValue f
        where f (ValueDouble x) = Just x
              f _ = Nothing
    getType _ = TDouble

instance XmlRpcType LocalTime where
    toValue = ValueDateTime
    fromValue = simpleFromValue f
        where f (ValueDateTime x) = Just x
              f _ = Nothing
    getType _ = TDateTime

instance XmlRpcType CalendarTime where
    toValue = toValue . calendarTimeToLocalTime
    fromValue = liftM localTimeToCalendarTime . fromValue
    getType _ = TDateTime

-- FIXME: array elements may have different types
instance OVERLAPPABLE_ XmlRpcType a => XmlRpcType [a] where
    toValue = ValueArray . map toValue
    fromValue v = case v of
                         ValueArray xs -> mapM fromValue xs
                         _ -> typeError v
    getType _ = TArray

-- FIXME: struct elements may have different types
instance OVERLAPPING_ XmlRpcType a => XmlRpcType [(String,a)] where
    toValue xs = ValueStruct [(n, toValue v) | (n,v) <- xs]

    fromValue v = case v of
                  ValueStruct xs -> mapM (\ (n,v') -> liftM ((,) n) (fromValue v')) xs
                  _ -> typeError v
    getType _ = TStruct

-- Tuple instances may be used for heterogenous array types.
instance (XmlRpcType a, XmlRpcType b, XmlRpcType c, XmlRpcType d,
          XmlRpcType e) =>
         XmlRpcType (a,b,c,d,e) where
    toValue (v,w,x,y,z) =
        ValueArray [toValue v, toValue w, toValue x, toValue y, toValue z]
    fromValue (ValueArray [v,w,x,y,z]) =
        liftM5 (,,,,) (fromValue v) (fromValue w) (fromValue x)
                      (fromValue y) (fromValue z)
    fromValue _ = throwError "Expected 5-element tuple!"
    getType _ = TArray

instance (XmlRpcType a, XmlRpcType b, XmlRpcType c, XmlRpcType d) =>
         XmlRpcType (a,b,c,d) where
    toValue (w,x,y,z) = ValueArray [toValue w, toValue x, toValue y, toValue z]
    fromValue (ValueArray [w,x,y,z]) =
        liftM4 (,,,) (fromValue w) (fromValue x) (fromValue y) (fromValue z)
    fromValue _ = throwError "Expected 4-element tuple!"
    getType _ = TArray

instance (XmlRpcType a, XmlRpcType b, XmlRpcType c) => XmlRpcType (a,b,c) where
    toValue (x,y,z) = ValueArray [toValue x, toValue y, toValue z]
    fromValue (ValueArray [x,y,z]) =
        liftM3 (,,) (fromValue x) (fromValue y) (fromValue z)
    fromValue _ = throwError "Expected 3-element tuple!"
    getType _ = TArray

instance (XmlRpcType a, XmlRpcType b) => XmlRpcType (a,b) where
    toValue (x,y) = ValueArray [toValue x, toValue y]
    fromValue (ValueArray [x,y]) = liftM2 (,) (fromValue x) (fromValue y)
    fromValue _ = throwError "Expected 2-element tuple."
    getType _ = TArray

-- | Get a field value from a (possibly heterogeneous) struct.
getField :: (MonadFail m, XmlRpcType a) =>
            String           -- ^ Field name
         -> [(String,Value)] -- ^ Struct
         -> Err m a
getField x xs = maybeToM ("struct member " ++ show x ++ " not found")
                (lookup x xs) >>= fromValue

-- | Get a field value from a (possibly heterogeneous) struct.
getFieldMaybe :: (Monad m, XmlRpcType a) =>
            String           -- ^ Field name
         -> [(String,Value)] -- ^ Struct
         -> Err m (Maybe a)
getFieldMaybe x xs = case lookup x xs of
                                      Nothing -> return Nothing
                                      Just v -> liftM Just (fromValue v)

--
-- Converting to XR types
--

toXRValue :: Value -> XR.Value
toXRValue (ValueInt x) = XR.Value [XR.Value_AInt (XR.AInt (showInt x))]
toXRValue (ValueBool b) = XR.Value [XR.Value_Boolean (XR.Boolean (showBool b))]
toXRValue (ValueString s) = XR.Value [XR.Value_AString (XR.AString (showString s))]
toXRValue (ValueUnwrapped s) = XR.Value [XR.Value_Str s]
toXRValue (ValueDouble d) = XR.Value [XR.Value_ADouble (XR.ADouble (showDouble d))]
toXRValue (ValueDateTime t) =
   XR.Value [ XR.Value_DateTime_iso8601 (XR.DateTime_iso8601 (showDateTime t))]
toXRValue (ValueBase64 s) = XR.Value [XR.Value_Base64 (XR.Base64 (showBase64 s))]
toXRValue (ValueStruct xs) = XR.Value [XR.Value_Struct (XR.Struct (map toXRMember xs))]
toXRValue (ValueArray xs) =
    XR.Value [XR.Value_Array (XR.Array (XR.Data (map toXRValue xs)))]

showInt :: Int -> String
showInt = show

showBool :: Bool -> String
showBool b = if b then "1" else "0"

-- escapes &, <, and <
showString :: String -> String
showString = replace ">" "&gt;" . replace "<" "&lt;" . replace "&" "&amp;"

-- | Shows a double in signed decimal point notation.
showDouble :: Double -> String
showDouble d = showFFloat Nothing d ""

-- | Shows a date and time on the format: YYYYMMDDTHH:mm:SS
showDateTime :: LocalTime -> String
showDateTime t = formatTime defaultTimeLocale xmlRpcDateFormat t

showBase64 :: BS.ByteString -> String
showBase64 = BS.unpack . Base64.encode

toXRMethodCall :: MethodCall -> XR.MethodCall
toXRMethodCall (MethodCall name vs) =
    XR.MethodCall (XR.MethodName name) (Just (toXRParams vs))

toXRMethodResponse :: MethodResponse -> XR.MethodResponse
toXRMethodResponse (Return val) = XR.MethodResponseParams (toXRParams [val])
toXRMethodResponse (Fault code str) =
    XR.MethodResponseFault (XR.Fault (toXRValue (faultStruct code str)))

toXRParams :: [Value] -> XR.Params
toXRParams vs = XR.Params (map (XR.Param . toXRValue) vs)

toXRMember :: (String, Value) -> XR.Member
toXRMember (n, v) = XR.Member (XR.Name n) (toXRValue v)

--
-- Converting from XR types
--

fromXRValue :: Monad m => XR.Value -> Err m Value
fromXRValue (XR.Value vs)
  =  case (filter notstr vs) of
       []     -> liftM  (ValueUnwrapped . concat) (mapM (readString . unstr) vs)
       (v:_)  -> f v
  where
  notstr (XR.Value_Str _)  = False
  notstr _                 = True

  unstr (XR.Value_Str x)  = x

  f (XR.Value_I4 (XR.I4 x)) = liftM ValueInt (readInt x)
  f (XR.Value_I8 (XR.I8 x)) = liftM ValueInt (readInt x)
  f (XR.Value_AInt (XR.AInt x)) = liftM ValueInt (readInt x)
  f (XR.Value_Boolean (XR.Boolean x)) = liftM ValueBool (readBool x)
  f (XR.Value_ADouble (XR.ADouble x)) = liftM ValueDouble (readDouble x)
  f (XR.Value_AString (XR.AString x)) = liftM ValueString (readString x)
  f (XR.Value_DateTime_iso8601 (XR.DateTime_iso8601 x)) =
    liftM ValueDateTime (readDateTime x)
  f (XR.Value_Base64 (XR.Base64 x)) = liftM ValueBase64 (readBase64 x)
  f (XR.Value_Struct (XR.Struct ms)) =
    liftM ValueStruct (mapM fromXRMember ms)
  f (XR.Value_Array (XR.Array (XR.Data xs))) =
    liftM ValueArray (mapM fromXRValue xs)


fromXRMember :: Monad m => XR.Member -> Err m (String,Value)
fromXRMember (XR.Member (XR.Name n) xv) = liftM (\v -> (n,v)) (fromXRValue xv)

-- | From the XML-RPC specification:
--
-- \"An integer is a 32-bit signed number. You can include a plus or
-- minus at the beginning of a string of numeric characters. Leading
-- zeros are collapsed. Whitespace is not permitted. Just numeric
-- characters preceeded by a plus or minus.\"
readInt :: Monad m => String -> Err m Int
readInt s = errorRead reads "Error parsing integer" s


-- | From the XML-RPC specification:
--
-- \"0 (false) or 1 (true)\"
readBool :: Monad m => String -> Err m Bool
readBool s = errorRead readsBool "Error parsing boolean" s
    where readsBool "true" = [(True,"")]
          readsBool "false" = [(False,"")]
          readsBool "1" = [(True,"")]
          readsBool "0" = [(False,"")]
          readsBool _ = []

-- | From the XML-RPC specification:
--
-- \"Any characters are allowed in a string except \< and &, which are
-- encoded as &lt; and &amp;. A string can be used to encode binary data.\"
--
-- To work with implementations (such as some Python bindings for example)
-- which also escape \>, &gt; is unescaped. This is non-standard, but
-- seems unlikely to cause problems.
readString :: Monad m => String -> Err m String
readString = return . replace "&amp;" "&" . replace "&lt;" "<"
             . replace "&gt;" ">"


-- | From the XML-RPC specification:
--
-- There is no representation for infinity or negative infinity or \"not
-- a number\". At this time, only decimal point notation is allowed, a
-- plus or a minus, followed by any number of numeric characters,
-- followed by a period and any number of numeric
-- characters. Whitespace is not allowed. The range of allowable values
-- is implementation-dependent, is not specified.
--
-- FIXME: accepts more than decimal point notation
readDouble :: Monad m => String -> Err m Double
readDouble s = errorRead reads "Error parsing double" s

-- | From <http://groups.yahoo.com/group/xml-rpc/message/4733>:
--
--   \"Essentially \"dateTime.iso8601\" is a misnomer and the format of the
--   content of this element should not be assumed to comply with the
--   variants of the ISO8601 standard. Only assume YYYYMMDDTHH:mm:SS\"
-- FIXME: make more robust
readDateTime :: Monad m => String -> Err m LocalTime
readDateTime dt =
    maybe
        (fail $ "Error parsing dateTime '" ++ dt ++ "'")
        return
        (parseTimeM True defaultTimeLocale xmlRpcDateFormat dt)

localTimeToCalendarTime :: LocalTime -> CalendarTime
localTimeToCalendarTime l =
    let (y,mo,d) = toGregorian (localDay l)
        TimeOfDay { todHour = h, todMin = mi, todSec = s } = localTimeOfDay l
        (_,_,wd) = toWeekDate (localDay l)
        (_,yd) = toOrdinalDate (localDay l)
     in CalendarTime {
                      ctYear    = fromIntegral y,
                      ctMonth   = toEnum (mo-1),
                      ctDay     = d,
                      ctHour    = h,
                      ctMin     = mi,
                      ctSec     = truncate s,
                      ctPicosec = 0,
                      ctWDay    = toEnum (wd `mod` 7),
                      ctYDay    = yd,
                      ctTZName  = "UTC",
                      ctTZ      = 0,
                      ctIsDST   = False
                     }

calendarTimeToLocalTime :: CalendarTime -> LocalTime
calendarTimeToLocalTime ct =
    let (y,mo,d) = (ctYear ct, ctMonth ct, ctDay ct)
        (h,mi,s) = (ctHour ct, ctMin ct, ctSec ct)
     in LocalTime {
                   localDay = fromGregorian (fromIntegral y) (fromEnum mo + 1) d,
                   localTimeOfDay = TimeOfDay { todHour = h, todMin = mi, todSec = fromIntegral s }
                  }

-- FIXME: what if data contains non-base64 characters?
readBase64 :: Monad m => String -> Err m BS.ByteString
readBase64 = return . Base64.decode . BS.pack

fromXRParams :: Monad m => XR.Params -> Err m [Value]
fromXRParams (XR.Params xps) = mapM (\(XR.Param v) -> fromXRValue v) xps

fromXRMethodCall :: Monad m => XR.MethodCall -> Err m MethodCall
fromXRMethodCall (XR.MethodCall (XR.MethodName name) params) =
    liftM (MethodCall name) (fromXRParams (fromMaybe (XR.Params []) params))

fromXRMethodResponse :: MonadFail m => XR.MethodResponse -> Err m MethodResponse
fromXRMethodResponse (XR.MethodResponseParams xps) =
    liftM Return (fromXRParams xps >>= onlyOneResult)
fromXRMethodResponse (XR.MethodResponseFault (XR.Fault v)) =
    do
    struct <- fromXRValue v
    vcode <- structGetValue "faultCode" struct
    code <- fromValue vcode
    vstr <- structGetValue "faultString" struct
    str <- fromValue vstr
    return (Fault code str)

--
-- Parsing calls and reponses from XML
--

-- | Parses a method call from XML.
parseCall :: (Show e, MonadError e m) => String -> Err m MethodCall
parseCall c =
    do
    mxc <- errorToErr (readXml c)
    xc <- eitherToM "Error parsing method call" mxc
    fromXRMethodCall xc

-- | Parses a method response from XML.
parseResponse :: (Show e, MonadError e m, MonadFail m) => String -> Err m MethodResponse
parseResponse c =
    do
    mxr <- errorToErr (readXml c)
    xr <- eitherToM "Error parsing method response" mxr
    fromXRMethodResponse xr

--
-- Rendering calls and reponses to XML
--

-- | Makes an XML-representation of a method call.
-- FIXME: pretty prints ugly XML
renderCall :: MethodCall -> BSL.ByteString
renderCall = showXml' False . toXRMethodCall

-- | Makes an XML-representation of a method response.
-- FIXME: pretty prints ugly XML
renderResponse :: MethodResponse -> BSL.ByteString
renderResponse  = showXml' False . toXRMethodResponse

showXml' :: XmlContent a => Bool -> a -> BSL.ByteString
showXml' dtd x = case toContents x of
                   [CElem _ _] -> (document . toXml dtd) x
                   _ -> BSL.pack ""