summaryrefslogtreecommitdiffstats
path: root/pounceman.sh
blob: 180b36e048272020dd0468f64a458853406d9c20 (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
#!/bin/bash

DIALOG_CANCEL=1
DIALOG_ESC=255
DIALOG_EXTRA=3
HEIGHT=0
WIDTH=0
pouncedir="/var/lib/pounce"

display_result() {
  dialog --title "$1" \
    --no-collapse \
    --msgbox "$result" 0 0
}

menu() {
  exec 3>&1
  selection=$(dialog \
    --backtitle "LibertaCasa IRC Services - Pounce Division - https://liberta.casa/" \
    --title "Welcome!" \
    --clear \
    --cancel-label "Exit" \
    --menu "Please select:" $HEIGHT $WIDTH 4 \
    "1" "Display active networks" \
    "2" "Display disabled networks" \
    "3" "Add new network" \
    "4" "Enable network" \
    "5" "Disable network" \
    2>&1 1>&3)
  exit_status=$?
  exec 3>&-
}

get_networks() {
  exec 3>&1
  if [ $pouncedir = "" ]; then
    exit
  fi
  pouncedir="/var/lib/pounce"
  if ! $(find $pouncedir/users/$USER/$1 -mindepth 0 -maxdepth 0 -empty | grep -q .); then
    COUNTER=1
    RADIOLIST=""
    user_choice=""
    for i in $pouncedir/users/$USER/$1/*; do
  	  FILENAME=$(basename "$i")
  	  RADIOLIST="$RADIOLIST $FILENAME $FILENAME off"
  	  let COUNTER=COUNTER+1
    done
    if [ $1 = "enabled" ]; then
	    user_choice=$(dialog --ok-label "Edit" \
		    --backtitle "LibertaCasa IRC Services - Pounce Division - https://liberta.casa/" \
	  	  --radiolist "Networks" 0 0 $COUNTER \
  		  $RADIOLIST \
	    2>&1 1>&3)
    fi
    if [ $1 = "disabled" ]; then
	    user_choice=$(dialog --ok-label "Edit" \
		    --backtitle "LibertaCasa IRC Services - Pounce Division - https://liberta.casa/" \
	  	  --radiolist "Networks" 0 0 $COUNTER \
  		  $RADIOLIST \
	    2>&1 1>&3)
    fi
    exec 3>&-
    echo $user_choice > /tmp/userchoice
  else
    dialog --title "No entries found" \
	    --backtitle "LibertaCasa Pounce Configurator" \
	    --msgbox "There seem to be no $1 networks in your account." \
	    10 60
    user_choice=""
  fi
}

edit_network() {
  tmpfile=$(mktemp /tmp/$USER.XXXXXXXXXXXXXXXXXXXX)
  cp $pouncedir/users/$USER/$1/$user_choice $tmpfile
  nw_name=$user_choice
  nw_hostname=$(egrep '(^|\s)host =' $tmpfile | sed 's/host = //' - )
  nw_nickname=$(egrep '(^|\s)nick =' $tmpfile | sed 's/nick = //' - )
  nw_password=$(egrep '(^|\s)sasl-plain =' $tmpfile | sed 's/sasl-plain = //' - )
  nw_channels=$(egrep '(^|\s)join =' $tmpfile | sed 's/join = //' - )
  nw_away=$(grep 'away' $tmpfile | sed 's/away = //' -)
  echo "$nw_hostname\n$nw_nickname\n$nw_password\n$nw_channels\n$nw_away" > /tmp/nwvars
  exec 3>&1
  user_input=$(dialog --ok-label "Save" \
	 --extra-button \
	 --extra-label "Delete" \
	 --backtitle "LibertaCasa IRC Services - Pounce Division - https://liberta.casa/" \
	 --title "Edit IRC Network" \
	 --form "$USER/$nw_name:" \
  15 60 0 \
	 "Hostname:"       1 1 "$nw_hostname"	1 18 33 0 \
	 "Nickname:"       2 1 "$nw_nickname"	2 18 33 0 \
	 "SASL Password:"  3 1 "$nw_password"	3 18 33 0 \
	 "Channels:"	   4 1 "$nw_channels"	4 18 33 0 \
	 "Away message:"   5 1 "$nw_away"	5 18 33 0 \
  2>&1 1>&3)
  exit_status=$?
  exec 3>&-
  echo $user_input > /tmp/userinput
  case $exit_status in
          0) 
	     tmp_name=$(echo "$user_input" | sed -n 1p)
	     tmp_hostname=$(echo "$user_input" | sed -n 2p)
	     tmp_nickname=$(echo "$user_input" | sed -n 3p)
	     tmp_password=$(echo "$user_input" | sed -n 4p)
	     tmp_channels=$(echo "$user_input" | sed -n 5p)
	     tmp_away=$(echo "$user_input" | sed -n 6p)
	     echo "$tmp_name\n$tmp_nickname\n$tmp_password\n$tmp_channels\n$tmp_away" > /tmp/tmpvars
	     sed -e "s/$(egrep '(^|\s)host =' $tmpfile)/host = $tmp_hostname/" -i $tmpfile
	     sed -e "s/$(egrep '(^|\s)nick =' $tmpfile)/nick = $tmp_nickname/" -i $tmpfile
	     sed -e "s+$(egrep '(^|\s)real =' $tmpfile)+real = $tmp_nickname - https://liberta.casa/+" -i $tmpfile
	     sed -e "s/$(egrep '(^|\s)user =' $tmpfile)/user = $tmp_nickname/" -i $tmpfile
	     sed -e "s/$(egrep '(^|\s)sasl-plain =' $tmpfile)/sasl-plain = $tmp_password/" -i $tmpfile
	     sed -e "s/$(egrep '(^|\s)join =' $tmpfile)/join = $tmp_channels/" -i $tmpfile
	     sed -e "s/$(egrep '(^|\s)away =' $tmpfile)/away = $tmp_away/" -i $tmpfile
	     DIFF=$(diff $pouncedir/users/$USER/$1/$user_choice $tmpfile)
	     if [ ! "$DIFF" = "" ]; then
	       cp $tmpfile $pouncedir/users/$USER/$1/$user_choice
	     fi
	     rm $tmpfile
	  ;;
  esac
  nw_name=""
  nw_hostname=""
  nw_nickname=""
  nw_password=""
  nw_channels=""
  nw_away=""
  tmp_name=""
  tmp_hostname=""
  tmp_nickname=""
  tmp_password=""
  tmp_channels=""
  tmp_away=""
  tmpfile=""
  DIFF=""
}

set_network() {
  if [ $1 = "disabled" ]; then
    mv $pouncedir/users/$USER/disabled/$user_choice $pouncedir/users/$USER/enabled/$user_choice
  fi
  if [ $1 = "enabled" ]; then
    mv $pouncedir/users/$USER/enabled/$user_choice $pouncedir/users/$USER/disabled/$user_choice
  fi
}

remove_network() {
  dialog --title "Delete network" \
	  --backtitle "LibertaCasa Pounce Configurator" \
	  --yesno "This will permanently delete the network configuration $USER/$user_choice - Are you sure?" 7 60
  exit_status=$?
  case $exit_status in
	  0) rm -f $(find $pouncedir/users/$USER -type f -name "$user_choice");;
  esac
}

add_network() {
  exec 3>&1
  user_input=$(dialog --ok-label "Submit" \
	 --backtitle "LibertaCasa IRC Services - Pounce Division - https://liberta.casa/" \
	 --title "New IRC Network" \
	 --form "Network specific details:" \
  20 50 0 \
	 "Arbitrary name:" 1 1 "$nw_name"	1 18 33 0 \
	 "Hostname:"       2 1 "$nw_hostname"	2 18 33 0 \
	 "Nickname:"       3 1 "$nw_nickname"	3 18 33 0 \
	 "SASL Password:"  4 1 "$nw_password"	4 18 33 0 \
	 "Channels:"	   5 1 "$nw_channels"	5 18 33 0 \
	 "Away message:"   6 1 "$nw_away"	6 18 33 0 \
  2>&1 1>&3)
  #3>&1 1>&2 2>&3 3>&-)
  exec 3>&-
  echo $user_input > /tmp/userinput
  tmp_name=$(echo "$user_input" | sed -n 1p)
  tmp_hostname=$(echo "$user_input" | sed -n 2p)
  tmp_nickname=$(echo "$user_input" | sed -n 3p)
  tmp_password=$(echo "$user_input" | sed -n 4p)
  tmp_channels=$(echo "$user_input" | sed -n 5p)
  tmp_away=$(echo "$user_input" | sed -n 6p)
  if [ ! $tmp_name = "TEMPLATE" ]; then
    cp $pouncedir/TEMPLATE $pouncedir/users/$USER/disabled/$tmp_name
    sed -e "s/%%POUNCEDIR%%/$pouncedir/" -i $pouncedir/users/$USER/disabled/$tmp_name
    sed -e "s/%%USER%%/$USER/" -i $pouncedir/users/$USER/disabled/$tmp_name
    sed -e "s/%%NAME%%/$tmp_name/" -i $pouncedir/users/$USER/disabled/$tmp_name
    sed -e "s/%%PASSWORD%%/$tmp_password/" -i $pouncedir/users/$USER/disabled/$tmp_name
    sed -e "s/%%HOSTNAME%%/$tmp_hostname/" -i $pouncedir/users/$USER/disabled/$tmp_name
    sed -e "s/%%CHANNELS%%/$tmp_channels/" -i $pouncedir/users/$USER/disabled/$tmp_name
    sed -e "s/%%USERNAME%%/$tmp_nickname/" -i $pouncedir/users/$USER/disabled/$tmp_name
    sed -e "s/%%AWAY%%/$tmp_away/" -i $pouncedir/users/$USER/disabled/$tmp_name
 fi
}

while true; do
  menu
  case $exit_status in
    $DIALOG_CANCEL)
      clear
      echo "Good bye!"
      exit
      ;;
    $DIALOG_ESC)
      clear
      echo "See you!" >&2
      exit 1
      ;;
  esac
  case $selection in
    1 )
      #result=$(ls $pouncedir/users/$USER/enabled/)
      #display_result "Enabled networks:"
      get_networks "enabled"
      if [ ! -z "$user_choice" ]; then
	      edit_network "enabled"
	      if [ $exit_status = $DIALOG_EXTRA ]; then
	              remove_network
	      fi
      fi
      ;;
    2 )
      #result=$(ls $pouncedir/users/$USER/disabled/)
      #display_result "Disabled networks:"
      get_networks "disabled"
      if [ ! -z "$user_choice" ]; then
	      edit_network "disabled"
	      if [ $exit_status = $DIALOG_EXTRA ]; then
	              remove_network
	      fi
      fi
      ;;
    3 )
      add_network
      ;;
    4 )
      get_networks "disabled"
      if [ ! -z "$user_choice" ]; then
	      set_network "disabled"
      fi
      ;;
    5 )
      get_networks "enabled"
      if [ ! -z "$user_choice" ]; then
	      set_network "enabled"
      fi
      ;;
  esac
done