From 2f6ec8119bb5a11e2850b0a7a0763ad798e3fab6 Mon Sep 17 00:00:00 2001 From: jluehrs2 Date: Tue, 4 Sep 2007 00:18:43 -0500 Subject: split between internal/public functions in misc.lua --- src/irc/misc.lua | 105 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/src/irc/misc.lua b/src/irc/misc.lua index 58208ba..bf4a671 100644 --- a/src/irc/misc.lua +++ b/src/irc/misc.lua @@ -36,36 +36,8 @@ local function exists(filename) end -- }}} --- public functions {{{ --- parse_user {{{ ---- --- Gets the various parts of a full username. --- @param user A usermask (i.e. returned in the from field of a callback) --- @return nick --- @return username (if it exists) --- @return hostname (if it exists) -function parse_user(user) - local found, bang, nick = user:find("^([^!]*)!") - if found then - user = user:sub(bang + 1) - else - return user - end - local found, equals = user:find("^.=") - if found then - user = user:sub(3) - end - local found, at, username = user:find("^([^@]*)@") - if found then - return nick, username, user:sub(at + 1) - else - return nick, user - end -end --- }}} - --- TODO: the rest of these shouldn't be public --- split {{{ +-- internal functions {{{ +-- _split {{{ -- -- Splits str into substrings based on several options. -- @param str String to split @@ -77,7 +49,7 @@ end -- in str will be considered one substring) -- @param rquotes String of characters to use as closing quotes -- @return Array of strings, one for each substring that was separated out -function split(str, delim, end_delim, lquotes, rquotes) +function _split(str, delim, end_delim, lquotes, rquotes) -- handle arguments {{{ delim = "["..(delim or DELIM).."]" if end_delim then end_delim = "["..end_delim.."]" end @@ -133,33 +105,33 @@ function split(str, delim, end_delim, lquotes, rquotes) end -- }}} --- basename {{{ +-- _basename {{{ -- -- Returns the basename of a file (the part after the last directory separator). -- @param path Path to the file -- @param sep Directory separator (optional, defaults to PATH_SEP) -- @return The basename of the file -function basename(path, sep) +function _basename(path, sep) sep = sep or PATH_SEP if not path:find(sep) then return path end return socket.skip(2, path:find(".*" .. sep .. "(.*)")) end -- }}} --- dirname {{{ +-- _dirname {{{ -- -- Returns the dirname of a file (the part before the last directory separator). -- @param path Path to the file -- @param sep Directory separator (optional, defaults to PATH_SEP) -- @return The dirname of the file -function dirname(path, sep) +function _dirname(path, sep) sep = sep or PATH_SEP if not path:find(sep) then return "." end return socket.skip(2, path:find("(.*)" .. sep .. ".*")) end -- }}} --- str_to_int {{{ +-- _str_to_int {{{ -- -- Converts a number to a low-level int. -- @param str String representation of the int @@ -167,7 +139,7 @@ end -- @param endian Which endianness to use (big, little, host, network) (defaultsi -- to ENDIANNESS) -- @return A string whose first INT_BYTES characters make a low-level int -function str_to_int(str, bytes, endian) +function _str_to_int(str, bytes, endian) bytes = bytes or INT_BYTES endian = endian or ENDIANNESS local ret = "" @@ -181,13 +153,13 @@ function str_to_int(str, bytes, endian) end -- }}} --- int_to_str {{{ +-- _int_to_str {{{ -- -- Converts a low-level int to a number. -- @param int String whose bytes correspond to the bytes of a low-level int -- @param endian Endianness of the int argument (defaults to ENDIANNESS) -- @return String representation of the low-level int argument -function int_to_str(int, endian) +function _int_to_str(int, endian) endian = endian or ENDIANNESS local ret = 0 for i = 1, int:len() do @@ -200,13 +172,13 @@ function int_to_str(int, endian) end -- }}} --- ip_str_to_int {{{ +-- _ip_str_to_int {{{ -- TODO: handle endianness here -- -- Converts a string IP address to a low-level int. -- @param ip_str String representation of an IP address -- @return Low-level int representation of that IP address -function ip_str_to_int(ip_str) +function _ip_str_to_int(ip_str) local i = 3 local ret = 0 for num in ip_str:gmatch("%d+") do @@ -217,13 +189,13 @@ function ip_str_to_int(ip_str) end -- }}} --- ip_int_to_str {{{ +-- _ip_int_to_str {{{ -- TODO: handle endianness here -- -- Converts an int to a string IP address. -- @param ip_int Low-level int representation of an IP address -- @return String representation of that IP address -function ip_int_to_str(ip_int) +function _ip_int_to_str(ip_int) local ip = {} for i = 3, 0, -1 do local new_num = math.floor(ip_int / 2^(i * 8)) @@ -234,13 +206,13 @@ function ip_int_to_str(ip_int) end -- }}} --- get_unique_filename {{{ +-- _get_unique_filename {{{ -- -- Returns a unique filename. -- @param filename Filename to start with -- @return Filename (same as the one we started with, except possibly with some -- numbers appended) which does not currently exist on the filesystem -function get_unique_filename(filename) +function _get_unique_filename(filename) if not exists(filename) then return filename end local count = 1 @@ -253,39 +225,39 @@ function get_unique_filename(filename) end -- }}} --- try_call {{{ +-- _try_call {{{ -- -- Call a function, if it exists. -- @param fn Function to try to call -- @param ... Arguments to fn -- @return The return values of fn, if it was successfully called -function try_call(fn, ...) +function _try_call(fn, ...) if base.type(fn) == "function" then return fn(...) end end -- }}} --- try_call_warn {{{ +-- _try_call_warn {{{ -- -- Same as try_call, but complain if the function doesn't exist. -- @param msg Warning message to use if the function doesn't exist -- @param fn Function to try to call -- @param ... Arguments to fn -- @return The return values of fn, if it was successfully called -function try_call_warn(msg, fn, ...) +function _try_call_warn(msg, fn, ...) if base.type(fn) == "function" then return fn(...) else - irc_debug.warn(msg) + irc_debug._warn(msg) end end -- }}} --- value_iter {{{ +-- _value_iter {{{ -- -- Iterator to iterate over just the values of a table. -function value_iter(state, arg, pred) +function _value_iter(state, arg, pred) for k, v in base.pairs(state) do if arg == v then arg = k end end @@ -302,3 +274,32 @@ function value_iter(state, arg, pred) end -- }}} -- }}} + +-- public functions {{{ +-- parse_user {{{ +--- +-- Gets the various parts of a full username. +-- @param user A usermask (i.e. returned in the from field of a callback) +-- @return nick +-- @return username (if it exists) +-- @return hostname (if it exists) +function parse_user(user) + local found, bang, nick = user:find("^([^!]*)!") + if found then + user = user:sub(bang + 1) + else + return user + end + local found, equals = user:find("^.=") + if found then + user = user:sub(3) + end + local found, at, username = user:find("^([^@]*)@") + if found then + return nick, username, user:sub(at + 1) + else + return nick, user + end +end +-- }}} +-- }}} -- cgit v1.2.3-54-g00ecf