summaryrefslogtreecommitdiff
path: root/wifi-bluetooth-conf.el
blob: 4024ffbce2749ee1cf9bba8a13e442bbcfbb2fd3 (plain)
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
;;; wifi-bluetooth-conf.el --- Configuration to manage wifi and bluetooth  -*- lexical-binding: t; -*-

;; Copyright (C) 2021  李俊緯

;; Author: 李俊緯 <mmemmew@gmail.com>
;; Keywords: comm, hardware, games, unix

;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Configuration to manage wifi and bluetooth.

;;; Code:

;;;###autoload
(defvar durand-wifi-on-p nil
  "If WIFI is on or not.
This is defined in \"/Users/durand/.emacs.d/view-conf.el\"")

;;;###autoload
(defvar durand-bluetooth-on-p nil
  "If BLUETOOTH is on or not.
This is defined in \"/Users/durand/.emacs.d/view-conf.el\"")

;;;###autoload
(defun durand-wifi-filter (proc output)
  "Filter function to set the wifi variable.
This should only be used for the process \"durand-wifi\".
This is defined in \"/Users/durand/.emacs.d/view-conf.el\""
  (cond
   ((string= (process-name proc) "durand-wifi"))
   ((user-error "Filter function associated with a wrong process.")))
  (setq durand-wifi-on-p (string-match "On$" output)))

;;;###autoload
(defun durand-bluetooth-filter (proc output)
  "Filter function to set the bluetooth variable.
This should only be used for the process \"durand-bluetooth\".
This is defined in \"/Users/durand/.emacs.d/view-conf.el\""
  (cond
   ((string= (process-name proc) "durand-bluetooth"))
   ((user-error "Filter function associated with a wrong process.")))
  (setq durand-bluetooth-on-p (string-match "1" output)))

;;;###autoload
(defun durand-wifi-toggle ()
  "Check if WIFI is enabled, then ask to toggle WIFI."
  (interactive)
  (make-process
   :name "durand-wifi"
   :buffer nil
   :command '("networksetup" "-getairportpower" "en0")
   :filter #'durand-wifi-filter
   :sentinel #'ignore)
  (accept-process-output (get-process "durand-wifi"))
  (let* ((prompt (format "WIFI is %s. Do you want to turn WIFI %s"
                         (cond (durand-wifi-on-p "on")
                               ("off"))
                         (cond (durand-wifi-on-p "off?")
                               ("on?"))))
         (decision (y-or-n-p prompt)))
    (cond
     (decision
      (let ((new-state (cond (durand-wifi-on-p "off")
                             ("on"))))
        (make-process
         :name "durand-toggle-wifi"
         :buffer nil
         :command (list
                   "networksetup" "-setairportpower"
                   "en0" new-state)
         :sentinel #'ignore
         :filter #'ignore)
        (message "WIFI turned %s" new-state))))))

;;;###autoload
(defun durand-bluetooth-toggle ()
  "Check if BLUETOOTH is enabled, then ask to toggle BLUETOOTH."
  (interactive)
  (make-process
   :name "durand-bluetooth"
   :buffer nil
   :command '("blueutil" "-p")
   :filter #'durand-bluetooth-filter
   :sentinel 'ignore)
  (accept-process-output (get-process "durand-bluetooth"))
  (let* ((prompt (format "BLUETOOTH is %s. Do you want to turn BLUETOOTH %s"
                         (cond (durand-bluetooth-on-p "on")
                               ("off"))
                         (cond (durand-bluetooth-on-p "off?")
                               ("on?"))))
         (decision (y-or-n-p prompt)))
    (cond
     (decision
      (let ((new-state (cond (durand-bluetooth-on-p "0")
                             ("1"))))
        (make-process
         :name "durand-toggle-bluetooth"
         :buffer nil
         :command (list "blueutil" "-p" new-state)
         :sentinel 'ignore
         :filter 'ignore)
        (message "BLUETOOTH turned %s"
                 (cond (durand-bluetooth-on-p "off")
                       ("on"))))))))

;;;###autoload
(defconst durand-wifi-executable
  "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport"
  "The executable to manage some WIFI settings.")

;;;###autoload
(defconst durand-wifi-bluetooth-buffer
  "*durand-wifi-bluetooth*"
  "The buffer to accept outputs from commands managing WIFI or BLUETOOTH.")

;;;###autoload
(defun durand-wifi-list-ports (&optional arg)
  "List available wifi networks.
If ARG is non-nil, then list the currently connected port."
  (interactive "P")
  (cond
   ((and (get-buffer durand-wifi-bluetooth-buffer)
         (buffer-live-p (get-buffer durand-wifi-bluetooth-buffer)))
    (kill-buffer durand-wifi-bluetooth-buffer)))
  (cond ((null arg)
         (make-process
          :name "durand-wifi-list-ports"
          :buffer durand-wifi-bluetooth-buffer
          :command (list durand-wifi-executable "-I")
          :sentinel #'durand-wifi-list-sentinel))
        ((make-process
          :name "durand-wifi-list-ports"
          :buffer durand-wifi-bluetooth-buffer
          :command (list durand-wifi-executable "-s")
          :sentinel #'durand-wifi-list-sentinel) )))

;;;###autoload
(defvar durand-wifi-ssid-machine-alist
  (list
   (cons "20-1" "wifi-2")
   (cons "1-20-4F" "wifi-4")
   (cons "1-20-4F_2EX" "wifi-4"))
  "A list of associations between the SSID of WIFI and the
machine name that we can use to search for the password in my
secret file.")

;;;###autoload
(defvar durand-wifi-scanned-ssids nil
  "The variable that holds the list of SSIDs of WIFI ports found
during the scan.")

;;;###autoload
(defvar durand-wifi-join-history nil
  "The variable that holds the history of WIFI ports joined.")

;;;###autoload
(defun durand-wifi-join-port (port &optional pwd)
  "Join PORT.
If PWD is non-nil, use that as the password."
  (interactive (let ((po (completing-read
                           "Port to join: " durand-wifi-scanned-ssids
                           nil nil nil 'durand-wifi-join-history))
                     (pwd (read-string "Password: ")))
                 (cond ((and pwd (not (string= pwd "")))
                        (list po pwd))
                       ((list po)))))
  (cond
   (pwd)
   ((let ((search-result
           (alist-get port
                      durand-wifi-ssid-machine-alist
                      nil nil #'string=)))
      (cond
       (search-result
        (setq
         pwd
         (funcall
          (plist-get
           (car (auth-source-search :host search-result))
           :secret))))))))
  (make-process
   :name "durand-wifi-join-port"
   :buffer durand-wifi-bluetooth-buffer
   :command (append
             (list "networksetup" "setairportnetwork" "en0"
                   port)
             (cond ((and pwd (not (string= pwd "")))
                    (list pwd))))
   :filter #'durand-wifi-join-filter))

;;;###autoload
(defun durand-wifi-list-sentinel (process status)
  "Extract and message the information about available WIFI's SSID's.
PROCESS should have the name \"durand-wifi-list-ports\".

This only does something when STATUS is the string \"finished\\n\"."
  (cond ((string-equal (process-name process) "durand-wifi-list-ports"))
        ((error "The list sentinel is applied to a wrong process: %S"
                process)))
  (let (first-var)
    (cond ((string-equal status "finished\n")
           (with-current-buffer durand-wifi-bluetooth-buffer
             (goto-char (point-min))
             (cond ((re-search-forward "AirPort: Off" nil t)
                    (message "Wifi is not enabled."))
                   ((re-search-forward "\\s-+SSID" (line-end-position) t)
                    (setq first-var t)
                    (setq durand-wifi-scanned-ssids nil)
                    (forward-line 1)
                    (let ((start 0))
                      (while (re-search-forward "\\S-" (line-end-position) t)
                        (forward-char -1)
                        (setq start (point))
                        (re-search-forward "\\s-" (line-end-position) t)
                        (forward-char -1)
                        (setq durand-wifi-scanned-ssids
                              (cons (buffer-substring-no-properties
                                     start (point))
                                    durand-wifi-scanned-ssids))
                        (forward-line 1))
                      (erase-buffer)
                      (goto-char (point-min))
                      (insert (string-join durand-wifi-scanned-ssids (string 10)))
                      (goto-char (point-min))))
                   ((re-search-forward "\\s-+SSID: " nil t)
                    (message (buffer-substring-no-properties
                              (point) (line-end-position))))))
           (cond (first-var
                  (display-buffer durand-wifi-bluetooth-buffer
                                  '((display-buffer-in-side-window)
                                    (side . bottom)
                                    (window-height . #'durand-fit-window-to-buffer-with-max)))
                  (with-current-buffer durand-wifi-bluetooth-buffer 
                    (fit-window-to-buffer
                     (get-buffer-window (current-buffer))
                     (floor (* durand-window-max-height
                               (frame-height
                                (window-frame
                                 (get-buffer-window (current-buffer))))))))))))))

;;;###autoload
(defun durand-wifi-join-filter (proc output)
  "Message the OUTPUT of the PROCESS."
  (cond ((string= (process-name proc) "durand-wifi-join-port"))
        ((error "The join filter is applied to a wrong process: %S"
                proc)))
  (message output))

(define-key global-map (vector 3 ?w ?w) #'durand-wifi-toggle)
(define-key global-map (vector 3 ?w ?b) #'durand-bluetooth-toggle)
(define-key global-map (vector 3 ?w ?l) #'durand-wifi-list-ports)
(define-key global-map (vector 3 ?w ?j) #'durand-wifi-join-port)

;; Control the display of the buffer
;; (setq display-buffer-alist
;;       (delete (assoc durand-wifi-bluetooth-buffer display-buffer-alist #'string-match)
;;               display-buffer-alist))
;; (cond
;;  ((assoc durand-wifi-bluetooth-buffer display-buffer-alist #'string-match)
;;   (setcdr (assoc durand-wifi-bluetooth-buffer
;;                  display-buffer-alist #'string-match)
;;           '((display-buffer-at-bottom)
;;             (window-height . #'durand-fit-window-to-buffer-with-max))))
;;  (t
;;   (add-to-list 'display-buffer-alist
;;                `(,(rx-to-string `(seq bos ,durand-wifi-bluetooth-buffer eos))
;;                  (display-buffer-at-bottom)
;;                  (window-height . #'durand-fit-window-to-buffer-with-max)))))

(provide 'wifi-bluetooth-conf)
;;; wifi-bluetooth-conf.el ends here