Skip to content
141 changes: 120 additions & 21 deletions HexChat/buffextras.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
-- SPDX-License-Identifier: MIT
hexchat.register('Buffextras', '1', "Format messages from ZNC's buffextras module")

local function strip_brackets (str)
return str:sub(2, #str - 1)
end

hexchat.hook_server_attrs('PRIVMSG', function (word, word_eol, attrs)
if not word[1]:match('^:%*buffextras!') then
return
Expand All @@ -23,20 +19,11 @@ hexchat.hook_server_attrs('PRIVMSG', function (word, word_eol, attrs)

if is_event('joined') then
emit('Join', nick, channel, host)
elseif is_event('quit with message') then
emit('Quit', nick, strip_brackets(word_eol[8]), host)
elseif is_event('quit:') then
elseif is_event('quit') then
emit('Quit', nick, word_eol[6], host)
elseif is_event('parted with message') then
local reason = strip_brackets(word_eol[8])
if reason ~= '' then
emit('Part with Reason', nick, host, channel, reason)
else
emit('Part', nick, host, channel)
end
elseif is_event('parted:') then
elseif is_event('parted') then
local reason = word_eol[6]
if reason ~= '' then
if reason and reason ~= '' then
emit('Part with Reason', nick, host, channel, reason)
else
emit('Part', nick, host, channel)
Expand All @@ -46,13 +33,125 @@ hexchat.hook_server_attrs('PRIVMSG', function (word, word_eol, attrs)
elseif is_event('changed the topic to') then
emit('Topic Change', nick, word_eol[9], channel)
elseif is_event('kicked') then
if word[7] == "Reason:" then
emit('Kick', nick, word[6], channel, strip_brackets(word_eol[8]))
emit('Kick', nick, word[6], channel, word_eol[9])
elseif is_event('set mode') then
modes = word_eol[7]:match('^(.*%S)')
name = nick
if name == nil then
name = word[4]:match('^:([^!]+)$')
end
if hexchat.prefs['irc_raw_modes'] == true then
emit('Raw Modes', name, string.format('%s %s', channel, modes))
else
emit('Kick', nick, word[6], channel, word_eol[9])
local nickmodes = hexchat.props['nickmodes']
local chanmodes = hexchat.props['chanmodes']

server = hexchat.get_info('server')
local chanmodes = chanmodes[server]
if chanmodes == nil then
chanmodes = 'beI,k,l'
end

modes_for_lists, modes_with_param, modes_with_param_when_set, modes_without_param = chanmodes:match('^([^,]*),?([^,]*),?([^,]*),?([^,]*)$')

local sign
local param_pos = 8
local flags = word[7]
for i = 1, #flags do
flag = flags:sub(i,i)
if flag == '+' then
sign = '+'
elseif flag == '-' then
sign = '-'
elseif flag == 'k' then
if sign == '+' then
param = word[param_pos]
param_pos = param_pos + 1
emit('Channel Set Key', name, param)
else
emit('Channel Remove Keyword', name)
end
elseif flag == 'l' then
if sign == '+' then
param = word[param_pos]
param_pos = param_pos + 1
emit('Channel Set Limit', name, param)
else
emit('Channel Remove Limit', name)
end
elseif flag == 'o' then
param = word[param_pos]
param_pos = param_pos + 1
if sign == '+' then
emit('Channel Operator', name, param)
else
emit('Channel DeOp', name, param)
end
elseif flag == 'h' then
param = word[param_pos]
param_pos = param_pos + 1
if sign == '+' then
emit('Channel Half-Operator', name, param)
else
emit('Channel DeHalfOp', name, param)
end
elseif flag == 'v' then
param = word[param_pos]
param_pos = param_pos + 1
if sign == '+' then
emit('Channel Voice', name, param)
else
emit('Channel DeVoice', name, param)
end
elseif flag == 'b' then
param = word[param_pos]
param_pos = param_pos + 1
if sign == '+' then
emit('Channel Ban', name, param)
else
emit('Channel UnBan', name, param)
end
elseif flag == 'e' then
param = word[param_pos]
param_pos = param_pos + 1
if sign == '+' then
emit('Channel Exempt', name, param)
else
emit('Channel Remove Exempt', name, param)
end
elseif flag == 'I' then
param = word[param_pos]
param_pos = param_pos + 1
if sign == '+' then
emit('Channel INVITE', name, param)
else
emit('Channel Remove Invite', name, param)
end
elseif flag == 'q' and string.find(modes_for_lists, 'q') then
param = word[param_pos]
param_pos = param_pos + 1
if sign == '+' then
emit('Channel Quiet', name, param)
else
emit('Channel UnQuiet', name, param)
end
elseif string.find(nickmodes, flag) or string.find(modes_for_lists, flag) or string.find(modes_with_param, flag) then
param = word[param_pos]
param_pos = param_pos + 1
emit('Channel Mode Generic', name, sign, flag, string.format('%s %s', channel, param))
elseif string.find(modes_with_param_when_set, flag) then
if sign == '+' then
param = word[param_pos]
param_pos = param_pos + 1
emit('Channel Mode Generic', name, sign, flag, string.format('%s %s', channel, param))
else
emit('Channel Mode Generic', name, sign, flag, channel)
end
else
emit('Channel Mode Generic', name, sign, flag, channel)
end
end
end
elseif is_event('set mode') then
emit('Raw Modes', nick, string.format('%s %s', channel, word_eol[7]))
else
return -- Unknown event
end
Expand Down