summary refs log tree commit diff
path: root/bin/connmenu
blob: 9faa5de6f4b83932529c5e759a68a408a9310461 (plain) (blame)
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
#!/bin/bash
# This is a modified version of:
# https://github.com/march-linux/connman_dmenu
#
# changes:
# * include DEBUG=1 env var switch
# * use menu (my bemenu wrapper)
# * replace dmenu_notify with notify-send
# * include icons in menu prompts
# * report script errors instead of failing silently
# * make use of bemenu's password mode

set -e
[ -n "$DEBUG" ] && set -x
export UNSAFE=1

readonly SCAN_RESULT=/tmp/connman.scan
readonly STORAGE_PATH=/var/lib/connman
readonly VPN_STORAGE_PATH=/var/lib/connman-vpn

dmenu() {
	menu "$@"
}
notifysend() {
	[ -z "$notifyicon" ] && notifyicon=wifi
	local msg=$1
	shift 1
	echo "$msg"
	notification_id=$(notify-send "connman" "$msg" -i "$(iconez -sn "$notifyicon")" --print-id "$@")
}

get_services() {
	# if [[ -f $SCAN_RESULT ]] && [[ $(($(date +%s) - $(stat -c %Y $SCAN_RESULT))) -gt 9000 ]]; then

	if [[ -f $SCAN_RESULT ]]; then
		notifysend 'another connman_dmenu is running'
		exit 1
	fi

	local notification_id
	local notifyicon=wifi-sync
	notifysend "scanning for wifi services..." --expire-time=0

	trap 'rm -f $SCAN_RESULT' EXIT
	connmanctl enable wifi &>/dev/null
	connmanctl scan wifi &>/dev/null
	clear-notification "$notification_id"

	connmanctl services |
		awk -F ' +' '{ service_id=$NF; $NF=""; info=$1; $1=""; name=substr($0, 2, length-2); gsub(/[^a-zA-Z0-9-]/, "_", name); if (substr(info, 0, 1) == "*") {name="*" name} }
	name { print name, service_id }' >$SCAN_RESULT
}

# $1 = index
index_to_name() {
	[[ -f $SCAN_RESULT ]] || exit 1
	awk -v line="$1" 'NR == line { print $1 }' $SCAN_RESULT
}

# $1 = index
index_to_service() {
	[[ -f $SCAN_RESULT ]] || exit 1
	awk -v line="$1" 'NR == line { print $2 }' $SCAN_RESULT
}

# $1 = service id
get_service_security() {
	cut -d _ -f 5 <<<"$1"
}

# $1 = service id
get_service_signal() {
	connmanctl services "$1" | awk '$1 == "Strength" { print $3 }'
}

# $1 = service id
get_service_state() {
	connmanctl services "$1" | awk '$1 == "State" { print $3 }'
}

create_dmenu() {
	[[ -f $SCAN_RESULT ]] || exit 1
	# echo 'setup vpn pptp'
	local order=1
	local name
	local service_id
	local security
	local signal
	local disconnect
	while read -r name service_id; do
		security=''
		signal=''
		disconnect=''
		[[ ! "$(get_service_state "$service_id")" =~ ^(idle|failure)$ ]] && disconnect='(disconnect)'
		case "$service_id" in
		wifi_*)
			security="$(get_service_security "$service_id")"
			signal="$(get_service_signal "$service_id")"
			;;
		vpn_*)
			security=vpn
			;;
		esac
		printf '%2s  %-40s%9s %-3s %s\n' "$order" "$name" "$security" "$signal" "$disconnect"
		((order++))
	done <$SCAN_RESULT
}

# $1 = question
# $2 = var name
dmenu_ask() {
	local msg=$1
	local var=$2
	shift 2

	IFS= read -r "${var?}" < <(: | dmenu -p "󱛆 $msg" "$@")
	if [[ ! "${!var}" ]]; then
		(
			notifyicon=wifi-alert
			notifysend "invalid $var"
		)
		exit 1
	fi
}

trap '{ notify-send connman "an error occured" -c error -i "$(iconez -sn wifi-alert)"; }' ERR
get_services
index="$(create_dmenu | dmenu -l 10 -i -p '󱛆 select wifi service' | sed 's/^ *//g' | cut -d ' ' -f 1)"

# if [[ "$index" == setup ]]; then
# 	# create vpn mode
# 	dmenu_ask 'name this PPTP VPN' name
# 	name="${name// /_}"
# 	dmenu_ask 'please provide VPN domain' domain
# 	dmenu_ask 'please provide identity' identity
# 	dmenu_ask 'please provide password' password --password
# 	cat >"$VPN_STORAGE_PATH/$name.config" <<-EOF
# 		[provider_$name]
# 		Type = PPTP
# 		Name = $name
# 		Host = $(dig +short A "$domain" | sort -n | head -n1)
# 		Domain = $domain
# 		PPTP.User = $identity
# 		PPTP.Password = $password
# 	EOF
# 	notifysend "VPN $name is created"
# 	exit 0
# fi

service_id="$(index_to_service "$index")"
[[ "$service_id" ]] || exit 1

name="$(index_to_name "$index")"
echo "$name { $service_id }"

if [[ ! "$(get_service_state "$service_id")" =~ ^(idle|failure)$ ]]; then
	connmanctl disconnect "$service_id"
	(
		notifyicon=wifi-remove
		notifysend "$name disconnected"
	)
	exit 0
fi

security="$(get_service_security "$service_id")"

# create service file for encryption
if [[ ! "$name" =~ ^\* ]] && [[ "$security" =~ ^(ieee8021x|psk|wep)$ ]]; then
	swx -t connmanctl
	wtype -s 200 "agent on"
	wtype -s 200 -k enter
	wtype -s 200 "connect $service_id"
	wtype -s 200 -k enter

	# config_file="$STORAGE_PATH/$service_id/settings"
	# if [[ -f "$config_file" && no != "$(echo -e 'yes\nno' | dmenu -p '󱛆 use previous profile?')" ]]; then
	# 	echo "use old profile: $config_file"
	# else
	# 	password=
	# 	dmenu_ask 'please provide password' password --password
	# 	[ -n "$password" ] || exit 1
	#
	# 	case "$security" in
	# 	ieee8021x)
	# 		notifysend "unable to setup $security networks" -c error
	# 		exit 1
	#
	# 		# dmenu_ask 'please provide identity' identity
	# 		# case "$(echo -e 'PEAP/MSCHAPV2\nTTLS/PAP' | dmenu -p '󱛆 please specify EAP type')" in
	# 		# PEAP/MSCHAPV2)
	# 		# 	eap=peap
	# 		# 	phase2=MSCHAPV2
	# 		# 	;;
	# 		# TTLS/PAP)
	# 		# 	eap=ttls
	# 		# 	phase2=PAP
	# 		# 	;;
	# 		# *)
	# 		# 	(
	# 		# 		notifyicon=wifi-alert
	# 		# 		notifysend 'invalid EAP' -c error
	# 		# 	)
	# 		# 	exit 1
	# 		# 	;;
	# 		# esac
	# 		# mkdir -p "$(dirname "$config_file")"
	# 		# cat >"$config_file" <<-EOF
	# 		# 	[service_$service_id]
	# 		# 	Type = wifi
	# 		# 	Name = $name
	# 		# 	EAP = $eap
	# 		# 	Phase2 = $phase2
	# 		# 	Identity = $identity
	# 		# 	Passphrase = $password
	# 		# EOF
	# 		;;
	# 	psk | wep)
	# 		mkdir -p "$(dirname "$config_file")"
	# 		cat >"$config_file" <<-EOF
	# 			[service_$service_id]
	# 			Type = wifi
	# 			Name = $name
	# 			Passphrase = $password
	# 		EOF
	# 		;;
	# 	esac
	# 	chmod 600 "$config_file"
	# fi
fi

connman_msg="$(timeout 10 connmanctl connect "$service_id" 2>&1 | head -n 1)"
if [[ "$connman_msg" == Connected* ]]; then
	(
		notifyicon=wifi-check
		notifysend "connected to $name"
	)
else
	error_msg='automatic timeout for connman_dmenu'
	[[ "$connman_msg" ]] && error_msg="$(cut -d ' ' -f 3- <<<"$connman_msg")"
	(
		notifyicon=wifi-cancel
		notifysend "cannot connect to $name ($error_msg)" -c error
	)
fi