summaryrefslogtreecommitdiff
path: root/tears.cgi
blob: 11dfe6fa79617f22fe46a52ea27b8a690f911cf6 (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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/haserl --shell=/bin/bash

<%

VERSION=0.4

path=$(/opt/bin/pdecode <<<"${PATH_INFO#/}")

error() {
  echo -e "Content-Type: text/plain; charset=utf-8\n"
  echo "$1"
  exit 1
}

irc() {
  echo >/dev/null
  #/opt/bin/birk -c radionova "$1"
}

byline() {
  echo "skrevet av $(ls -ld $1 | awk '{print $3}')"
}

info() {
  stat -c '%n: %U %G %A %Y' $1
}

link() {
  local raw=${REQUEST_URI%\?*}
  local clean="/${raw%/}"
  local rawt="$BASE/$2"
  local cleant="/${rawt%/}"
  local target=""
  [ -n "$3" ] && target=" target='$3'"
  [ $cleant = $clean ] && echo "$1" || echo "<a$target href='$BASE/$2'>$1</a>"
}

qlink() {
  text="$1" && url="$BASE/$2" && shift 2
  local IFS=\& && echo -ne "<a href='$url?$*'>$text</a>\n"
}

redirect() {
  echo -ne "Location: $SCHEME$HOST$1\r\n"
  echo -ne "Status: 302\r\n"
  echo -ne "Content-Type: text/html; charset=utf-8\r\n\r\n"
  echo "٩(͡๏̯͡๏)۶>"
  exit
}

stikkordsøk() {
  [ "$action" = index.html -o "$action" = ditt -o "$action" = gruppe ] || return
  echo "<form method=get>"
  echo "<p><b>stikkordsøk</b>"
  echo "<br><input type=text size=10 name=stikkord value='$GET_stikkord'>"
  echo "<input type=hidden name=dato value='$GET_dato'>"
  echo "</form>"
}

kalender() {
  [ "$action" = index.html -o "$action" = ditt -o "$action" = gruppe ] || return
  echo "<p><b>for mye? reis i tid…</b>"
  IFS=- read y m d <<<"$GET_dato"
  n=0
  [ -n "$GET_stikkord" ] && n=1
  if [ -z "$y" ]; then
    for directory in $(cut -d / -f $((n + 2)) | sort | uniq); do
      year=$(basename $directory)
      qlink "$year" "$action/$args" "dato=$year" "stikkord=$GET_stikkord"
    done
  elif [ -z "$m" ]; then
    echo "<b>"
    qlink "$y" "$action/$args" "stikkord=$GET_stikkord"
    echo "</b><br>"
    for directory in $(cut -d / -f $((n + 3)) | sort | uniq); do
      month=$(basename $directory)
      qlink "$month" "$action/$args" "dato=$y-$month" "stikkord=$GET_stikkord"
    done
  elif [ -z "$d" ]; then
    echo "<b>"
    qlink "$y" "$action/$args" "stikkord=$GET_stikkord"
    echo "/"
    qlink "$m" "$action/$args" "dato=$y" "stikkord=$GET_stikkord"
    echo "</b><br>"
    for directory in $(cut -d / -f $((n + 4)) | sort | uniq); do
      day=$(basename $directory)
      qlink "$day" "$action/$args" "dato=$y-$m-$day" "stikkord=$GET_stikkord"
    done
  else
    echo "<b>"
    qlink "$y" "$action/$args" "stikkord=$GET_stikkord"
    echo "/"
    qlink "$m" "$action/$args" "dato=$y" "stikkord=$GET_stikkord"
    echo "/"
    qlink "$d" "$action/$args" "dato=$y-$m" "stikkord=$GET_stikkord"
    echo "</b><br>"
  fi
}

meny() {
  echo -e "Content-Type: text/html; charset=utf-8\n"
  echo "<!doctype html>"
  echo "<title>tears</title>"
  echo "<link href=$BASE/tears.css rel=stylesheet>"
  echo "<script src=$BASE/date.js></script>"
  echo "<script src=$BASE/sortable.js></script>"
  echo "<script src=$BASE/tears.js></script>"
  echo "<div id='meny'>"
  link "<pre>$(figlet -f threepoint tears)</pre>" ""
  echo "<p><b>hei, $(whoami) &hearts;</b><br>"
  link 'skriv en artikkel' 'skriv'
  echo "<p><b>du har tilgang til…</b><br>"
  link 'dine artikler' 'ditt' && echo "<br>"
  for group in $available; do
    link "$group" "gruppe/$group"
  done
  kalender
  stikkordsøk
  echo "<p><b>kos deg! ٩(͡๏̯͡๏)۶</b>"
  echo "<p class=footer><b>tears $VERSION</b><br>"
  link 'todo' 'todo' && echo '|' && link 'teatime' 'teatime'
  echo "</div>"
}

liste() {
  n=$((${GET_n:-0} + 7))
  finder="$finder -printf '%h\t%TT\t%f\n'"
  #$finder | sort -r | head -$n | tail -7 | while read path; do
  while read path; do
    d=$(cut -f 1 <<<"$path")
    f=$(cut -f 3 <<<"$path")
    ok=1
    [ "$f" ] && kort $d/$f
  done <<< $(eval $finder | sort -r | head -$n | tail -7)
  if [ -z "$ok" ]; then
    echo "<p><b>Ingen artikler</b>"
  else
    echo "<hr><p>"
    qlink "eldre artikler…" "$action/$args" "dato=$GET_dato" "n=$n"
  fi
}

stikkord() {
  find -L stikkord -samefile "$1" | while read file; do
    local imp=${file#stikkord/}
    local ord=${imp%%/*}
    [ -z "$first" ] || echo -n " "
    echo -n "$ord"
    first=1
  done
}

stikk() {
  find -L stikkord -samefile "$1" -delete
  for ord in $2; do
    local path="stikkord/$ord/${1#artikler/}"
    umask 000
    mkdir -p -m 777 "$(dirname $path)"
    ln -s "../../../../../$1" "$path"
  done
}

skjema() {
  [ ! -f "$1" ] && echo "<p>finner ikke $1: <pre>$(xxd <<<$1)</pre>" && exit
  [ ! -w "$1" ] && echo "<p>du har ikke skrivetilgang" && exit
  url=$(/opt/bin/pencode <<< $1)
  echo "<form id=update-article method=post action='$BASE/oppdater/$url'>"
  lupin -f $1
  echo "<p><label>Stikkord:</label>"
  echo "<input type='text' name='stikkord' value='$(stikkord $1)'>"
  echo "<p>"
  echo "<input type='submit' value='Lagre'>"
  echo "<input name=delete type='submit' value='Slett'>"
  echo "<input name=publish type='submit' value='Publiser'>"
  [ -r "$1" ] && link "Vis" $(/opt/bin/pencode / <<<"vis/$1") preview
  echo "</form>"
  #echo "<p><b>$(info $1)</b>"
}

vis() {
  [ ! -f "$1" ] && echo "<p>finner ikke $1: <pre>$(xxd <<<$1)</pre>" && exit
  [ ! -r "$1" ] && echo "<p>du har ikke lesetilgang" && exit
  echo "<article>"
  lupin $1 | /opt/bin/embed
  echo "</article>"
}

lagre() {
  path=$(tr '-' '/' <<< $POST_publisert)
  slug=$(sed -e 's|[ /]|-|g' -e 's|[?:]||g' <<<$POST_title)
  [ -z "$slug" ] && error "Ugyldig overskrift"
  [ -f "artikler/$path/$slug.txt" ] && error "Artikkelen finnes allerede"
  umask 0000
  mkdir -p "artikler/$path"
  lupin -p -e - >"artikler/$path/$slug.txt"
  if [ -z "$POST_privat" ]; then
    chmod 660 "artikler/$path/$slug.txt"
  else
    chmod 640 "artikler/$path/$slug.txt"
  fi
  chgrp $POST_gruppe "artikler/$path/$slug.txt"
  irc "$(whoami) skriver ny artikkel: $POST_title"
  redirect "$BASE/rediger/artikler/$path/$slug.txt"
}

oppdater() {
  [ ! -f "$1" ] && error "finner ikke $1"
  [ ! -w "$1" ] && error "ikke skrivetilgang til $1"

  if [[ -n "$POST_delete" ]]; then
    rm "$1"
    redirect "$BASE"
  fi

  [ -z "$POST_title" ] && error "ingen overskrift"
  [ -z "$POST_stikkord" ] && error "ingen stikkord"

  stikk "$1" "$POST_stikkord"
  lupin -p -e - >$1
  irc "$(whoami) redigerte $POST_title"
  [ -n "$POST_publish" ] && chmod o+r "$1"
  pushd /home/bie/maker >/dev/null; ./maker; popd >/dev/null
  redirect "$BASE/rediger/$1"
}

kort() {
  IFS=/. read -r type y m d hhmm slug ext <<< "$1"
  lupin -t $1
  echo "<p class='x'>$(byline $1), $y/$m/$d"
  [ -r "$1" ] && echo "·" && link "vis" $(/opt/bin/pencode / <<<"vis/$1")
  [ -w "$1" ] && echo "·" && link "rediger" $(/opt/bin/pencode / <<<"rediger/$1")
  lupin -s $1
}

velkommen() {
  echo "<blockquote><p>I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like tears in rain.<p>Time to die.</blockquote>"
}

forbered() {
  echo "<h2>skriv en artikkel…</h2>"
  echo "<form method=post action='$BASE/lagre'>"
  echo "<p><label>Overskrift:</label>"
  echo "<input type=text name=title size=24>"
  echo "<p><label>Dato:</label>"
  echo "<input type=date name=publisert value=$(date +%Y-%m-%d) size=10>"
  echo "<p><label>*</label>"
  echo "<select name='type'>"
  for type in blueprints/*.lua; do
    id=$(basename $type .lua)
    echo "<option value='$id'>$id</option>"
  done
  echo "</select>"
  echo "for"
  echo "<select name=gruppe>"
  for group in $available; do
    echo "<option>$group</option>" 
  done
  echo "</select>"
  echo "(<input type=checkbox name=privat>privat artikkel?)"
  echo "<p><label>*</label><input type=submit value='Forbered artikkel...'>"
  echo "</form>"
}

[ -f tears.rc ] && . tears.rc

umask 0022

groups=$(groups | tr " " "\n" | sort)
available=$(comm -12 - <(ls redaksjoner | sort) <<<"$groups")
#action=${PATH_INFO#$BASE/}
action=${PATH_INFO#/}
if [[ "$action" =~ "/" ]]; then
  args=${action#*/}
  action=${action%%/*}
fi
if [[ "$action" == rediger && $path == stikkord/* ]]; then
  redirect "$BASE/rediger/artikler/${path#stikkord/*/}"
fi
mappe="artikler"
[ -n "$GET_stikkord" ] && mappe="stikkord/$GET_stikkord"
kilde="$mappe/$(sed 's|-|/|g' <<<$GET_dato)"
finder="find -L $kilde -type f -not -name '*~'"
[ "$action" = gruppe ] && finder="$finder -group $args"
[ "$action" = ditt ] && finder="$finder -user $REMOTE_USER"
[ "$REQUEST_METHOD" = GET ] && eval $finder | meny
case $action in
  "") velkommen;;
  index.html|ditt|gruppe) liste;;
  skriv) forbered;;
  rediger) skjema $args;;
  vis) vis $args;;
  lagre) lagre;;
  oppdater) oppdater $args;;
  teatime) irc "$(whoami) <3 teatime"; echo "<p><img class='teatime' src='https://kyber.io/orz/src/1352063360909860.jpg'><br>";;
  spökvåningen) echo "<p><img src='https://kyber.io/orz/src/1360086359591352.jpg'>";;
  todo) echo "<pre>$(cat TODO)</pre>";;
esac
%>