X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Firc.lua;h=948864e108da671deea445d2ae15b656352678e8;hb=HEAD;hp=49a11d3a0d32117a59e9798d27f8ee74542af245;hpb=16a65213dcafa306f3d901762d36751a3f456979;p=luairc.git diff --git a/src/irc.lua b/src/irc.lua index 49a11d3..948864e 100644 --- a/src/irc.lua +++ b/src/irc.lua @@ -1,31 +1,46 @@ --- -- Implementation of the main LuaIRC module --- initialization {{{ -local base = _G -local constants = require 'irc.constants' -local ctcp = require 'irc.ctcp' -local c = ctcp._ctcp_quote -local irc_debug = require 'irc.debug' -local message = require 'irc.message' -local misc = require 'irc.misc' -local socket = require 'socket' -local os = require 'os' -local string = require 'string' -local table = require 'table' --- }}} - --- -- LuaIRC - IRC framework written in Lua --- @release 0.2 -module 'irc' +-- @release 0.3 +local irc = {} -- constants {{{ -_VERSION = 'LuaIRC 0.2' +irc._VERSION = 'LuaIRC 0.3 (Lua 5.3 Port)' +-- }}} + +-- libraries {{{ +local libs = {} + +libs.irc = irc +libs.socket = require 'socket' + +local old_libs = _G.libs +_G.libs = libs + +libs.constants = require 'irc.constants' +libs.ctcp = require 'irc.ctcp' +libs.debug = require 'irc.debug' +libs.misc = require 'irc.misc' +libs.channel = require 'irc.channel' +libs.dcc = require 'irc.dcc' +libs.message = require 'irc.message' + +_G.libs = old_libs + +-- localize modules {{{ +local constants = libs.constants +local ctcp = libs.ctcp +local c = ctcp._ctcp_quote +local irc_debug = libs.debug +local message = libs.message +local misc = libs.misc +local socket = libs.socket -- }}} -- classes {{{ -local Channel = base.require 'irc.channel' +local Channel = libs.channel -- }}} -- local variables {{{ @@ -51,14 +66,14 @@ local ip = nil -- }}} -- defaults {{{ -TIMEOUT = 60 -- connection timeout -NETWORK = "localhost" -- default network -PORT = 6667 -- default port -NICK = "luabot" -- default nick -USERNAME = "LuaIRC" -- default username -REALNAME = "LuaIRC" -- default realname -DEBUG = false -- whether we want extra debug information -OUTFILE = nil -- file to send debug output to - nil is stdout +irc.TIMEOUT = 60 -- connection timeout +irc.NETWORK = "localhost" -- default network +irc.PORT = 6667 -- default port +irc.NICK = "luabot" -- default nick +irc.USERNAME = "LuaIRC" -- default username +irc.REALNAME = "LuaIRC" -- default realname +irc.DEBUG = false -- whether we want extra debug information +irc.OUTFILE = nil -- file to send debug output to - nil is stdout -- }}} -- private functions {{{ @@ -68,21 +83,21 @@ local function main_loop_iter() local rready, wready, err = socket.select(rsockets, wsockets) if err then irc_debug._err(err); return false; end - for _, sock in base.ipairs(rready) do + for _, sock in ipairs(rready) do local cb = socket.protect(rcallbacks[sock]) local ret, err = cb(sock) if not ret then irc_debug._warn("socket error: " .. err) - _unregister_socket(sock, 'r') + irc._unregister_socket(sock, 'r') end end - for _, sock in base.ipairs(wready) do + for _, sock in ipairs(wready) do local cb = socket.protect(wcallbacks[sock]) local ret, err = cb(sock) if not ret then irc_debug._warn("socket error: " .. err) - _unregister_socket(sock, 'w') + irc._unregister_socket(sock, 'w') end end @@ -103,7 +118,7 @@ local function incoming_message(sock) local msg = message._parse(raw_msg) misc._try_call_warn("Unhandled server message: " .. msg.command, handlers["on_" .. msg.command:lower()], - (misc._parse_user(msg.from)), base.unpack(msg.args)) + (misc._parse_user(msg.from)), table.unpack(msg.args)) return true end -- }}} @@ -119,7 +134,7 @@ end -- command handlers {{{ -- on_nick {{{ function handlers.on_nick(from, new_nick) - for chan in channels() do + for chan in irc.channels() do chan:_change_nick(from, new_nick) end callback("nick_change", new_nick, from) @@ -128,7 +143,7 @@ end -- on_join {{{ function handlers.on_join(from, chan) - base.assert(serverinfo.channels[chan], + assert(serverinfo.channels[chan], "Received join message for unknown channel: " .. chan) if serverinfo.channels[chan].join_complete then serverinfo.channels[chan]:_add_user(from) @@ -157,7 +172,7 @@ function handlers.on_mode(from, to, mode_string, ...) if to:sub(1, 1) == "#" then -- handle channel mode requests {{{ - base.assert(serverinfo.channels[to], + assert(serverinfo.channels[to], "Received mode change for unknown channel: " .. to) local chan = serverinfo.channels[to] local ind = 1 @@ -204,7 +219,7 @@ end -- on_topic {{{ function handlers.on_topic(from, chan, new_topic) - base.assert(serverinfo.channels[chan], + assert(serverinfo.channels[chan], "Received topic message for unknown channel: " .. chan) serverinfo.channels[chan]._topic.text = new_topic serverinfo.channels[chan]._topic.user = from @@ -223,7 +238,7 @@ end -- on_kick {{{ function handlers.on_kick(from, chan, to) - base.assert(serverinfo.channels[chan], + assert(serverinfo.channels[chan], "Received kick message for unknown channel: " .. chan) if serverinfo.channels[chan].join_complete then serverinfo.channels[chan]:_remove_user(to) @@ -235,7 +250,7 @@ end -- on_privmsg {{{ function handlers.on_privmsg(from, to, msg) local msgs = ctcp._ctcp_split(msg) - for _, v in base.ipairs(msgs) do + for _, v in ipairs(msgs) do local msg = v.str if v.ctcp then -- ctcp message {{{ @@ -245,16 +260,16 @@ function handlers.on_privmsg(from, to, msg) table.remove(words, 1) -- not using try_call here because the ctcp specification requires -- an error response to nonexistant commands - if base.type(ctcp_handlers[cb]) == "function" then + if type(ctcp_handlers[cb]) == "function" then ctcp_handlers[cb](from, to, table.concat(words, " ")) else - notice(from, c("ERRMSG", received_command, ":Unknown query")) + irc.notice(from, c("ERRMSG", received_command, ":Unknown query")) end -- }}} else -- normal message {{{ if to:sub(1, 1) == "#" then - base.assert(serverinfo.channels[to], + assert(serverinfo.channels[to], "Received channel msg from unknown channel: " .. to) callback("channel_msg", serverinfo.channels[to], from, msg) else @@ -269,7 +284,7 @@ end -- on_notice {{{ function handlers.on_notice(from, to, msg) local msgs = ctcp._ctcp_split(msg) - for _, v in base.ipairs(msgs) do + for _, v in ipairs(msgs) do local msg = v.str if v.ctcp then -- ctcp message {{{ @@ -283,7 +298,7 @@ function handlers.on_notice(from, to, msg) else -- normal message {{{ if to:sub(1, 1) == "#" then - base.assert(serverinfo.channels[to], + assert(serverinfo.channels[to], "Received channel msg from unknown channel: " .. to) callback("channel_notice", serverinfo.channels[to], from, msg) else @@ -297,7 +312,7 @@ end -- on_quit {{{ function handlers.on_quit(from, quit_msg) - for name, chan in base.pairs(serverinfo.channels) do + for name, chan in pairs(serverinfo.channels) do chan:_remove_user(from) end callback("quit", from, quit_msg) @@ -307,7 +322,7 @@ end -- on_ping {{{ -- respond to server pings to make sure it knows we are alive function handlers.on_ping(from, respond_to) - send("PONG", respond_to) + irc.send("PONG", respond_to) end -- }}} -- }}} @@ -316,7 +331,7 @@ end -- on_rpl_topic {{{ -- catch topic changes function handlers.on_rpl_topic(from, chan, topic) - base.assert(serverinfo.channels[chan], + assert(serverinfo.channels[chan], "Received topic information about unknown channel: " .. chan) serverinfo.channels[chan]._topic.text = topic end @@ -324,7 +339,7 @@ end -- on_rpl_notopic {{{ function handlers.on_rpl_notopic(from, chan) - base.assert(serverinfo.channels[chan], + assert(serverinfo.channels[chan], "Received topic information about unknown channel: " .. chan) serverinfo.channels[chan]._topic.text = "" end @@ -333,21 +348,21 @@ end -- on_rpl_topicdate {{{ -- "topic was set by at