-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjjp
executable file
·197 lines (167 loc) · 4.05 KB
/
jjp
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
#!/usr/bin/awk -f
BEGIN {
no_color = "NO_COLOR" in ENVIRON
if (!no_color) {
color["red"] = sprintf("\033[31m")
color["white"] = sprintf("\033[37m")
color["default"] = sprintf("\033[m")
color["dark_gray"] = sprintf("\033[38;5;8m")
color["reset"] = sprintf("\033[m")
color["header"] = color["default"]
color["time"] = color["dark_gray"]
color["time_highlight"] = color["red"]
color["others_msg"] = color["default"]
color["self_nick"] = color["white"]
color["self_msg"] = color["default"]
color["status_nick"] = color["dark_gray"]
color["status_msg"] = color["dark_gray"]
color["status_msg_srv"] = color["default"]
}
}
function isleap(year)
{
return (year % 4 == 0) \
&& (year % 100 != 0) \
|| (year % 400 == 0)
}
function mdays(mon, year)
{
return (mon == 2) \
? (28 + isleap(year)) \
: (30 + (mon + (mon > 7)) % 2)
}
# Split the time in seconds since epoch into a table, with fields named
# as with gmtime(3): tm["year"], tm["mon"], tm["mday"], tm["hour"],
# tm["min"], tm["sec"].
function gmtime(sec, tm)
{
tm["year"] = 1970
while (sec >= (s = 86400 * (365 + isleap(tm["year"])))) {
tm["year"]++
sec -= s
}
tm["mon"] = 1
while (sec >= (s = 86400 * mdays(tm["mon"], tm["year"]))) {
tm["mon"]++
sec -= s
}
tm["mday"] = 1
while (sec >= (s = 86400)) {
tm["mday"]++
sec -= s
}
tm["hour"] = 0
while (sec >= 3600) {
tm["hour"]++
sec -= 3600
}
tm["min"] = 0
while (sec >= 60) {
tm["min"]++
sec -= 60
}
tm["sec"] = sec
}
function tz( cmd, date, h, m)
{
cmd = "date +%z 2>/dev/null"
cmd | getline date
close(cmd)
h = substr(date, 2, 2)
m = substr(date, 4, 2)
tzoffset = substr(date, 1, 1) (h * 3600 + m * 60)
}
function print_header(text)
{
printf "%s%s%s%s", (NR != 1 ? "\n\n" : ""),
color["header"], text, color["reset"]
}
BEGIN {
FS = " "
tz()
for (i = 32; i < 127; i++)
char_to_num[sprintf("%c", i)] = i
} !NF {
next
} $1 == "==>" { # Handle tail's "==> filename <==" headers.
# Prevent message groupings from spanning over headers.
lastnick = ""
gsub("(^==> (.*/)?| <==$)", "")
print_header("== " $0 " ==")
next
} {
if (NF < 2) {
printf "\n%s", $0
lastnick = ""
fflush("/dev/stdout")
next
}
gmtime($1 + tzoffset, tm)
date = sprintf("%04d/%02d/%02d", tm["year"], tm["mon"], tm["mday"])
time = sprintf("%02d:%02d", tm["hour"], tm["min"])
# Display date only when it actually changed.
if (lastdate != date) {
lastnick = ""
lastdate = date
print_header("-- " date " --")
}
nickend = index($2, ">")
type = substr($2, nickend+1)
msg = substr($0, index($0, ">") + 2 + length(type))
nick = substr($2, 2, nickend-2)
# Set the date color for highlights.
if (index(type, "!")) {
printf "\a"
time_color = color["time_highlight"]
} else {
time_color = color["time"]
}
# When JJ_SERVERLOG is set, print messages without nick and in a
# different color.
if (ENVIRON["JJ_SERVERLOG"] != "") {
printf "\n%s%s%s %s%s",
time_color, time, color["status_msg_srv"],
msg, color["reset"]
fflush("/dev/stdout")
next
}
if (!no_color) {
# Status message.
if (nick == "-") {
nick_color = color["status_nick"]
msg_color = color["status_msg"]
# My message.
} else if (index(type, "*")) {
nick_color = color["self_nick"]
msg_color = color["self_msg"]
# Other message.
} else {
# Calculate nick color if necessary.
if ((nick_color = nick_colors[nick]) == "") {
i = 1
sum = 0
while (char = substr(nick, i++, 1))
sum = sum + char_to_num[char]
nick_color = nick_colors[nick] = \
sprintf("\033[38;5;%dm", sum % 232)
}
msg_color = color["others_msg"]
}
}
# Message grouping.
if (lastnick != nick) {
print ""
lastnick = nick
} else if (nick != "-")
nick = ""
# Handle ACTION.
if (index(type, "a")) {
msg = (length(nick) ? nick : lastnick) " " msg
nick = "*"
}
printf "\n%s%s%s %s%10s%s %s%s%s",
time_color, time, color["reset"],
nick_color, nick, color["reset"],
msg_color, msg, color["reset"]
fflush("/dev/stdout")
} END { print "" }