]>
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()
43 for line
in sys
.stdin
.buffer:
44 [nick
, user
, host
], command
, arguments
= parse(line
)
46 if command
in (b
"PRIVMSG", b
"NOTICE") and len(arguments
) == 2 and nick
:
48 nick_start
= len(b
":")
49 nick_end
= nick_start
+len(nick
)
50 cmd_start
= nick_end
+len(b
"!")+len(user
)+len(b
"@")+len(host
)+len(b
" ")
51 cmd_end
= cmd_start
+len(command
)
52 chan_start
= cmd_end
+len(b
" ")
53 chan_end
= chan_start
+len(chan
)
54 msg_start
= chan_end
+len(b
" ")
55 msg_end
= msg_start
+len(msg
)
56 if line
[msg_start
] == ord(':'):
60 hi_start
, hi_end
= msg_start
, msg_start
61 if m
:= re
.search(rb
"\b%s\b" % re
.escape(myself
), msg
):
62 hi_start
= msg_start
+ m
.start(0)
63 hi_end
= msg_start
+ m
.end(0)
66 DIM
, line
[:nick_start
],
67 BOLD
, line
[nick_start
:nick_end
],
68 DIM
, line
[nick_end
:chan_start
],
69 BOLD
, line
[chan_start
:chan_end
],
70 DIM
, line
[chan_end
:msg_start
],
71 RESET
, line
[msg_start
:hi_start
],
72 REV
, line
[hi_start
:hi_end
],
73 RESET
, line
[hi_end
:msg_end
],
74 REV
, line
[msg_end
:].rstrip(b
"\r\n"),
76 elif command
in (b
"PING", b
"PONG"):
79 if command
== b
"001" and arguments
:
80 myself
= arguments
[0] # store own username for later highlighting it
82 say(DIM
, line
.rstrip(b
"\r\n"), RESET
)