1# $Id$
2#
3# Support for XMPP/Jabber MIME Type (XEP-0081)
4#
5
6namespace eval ::xmppmime {
7    set used 0
8    set handle 0
9    set loaded 0
10    set connected 0
11    set queue {}
12}
13
14package require xmpp
15
16proc ::xmppmime::load {path} {
17    # TODO: catch file errors
18    set f [open $path]
19    set content [read $f]
20    close $f
21
22    ::xmpp::xml::parseData $content ::xmppmime::parse
23}
24
25proc ::xmppmime::parse {xmldata} {
26    ::xmpp::xml::split $xmldata tag xmlns attrs cdata subels
27
28    set jid [::xmpp::xml::getAttr $attrs jid]
29
30    switch -- $tag {
31	message {
32	    send_event [list message $jid]
33	}
34
35	chat {
36	    send_event [list chat $jid]
37	}
38
39	groupchat {
40	    send_event [list groupchat $jid]
41	}
42
43	subscribe {
44	    send_event [list subscribe $jid]
45	}
46
47	vcard {
48	    send_event [list vcard $jid]
49	}
50
51	register {
52	    send_event [list register $jid]
53	}
54
55	disco {
56	    send_event [list groupchat $jid]
57	}
58    }
59}
60
61proc ::xmppmime::send_event {event} {
62    variable handle
63    variable used
64
65    set used 1
66
67    if {!$handle} {
68	if {[tk appname] == "tkabber"} {
69	    set handle 1
70	}
71    }
72
73    if {$handle} {
74	recv_event $event
75    } else {
76	if {[catch {send tkabber [list ::xmppmime::recv_event $event]}]} {
77	    set handle 1
78	    recv_event $event
79	}
80    }
81}
82
83proc ::xmppmime::recv_event {event} {
84    variable queue
85    variable loaded
86
87    if {$loaded} {
88	process_event $event
89    } else {
90	lappend queue $event
91    }
92}
93
94proc ::xmppmime::process_event {event} {
95    variable queue
96    variable connected
97    set rest [lassign $event type jid]
98
99    set xlib [lindex [connections] 0]
100
101    switch -- $type {
102	message {
103	    message::send_dialog -to $jid
104	}
105
106	chat {
107	    chat::open_window [chat::chatid $xlib $jid] chat
108	}
109
110	groupchat {
111	    if {$connected} {
112		muc::join $jid
113	    } else {
114		lappend queue $event
115	    }
116	}
117
118	subscribe {
119	    message::send_subscribe_dialog $jid
120	}
121
122	vcard {
123	    if {$connected} {
124		userinfo::open $xlib $jid
125	    } else {
126		lappend queue $event
127	    }
128	}
129
130	register {
131	    if {$connected} {
132		register::open $xlib $jid
133	    } else {
134		lappend queue $event
135	    }
136	}
137
138	disco {
139	    if {$connected} {
140		disco::browser::open_win $xlib $jid
141	    } else {
142		lappend queue $event
143	    }
144	}
145    }
146}
147
148proc ::xmppmime::is_done {} {
149    variable handle
150    variable used
151    return [expr {$used && !$handle}]
152}
153
154proc ::xmppmime::process_queue {} {
155    variable queue
156    variable loaded
157
158    set loaded 1
159    set oldqueue $queue
160    set queue {}
161    foreach event $oldqueue {
162	process_event $event
163    }
164}
165
166hook::add finload_hook ::xmppmime::process_queue
167
168proc ::xmppmime::connected {xlib} {
169    variable connected
170    set connected 1
171    process_queue
172}
173
174hook::add connected_hook ::xmppmime::connected
175
176proc ::xmppmime::disconnected {xlib} {
177    variable connected
178    set connected [expr {[connections] != {}}]
179}
180
181hook::add disconnected_hook ::xmppmime::disconnected
182
183