summaryrefslogtreecommitdiff
path: root/convert.sh
blob: 0663566a6eb57664a8f08396bc17daf042feab98 (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
#!/bin/bash

dir='/srv/arch-mirror/arch/arch/archlinux32/irc-logs/#archlinux-ports/'

declare -A colors

create_color() {
  if [ -z "${colors["$1"]}" ]; then
    colors["$1"]=$(
      find "${dir}" -type f -name '*-*-*.html' -exec \
        grep -h "style=\"color:#[0-9a-f]\{6\}\"><$1>" {} \; | \
        sed 's@.* style="color:#\([0-9a-f]\{6\}\)"><.*@\1@' | \
        sort -u
    )
  fi
  if [ -z "${colors["$1"]}" ]; then
    colors["${b}"]=$(hexdump -n 4 -e '4/4 "%08X" 1 "\n"' /dev/urandom | head -c 6)
#    >&2 printf 'unkown user "%s"\n' "$1"
#    exit 42
  fi
  if [ $(echo "${colors["$1"]}" | wc -l) -ne 1 ]; then
    >&2 printf 'user "%s" has multiple colors\n' "$1"
    exit 42
  fi
}

if [ "$1" = 'html' ]; then
  shift
  sed -n '
    /^<font size="2">/{
      s@^<font size="2">(\([^)]\+\))</font><b> @\1 @
      /entered the room\.<\/b><br\/>$/{
        s@^\(\S\+\) \(.*\S\) \[.*] entered the room\.<\/b><br\/>$@\1 --> | \2 has joined #archlinux-ports @
        p
        d
      }
      / left the room /{
        s@^\(\S\+\) \(.*\S\) left the room (quit: \(.*\))\.</b><br/>$@\1 <-- | \2 has quit (\3)@
        p
        d
      }
      / left the room\./{
        s@^\(\S\+\) \(.*\S\) left the room\.</b><br/>$@\1 <-- | \2 has quit (Quit: \2)@
        p
        d
      }
      / is now known as /{
        s@^\(\S\+\) \(.*\S\)\s*</b><br/>$@\1 -- | \2@
        p
        d
      }
      d
    }
    \@</font> <b>\*\*\*\S\+</b></font> @{
      s@^<font color="#[0-9A-F]\{6\}"><font size="2">(\([^)]\+\))</font> <b>\*\*\*\(\S*\)</b></font> \(.*\)<br/>$@\1 * | \2 \3@
      p
      d
    }
    /<font size="2">/{
      s@^<font color="#[0-9A-F]\{6\}"><font size="2">(\([^)]\+\))</font> <b>\(\S\+\):</b></font> @\1 \2 | @
      s@<br/>$@@
      p
      d
    }
  '
else
  sed '
    s/([^()]*@[^()]*) //
  '
  cat
fi | \
sed '
  :a
    $!N
    s/\n\s\+ | / /
    ta
  P
  D
' | \
while read -r a b dummy c; do
  if [ "${dummy}" != '|' ]; then
    >&2 printf 'wrong dummy "%s"\n' "${dummy}"
    exit 42
  fi
  time=$(
    date -d@$(($(date -d"${a}" +%s)+3600*$2)) +%T
  )
  if [ "${b}" = '-->' ]; then
    name="${c%% *}"
    channel="${c##* }"
    printf '<a href="#%s" name="%s" class="time">[%s]</a> -!- <span class="join">%s</span> has joined %s\n<br />\n' \
      "${time}"  "${time}" "${time}" "${name}" "${channel}"
    continue
  fi
  if [ "${b}" = '<--' ]; then
    name="${c%% *}"
    reason="${c##* (}"
    reason="${reason%)*}"
    printf '<a href="#%s" name="%s" class="time">[%s]</a> -!- <span class="quit">%s</span> has quit [%s]\n<br />\n' \
      "${time}"  "${time}" "${time}" "${name}" "${reason}"
    continue
  fi
  if [ "${b}" = '--' ]; then
    before="${c%% *}"
    after="${c##* }"
    printf '<a href="#%s" name="%s" class="time">[%s]</a> <span class="nick">%s</span> is now known as <span class="nick">%s</span>\n<br />\n' \
      "${time}" "${time}" "${time}" "${before}" "${after}"
    continue
  fi
  if [ "${b}" = '*' ]; then
    nick="${c%% *}"
    action="${c#* }"
    create_color "${nick}"
    printf '<a href="#%s" name="%s" class="time">[%s]</a> <span class="person" style="color:#%s">* %s %s</span>\n<br />\n' \
      "${time}" "${time}" "${time}" "${colors["${nick}"]}" "${nick}" "${action}"
    continue
  fi
  create_color "${b}"
  printf '<a href="#%s" name="%s" class="time">[%s]</a> <span class="person" style="color:#%s">&lt;%s&gt;</span> %s\n<br />\n' \
    "${time}" "${time}" "${time}" "${colors["${b}"]}" "${b}" "${c}"
done