1# $Id$
2
3namespace eval gateway {
4    variable msgid 0
5
6    hook::add roster_service_popup_menu_hook \
7	[namespace current]::add_menu_item 30
8}
9
10proc gateway::add_menu_item {m xlib jid} {
11
12    $m add command -label [::msgcat::mc "Add user to roster..."] \
13	-command [list [namespace current]::subscribe_dialog $xlib $jid]
14}
15
16proc gateway::subscribe_dialog {xlib service} {
17    variable msgid
18
19    set mw .gwmsg$msgid
20    toplevel $mw
21    wm group $mw .
22
23    set title [::msgcat::mc "Send subscription at %s" $service]
24
25    wm title $mw $title
26    wm iconname $mw $title
27
28    set bbox [ButtonBox $mw.buttons -spacing 0 -padx 10 -default 0]
29    $bbox add -text [::msgcat::mc "Subscribe"] \
30        -command [list [namespace current]::send_subscribe $mw $xlib $service]
31    $bbox add -text [::msgcat::mc "Cancel"] -command [list destroy $mw]
32
33    bind $mw <Return> "ButtonBox::invoke [double% $bbox] default"
34    bind $mw <Escape> "ButtonBox::invoke [double% $bbox] 1"
35
36    pack $bbox -side bottom -anchor e -padx 2m -pady 2m
37
38    set sep [Separator::create $mw.sep -orient horizontal]
39    pack $sep -pady 1m -fill x -side bottom
40
41    frame $mw.frame
42    pack $mw.frame -side top -fill both -expand yes -padx 2m -pady 2m
43
44    label $mw.prompt
45    pack $mw.prompt -side top -anchor w -in $mw.frame
46
47    variable $mw.prompt fulljid
48    bind $mw <Destroy> [list catch [list unset [namespace current]::%W.prompt]]
49
50    ::xmpp::sendIQ $xlib get \
51        -query [::xmpp::xml::create query \
52		    -xmlns jabber:iq:gateway] \
53	-to $service \
54        -command [list [namespace current]::configure_prompt $mw.prompt]
55
56    frame $mw.subj
57    label $mw.subj.lab -text [::msgcat::mc "Send subscription to: "]
58    entry $mw.subj.entry
59    pack $mw.subj.lab -side left
60    pack $mw.subj.entry -side left -fill x -expand yes
61    pack $mw.subj -side top -anchor w -fill x -expand yes -in $mw.frame
62
63    frame $mw.space
64    pack $mw.space -side top -fill x -in $mw.frame -pady 0.5m
65
66    ScrolledWindow $mw.sw
67    pack $mw.sw -side top -fill both -expand yes -in $mw.frame
68
69    textUndoable $mw.body -width 60 -height 8 -wrap word
70    $mw.body insert 0.0 [::msgcat::mc "I would like to add you to my roster."]
71    $mw.sw setwidget $mw.body
72
73    focus $mw.subj.entry
74
75    incr msgid
76}
77
78proc gateway::configure_prompt {w res child} {
79    if {![winfo exists $w]} return
80
81    $w configure \
82	-text [::msgcat::mc "Enter screenname of contact you want to add"]
83
84    if {$res != "ok"} {
85	return
86    }
87
88    ::xmpp::xml::split $child tag xmlns attrs cdata subels
89
90    foreach ch $subels {
91	::xmpp::xml::split $ch stag sxmlns sattrs scdata ssubels
92
93	if {($stag == "desc") && ($scdata != "")} {
94	    $w configure -text $scdata
95	    variable $w screenname
96	    break
97	}
98    }
99}
100
101proc gateway::send_subscribe {mw xlib service} {
102    variable $mw.prompt
103
104    switch -- [set $mw.prompt] {
105	fulljid {
106	    $mw.subj.entry insert end "@$service"
107	    message::send_subscribe $mw $xlib
108	}
109	screenname {
110	    set screenname [$mw.subj.entry get]
111	    ::xmpp::sendIQ $xlib set \
112		-query [::xmpp::xml::create query \
113			    -xmlns jabber:iq:gateway \
114			    -subelement [::xmpp::xml::create prompt \
115					    -cdata $screenname]] \
116		-to $service \
117		-command [list [namespace current]::gw_send_subscribe $mw \
118			       "$screenname@$service" \
119			       $xlib]
120	}
121    }
122}
123
124proc gateway::gw_send_subscribe {mw fallback xlib res child} {
125
126    set jid $fallback
127
128    if {$res == "ok"} {
129	::xmpp::xml::split $child tag xmlns attrs cdata subels
130
131	foreach ch $subels {
132	    ::xmpp::xml::split $ch stag sxmlns sattrs scdata ssubels
133
134	    if {($stag == "jid" || ($stag == "prompt")) && \
135		    ($scdata != "")} {
136		set jid $scdata
137		break
138	    }
139	}
140    }
141
142    $mw.subj.entry delete 0 end
143    $mw.subj.entry insert 0 $jid
144    message::send_subscribe $mw $xlib
145}
146
147
148proc gateway::convert_jid {xlib jid args} {
149
150    ::xmpp::sendIQ $xlib get \
151        -query [::xmpp::xml::create query \
152		    -xmlns jabber:iq:gateway] \
153	-to $jid \
154        -command [list [namespace current]::convert_jid_dialog \
155		       $xlib $jid]
156}
157
158proc gateway::convert_jid_dialog {xlib jid res child} {
159    variable msgid
160
161    if {$res != "ok"} return
162
163    set w .gwmsg$msgid
164
165    Dialog $w -title [::msgcat::mc "Screenname conversion"] \
166        -separator 1 -anchor e -modal none \
167	-default 0 -cancel 1
168
169    set f [$w getframe]
170
171    $w add -text [::msgcat::mc "Convert"] \
172        -command [list [namespace current]::convert_screenname $w $xlib $jid]
173    $w add -text [::msgcat::mc "Cancel"] -command [list destroy $w]
174
175    ::xmpp::xml::split $child tag xmlns attrs cdata subels
176
177    set row 0
178    foreach ch $subels {
179	::xmpp::xml::split $ch stag sxmlns sattrs scdata ssubels
180
181	switch -- $stag {
182	    desc {
183		if {![winfo exists $f.desc]} {
184		    message $f.desc -text $scdata -width 15c
185		    grid $f.desc -row $row -column 0 \
186			-columnspan 2 -sticky w -pady 2m
187		}
188	    }
189	    prompt {
190		if {![winfo exists $f.prompt]} {
191		    label $f.lprompt -text [::msgcat::mc "Screenname:"]
192		    entry $f.prompt
193		    grid $f.lprompt -row $row -column 0
194		    grid $f.prompt -row $row -column 1 -sticky ew -padx 1m
195		}
196	    }
197	}
198	incr row
199    }
200
201    incr msgid
202
203    $w draw $f.prompt
204}
205
206proc gateway::convert_screenname {w xlib jid} {
207    set f [$w getframe]
208    set screenname [$f.prompt get]
209
210    destroy $w
211
212    ::xmpp::sendIQ $xlib set \
213	-query [::xmpp::xml::create query \
214		    -xmlns jabber:iq:gateway \
215		    -subelement [::xmpp::xml::create prompt \
216				    -cdata $screenname]] \
217	-to $jid \
218	-command [list [namespace current]::display_conversion $w $screenname]
219}
220
221proc gateway::display_conversion {w screenname res child} {
222
223    if {$res != "ok"} {
224	NonmodalMessageDlg $w -aspect 50000 -icon error \
225	    -message [::msgcat::mc "Error while converting screenname: %s." \
226			  [error_to_string $child]]
227    } else {
228	set jid ""
229
230	::xmpp::xml::split $child tag xmlns attrs cdata subels
231
232	foreach ch $subels {
233	    ::xmpp::xml::split $ch stag sxmlns sattrs scdata ssubels
234
235	    if {($stag == "jid" || ($stag == "prompt")) && \
236		    ($scdata != "")} {
237		set jid $scdata
238		break
239	    }
240	}
241
242	NonmodalMessageDlg $w -aspect 50000 \
243	    -title [::msgcat::mc "Screenname conversion"] \
244	    -message [::msgcat::mc "Screenname: %s\n\nConverted JID: %s" \
245			  $screenname $jid]
246    }
247}
248
249hook::add postload_hook \
250    [list disco::browser::register_feature_handler jabber:iq:gateway \
251	  [namespace current]::gateway::convert_jid \
252	  -desc [list * [::msgcat::mc "Convert screenname"]]]
253
254