summaryrefslogtreecommitdiff
path: root/bbsh
blob: 51e2c13c2b3e1ed3c80d510e023f8792356e247d (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
#!/bin/sh

BDIR="/var/bbs/boards"

###### 
NUMW='3'
BRDW='18'
TITW='50'
TRDW='12'
TOTW="$(echo "$NUMW+$BRDW+$TITW+$TRDW" | bc)"
HR="------------------------------------------------------------------------------\n"


#set -x

######
PROMPT='COMMAND :> '
DFLTBOARD=""
DFLTPAGER=${PAGER:-"less"}
CURBOARD=${1:-"$DFLTBOARD"}

##function
__prompt(){

	printf '[%s] %s' "$CURBOARD" "$PROMPT"	
}

##function
__board_prompt(){
	printf "%s" "Go to which board? (use index) "
}

##function
__thread_prompt(){
	printf "%s" "Read which thread? "
}

##function
select_board(){
	__board_prompt
	read -r board
	NEW_BOARD="$(ls "$BDIR" | tail -n +"$board" | head -1)"
	[ -z "${NEW_BOARD}" ] && echo "invalid board number" && return 1
	return 0

}

##function
get_boards(){
	echo $(ls "${BDIR}")
}


##function
show_boards(){

	printf -- "$HR"
	for b in $(get_boards); do
		t="$(head -1 "$BDIR/$b"/topic | sed -E 's/^(.{50}).*/\1/g')"
		n="$(ls "$BDIR/$b" | grep -E "^[0-9]+" | wc -l | sed -E 's/ //g')"
		printf "%-"$BRDW"s %-"$TITW"s [ %2s boards ]\n" "$b" "$t" "$n"
	done | sort | nl -v1 -w3 -s " " 
	printf -- "$HR"

}

##function
show_help(){

	printf '%s\n' \
		'	[Bulletin Boards Commands]'\
		'	----------------------------------------------------------'\
		'	(l)ist ......... List all available boards'\
		'	(g)oto ......... Goto a board by name or number'\
		'	(h)elp ......... Show this help message'\
		'	(m)essages...... List all messages in current board'\
		'	(t)ype ......... Display the contents of a message thread'\
		'	(q)uit ......... Quit bbsh'
}

##function
print_thread_title(){

	date="$(echo "$(basename $1)" | cut -d "-" -f 1 )"
	author="$(echo "$(basename $1)" | cut -d "-" -f 2)"
	subject="$(head -1 $1/subject | sed -E 's/^(.{50}).*/\1/g')"
	posts="$(ls $1 | grep -v '^subject$' | wc -l | sed -E 's/\ +//g')"
	printf "%-10s %-16s %-"$TITW"s [ %2s posts ]\n" "$date" "$author" "$subject" "$posts"
}


##function
list_board_messages(){ 
	[ -z "$CURBOARD" ] && { 
		printf "%s\n" "Not at any board!  Hit `l` to list boards, `g` to go to one." 
			return 
	}
	printf -- "$HR"
	for i in $(ls $BDIR/$CURBOARD | sort -rn | grep -v "^topic") ; do
		print_thread_title "$BDIR/$CURBOARD/$i"
	done | sort -rk1 | nl -w3 -v1 -s" "
	printf -- "$HR"
}

##function
type_message(){

	subject="$2"	
	author="$(echo $(basename $1) | cut -d "-" -f 2)"
	date="$(echo $(basename $1) | cut -d "-" -f 1)"
	printf -- "--------------------------\n"
	printf "SUBJECT:    %s\n" "$subject"
	printf "AUTHOR:     %s\n" "$author" 
	printf "POSTED:     %s\n" "$date"
	printf -- "--------------------------\n"
	cat $1

}


##function
type_thread(){ 
	[ -z "$CURBOARD" ] && { 
		printf "%s\n" "Not at any board!  Hit `l` to list boards, `g` to go to one." 
			return 
	}
	__thread_prompt
	read -r thread
	THREAD="$(ls "$BDIR/$CURBOARD" | sort -rn | grep -v "^topic$" | tail -n +"$thread" | head -1)"
	[ -z "${THREAD}" ] && echo "Invalid thread index!" && return 1
	subject="$(head -1 "$BDIR/$CURBOARD/$THREAD/subject")"
	for i in $(ls "$BDIR/$CURBOARD/$THREAD" | sort -n | grep -v "^subject$"); do 
		type_message "$BDIR/$CURBOARD/$THREAD/$i" "$subject"
	done | ${DFLTPAGER}

} ##function
read_cmd(){
	__prompt
	while read -r cmd; do 
		case $cmd in 

			g)
				select_board
				[ $? -eq 0 ] && exec $0 ${NEW_BOARD}
				;;
			h)
				show_help
				;;
			l)
				show_boards
				;;
			q)
				exit 0
				;;
			m)
				list_board_messages
				;;
			t)
				type_thread
				;;
			*)
				show_boards
				;;
		esac
		__prompt
	done
}


show_boards
read_cmd