-
Notifications
You must be signed in to change notification settings - Fork 0
/
wlt
executable file
·224 lines (196 loc) · 4.89 KB
/
wlt
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
#!/bin/sh
#
# Copyright (C) 2015 Boyuan Yang <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Fondation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#
##########################################################################
HAVE_ICONV=""
WLT_CONFIG_FILE="/etc/wlt.conf"
WLT_CONFIG_FILE_LOCAL="$HOME/.config/wlt.conf"
# Strings
WLT_SCRIPT_VERSION="0.1.1"
WLT_NO_WGET_WARN="Sorry, it seems that you haven't install wget tool.\n\
Please install wget first."
WLT_SANITY_CHECK_FAILED="Sanity check failed, Invalid wlt parameter encountered."
WLT_ISP_USEDEFAULT="No valid value found. Using 0 as default."
WLT_TIME_USEDEFAULT="No valid value found. Using 14400 as default."
# Functions
reset_user_credential()
{
WLT_USERNAME=""
WLT_PASSWORD=""
WLT_ISP=""
WLT_TIME=""
}
read_user_credential()
{
# load global config file
if [ -f $WLT_CONFIG_FILE ]; then
. ${WLT_CONFIG_FILE}
fi
# load user config file (preferred)
if [ -f $WLT_CONFIG_FILE_LOCAL ]; then
. ${WLT_CONFIG_FILE_LOCAL}
fi
if [ ! "x${EnableDefaultWltCredential}" = "x1" ]; then
reset_user_credential
fi
}
print_usage()
{
echo "Usage: $0 [-ohd] (invalid)[upte]"
return 0
}
print_header()
{
echo "USTC WLT Script ${WLT_SCRIPT_VERSION}"
echo "Copyright (C) 2015 Boyuan Yang\n"
echo "This is free software; see the source for copying conditions. There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
}
print_no_wget()
{
echo "${WLT_NO_WGET_WARN}"
}
print_ISP_info()
{
# FIXME
return
}
params_init()
{
which "iconv" > /dev/null 2>&1
if [ "x$?" = "x0" ]; then
HAVE_ICONV="Y"
else
HAVE_ICONV=""
fi
}
process_abort()
{
echo "\nOperation aborted."
exit 1
}
##
# 检查当前参数是否有效
#
process_sanity_check()
{
if [ "x${WLT_USERNAME} = "x" -o "x${WLT_PASSWORD} = "x" ]; then
echo "${WLT_SANITY_CHECK_FAILED}"
process_abort
fi
if [ "x${WLT_ISP}" = "x" -o "x${WLT_TIME}" = "x" ]; then
echo "${WLT_SANITY_CHECK_FAILED}"
process_abort
fi
}
##
# 分析结果是否正确
# FIXME
process_analyze_result()
{
return 0
}
process_print_success()
{
echo "Operation success.\n"
return 0
}
process_post_wget()
{
if [ "x${HAVE_ICONV}" = "xY" ]; then
wget --post-data "name=${WLT_USERNAME}&password=${WLT_PASSWORD}&cmd=set&type=${WLT_ISP}&exp=${WLT_TIME}" http://wlt.ustc.edu.cn/cgi-bin/ip -O - | iconv -f GBK -t UTF-8
else
wget --post-data "name=${WLT_USERNAME}&password=${WLT_PASSWORD}&cmd=set&type=${WLT_ISP}&exp=${WLT_TIME}" http://wlt.ustc.edu.cn/cgi-bin/ip -O -
fi
if [ ! "x$?" = "x0" ]; then
process_abort
else
process_print_success
fi
}
main_function_default()
{
echo -n " * input your wlt username: "
read WLT_USERNAME
echo -n " * input your wlt password: "
stty -echo
read WLT_PASSWORD
stty echo
echo ""
echo -n " * select your preferred ISP [0-8]: "
print_ISP_info
read WLT_ISP
if [ "x${WLT_ISP}" = "x" ]; then
echo "${WLT_ISP_USEDEFAULT}"
WLT_ISP="0"
fi
echo -n " * select wlt activation duration [0/3600/14400/39600/50400]: "
read WLT_TIME
if [ "x${WLT_TIME}" = "x" ]; then
echo "${WLT_TIME_USEDEFAULT}"
WLT_TIME="14400"
fi
echo -n "\nProcess now? [y/n]"
USERINPUT="n"
read USERINPUT
if [ "x${USERINPUT}" = "x" -o "x${USERINPUT}" = "xy" ]; then
process_sanity_check
process_post_wget
else
process_abort
fi
}
# Main
# TODO FIXME i18n support
# TODO FIXME getopt
# :oh means bash deal
print_header
params_init
reset_user_credential
while getopts ohdu:p:t:e: OPTION
do
case ${OPTION} in
h)
# print help message
print_usage
exit 1
;;
d)
# use default section
read_user_credential
process_sanity_check
process_post_wget
exit 0
;;
o)
;;
\?)
# cannot be recognized
print_usage
exit 1
;;
esac
done
which "wget" > /dev/null 2>&1
if [ ! "x$?" = "x0" ]; then
print_no_wget
exit 1
fi
main_function_default
exit 0