1 ;;; enwc-wicd.el --- The Wicd backend to ENWC
3 ;; Copyright (C) 2012,2013 Free Software Foundation
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 (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"
106 (defun enwc-wicd-get-wireless-nw-props (id)
108 (cons x (enwc-wicd-get-wireless-network-property id x)))
109 enwc-wicd-details-list))
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"))
118 (defun enwc-wicd-connect (id)
119 "Wicd connect function.
120 This calls the D-Bus method on Wicd to connect to wireless
122 (enwc-wicd-dbus-wireless-call-method "ConnectWireless" id))
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"))
131 enwc-wicd-current-nw-id))
133 (defun enwc-wicd-check-connecting ()
134 "The Wicd check connecting function."
135 (enwc-wicd-dbus-wireless-call-method "CheckIfWirelessConnecting"))
137 (defun enwc-wicd-disconnect ()
138 "Wicd disconnect function."
139 (enwc-wicd-dbus-wireless-call-method "DisconnectWireless"))
141 (defun enwc-wicd-get-wired-profiles ()
142 "Gets the list of wired network profiles."
143 (enwc-wicd-dbus-wired-call-method "GetWiredProfileList"))
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")))
152 (defun enwc-wicd-wired-disconnect ()
153 "Disconnects from the wired connection."
154 (enwc-wicd-dbus-wired-call-method "DisconnectWired"))
156 (defun enwc-wicd-is-wired ()
157 "Checks to see if wired is connected."
158 (not (not (enwc-wicd-dbus-wired-call-method "GetWiredIP"))))
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))
164 ;; Each entry in sec-types should be:
165 ;; ("IDENT" (("Name" . "NAME") ("reqs" . (("key1" . "Entry1") ("key2" . "Entry2") ... ))))
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.
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\") ...))))"
183 (insert-file-contents (concat "/etc/wicd/encryption/templates/active"
186 (setq sec-types (split-string (buffer-string) "\n")))
189 (if (not (eq (length x) 0))
192 (insert-file-contents (concat "/etc/wicd/encryption/templates/"
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) " ")))
201 (cons (cons (pop str-reqs)
204 (cons x (cons (cons "Name" name) (cons (cons "reqs" (cons reqs nil)) nil)))
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."
214 (enwc-wicd-get-wired-nw-prop id ent)
215 (enwc-wicd-get-wireless-network-property id ent)))
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))))
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") ""))
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") ""))
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") ""))
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") "")))
247 (defun enwc-wicd-set-nw-prop (wired id prop val)
248 "Sets the network property PROP of the network with id ID
250 WIRED indicates whether this is a wired network."
252 (enwc-wicd-dbus-wired-call-method "SetWiredProperty"
254 (enwc-wicd-dbus-wireless-call-method "SetWirelessProperty"
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."
261 (enwc-wicd-dbus-wired-call-method "SaveWiredNetworkProfile" id)
262 (enwc-wicd-dbus-wireless-call-method "SaveWirelessNetworkProfile" id)))
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))))
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)))
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)))
281 (enwc-wicd-set-nw-prop wired id "enctype" enctype)
282 (if (not (string= enctype "None"))
283 (dolist (x (cadr (assoc "reqs"
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))
291 (defun enwc-wicd-wireless-prop-changed (state info)
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))))
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
311 (dbus-register-signal :system
312 enwc-wicd-dbus-service
314 enwc-wicd-dbus-service
316 'enwc-wicd-wireless-prop-changed)