fix dbd logic error
[pdt.git] / pdt.sh
CommitLineData
c0e2704f
IK
1#!/bin/bash
2# Copyright (C) 2019 Ian Kelling
3# SPDX-License-Identifier: AGPL-3.0-or-later
4
5# meant to be sourced.
6
7PATH="$HOME/.local/bin:$PATH"
8
9# usage: rbow [-PROFILE_NAME] [COMMAND]
10#
11# Wrapper for rainbowstream to use multiple logins and tweet directly
12# from the command line. PROFILE_NAME can be anything you want.
13rbow() {
14 local oath
15 oath=$HOME/.rainbow_oauth
16 if [[ $1 == -* ]]; then
17 rm -f $oath
18 ln -s $oath$1 $oath
19 shift
20 fi
21 source ~/src/rainbowstream/venv/bin/activate
22 if (($#)); then
23 f=$(mktemp)
24 cat >$f <<'EOF'
25# adds a short delay after each send for more reliable operation
26set force_conservative 0
27spawn "rainbowstream"
28# wait for prompt
29expect -nocase timeout {exit 1} "@*]: "
30set cmd [lindex $argv 0];
31send "$cmd\r\rq\r"
32interact
33EOF
34 expect $f "$*" || { deactivate; rm -f $f; return 1; }
35 rm -f $f
36 else
37 rainbowstream
38 fi
39 deactivate
40}
41
42diaspora() {
43 source ~/src/diaspy/venv/bin/activate
44 python3 ~/src/jan-pona-mute/jan-pona-mute.py "$@" || { deactivate; return 1; }
45 deactivate
46}
47
48# usage: toot [-PROFILE_NAME] [TOOT_ARGS]
49toot() {
50 source ~/src/toot/venv/bin/activate
51 if [[ $1 == -* ]]; then
52 account=${1#-}
53 shift
54 command toot activate --quiet $account || { deactivate; return 1; }
55 fi
56 command toot "$@" || { deactivate; return 1; }
57 deactivate
58}
59
60
61# post to mastodon + twitter + gnu social post + diaspora
62#
63# usage: pdt [--dbd] [-m MEDIA_FILE] [POST]
64pdt() {
65 if [[ $pdttest ]]; then
66 rbow_account=iank
67 gs_account=fsfes
68 mastodon_account=iank@hostux.social
69 else
70 rbow_account=fsf
71 gs_account=fsf
72 mastodon_account=fsf@hostux.social
73 fi
74 dbd=false
75 while [[ $1 == -* ]]; do
76 case $1 in
77 -m)
78 media="$2"
79 shift 2
80 if [[ ! -e $media ]]; then
81 echo "error: file not found $media"
82 return 1
83 fi
84 if [[ $media == *\ * ]]; then
85 echo "error: file path contains a space. move it to non-space path"
86 return 1
87 fi
88 rbow_arg=" --i $media"
89 toot_arg="--media $media"
90 dia_arg="-p $media "
91 gs_arg="-F media=@$media"
92 ;;
93 --dbd)
94 dbd=true
95 rbow_account=dbd
96 gs_account=dbd
97 mastodon_account=endDRM@hostux.social
98 shift
99 ;;
100 esac
101 done
102 # if we have no argument
103 if (( ! $# )); then
104 read -r -p "input PDT text: " input
105 set -- "$input"
106 fi
107 if [[ $- == *i* ]]; then
108 echo "About to PDT the following line. Press enter to confirm or ctrl-c to quit:"
109 echo "$input"
110 read
111 fi
112 fails=()
113 if ! rbow -$rbow_account t "$*" $rbow_arg; then
114 fails+=(tweet)
115 fi
116 if ! toot -$mastodon_account post "$*" $toot_arg; then
117 fails+=(toot)
118 fi
c5176a05 119 if ! $dbd; then
c0e2704f
IK
120 if ! printf "post %s\n" "${dia_arg}$*" | diaspora; then
121 fails+=(diaspora)
122 fi
123 fi
124 # https://gnusocial.net/doc/twitterapi
125 if ! curl -o /dev/null -sS -u "$gs_account:$(cat ~/.gnusocial_login-$gs_account)" \
126 $gs_arg -F "status=$*" https://status.fsf.org/api/statuses/update.xml; then
127 fails+=(gnu-social)
128 fi
129 if (( ${#fails[@]} )); then
130 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}"); echo
131 echo "FSF ERROR: ${fails[*]} might not have posted" >&2
132 fi
133}
134
135pdt-setup() {
136
137 if [[ $pdttest ]]; then
138 mastodon_accounts=(iank)
139 else
140 mastodon_accounts=(fsf endDRM)
141 fi
142
143 mkdir -p ~/src
144 cd ~/src
145 for repo in errhandle rainbowstream diaspy jan-pona-mute; do
146 if [[ -e $repo ]]; then
147 cd $repo
148 git fetch
149 git reset --hard origin/master
150 git clean -xfffd
151 cd ..
152 else
153 git clone https://vcs.fsf.org/git/$repo.git
154 fi
155 done
156 source ~/src/errhandle/err
157 cp ~/src/rainbowstream/rainbowstream/colorset/config ~/.rainbow_config.json
158
159 if [[ ! -e ~/.local/bin/pip ]]; then
160 tmp=$(mktemp)
161 wget -O$tmp https://bootstrap.pypa.io/get-pip.py
162 python3 $tmp --user
163 hash -r
164 fi
165
166
167 if [[ ! -e ~/.local/bin/virtualenv ]]; then
168 python3 -m pip install --user virtualenv
169 fi
170
171 cd ~/src/rainbowstream
172 python3 -m virtualenv -p python3 venv
173 source venv/bin/activate
174 python3 -m pip install -r requirements.txt
175 python3 -m pip install -e .
176 deactivate
177
178
179 # This repo's upstream is https://alexschroeder.ch/cgit/diaspy which is
180 # recommended in the install instructions here:
181 # https://alexschroeder.ch/cgit/jan-pona-mute/about/
182 cd ~/src/diaspy
183 python3 -m virtualenv -p python3 venv
184 source venv/bin/activate
185 python3 -m pip install -r requirements.txt
186 python3 -m pip install -e .
187 deactivate
188
189 rm -rf ~/src/toot
190 mkdir -p ~/src/toot
191 cd ~/src/toot
192 python3 -m virtualenv -p python3 venv
193 source venv/bin/activate
194 # pip freeze after a pip install.
195 cat >requirements.txt <<'EOF'
196beautifulsoup4==4.8.2
197certifi==2019.11.28
198chardet==3.0.4
199idna==2.8
200requests==2.22.0
201soupsieve==1.9.5
202toot==0.25.2
203urllib3==1.25.8
204urwid==2.1.0
205wcwidth==0.1.8
206EOF
207 python3 -m pip install -r requirements.txt
208 deactivate
209
210
211 if [[ ! -s ~/.config/jan-pona-mute/login ]]; then
212 mkdir ~/.config/jan-pona-mute
213 account=fsf@framasphere.org
214 read -r -p "enter the password for $account > " pass
215 # background: format for this found using the save command
216 cat >~/.config/jan-pona-mute/login <<EOF
217account $account
218password $pass
219login
220EOF
221 fi
222
223 for account in dbd fsf; do
224 if [[ ! -s ~/.rainbow_oauth-$account ]]; then
225 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}");
226 echo "Please login to $account on twitter in your main browser then press enter. After rainbowstream prompt loads, quit with command q"
227 read -r
228 rbow -$account
229 fi
230 if [[ ! -s ~/.gnusocial_login-$account ]]; then
231 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}");
232 read -r -p "please enter the password for $account@status.fsf.org > " pass
233 touch ~/.gnusocial_login-$account
234 chmod 600 ~/.gnusocial_login-$account
235 printf "%s\n" "$pass" > ~/.gnusocial_login-$account
236 fi
237 done
238
239 for account in ${mastodon_accounts[@]}; do
240 if ! toot activate $account@hostux.social &>/dev/null; then
241 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}");
242 echo "Please login to $account on https://hostux.social in your main browser then press enter."
243 read -r
244 toot login -i hostux.social
245 fi
246 done
247 err-allow
248}