]> code.delx.au - gnu-emacs-elpa/blob - packages/enwc/enwc-wicd.el
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / enwc / enwc-wicd.el
1 ;;; enwc-wicd.el --- The Wicd backend to ENWC
2
3 ;; Copyright (C) 2012,2013 Free Software Foundation, Inc.
4
5 ;; Author: Ian Dunn
6 ;; Keywords: enwc, network, wicd, manager, nm
7
8 ;; This file is part of ENWC
9
10 ;; ENWC is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; ENWC is distributed in the hope that it will be useful, but WITHOUT
16 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 ;; License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with ENWC; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 ;; 02110-1301, USA.
24
25
26 ;;; Commentary:
27 ;; TODO
28
29 (require 'enwc)
30
31 (defgroup enwc-wicd nil
32 "*Wicd variables for ENWC."
33 :prefix "enwc-wicd-"
34 :group 'enwc)
35
36 (defcustom enwc-wicd-dbus-service "org.wicd.daemon"
37 "The wicd D-Bus service identifier."
38 :group 'enwc-wicd
39 :type 'string)
40
41 (defcustom enwc-wicd-dbus-wireless-path "/org/wicd/daemon/wireless"
42 "The wicd wireless D-Bus path."
43 :group 'enwc-wicd
44 :type 'string)
45
46 (defcustom enwc-wicd-dbus-wireless-interface "org.wicd.daemon.wireless"
47 "The wicd wireless D-Bus interface."
48 :group 'enwc-wicd
49 :type 'string)
50
51 (defcustom enwc-wicd-dbus-wired-path "/org/wicd/daemon/wired"
52 "The wicd wired D-Bus path."
53 :group 'enwc-wicd
54 :type 'string)
55
56 (defcustom enwc-wicd-dbus-wired-interface "org.wicd.daemon.wired"
57 "The wicd wired D-Bus interface."
58 :group 'enwc-wicd
59 :type 'string)
60
61 (defvar enwc-wicd-details-list
62 '("essid" "bssid" "quality" "encryption" "mode" "channel")
63 "The list of the desired details to be obtained from each network.")
64
65 (defvar enwc-wicd-current-ap "")
66
67 (defvar enwc-wicd-current-nw-id -1)
68
69 (defun enwc-wicd-dbus-wireless-call-method (method &rest args)
70 "Calls D-Bus method METHOD with arguments ARGS within
71 the wicd wireless interface."
72 (apply 'dbus-call-method :system
73 enwc-wicd-dbus-service
74 enwc-wicd-dbus-wireless-path
75 enwc-wicd-dbus-wireless-interface
76 method
77 :timeout 25000
78 args))
79
80 (defun enwc-wicd-dbus-wired-call-method (method &rest args)
81 "Calls D-Bus method METHOD with arguments ARGS within
82 the wicd wired interface."
83 (apply 'dbus-call-method :system
84 enwc-wicd-dbus-service
85 enwc-wicd-dbus-wired-path
86 enwc-wicd-dbus-wired-interface
87 method
88 :timeout 25000
89 args))
90
91 (defun enwc-wicd-scan ()
92 "Wicd scan function."
93 (enwc-wicd-dbus-wireless-call-method "Scan"))
94
95 (defun enwc-wicd-get-networks ()
96 "Wicd get networks function. Just returns a number sequence."
97 (number-sequence 0 (1- (enwc-wicd-dbus-wireless-call-method "GetNumberOfNetworks"))))
98
99 (defvar enwc-wicd-prop-values nil)
100 (defvar enwc-wicd-prop-num 0)
101
102 (defun enwc-wicd-nw-prop-handler (&rest args)
103 (setq enwc-wicd-prop-values (cons args enwc-wicd-prop-values))
104 (setq enwc-wicd-prop-num (1+ enwc-wicd-prop-num)))
105
106 (defun enwc-wicd-get-wireless-network-property (id prop)
107 "Wicd get wireless network property function.
108 This calls the D-Bus method on Wicd to get the property PROP
109 from wireless network with id ID."
110 (dbus-call-method-asynchronously :system
111 enwc-wicd-dbus-service
112 enwc-wicd-dbus-wireless-path
113 enwc-wicd-dbus-wireless-interface
114 "GetWirelessProperty"
115 'enwc-wicd-nw-prop-handler
116 :int32 id
117 :string prop)
118
119 ;;(enwc-wicd-dbus-wireless-call-method "GetWirelessProperty" id prop)
120 )
121
122 (defun enwc-wicd-build-prop-list (prop-list det-list)
123 (let (ret
124 (act-det-list (reverse det-list)))
125 (while prop-list
126 (let ((cur-prop (pop prop-list))
127 (cur-det (pop act-det-list)))
128 (setq ret (append ret (cons (cons cur-det (car cur-prop)) nil)))
129 ))
130 ret))
131
132 (defun enwc-wicd-get-wireless-nw-props (id)
133 (setq enwc-wicd-prop-values nil)
134 (setq enwc-wicd-prop-num 0)
135 (mapc (lambda (x)
136 (enwc-wicd-get-wireless-network-property id x))
137 enwc-wicd-details-list)
138 ;; Wait for less than a second.
139 (while (< enwc-wicd-prop-num 6)
140 (read-event nil nil 0.001))
141 (enwc-wicd-build-prop-list enwc-wicd-prop-values enwc-wicd-details-list))
142
143 (defun enwc-wicd-get-encryption-type (id)
144 "Wicd get encryption type function.
145 This calls the D-Bus method on Wicd to get the encryption_method
146 property from wireless network with id ID."
147 (enwc-wicd-dbus-wireless-call-method "GetWirelessProperty"
148 id "encryption_method"))
149
150 (defun enwc-wicd-connect (id)
151 "Wicd connect function.
152 This calls the D-Bus method on Wicd to connect to wireless
153 network with id ID."
154 (enwc-wicd-dbus-wireless-call-method "ConnectWireless" id))
155
156 (defun enwc-wicd-get-current-nw-id (wired)
157 "Wicd get current network id function.
158 This calls the D-Bus method on Wicd to get the current
159 wireless network id."
160 ;;(enwc-wicd-dbus-wireless-call-method "GetCurrentNetworkID"))
161 (if wired
162 -1
163 enwc-wicd-current-nw-id))
164
165 (defun enwc-wicd-check-connecting ()
166 "The Wicd check connecting function."
167 (enwc-wicd-dbus-wireless-call-method "CheckIfWirelessConnecting"))
168
169 (defun enwc-wicd-disconnect ()
170 "Wicd disconnect function."
171 (enwc-wicd-dbus-wireless-call-method "DisconnectWireless"))
172
173 (defun enwc-wicd-get-wired-profiles ()
174 "Gets the list of wired network profiles."
175 (enwc-wicd-dbus-wired-call-method "GetWiredProfileList"))
176
177 (defun enwc-wicd-wired-connect (id)
178 "Connects to the wired network with profile id ID."
179 (let* ((profs (enwc-get-wired-profiles))
180 (prf (nth id profs)))
181 (enwc-wicd-dbus-wired-call-method "ReadWiredNetworkProfile" prf)
182 (enwc-wicd-dbus-wired-call-method "ConnectWired")))
183
184 (defun enwc-wicd-wired-disconnect ()
185 "Disconnects from the wired connection."
186 (enwc-wicd-dbus-wired-call-method "DisconnectWired"))
187
188 (defun enwc-wicd-is-wired ()
189 "Checks to see if wired is connected."
190 (not (not (enwc-wicd-dbus-wired-call-method "GetWiredIP"))))
191
192 (defun enwc-wicd-get-wired-nw-prop (id det)
193 "Gets property DET from the wired network with id ID."
194 (enwc-wicd-dbus-wired-call-method "GetWiredProperty" id det))
195
196 ;; Each entry in sec-types should be:
197 ;; ("IDENT" (("Name" . "NAME") ("reqs" . (("key1" . "Entry1") ("key2" . "Entry2") ... ))))
198 ;; Where:
199 ;; "IDENT" => String that identifies this to the backend.
200 ;; "NAME" => String that ENWC displays
201 ;; "reqs" => Constant string, but the association list holds entries
202 ;; required by the security type, i.e. user, passphrase, etc.
203 ;; "keyXX" => String that the backend uses for this security entry.
204 ;; "EntryXX" => String that ENWC displays for this security entry.
205
206 (defun enwc-wicd-get-sec-types (wired)
207 "Gets the list of security types.
208 WIRED indicates whether this is a wired connection.
209 The returned list will be in the format:
210 (name . ((\"Name\" . \"DISPLAY-NAME\")
211 (\"reqs\" . ((\"Display\" . \"id\") ...))))"
212 (let (sec-types
213 ret-list)
214 (with-temp-buffer
215 (insert-file-contents (concat "/etc/wicd/encryption/templates/active"
216 (if wired
217 "_wired")))
218 (setq sec-types (split-string (buffer-string) "\n")))
219 (setq ret-list
220 (mapcar (lambda (x)
221 (if (not (eq (length x) 0))
222 (let (name reqs)
223 (with-temp-buffer
224 (insert-file-contents (concat "/etc/wicd/encryption/templates/"
225 x))
226 (re-search-forward "name[ \t]*=[ \t]*\\([^\n]*\\)[\n]")
227 (setq name (match-string 1))
228 (re-search-forward "require[ \t]*\\([^\n]*\\)[\n]")
229 (let ((str-reqs (split-string (match-string 1) " ")))
230 (while str-reqs
231 (setq reqs
232 (append reqs
233 (cons (cons (pop str-reqs)
234 (pop str-reqs))
235 nil)))))
236 (cons x (cons (cons "Name" name) (cons (cons "reqs" (cons reqs nil)) nil)))
237 ))))
238 sec-types))))
239
240 (defun enwc-wicd-get-profile-ent (wired id ent)
241 "Gets profile entry ENT from the network with id ID.
242 WIRED is set to indicate whether this is a wired network.
243 This function is a wrapper around the *-get-(wired|wireless)-nw-prop
244 functions, allowing for a single function that checks for wired."
245 (if wired
246 (enwc-wicd-get-wired-nw-prop id ent)
247 (enwc-wicd-dbus-wireless-call-method "GetWirelessProperty" id ent)))
248
249 (defun enwc-wicd-get-nw-info (wired id)
250 (let ((dns-list (enwc-wicd-get-dns wired id)))
251 (list (cons (cons "addr" (enwc-wicd-get-ip-addr wired id)) nil)
252 (cons (cons "netmask" (enwc-wicd-get-netmask wired id)) nil)
253 (cons (cons "gateway" (enwc-wicd-get-gateway wired id)) nil)
254 (cons (cons "dns1" (nth 0 dns-list)) nil)
255 (cons (cons "dns2" (nth 1 dns-list)) nil))))
256
257 (defun enwc-wicd-get-ip-addr (wired id)
258 "Gets the IP Address from the network with id ID.
259 Wired is set to indicate whether this is a wired network."
260 (or (enwc-wicd-get-profile-ent wired id "ip") ""))
261
262 (defun enwc-wicd-get-netmask (wired id)
263 "Gets the Netmask from the network with id ID.
264 WIRED is set to indicate whether this is a wired network."
265 (or (enwc-wicd-get-profile-ent wired id "netmask") ""))
266
267 (defun enwc-wicd-get-gateway (wired id)
268 "Gets the Gateway from the network with id ID.
269 WIRED is set to indicate whether this is a wired network."
270 (or (enwc-wicd-get-profile-ent wired id "gateway") ""))
271
272 (defun enwc-wicd-get-dns (wired id)
273 "Gets the list of DNS servers from the network with id ID.
274 WIRED is set to indicate whether this is a wired network."
275 (list (or (enwc-wicd-get-profile-ent wired id "dns1") "")
276 (or (enwc-wicd-get-profile-ent wired id "dns2") "")
277 (or (enwc-wicd-get-profile-ent wired id "dns3") "")))
278
279 (defun enwc-wicd-set-nw-prop (wired id prop val)
280 "Sets the network property PROP of the network with id ID
281 to VAL.
282 WIRED indicates whether this is a wired network."
283 (if wired
284 (enwc-wicd-dbus-wired-call-method "SetWiredProperty"
285 id prop val)
286 (enwc-wicd-dbus-wireless-call-method "SetWirelessProperty"
287 id prop val)))
288
289 (defun enwc-wicd-save-nw-profile (wired id)
290 "Save the network profile with for the network with id ID.
291 WIRED indicates whether this is a wired network."
292 (if wired
293 (enwc-wicd-dbus-wired-call-method "SaveWiredNetworkProfile" id)
294 (enwc-wicd-dbus-wireless-call-method "SaveWirelessNetworkProfile" id)))
295
296 (defun enwc-wicd-save-nw-settings (wired id settings)
297 "Saves the settings indicated by the association list SETTINGS for
298 the network with id ID."
299 (let ((enctype (cdr (assoc "enctype" settings))))
300
301 (enwc-wicd-set-nw-prop wired id "ip"
302 (cdr (assoc "addr" settings)))
303 (enwc-wicd-set-nw-prop wired id "netmask"
304 (cdr (assoc "netmask" settings)))
305 (enwc-wicd-set-nw-prop wired id "gateway"
306 (cdr (assoc "gateway" settings)))
307
308 (enwc-wicd-set-nw-prop wired id "dns1"
309 (cdr (assoc "dns1" settings)))
310 (enwc-wicd-set-nw-prop wired id "dns2"
311 (cdr (assoc "dns2" settings)))
312
313 (enwc-wicd-set-nw-prop wired id "enctype" enctype)
314 (if (not (string= enctype "None"))
315 (dolist (x (cadr (assoc "reqs"
316 (cdr (assoc enctype
317 (enwc-wicd-get-sec-types wired))))))
318 (enwc-wicd-set-nw-prop wired id (car x)
319 (cdr (assoc (car x) settings)))))
320 (enwc-wicd-save-nw-profile wired id))
321 )
322
323 (defun enwc-wicd-wireless-prop-changed (state info)
324 (if state
325 (if (eq state 0)
326 (setq enwc-wicd-current-ap ""
327 enwc-wicd-current-nw-id -1)
328 (setq enwc-wicd-current-ap (car (cadr info))
329 enwc-wicd-current-nw-id (or (and info
330 (nthcdr 3 info)
331 (caar (nthcdr 3 info))
332 (string-to-number (caar (nthcdr 3 info))))
333 -1)))
334 ))
335
336 (defun enwc-wicd-setup ()
337 ;; Thanks to Michael Albinus for pointing out this signal.
338 (dbus-register-signal :system
339 enwc-wicd-dbus-service
340 enwc-wicd-dbus-wireless-path
341 enwc-wicd-dbus-wireless-interface
342 "SendEndScanSignal"
343 'enwc-process-scan)
344
345 (dbus-register-signal :system
346 enwc-wicd-dbus-service
347 "/org/wicd/daemon"
348 enwc-wicd-dbus-service
349 "StatusChanged"
350 'enwc-wicd-wireless-prop-changed)
351 )
352
353 (provide 'enwc-wicd)
354
355 ;;; End of File