Skip to content

Commit

Permalink
Bug fixing spree (see commit for details)
Browse files Browse the repository at this point in the history
lib/irc:

- Fix parsing messages where arguments include a forward slash
- When errors occur, print out error regardless of in debug mode or not

lib/models/channel:

- Add @_bot which includes a reference to the instance of the bot
  - Lua's VM is capable of handling garbage collection over cyclic
    references, otherwise this could introduce a large memory leak.

lib/models/user:

- Add @_bot (similar to above)
- Add assertions over @nick, @user, @host

plugins/command:

- Fix missed case for COMMAND_OK when msgid is passed and using colors
- In command `test`, use the Option invocation of checking accounts

plugins/data:

- Use Option when initializing accounts for users
- Fix calling constructors for User and Channel to reflect above changes
  • Loading branch information
RyanSquared committed Feb 8, 2020
1 parent 18cf8ee commit c4524f8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
14 changes: 7 additions & 7 deletions lib/irc.moon
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class IRCClient
tags <- '@' tag (';' tag)*
tag <- {|
{:is_client: {'+'} :}? -- check: if tag.is_client
{:vendor: {[^/]+} '/' :}?
{:vendor: {[^/ ]+} '/' :}?
{:key: {[^=; ]+} -> esc_tag :}
{:value: ('=' {[^; ]*} -> esc_tag) :}?
|}
Expand Down Expand Up @@ -438,9 +438,9 @@ class IRCClient
pcall: (fn, ...)=>
ok, err = xpcall fn, self\log_traceback, self, ...
if not ok
Logger.debug "Arguments:"
Logger.print "Arguments:"
for arg in *{...}
Logger.debug tostring arg
Logger.print tostring arg
return ok, err

--- Call a function without self and, when failed, print debug information
Expand All @@ -449,16 +449,16 @@ class IRCClient
pcall_bare: (fn, ...)=>
ok, err = xpcall fn, self\log_traceback, ...
if not ok
Logger.debug "Arguments:"
Logger.debug tostring arg for arg in *{...}
Logger.print "Arguments:"
Logger.print tostring arg for arg in *{...}
return ok, err

--- Print a traceback using the internal logging mechanism
-- @see IRCClient\log
log_traceback: (err)=>
err = tostring err
Logger.debug moonscript.errors.rewrite_traceback debug.traceback!, err
Logger.debug "#{Logger.level.error} ---"
Logger.print moonscript.errors.rewrite_traceback debug.traceback!, err
Logger.print "#{Logger.level.error} ---"
return err

--- Log message from IRC server (used in plugins)
Expand Down
3 changes: 2 additions & 1 deletion lib/models/channel.moon
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Map, Option from require 'lib.std'

class Channel
new: (input)=>
new: (bot, input={})=>
@_bot = assert bot
@status = Option input.status -- Bot's status in that channel
@users = Map input.users or {}
@statuses = Map input.statuses or {}
Expand Down
8 changes: 5 additions & 3 deletions lib/models/user.moon
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Map, Option from require 'lib.std'

class User
new: (user, host, opts)=>
@user = input.user
@host = input.host
new: (bot, nick, user, host, opts={})=>
@_bot = assert bot
@nick = assert nick
@user = assert user
@host = assert host
@account = Option opts.account
@away = opts.away == true
@channels = Map opts.channels or {}
Expand Down
6 changes: 4 additions & 2 deletions plugins/command.moon
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ IRCClient\add_sender 'COMMAND_OK', (target, command_name, message, msgid)=>
if msgid
if @config.strip_colors
@send "PRIVMSG", target, "[#{command_name}] #{message}", "+draft/reply": msgid
else
@send "PRIVMSG", target, "[\00303#{command_name}\003] #{message}", "+draft/reply": msgid
else
if @config.strip_colors
@send "PRIVMSG", target, "[#{command_name}] #{message}"
Expand Down Expand Up @@ -68,8 +70,8 @@ IRCClient\add_command "test", async: true, (prefix, target, tags)=>
sleep 5

@users\get(nick)\and_then (client)->
if client.account != ""
@send_ok "Account name: #{client.account}" if client.account != ""
if client.account\is_some!
@send_ok "Account name: #{client.account\unwrap!}"
else
@send_err "Account not known for: #{nick}"

Expand Down
24 changes: 13 additions & 11 deletions plugins/data.moon
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ IRCClient\add_handler 'AWAY', (prefix, args)=>
@users\expect(nick).away = args[#args]

IRCClient\add_handler 'ACCOUNT', (prefix, args)=>
@users\expect(nick).account = args[1] != "*" and args[1] or nil
@users\expect(nick).account = Option args[1] != "*" and args[1] or nil

IRCClient\add_handler 'RENAME', (prefix, args)=>
{old, new} = args
Expand All @@ -65,7 +65,7 @@ IRCClient\add_handler 'JOIN', (prefix, args, tags={})=>
channel = args[1]
{:nick, :user, :host} = prefix

new_user = User user, host, :account
new_user = User self, nick, user, host, :account

-- Make sure channel exists
-- Confirm channel exists in bot object
Expand All @@ -79,7 +79,7 @@ IRCClient\add_handler 'JOIN', (prefix, args, tags={})=>
else
@send_raw 'WHO', channel

channel_entry\or_insert Channel {
channel_entry\or_insert Channel self, {
users: {
[nick]: new_user
}
Expand All @@ -93,7 +93,7 @@ IRCClient\add_handler 'JOIN', (prefix, args, tags={})=>
@users\set nick, new_user

IRCClient\add_hook 'WHOX_001', (nick, account)=>
@users\expect(nick).account = account if account != '0'
@users\expect(nick).account = Option(account) if account != '0'

IRCClient\add_handler 'NICK', (prefix, args)=>
old = prefix.nick
Expand All @@ -119,7 +119,7 @@ IRCClient\add_handler '353', (prefix, args)=>
channel = @channels\expect target

for text in args[#args]\gmatch '%S+'
local status, pre, nick, user, host
local status, pre, nick, ident, host
if text\match statuses
status, pre = text\match "^(#{statuses}*)(.-)$"
else
Expand All @@ -129,14 +129,14 @@ IRCClient\add_handler '353', (prefix, args)=>
else
nick = pre

channel.statuses\set nick, Option status
channel.statuses\set nick, status

if @users\contains_key nick -- NAMES not triggered from JOIN
user = @users\expect nick
user.user = ident
user.host = host
user.user = ident if ident
user.host = host if host
else
@users\set nick, User(ident, host)
@users\set nick, User(self, nick, ident or "", host or "")

-- Make sure channels and users both have links to each other
channel.users\set nick, @users\expect nick
Expand All @@ -145,7 +145,9 @@ IRCClient\add_handler '353', (prefix, args)=>
IRCClient\add_handler '352', (prefix, args)=>
-- Result of WHO
_, user, host, _, nick, away = unpack args
client = @users\entry(nick)\or_insert_with -> User :user, :host
client = @users\entry(nick)\or_insert_with -> User self, nick, user, host
client.user = user
client.host = host
client.away = away\sub(1, 1) == "G" -- "H"ere or "G"one

IRCClient\add_handler 'CHGHOST', (prefix, args)=>
Expand Down Expand Up @@ -178,4 +180,4 @@ IRCClient\add_handler 'QUIT', (prefix, args)=>
IRCClient\add_handler 'PRIVMSG', priority: priority.HIGH, (prefix, args, tags={})=>
account_tag = @get_tag tags, key: "account"
if account_tag and prefix.nick and @users\contains_key prefix.nick
@users\expect(prefix.nick).account = account_tag.value
@users\expect(prefix.nick).account = Option account_tag.value

0 comments on commit c4524f8

Please sign in to comment.