]> code.delx.au - gnu-emacs-elpa/blob - lisp/enwc-wicd.el
f946b41f8da66b8c2a73b7d3147241c4edbda181
[gnu-emacs-elpa] / lisp / enwc-wicd.el
1 ;;; enwc-wicd.el --- The Wicd backend to ENWC
2
3 ;; Copyright (C) 2012,2013 Free Software Foundation
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
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 2000
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 (defun enwc-wicd-get-wireless-network-property (id prop)
100 "Wicd get wireless network property function.
101 This calls the D-Bus method on Wicd to get the property PROP
102 from wireless network with id ID."
103 (enwc-wicd-dbus-wireless-call-method "GetWirelessProperty"
104 id prop))
105
106 (defun enwc-wicd-get-wireless-nw-props (id)
107 (mapcar (lambda (x)
108 (cons x (enwc-wicd-get-wireless-network-property id x)))
109 enwc-wicd-details-list))
110
111 (defun enwc-wicd-get-encryption-type (id)
112 "Wicd get encryption type function.
113 This calls the D-Bus method on Wicd to get the encryption_method
114 property from wireless network with id ID."
115 (enwc-wicd-dbus-wireless-call-method "GetWirelessProperty"
116 id "encryption_method"))
117
118 (defun enwc-wicd-connect (id)
119 "Wicd connect function.
120 This calls the D-Bus method on Wicd to connect to wireless
121 network with id ID."
122 (enwc-wicd-dbus-wireless-call-method "ConnectWireless" id))
123
124 (defun enwc-wicd-get-current-nw-id (wired)
125 "Wicd get current network id function.
126 This calls the D-Bus method on Wicd to get the current
127 wireless network id."
128 ;;(enwc-wicd-dbus-wireless-call-method "GetCurrentNetworkID"))
129 (if wired
130 -1
131 enwc-wicd-current-nw-id))
132
133 (defun enwc-wicd-check-connecting ()
134 "The Wicd check connecting function."
135 (enwc-wicd-dbus-wireless-call-method "CheckIfWirelessConnecting"))
136
137 (defun enwc-wicd-disconnect ()
138 "Wicd disconnect function."
139 (enwc-wicd-dbus-wireless-call-method "DisconnectWireless"))
140
141 (defun enwc-wicd-get-wired-profiles ()
142 "Gets the list of wired network profiles."
143 (enwc-wicd-dbus-wired-call-method "GetWiredProfileList"))
144
145 (defun enwc-wicd-wired-connect (id)
146 "Connects to the wired network with profile id ID."
147 (let* ((profs (enwc-get-wired-profiles))
148 (prf (nth id profs)))
149 (enwc-wicd-dbus-wired-call-method "ReadWiredNetworkProfile" prf)
150 (enwc-wicd-dbus-wired-call-method "ConnectWired")))
151
152 (defun enwc-wicd-wired-disconnect ()
153 "Disconnects from the wired connection."
154 (enwc-wicd-dbus-wired-call-method "DisconnectWired"))
155
156 (defun enwc-wicd-is-wired ()
157 "Checks to see if wired is connected."
158 (not (not (enwc-wicd-dbus-wired-call-method "GetWiredIP"))))
159
160 (defun enwc-wicd-get-wired-nw-prop (id det)
161 "Gets property DET from the wired network with id ID."
162 (enwc-wicd-dbus-wired-call-method "GetWiredProperty" id det))
163
164 ;; Each entry in sec-types should be:
165 ;; ("IDENT" (("Name" . "NAME") ("reqs" . (("key1" . "Entry1") ("key2" . "Entry2") ... ))))
166 ;; Where:
167 ;; "IDENT" => String that identifies this to the backend.
168 ;; "NAME" => String that ENWC displays
169 ;; "reqs" => Constant string, but the association list holds entries
170 ;; required by the security type, i.e. user, passphrase, etc.
171 ;; "keyXX" => String that the backend uses for this security entry.
172 ;; "EntryXX" => String that ENWC displays for this security entry.
173
174 (defun enwc-wicd-get-sec-types (wired)
175 "Gets the list of security types.
176 WIRED indicates whether this is a wired connection.
177 The returned list will be in the format:
178 (name . ((\"Name\" . \"DISPLAY-NAME\")
179 (\"reqs\" . ((\"Display\" . \"id\") ...))))"
180 (let (sec-types
181 ret-list)
182 (with-temp-buffer
183 (insert-file-contents (concat "/etc/wicd/encryption/templates/active"
184 (if wired
185 "_wired")))
186 (setq sec-types (split-string (buffer-string) "\n")))
187 (setq ret-list
188 (mapcar (lambda (x)
189 (if (not (eq (length x) 0))
190 (let (name reqs)
191 (with-temp-buffer
192 (insert-file-contents (concat "/etc/wicd/encryption/templates/"
193 x))
194 (re-search-forward "name[ \t]*=[ \t]*\\([^\n]*\\)[\n]")
195 (setq name (match-string 1))
196 (re-search-forward "require[ \t]*\\([^\n]*\\)[\n]")
197 (let ((str-reqs (split-string (match-string 1) " ")))
198 (while str-reqs
199 (setq reqs
200 (append reqs
201 (cons (cons (pop str-reqs)
202 (pop str-reqs))
203 nil)))))
204 (cons x (cons (cons "Name" name) (cons (cons "reqs" (cons reqs nil)) nil)))
205 ))))
206 sec-types))))
207
208 (defun enwc-wicd-get-profile-ent (wired id ent)
209 "Gets profile entry ENT from the network with id ID.
210 WIRED is set to indicate whether this is a wired network.
211 This function is a wrapper around the *-get-(wired|wireless)-nw-prop
212 functions, allowing for a single function that checks for wired."
213 (if wired
214 (enwc-wicd-get-wired-nw-prop id ent)
215 (enwc-wicd-get-wireless-network-property id ent)))
216
217 (defun enwc-wicd-get-nw-info (wired id)
218 (let ((dns-list (enwc-wicd-get-dns wired id)))
219 (list (cons (cons "addr" (enwc-wicd-get-ip-addr wired id)) nil)
220 (cons (cons "netmask" (enwc-wicd-get-netmask wired id)) nil)
221 (cons (cons "gateway" (enwc-wicd-get-gateway wired id)) nil)
222 (cons (cons "dns1" (nth 0 dns-list)) nil)
223 (cons (cons "dns2" (nth 1 dns-list)) nil))))
224
225 (defun enwc-wicd-get-ip-addr (wired id)
226 "Gets the IP Address from the network with id ID.
227 Wired is set to indicate whether this is a wired network."
228 (or (enwc-wicd-get-profile-ent wired id "ip") ""))
229
230 (defun enwc-wicd-get-netmask (wired id)
231 "Gets the Netmask from the network with id ID.
232 WIRED is set to indicate whether this is a wired network."
233 (or (enwc-wicd-get-profile-ent wired id "netmask") ""))
234
235 (defun enwc-wicd-get-gateway (wired id)
236 "Gets the Gateway from the network with id ID.
237 WIRED is set to indicate whether this is a wired network."
238 (or (enwc-wicd-get-profile-ent wired id "gateway") ""))
239
240 (defun enwc-wicd-get-dns (wired id)
241 "Gets the list of DNS servers from the network with id ID.
242 WIRED is set to indicate whether this is a wired network."
243 (list (or (enwc-wicd-get-profile-ent wired id "dns1") "")
244 (or (enwc-wicd-get-profile-ent wired id "dns2") "")
245 (or (enwc-wicd-get-profile-ent wired id "dns3") "")))
246
247 (defun enwc-wicd-set-nw-prop (wired id prop val)
248 "Sets the network property PROP of the network with id ID
249 to VAL.
250 WIRED indicates whether this is a wired network."
251 (if wired
252 (enwc-wicd-dbus-wired-call-method "SetWiredProperty"
253 id prop val)
254 (enwc-wicd-dbus-wireless-call-method "SetWirelessProperty"
255 id prop val)))
256
257 (defun enwc-wicd-save-nw-profile (wired id)
258 "Save the network profile with for the network with id ID.
259 WIRED indicates whether this is a wired network."
260 (if wired
261 (enwc-wicd-dbus-wired-call-method "SaveWiredNetworkProfile" id)
262 (enwc-wicd-dbus-wireless-call-method "SaveWirelessNetworkProfile" id)))
263
264 (defun enwc-wicd-save-nw-settings (wired id settings)
265 "Saves the settings indicated by the association list SETTINGS for
266 the network with id ID."
267 (let ((enctype (cdr (assoc "enctype" settings))))
268
269 (enwc-wicd-set-nw-prop wired id "ip"
270 (cdr (assoc "addr" settings)))
271 (enwc-wicd-set-nw-prop wired id "netmask"
272 (cdr (assoc "netmask" settings)))
273 (enwc-wicd-set-nw-prop wired id "gateway"
274 (cdr (assoc "gateway" settings)))
275
276 (enwc-wicd-set-nw-prop wired id "dns1"
277 (cdr (assoc "dns1" settings)))
278 (enwc-wicd-set-nw-prop wired id "dns2"
279 (cdr (assoc "dns2" settings)))
280
281 (enwc-wicd-set-nw-prop wired id "enctype" enctype)
282 (if (not (string= enctype "None"))
283 (dolist (x (cadr (assoc "reqs"
284 (cdr (assoc enctype
285 (enwc-wicd-get-sec-types wired))))))
286 (enwc-wicd-set-nw-prop wired id (car x)
287 (cdr (assoc (car x) settings)))))
288 (enwc-wicd-save-nw-profile wired id))
289 )
290
291 (defun enwc-wicd-wireless-prop-changed (state info)
292 (if state
293 (if (eq state 0)
294 (setq enwc-wicd-current-ap ""
295 enwc-wicd-current-nw-id -1)
296 (setq enwc-wicd-current-ap (caadr info)
297 enwc-wicd-current-nw-id (or (and info
298 (string-to-number (caar (cdddr info))))
299 -1)))
300 ))
301
302 (defun enwc-wicd-setup ()
303 ;; Thanks to Michael Albinus for pointing out this signal.
304 (dbus-register-signal :system
305 enwc-wicd-dbus-service
306 enwc-wicd-dbus-wireless-path
307 enwc-wicd-dbus-wireless-interface
308 "SendEndScanSignal"
309 'enwc-process-scan)
310
311 (dbus-register-signal :system
312 enwc-wicd-dbus-service
313 "/org/wicd/daemon"
314 enwc-wicd-dbus-service
315 "StatusChanged"
316 'enwc-wicd-wireless-prop-changed)
317 )
318
319 (provide 'enwc-wicd)
320
321 ;;; End of File