]>
git.gir.st - ircpipe.git/blob - contrib/highlight
5 def parse(line
): #-> [nick, user, host], command, arguments
6 prefix
, params
= b
"", []
7 command
, arguments
= None, []
8 nick
, user
, host
= None, None, None
10 line
= line
.rstrip(b
"\r\n")
12 if line
.startswith(b
":"):
13 prefix
, line
= line
.split(b
" ", 1)
14 # handle command and arguments:
15 while b
" " in line
and not line
.startswith(b
":"):
16 word
, line
= line
.split(b
" ", 1)
18 # handle final parameter:
19 params
+= [line
.removeprefix(b
":")]
22 # note: command may also be a numeric reply
23 command
, *arguments
= params
25 if b
"!" in prefix
and b
"@" in prefix
:
26 nick
, rest
= prefix
.removeprefix(b
":").split(b
"!", 1)
27 user
, host
= rest
.split(b
"@", 1)
29 host
= prefix
.removeprefix(b
":")
31 return [nick
, user
, host
], command
, arguments
39 sys
.stdout
.buffer.write(b
"".join(args
)+b
"\n")
40 sys
.stdout
.buffer.flush()
42 def findall(needle
, haystack
):
43 if not needle
: return []
44 spans
, offset
, length
= [], 0, len(needle
)
46 br
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 br
"abcdefghijklmnopqrstuvwxyz"
48 br
"0123456789-[\]^_`{|}")
50 while offset
< len(haystack
):
52 offset
= haystack
.index(needle
, offset
)
53 # check for a word boundary before and after nick:
54 if (offset
==0 or haystack
[offset
-1] not in nickchars
) \
55 and (offset
+length
==len(haystack
) or haystack
[offset
+length
] not in nickchars
):
56 spans
+= [(offset
, length
)]
58 except ValueError as e
:
59 break # no more matches
63 for line
in sys
.stdin
.buffer:
64 [nick
, user
, host
], command
, arguments
= parse(line
)
66 if command
in (b
"PRIVMSG", b
"NOTICE") and len(arguments
) == 2 and nick
:
68 nick_start
= len(b
":")
69 nick_end
= nick_start
+len(nick
)
70 cmd_start
= nick_end
+len(b
"!")+len(user
)+len(b
"@")+len(host
)+len(b
" ")
71 cmd_end
= cmd_start
+len(command
)
72 chan_start
= cmd_end
+len(b
" ")
73 chan_end
= chan_start
+len(chan
)
74 msg_start
= chan_end
+len(b
" ")
75 msg_end
= msg_start
+len(msg
)
76 if line
[msg_start
] == ord(':'):
80 hi_msg
= line
[msg_start
:msg_end
]
81 for s
,l
in reversed(findall(myself
, hi_msg
)):
82 hi_msg
= hi_msg
[:s
] + REV
+ hi_msg
[s
:s
+l
] + RESET
+ hi_msg
[s
+l
:]
85 # - support mIRC highlighting (colors, bold, etc)
86 # https://modern.ircdocs.horse/formatting.html
87 # including \x01ACTION privmsg prefix
88 # - highlight own nick and/or user-def'd strings
91 DIM
, line
[:nick_start
],
92 BOLD
, line
[nick_start
:nick_end
],
93 DIM
, line
[nick_end
:chan_start
],
94 BOLD
, line
[chan_start
:chan_end
],
95 DIM
, line
[chan_end
:msg_start
],
97 REV
, line
[msg_end
:].rstrip(b
"\r\n"),
99 elif command
in (b
"PING", b
"PONG"):
100 pass # hide (could also do "JOIN", "QUIT", motd, names, ...)
102 if command
== b
"001" and arguments
:
103 myself
= arguments
[0] # store own username for later highlighting it
105 DIM
, line
.rstrip(b
"\r\n"),