some playing with bbs - new fm
[mirrors/Programs.git] / bash / bbs / bbs.sh
... / ...
CommitLineData
1#!/bin/bash
2#Settings
3bbs_dir=$(dirname "$0");
4bbs_title="Harvie's BBS";
5#Misc
6fail_delay="5";
7
8#Directories
9config_dir="./config";
10data_dir="./data";
11utils_dir="./utils";
12#Data directories
13chatrooms_dir="$data_dir/chatrooms";
14boards_dir="$data_dir/boards";
15archive_dir="$data_dir/archive";
16#Files
17welcome_file="$config_dir/welcome";
18about_file="$config_dir/about";
19farewell_file="$config_dir/farewell";
20users_file="$config_dir/users";
21log_file="$config_dir/security.log";
22
23#Init...
24cd "$bbs_dir";
25tmp="/tmp/bbstmp-$$";
26dialog=$(which dialog);
27export DIALOGOPTS="--no-shadow --trim --title \"$bbs_title\" --backtitle \"$bbs_title\"";
28w(){ echo $(tput cols); } #dialog width
29h(){ echo $(($(tput lines)-0)); } #dialog height (LINES-5 to display backtitle)
30m(){ echo $(($(tput lines)-12)); } #dialog menu size
31bbs_ssh_ip=$(echo $SSH_CLIENT | cut -d' ' -f1); #Works only with OpenSSH
32alias ls=ls; #Get rid of that 'user friendly' crap ;)
33
34#Functions...
35bbs_welcome_banner() {
36 cat "$welcome_file" > "$tmp";
37 echo "$SSH_CLIENT" >>"$tmp"; #Works only with OpenSSH
38 $dialog --title "Welcome to $bbs_title" --exit-label "Enter" --textbox "$tmp" $(h) $(w);
39 if [ "$?" != 0 ]; then bbs_exit; fi;
40}
41
42bbs_about() {
43 $dialog --title "About $bbs_title" --exit-label "Agree" --textbox "$about_file" $(h) $(w);
44}
45
46bbs_exit() {
47 rm -f "$tmp";
48 clear;
49 cat "$farewell_file";
50 exit;
51}
52
53bbs_check_login() {
54 cat "$users_file" | grep "^[^# ]" | while read i; do
55 login=$(echo "$i" | cut -d: -f1);
56 passw=$(echo "$i" | cut -d: -f2);
57 group=$(echo "$i" | cut -d: -f3);
58 if [[ "$1" == "$login" && "$2" == "$passw" ]]; then
59 echo "$group";
60 echo "($(date +%T)) $bbs_ssh_ip: Login to user $1 successfull" >>"$log_file";
61 fi;
62 done;
63}
64
65bbs_preview() {
66 if [[ -d "$1" ]];
67 then echo '[DIR]' #$(ls -A "$1"); #<- uncomment rest of the line to have directory preview
68 else echo #$(head -n1 "$1"); #<- uncomment rest of the line to have file preview
69 fi;
70}
71
72bbs_browse_dir() { #brand new dialog filelisting - supposed to be secure to directory traversal!
73 while true; do
74
75 #call dialog with output of ls...
76 ls -A1 --group-directories-first "$1" | while read i; do
77 echo -n "$i"; echo -ne "\x00";
78 echo -n $(bbs_preview "$1/$i"); echo -ne "\x00"
79 done | xargs -0 $dialog --menu -- "Select the board" $(h) $(w) $(m) 2>"$tmp";
80
81 if [ "$?" != 0 ]; then
82 return 1;
83 #$dialog --pause "HOOOPS! Sorry. None or empty directory selected..." $(h) $(w) "$fail_delay";
84 else
85 selected=$( echo -n "$1/"; cat "$tmp" );
86 if [[ -d "$selected" ]];
87 then
88 bbs_browse_dir "$selected"; return "$?";
89 else
90 echo "$selected" >"$tmp"; return 0;
91 fi;
92 fi;
93
94 done;
95}
96
97bbs_browse_dir() { #brand new dialog filelisting - supposed to be secure to directory traversal!
98 while true; do
99
100 "$utils_dir/fselect" "$1" "$tmp"
101
102 if [ "$?" != 7 ]; then
103 return 1;
104 #$dialog --pause "HOOOPS! Sorry. None or empty directory selected..." $(h) $(w) "$fail_delay";
105 else
106 #$dialog --pause "HOOOPS! Sorry. None or empty directory selected..." $(h) $(w) "$fail_delay";
107 #selected=$( echo -n "$1/"; cat "$tmp" );
108 #echo "$selected" >"$tmp";
109 return 0;
110 fi;
111
112 done;
113}
114
115#bbs_file_select() { #deprecated
116# ls -A1 $@ | while read i; do
117# echo -n "$i"; echo -ne "\x00";
118# echo -n $(bbs_preview "$1/$i"); echo -ne "\x00"
119# done | xargs -0 $dialog --menu -- "Select the file" $(h) $(w) $(m);
120# return "$?";
121#}
122
123bbs_file_edit() {
124 #This is supposed to be a secure way to edit the file...
125 nano -DR "$1";
126}
127
128#Chat##################
129
130bbs_chat_send() {
131 $dialog --inputbox "Enter message" $(h) $(w) 2>"$tmp"
132 if [ "$?" != 0 ]; then return; fi;
133 message=$(cat "$tmp");
134 echo "($(date +%T))" "$login: $message" >>"$1"
135}
136
137bbs_chat_join() {
138 while true; do
139 $dialog --title "Chatroom $1 (esc to leave)" --exit-label "Write message" --tailbox "$1" $(h) $(w);
140 if [ "$?" != 0 ]; then return; fi;
141 bbs_chat_send "$1";
142 done;
143}
144
145bbs_chat() {
146 bbs_browse_dir "$chatrooms_dir";
147 if [ "$?" != 0 ]; then return; fi;
148 chatroom=$(cat "$tmp");
149 bbs_chat_join "$chatroom";
150}
151
152
153
154#Mail####################
155
156bbs_mail() {
157 $dialog --pause "HOOOPS! Sorry. Mail not yet ;o)" $(h) $(w) "$fail_delay";
158}
159
160#Archive#################
161
162bbs_board_read() {
163 #echo BBS_BOARD_READ "$1" | less;
164 #less "$1";
165 $dialog --textbox "$1" $(h) $(w);
166}
167
168bbs_archive() {
169 while true; do
170 bbs_browse_dir "$archive_dir";
171 if [ "$?" != 0 ];
172 then break;
173 else bbs_board_read $(cat "$tmp");
174 fi;
175 done;
176}
177
178#Forum###################
179
180bbs_board() {
181 while true; do
182 bbs_browse_dir "$boards_dir";
183 if [ "$?" != 0 ];
184 then break;
185 else bbs_board_read $(cat "$tmp");
186 fi;
187 done;
188}
189
190
191#System menu#############
192
193bbs_admin_console() {
194 #While this BBS is not complete, i will give the shell to the admin group to fix-up everything ;o)
195 #bash can be replaced by chrooted SASH (ex.) to improve security...
196 $dialog --insecure --passwordbox "Enter your password" $(h) $(w) 2>"$tmp";
197 if [ "$?" != 0 ]; then return; fi;
198 repassword=$(cat "$tmp");
199 if [[ "$group" == "admin" && "$repassword" == "$password" ]]; then
200 clear;
201 echo -e "This is management console. Press ctrl+d or type exit to leave...\n";
202 bash;
203 clear;
204 else
205 $dialog --pause "Authentification failed. Check your password and/or membership in admin group." $(h) $(w) "$fail_delay";
206 fi;
207}
208
209bbs_logs() {
210 if [ "$group" == "guest" ]; then
211 $dialog --pause "Users from guest group can't browse logs!" $(h) $(w) "$fail_delay";
212 return;
213 fi;
214 $dialog --textbox "$log_file" $(h) $(w);
215}
216
217bbs_user_info() {
218 echo "LOGIN: $login" >"$tmp";
219 echo "GROUP: $group" >>"$tmp";
220 echo >>"$tmp";
221 echo "IP (if any): $SSH_CLIENT" >>"$tmp"; #Works only with OpenSSH
222 echo "CONNECTION (if any): $SSH_CONNECTION" >>"$tmp"; #Works only with OpenSSH
223 $dialog --title "Userinfo for $login" --textbox "$tmp" $(h) $(w);
224}
225
226bbs_user_list() {
227 cat "$users_file" | grep "^[^# ]" | while read i; do
228 login=$(echo "$i" | cut -d: -f1);
229 group=$(echo "$i" | cut -d: -f3);
230 echo -e "$group\t\t$login";
231 done | sort -u >"$tmp";
232 $dialog --title "Userlist for $bbs_title" --textbox "$tmp" $(h) $(w);
233}
234
235bbs_register() { ########################################################<-incompleteeeeeeeee
236 echo -n "$SSH_CLIENT:$ " 2>"$tmp";
237 $dialog --inputbox "Send message to admins\n\
238 eg.:\nREGISTRATION: yournick:yourpasswd CONTACT:your@adress.net Few words about you here...\n\
239 or\nSome other message..." $(h) $(w) 2>>"$tmp";
240 if [ "$?" != 0 ]; then return; fi;
241}
242
243bbs_contact_admins() { ########################################################<-incompleteeeeeeeee
244 echo -n "$SSH_CLIENT:$ " 2>"$tmp";
245 $dialog --inputbox "Send message to admins\n\
246 eg.:\nREGISTRATION: yournick:yourpasswd CONTACT:your@adress.net Few words about you here...\n\
247 or\nSome other message..." $(h) $(w) 2>>"$tmp";
248 if [ "$?" != 0 ]; then return; fi;
249}
250
251bbs_system_menu() {
252 while true; do
253 $dialog --menu "System Menu" $(h) $(w) $(m)\
254 userinfo "Info about you"\
255 userlist "Info about other users"\
256 logs "View logs"\
257 register "Submit request for new account"\
258 contact "Contact administrators"\
259 admin "Enter admin management console..."\
260 back "Go back to the main menu..."\
261 2>"$tmp"
262 if [ "$?" != 0 ]; then return; fi;
263
264 choice=$(cat "$tmp")
265 case "$choice" in
266 "userinfo") bbs_user_info;;
267 "userlist") bbs_user_list;;
268 "logs") bbs_logs;;
269 "register") bbs_register;;
270 "contact") bbs_contact_admins;;
271 "admin") bbs_admin_console;;
272 "back") return;;
273 *) echo bad choice... keep trying.;;
274 esac
275 done;
276}
277
278#CODE###########################################################################
279#Welcome
280bbs_welcome_banner;
281
282#Login
283while true; do
284 $dialog --inputbox "Enter your nickname" $(h) $(w) 2>"$tmp";
285 if [ "$?" != 0 ]; then bbs_exit; fi;
286 login=$(cat "$tmp");
287 $dialog --insecure --passwordbox "Enter your password" $(h) $(w) 2>"$tmp";
288 if [ "$?" != 0 ]; then bbs_exit; fi;
289 password=$(cat "$tmp");
290
291 group=$(bbs_check_login "$login" "$password");
292 case "$group" in
293 "user") break;;
294 "admin") break;;
295 "guest") break;;
296 esac;
297 $dialog --pause "Login failed! Press enter to continue..." $(h) $(w) "$fail_delay";
298 echo "($(date +%T)) $bbs_ssh_ip: Login to user $login failed" >>"$log_file";
299done;
300
301if [[ "$group" == "guest" && -n "$bbs_ssh_ip" ]]; then
302 login="[$bbs_ssh_ip]$login";
303fi;
304
305 #Login debug ;)
306 #echo Loged as "$login:$group":$(mkpasswd "$password"); bbs_exit;
307
308while true; do
309 $dialog --no-cancel --menu "Main Menu" $(h) $(w) $(m)\
310 chat "Browse online chatrooms..."\
311 boards "Browse boards..."\
312 archive "Browse archive..."\
313 mail "Send/receive personal messages..."\
314 system "Enter system menu..."\
315 about "About this BBS..."\
316 exit "Logout"\
317 2>"$tmp"
318
319 choice=$(cat "$tmp")
320 case "$choice" in
321 "chat") bbs_chat;;
322 "boards") bbs_board;;
323 "archive") bbs_archive;;
324 "mail") bbs_mail;;
325 "system") bbs_system_menu;;
326 "about") bbs_about;;
327 "exit") bbs_exit;;
328 *) $dialog --pause "HOOOPS! Sorry. $choice not yet ;o)" $(h) $(w) "$fail_delay";;
329 esac
330done;
331
332bbs_exit;
This page took 0.687255 seconds and 4 git commands to generate.