File: PrefNet.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (307 lines) | stat: -rw-r--r-- 10,603 bytes parent folder | download | duplicates (4)
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
#  PrefNet.tcl ---
#  
#       This file is part of The Coccinella application. 
#       It implements network prefs dialogs and panel.
#      
#  Copyright (c) 2005-2007  Mats Bengtsson
#  
#   This program 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 3 of the License, or
#   (at your option) any later version.
#   
#   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#  
# $Id: PrefNet.tcl,v 1.13 2008-08-21 07:27:27 matben Exp $
 
package provide PrefNet 1.0

namespace eval ::PrefNet:: {

    ::hooks::register prefsInitHook          ::PrefNet::InitPrefsHook  20
    ::hooks::register prefsBuildHook         ::PrefNet::BuildPrefsHook 20
    ::hooks::register prefsSaveHook          ::PrefNet::SavePrefsHook  20
    ::hooks::register prefsCancelHook        ::PrefNet::CancelPrefsHook
    ::hooks::register prefsUserDefaultsHook  ::PrefNet::UserDefaultsHook
}

proc ::PrefNet::InitPrefsHook { } {
    global  prefs jprefs this
        
    # ip numbers, port numbers, and names.
    set prefs(thisServPort) 8235

    # The tinyhttpd server port number and base directory.
    set prefs(httpdPort) 8077
    
    set jprefs(tls,usecertfile) 0
    set jprefs(tls,certfile) ""
    set jprefs(tls,usekeyfile) 0
    set jprefs(tls,keyfile) ""
    set jprefs(tls,usecafile) 0
    set jprefs(tls,cafile) [file join $this(certificatesPath) cacerts.pem]

    ::PrefUtils::Add [list  \
      [list prefs(thisServPort)    prefs_thisServPort    $prefs(thisServPort)]   \
      [list prefs(httpdPort)       prefs_httpdPort       $prefs(httpdPort)]      \
      [list jprefs(tls,usecertfile)  jprefs_tls_usecertfile  $jprefs(tls,usecertfile)]   \
      [list jprefs(tls,certfile)     jprefs_tls_certfile     $jprefs(tls,certfile)]   \
      [list jprefs(tls,usekeyfile)   jprefs_tls_usekeyfile   $jprefs(tls,usekeyfile)]   \
      [list jprefs(tls,keyfile)      jprefs_tls_keyfile      $jprefs(tls,keyfile)]   \
      [list jprefs(tls,usecafile)   jprefs_tls_usecafile   $jprefs(tls,usecafile)]   \
      [list jprefs(tls,cafile)      jprefs_tls_cafile      $jprefs(tls,cafile)]   \
      ]    
    # in case the default certificate location is used, check whether the default 
    # certificate file is in place, if not, copy it.
    if { $jprefs(tls,cafile) eq [file join $this(certificatesPath) cacerts.pem]} {
        if {![file exists $jprefs(tls,cafile)]} { 
            file mkdir $this(certificatesPath)
	    if {[info exists ::starkit::topdir]} {
		# This file should always be available in the binary, no need to catch an exception here
	        file copy [file join $::starkit::topdir lib app-Coccinella certificates cacerts.pem] $jprefs(tls,cafile)
	    } else {
    		if {[catch {
        	    file copy [file join $this(appPath) certificates cacerts.pem] $jprefs(tls,cafile)
    		} err]} {
        	    ::Debug 2 "::PrefNet::InitPrefsHook: $err"
    		}
	    }
	}
    }
}

proc ::PrefNet::BuildPrefsHook {wtree nbframe} {
    
    if {![::Preferences::HaveTableItem General]} {
	::Preferences::NewTableItem {General} [mc "General"]
    }
    ::Preferences::NewTableItem {General {Network}} [mc "Network"]
    set wpage [$nbframe page {Network}]
    BuildTabPage $wpage
}

proc ::PrefNet::SavePrefsHook { } {
    ::Proxy::SavePrefsHook
    ServersSaveHook
}

proc ::PrefNet::CancelPrefsHook { } {
    ::Proxy::CancelPrefsHook
    NetworkCancelHook
}

proc ::PrefNet::UserDefaultsHook { } {
    ::Proxy::UserDefaultsHook
    ServersUserDefaultsHook
}

proc ::PrefNet::BuildTabPage {wpage} {
    
    set anchor [option get . dialogAnchor {}]
    set padding [option get . notebookPageSmallPadding {}]
    
    set wc $wpage.c
    ttk::frame $wc -padding $padding
    pack $wc -side top -anchor $anchor
 
    set nb $wc.nb
    ttk::notebook $nb -padding {8 8 8 4}
    pack $nb -side top -anchor $anchor
    
    ::Proxy::BuildFrame $nb.proxy
    $nb.proxy configure -padding $padding
    $nb add $nb.proxy -text [mc "Proxy"] -sticky news
    
    ::Proxy::BuildNATFrame $nb.nat
    $nb.nat configure -padding $padding
    $nb add $nb.nat -text [mc "NAT"] -sticky news
     
    BuildServersFrame $nb.serv
    $nb.serv configure -padding $padding
    $nb add $nb.serv -text [mc "Ports"] -sticky news

    BuildCertFrame $nb.cert
    $nb.cert configure -padding $padding
    $nb add $nb.cert -text [mc "Certificates"] -sticky news

    return $wpage
}
    
proc ::PrefNet::BuildServersFrame {w} {
    global  this prefs jprefs

    variable tmpServPrefs
    
    set tmpServPrefs(thisServPort)      $prefs(thisServPort)
    set tmpServPrefs(httpdPort)         $prefs(httpdPort)
    set tmpServPrefs(bytestreams,port)  $jprefs(bytestreams,port)

    ttk::frame $w
    
    set f $w.f
    ttk::frame $f
    pack $f -side top -anchor [option get . dialogAnchor {}]

    ttk::label $f.lserv -text [mc "Built in server"]:
    ttk::label $f.lhttp -text "HTTP:"
    ttk::label $f.lbs   -text [mc "File transfer"]:

    ttk::entry $f.eserv -width 6 -textvariable  \
      [namespace current]::tmpServPrefs(thisServPort)
    ttk::entry $f.ehttp -width 6 -textvariable  \
      [namespace current]::tmpServPrefs(httpdPort)
    ttk::entry $f.ebs -width 6 -textvariable  \
      [namespace current]::tmpServPrefs(bytestreams,port)

    grid  $f.lserv  $f.eserv  -sticky e -pady 2
    grid  $f.lhttp  $f.ehttp  -sticky e -pady 2
    grid  $f.lbs    $f.ebs    -sticky e -pady 2
    grid columnconfigure $f 0 -weight 1
    
    if {!$prefs(haveHttpd)} {
	$f.ehttp state {disabled}
    }
    bind $w <Destroy> [namespace code ServersFree]
    return $w
}

proc ::PrefNet::BuildCertFrame {w} {
    global  this prefs jprefs
    variable tmpCertPrefs
    
    set tmpCertPrefs(usecertfile) $jprefs(tls,usecertfile)
    set tmpCertPrefs(certfile)    $jprefs(tls,certfile)
    set tmpCertPrefs(usekeyfile)  $jprefs(tls,usekeyfile)
    set tmpCertPrefs(keyfile)     $jprefs(tls,keyfile)
    set tmpCertPrefs(usecafile)   $jprefs(tls,usecafile)
    set tmpCertPrefs(cafile)      $jprefs(tls,cafile)
    
    ttk::frame $w
    
    set f $w.f
    ttk::frame $f
    pack $f -side top -anchor [option get . dialogAnchor {}]
    
    ttk::checkbutton $f.ccert -text [mc "TLS certificate file"] \
      -variable [namespace current]::tmpCertPrefs(usecertfile)
    ttk::entry $f.ecert -textvariable [namespace current]::tmpCertPrefs(certfile)
    ttk::button $f.bcert -text [mc "Browse"]... \
      -command [namespace code BrowseCertFile]
    
    ttk::checkbutton $f.ckey -text [mc "TLS private key file"] \
      -variable [namespace current]::tmpCertPrefs(usekeyfile)
    ttk::entry $f.ekey -textvariable [namespace current]::tmpCertPrefs(keyfile)
    ttk::button $f.bkey -text [mc "Browse"]... \
      -command [namespace code BrowseKeyFile]    
    
    ttk::checkbutton $f.cacert -text [mc "TLS CA certificate file"] \
      -variable [namespace current]::tmpCertPrefs(usecafile)
    ttk::entry $f.ecacert -textvariable [namespace current]::tmpCertPrefs(cafile)
    ttk::button $f.bcacert -text [mc "Browse"]... \
      -command [namespace code BrowseCACertFile]    

    grid  $f.ccert  -  -sticky w
    grid  $f.ecert  $f.bcert
    grid $f.ecert -sticky ew
    
    grid  $f.ckey  -  -sticky w
    grid  $f.ekey  $f.bkey
    grid $f.ekey -sticky ew
        
    grid  $f.cacert  -  -sticky w
    grid  $f.ecacert  $f.bcacert
    grid $f.ecacert -sticky ew
    return $w
}

proc ::PrefNet::BrowseCertFile {} {
    variable tmpCertPrefs
    
    set fileName [tk_getOpenFile -title [mc "TLS certificate file"] -filetypes {}]
    if {[file exists $fileName]} {
	set tmpCertPrefs(certfile) $fileName
    }
}

proc ::PrefNet::BrowseKeyFile {} {
    variable tmpCertPrefs
    
    set fileName [tk_getOpenFile -title [mc "TLS private key file"] -filetypes {}]
    if {[file exists $fileName]} {
	set tmpCertPrefs(keyfile) $fileName
    }
}

proc ::PrefNet::BrowseCACertFile {} {
    variable tmpCertPrefs
    
    set fileName [tk_getOpenFile -title [mc "TLS CA certificate file"] -filetypes {}]
    if {[file exists $fileName]} {
	set tmpCertPrefs(cafile) $fileName
    }
}

proc ::PrefNet::ServersSaveHook {} {
    global  prefs jprefs
    variable tmpServPrefs
    variable tmpCertPrefs

    set prefs(thisServPort)      $tmpServPrefs(thisServPort)
    set prefs(httpdPort)         $tmpServPrefs(httpdPort)
    set jprefs(bytestreams,port) $tmpServPrefs(bytestreams,port)   
    
    foreach key {usecertfile certfile usekeyfile keyfile usecafile cafile} {
	set jprefs(tls,$key) $tmpCertPrefs($key)
    }
    if {$jprefs(tls,usecertfile)} {
	::Jabber::Jlib tls_configure -certfile $jprefs(tls,certfile)
    }
    if {$jprefs(tls,usekeyfile)} {
	::Jabber::Jlib tls_configure -keyfile $jprefs(tls,keyfile)
    }
    if {$jprefs(tls,usecafile)} {
	::Jabber::Jlib tls_configure -cafile $jprefs(tls,cafile)
    }
}

proc ::PrefNet::NetworkCancelHook {} {
    global  prefs jprefs
    variable tmpServPrefs
    variable tmpCertPrefs

    if {![string equal $prefs(thisServPort) $tmpServPrefs(thisServPort)] || \
      ![string equal $prefs(httpdPort) $tmpServPrefs(httpdPort)] || \
      ![string equal $jprefs(bytestreams,port) $tmpServPrefs(bytestreams,port)]} {
	::Preferences::HasChanged
      }
    if {![string equal $jprefs(tls,usecertfile) $tmpCertPrefs(usecertfile)] || \
      ![string equal $jprefs(tls,certfile) $tmpCertPrefs(certfile)] || \
      ![string equal $jprefs(tls,usekeyfile) $tmpCertPrefs(usekeyfile)] || \
      ![string equal $jprefs(tls,keyfile) $tmpCertPrefs(keyfile)] || \
      ![string equal $jprefs(tls,usecafile) $tmpCertPrefs(usecafile)] || \
      ![string equal $jprefs(tls,cafile) $tmpCertPrefs(cafile)]} {
	::Preferences::HasChanged
     }
}

proc ::PrefNet::ServersUserDefaultsHook {} {
    global  prefs jprefs
    variable tmpServPrefs

    set tmpServPrefs(thisServPort)      $prefs(thisServPort)
    set tmpServPrefs(httpdPort)         $prefs(httpdPort)
    set tmpServPrefs(bytestreams,port)  $jprefs(bytestreams,port)    
}

proc ::PrefNet::ServersFree {} {
    variable tmpServPrefs
    unset -nocomplain tmpServPrefs
}
    
#-------------------------------------------------------------------------------