-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload
executable file
·150 lines (138 loc) · 2.77 KB
/
upload
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
#!/usr/bin/env bash
BASE_URL='https://bbs.sjtu.edu.cn'
BASE_DIR="$HOME/.config/bbs_upload"
MAX_FILE_SIZE=1048577
AD="upload by upload: https://github.com/exaatto/yssy-util-scripts"
ret=0
function _do_req()
{
id=$1
shift
if [[ "XX$id" = "XXguest" ]]
then
curl -s $* | iconv -f gb2312
else
COOKEI_IN="$BASE_DIR/cookie.$id.in"
COOKEI_OUT="$BASE_DIR/cookie.$id.out"
touch $COOKEI_IN $COOKEI_OUT
curl -s -c $COOKEI_IN -b $COOKEI_OUT $* | iconv -f gb2312
cp $COOKEI_IN $COOKEI_OUT
fi
}
function do_upload()
{
id=$1
shift
files=$@
count=0
for file in $files
do
if _do_req \
$id \
"$BASE_URL/bbsdoupload" \
-F"board=$board" \
-F"up=@$file;filename=$( basename $file)" \
-F"level=1" \
-F"live=180" \
-F"exp=$AD" |\
grep '附件上载成功, URL为 :<P><font color=green>http://bbs.sjtu.edu.cn/file/[A-Za-z0-9]\+/[0-9]\+[^< ]*' -o |grep --color=auto 'http.*' -o
then
count=$(( $count + 1 ))
else
echo "file: $file upload failed." 1>&2
fi
done
return $count
}
function check_file_size()
{
ret=0
files=$@
for file in $files
do
size=$(stat -c%s $file 2>/dev/null)
if [[ $size -gt $MAX_FILE_SIZE ]]
then
echo "$file is too large, max ${MAX_FILE_SIZE}B."
ret=1
fi
done
return $ret
}
function check_board_exist()
{
board=$1
if _do_req 'guest' "$BASE_URL/bbsdoc?board=$board" | grep '<br>ERROR: 错误的讨论区<br><br>' >/dev/null
then
echo "wrong board: $board."
return 1
fi
return 0
}
function check_session_and_login()
{
id=$1
pw=$2
if _do_req $id "$BASE_URL/bbsleftnew" | grep '<a class=left href=bbslogout target=_top>\[注销本次登录\]</a>' >/dev/null
then
echo 'already login.' 1>&2
else
echo 'session expired, do login.' 1>&2
if _do_req \
$id \
"$BASE_URL/bbslogin" \
-d "id=$id&pw=$pw&submit=login" |\
grep '<title>出错啦</title>' >/dev/null
then
echo 'login incorect.' 1>&2
fi
fi
}
function _help()
{
cat <<EOF
USAGE: upload BOARD FILE [FILE ...]
upload FILE to [email protected]
-h display this help
EOF
}
if [[ $# < 2 ]]
then
_help
exit 0
fi
IFS_SAVE=$IFS
export IFS=$(echo -ne '\000')
board=$1
shift
files=$@
if ( check_file_size $files || check_board_exist $board )
then
:
else
export IFS=$IFS_SAVE; exit 0
fi
if [[ -d $BASE_DIR ]]
then
line=$( cat $BASE_DIR/passwd 2>/dev/null)
id=$( echo $line | cut -d ':' -f 1 )
pw=$( echo $line | cut -d ':' -f 2 )
else
mkdir -p $BASE_DIR
fi
if [[ "XX$id" = "XX" ]]
then
read -p "Your BBS ID: " id 1>&2
fi
if [[ "XX$pw" = "XX" ]]
then
stty -echo
read -p "Your BBS password: " pw 1>&2
stty echo
echo
fi
check_session_and_login $id $pw
do_upload $id $files
COUNT=$?
export IFS=$IFS_SAVE
exit $COUNT