1 ;;; enwc-wicd.el --- The Wicd backend to ENWC
3 ;; Copyright (C) 2012,2013 Free Software Foundation, Inc.
6 ;; Keywords: enwc, network, wicd, manager, nm
8 ;; This file is part of ENWC
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)
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.
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
31 (defgroup enwc-wicd nil
32 "*Wicd variables for ENWC."
36 (defcustom enwc-wicd-dbus-service "org.wicd.daemon"
37 "The wicd D-Bus service identifier."
41 (defcustom enwc-wicd-dbus-wireless-path "/org/wicd/daemon/wireless"
42 "The wicd wireless D-Bus path."
46 (defcustom enwc-wicd-dbus-wireless-interface "org.wicd.daemon.wireless"
47 "The wicd wireless D-Bus interface."
51 (defcustom enwc-wicd-dbus-wired-path "/org/wicd/daemon/wired"
52 "The wicd wired D-Bus path."
56 (defcustom enwc-wicd-dbus-wired-interface "org.wicd.daemon.wired"
57 "The wicd wired D-Bus interface."
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.")
65 (defvar enwc-wicd-current-ap "")
67 (defvar enwc-wicd-current-nw-id -1)
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
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
91 (defun enwc-wicd-scan ()
93 (enwc-wicd-dbus-wireless-call-method "Scan"))
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"))))
99 (defvar enwc-wicd-prop-values nil)
100 (defvar enwc-wicd-prop-num 0)
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)))
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
119 ;;(enwc-wicd-dbus-wireless-call-method "GetWirelessProperty" id prop)
122 (defun enwc-wicd-build-prop-list (prop-list det-list)
124 (act-det-list (reverse det-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)))
132 (defun enwc-wicd-get-wireless-nw-props (id)
133 (setq enwc-wicd-prop-values nil)
134 (setq enwc-wicd-prop-num 0)
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))
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"))
150 (defun enwc-wicd-connect (id)
151 "Wicd connect function.
152 This calls the D-Bus method on Wicd to connect to wireless
154 (enwc-wicd-dbus-wireless-call-method "ConnectWireless" id))
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"))
163 enwc-wicd-current-nw-id))
165 (defun enwc-wicd-check-connecting ()
166 "The Wicd check connecting function."
167 (enwc-wicd-dbus-wireless-call-method "CheckIfWirelessConnecting"))
169 (defun enwc-wicd-disconnect ()
170 "Wicd disconnect function."
171 (enwc-wicd-dbus-wireless-call-method "DisconnectWireless"))
173 (defun enwc-wicd-get-wired-profiles ()
174 "Gets the list of wired network profiles."
175 (enwc-wicd-dbus-wired-call-method "GetWiredProfileList"))
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")))
184 (defun enwc-wicd-wired-disconnect ()
185 "Disconnects from the wired connection."
186 (enwc-wicd-dbus-wired-call-method "DisconnectWired"))
188 (defun enwc-wicd-is-wired ()
189 "Checks to see if wired is connected."
190 (not (not (enwc-wicd-dbus-wired-call-method "GetWiredIP"))))
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))
196 ;; Each entry in sec-types should be:
197 ;; ("IDENT" (("Name" . "NAME") ("reqs" . (("key1" . "Entry1") ("key2" . "Entry2") ... ))))
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.
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\") ...))))"
215 (insert-file-contents (concat "/etc/wicd/encryption/templates/active"
218 (setq sec-types (split-string (buffer-string) "\n")))
221 (if (not (eq (length x) 0))
224 (insert-file-contents (concat "/etc/wicd/encryption/templates/"
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) " ")))
233 (cons (cons (pop str-reqs)
236 (cons x (cons (cons "Name" name) (cons (cons "reqs" (cons reqs nil)) nil)))
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."
246 (enwc-wicd-get-wired-nw-prop id ent)
247 (enwc-wicd-dbus-wireless-call-method "GetWirelessProperty" id ent)))
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))))
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") ""))
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") ""))
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") ""))
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") "")))
279 (defun enwc-wicd-set-nw-prop (wired id prop val)
280 "Sets the network property PROP of the network with id ID
282 WIRED indicates whether this is a wired network."
284 (enwc-wicd-dbus-wired-call-method "SetWiredProperty"
286 (enwc-wicd-dbus-wireless-call-method "SetWirelessProperty"
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."
293 (enwc-wicd-dbus-wired-call-method "SaveWiredNetworkProfile" id)
294 (enwc-wicd-dbus-wireless-call-method "SaveWirelessNetworkProfile" id)))
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))))
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)))
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)))
313 (enwc-wicd-set-nw-prop wired id "enctype" enctype)
314 (if (not (string= enctype "None"))
315 (dolist (x (cadr (assoc "reqs"
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))
323 (defun enwc-wicd-wireless-prop-changed (state info)
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
331 (caar (nthcdr 3 info))
332 (string-to-number (caar (nthcdr 3 info))))
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
345 (dbus-register-signal :system
346 enwc-wicd-dbus-service
348 enwc-wicd-dbus-service
350 'enwc-wicd-wireless-prop-changed)