summaryrefslogtreecommitdiff
path: root/pcr/aurvote/aurvote
blob: 1b9c8f1bf9d72771a59a16d8ea212e4094c80411 (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
#!/bin/bash
#
# aurvote : Tool to vote for favorite AUR packages
#
# Copyright (c) 2007-2010 Julien MISCHKOWITZ <wain@archlinux.fr>
# Copyright (c) 2011 tuxce <tuxce.net@gmail.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2, 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, see <http://www.gnu.org/licenses/>.
#

NAME='aurvote'
VERSION=1.4

TMPDIR=${TMPDIR:-/tmp}
AV_TMP="$TMPDIR/aurvote-$USER"

CONFIGFILE=${XDG_HOME_CONFIG:-~/.config}/aurvote

AUR_URL="https://aur.archlinux.org"
AUR_URL_LOGIN="$AUR_URL/login/"
AUR_URL_PKG_INFO="$AUR_URL/rpc.php"
AUR_URL_PKG_PAGE="$AUR_URL/packages/"
AUR_DOMAIN="aur.archlinux.org"
AUR_COOKIE="AURSID"
AUR_COOKIE_VALUE=""
AUR_SETLANG="?setlang=en"


version() {
    echo "$NAME $VERSION"
}

usage() {
    echo "$NAME $VERSION"
    echo
    echo "usage: $0 <option> <pkgname1> <pkgname2> ..."
    echo
    echo " --version, -V       shows version"
    echo " --help,    -h       shows this help"
    echo " --check,   -c       check for voted packages"
    echo " --vote,    -v       vote for packages"
    echo " --unvote,  -u       unvote packages"
    echo
    echo " --configure         create $CONFIGFILE"
    echo
}

error() {
    echo -e "Error: $*"
    exit 1
}

is_cookie_valid() {
    [[ -r $COOKIE_FILE ]] || return 1
    local expire
    expire=$(sed -ne '/^\(#HttpOnly_\|[^#]\)/ { s/#HttpOnly_//;p }' "$COOKIE_FILE" |
        awk "{ if (\$1==\"$AUR_DOMAIN\" && \$6==\"$AUR_COOKIE\") print \$5\" \"\$7; }")
    AUR_COOKIE_VALUE=${expire##* }
    expire=${expire%% *}
    [[ $expire ]] && { ((expire==0)) || ((expire>$(date +%s))); }
}

aur_login() {
    local args=()
    if ((PERSIST)); then
        is_cookie_valid && return 0
        args=(-d "remember_me=on")
    fi
    if [[ ! $user || ! $pass ]]; then
        error "$CONFIGFILE must have user name and password. Run:\n$NAME --configure"
    fi
    curl $CURL_OPT -L -fs -c "$COOKIE_FILE" "${args[@]}" -d "user=$user" \
         --data-urlencode "passwd=$pass" "$AUR_URL_LOGIN" \
         -o "$AV_TMP/login" || error "Unable to access $AUR_URL_LOGIN"
    err=$(sed -ne 's/.*ul class="errorlist"><li>\([^<]*\)<.*/\1/p' "$AV_TMP/login")
    [[ $err ]] && error "$err"
}

aur_check_vote() {
    local pkg
    for pkg in "${pkgnames[@]}"; do
        curl $CURL_OPT -fs -b "$COOKIE_FILE" \
             "${AUR_URL_PKG_PAGE}${pkg}${AUR_SETLANG}" \
             -o "$AV_TMP/$pkg.$PID" ||
                 error "Unable to get $pkg page"
        if sed '/<div id="news">/q' "$AV_TMP/$pkg.$PID" | grep -q /unvote/; then
            echo "already voted"
        elif sed '/<div id="news">/q' "$AV_TMP/$pkg.$PID" | grep -q /vote/; then
            echo "not voted"
        else
            echo "voted status not found"
        fi
    done
}


aur_vote() {
    local pkg
    (($1)) && vote="/vote/" || vote="/unvote/"
    for pkg in "${pkgnames[@]}"; do
        curl $CURL_OPT -fs -b "$COOKIE_FILE" \
             "${AUR_URL_PKG_PAGE}${pkg}${vote}" -o /dev/null
        if (($?)); then
            echo "Error: Can't (un)vote for $pkg"
        else
            echo "$pkg : vote changed"
        fi
    done
}

create_config_file() {
    local ans
    if [[ -f "$CONFIGFILE" ]]; then
        read -p "$CONFIGFILE exists. Replace ? [y/N] " ans
        [[ $ans != 'Y' && $ans != 'y' ]] && return 0
    fi
    echo -n > "$CONFIGFILE"
    if [[ ! -r "$CONFIGFILE" ]]; then
        error "Unable to create $CONFIGFILE"
    fi
    echo "Creation of $CONFIGFILE"
    read -p "AUR User : " ans
    printf "user=%q\n" "$ans" >> "$CONFIGFILE"
    read -p "AUR Password : " ans
    printf "pass=%q\n" "$ans" >> "$CONFIGFILE"
    read -p "Persistent login ? [Y/n] " ans
    [[ $ans = 'n' || $ans = 'N' ]] && return 0
    read -p "Path to the cookie file : [/var/tmp/aurvote-$USER.cookie] ? " ans
    printf "COOKIE_FILE=%q\n" "${ans:-/var/tmp/aurvote-$USER.cookie}" >> "$CONFIGFILE"
    echo
    echo "Creation complete."
}

### MAIN PROGRAM ###
umask 077
[[ -d "$AV_TMP" ]] || mkdir -p "$AV_TMP"
[[ -d "$AV_TMP" && -w "$AV_TMP" ]] || error "Cannot access to $AV_TMP"
PID=$$
ACTION="vote"
pkgnames=()
CURL_OPT=""
PERSIST=0

[[ -r "$CONFIGFILE" ]] && source "$CONFIGFILE"

[[ $COOKIE_FILE ]] && PERSIST=1

while [[ $1 ]]; do
    case $1 in
        --help|-h)    usage; exit 0;;
        --version|-V) version; exit 0;;
        --check|-c)   ACTION="check";;
        --configure)  ACTION="configure";;
        --vote|-v)    ACTION="vote";;
        --unvote|-u)  ACTION="unvote";;
        --id)         ;; # deprecated
        --insecure)   CURL_OPT+=" --insecure";; 
        -k)           PERSIST=1; shift; COOKIE_FILE="$1";;
        --*|-*)       usage; exit 1;;
        *)            pkgnames+=("$1");;
    esac
    shift
done

if [[ $ACTION = "configure" ]]; then
    create_config_file
    exit 0
fi

COOKIE_FILE=${COOKIE_FILE:-"$AV_TMP/cookies"}
pkgnames=("${pkgnames[@]%/*}") # compatibility with yaourt <= 1.2.1
[[ ! $pkgnames ]] && usage && exit 1
 
aur_login

case "$ACTION" in
    check)  aur_check_vote;;
    vote)   aur_vote 1;;
    unvote) aur_vote 0;;
esac

# vim: set ts=4 sw=4 et: