1# documentation: http://code.google.com/apis/calendar/docs/2.0/developers_guide_protocol.html
2
3package require rest
4package require tls
5::http::register https 443 [list ::tls::socket]
6
7set gcal(auth) {
8   url https://www.google.com/accounts/ClientLogin
9   method post
10   req_args { Email: Passwd: }
11   opt_args { source:tclrest }
12   static_args { service cl }
13   post_transform {
14       regexp {Auth=(.*)\n} $result -> result
15       return $result
16   }
17}
18
19set gcal(all_calendars) {
20    url http://www.google.com/calendar/feeds/default/allcalendars/%projection:full%
21    headers { Authorization {GoogleLogin auth=%token%} }
22    opt_args { gsessionid: }
23    body required
24}
25
26set gcal(own_calendars) {
27    url http://www.google.com/calendar/feeds/default/owncalendars/%projection:full%
28    headers { Authorization {GoogleLogin auth=%token%} }
29    opt_args { gsessionid: }
30}
31
32set gcal(new_calendar) {
33    url http://www.google.com/calendar/feeds/default/owncalendars/full
34    method post
35    content-type application/atom+xml
36    headers { Authorization {GoogleLogin auth=%token%} }
37    opt_args { gsessionid: }
38}
39
40set gcal(edit_calendar) {
41    url http://www.google.com/calendar/feeds/default/owncalendars/full/%calendar%
42    method put
43    content-type application/atom+xml
44    headers { Authorization {GoogleLogin auth=%token%} }
45    opt_args { gsessionid: }
46}
47
48set gcal(delete_calendar) {
49    url http://www.google.com/calendar/feeds/default/owncalendars/full/%calendar%
50    method delete
51    headers { Authorization {GoogleLogin auth=%token%} }
52    opt_args { gsessionid: }
53}
54
55set gcal(all_events) {
56    url http://www.google.com/calendar/feeds/%user:default%/%visibility:private%/%projection:full%
57    headers { Authorization {GoogleLogin auth=%token%} }
58    opt_args { gsessionid: }
59}
60
61set gcal(new_event) {
62    url http://www.google.com/calendar/feeds/default/private/full
63    method post
64    content-type application/atom+xml
65    headers { Authorization {GoogleLogin auth=%token%} }
66    opt_args { gsessionid: }
67}
68
69rest::create_interface gcal
70
71proc ::gcal::handle_redir {args} {
72    if {[catch {eval $args} out]} {
73        #puts "catch $out"
74        if {[lindex $out 1] == "302"} {
75            eval [linsert $args 1 -gsessionid [rest::parameters [lindex $out 2] gsessionid]]
76        } else {
77            return -code error $out
78        }
79    }
80}
81
82proc ::gcal::create_single_event_object {args} {
83    set defaults [dict create \
84        text ""               \
85        status confirmed      \
86        where ""              \
87    ]
88    set args [lindex [::rest::parse_opts {} {title: start: end:} {text: status: where:} $args] 0]
89    set args [dict merge $defaults $args]
90
91    set event {}
92    lappend event "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>"
93    lappend event "<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'></category>"
94    lappend event "<title type='text'>[dict get $args title]</title>"
95    lappend event "<content type='text'>[dict get $args text]</content>"
96    lappend event "<gd:when startTime='[clock format [clock scan [dict get $args start]] -format "%Y-%m-%dT%TZ"]' endTime='[clock format [clock scan [dict get $args end]] -format "%Y-%m-%dT%TZ"]'></gd:when>"
97    lappend event "<gd:eventStatus value='http://schemas.google.com/g/2005#event.[dict get $args status]'> </gd:eventStatus>"
98    lappend event "<gd:where valueString='[dict get $args where]'></gd:where>"
99    lappend event "</entry>"
100    return [join $event \n]
101}
102
103