/util.lua

http://github.com/JakobOvrum/LuaIRC · Lua · 136 lines · 110 code · 19 blank · 7 comment · 23 complexity · 256b550cc21d5827dcd7350dae3bb99c MD5 · raw file

  1. local setmetatable = setmetatable
  2. local sub = string.sub
  3. local byte = string.byte
  4. local char = string.char
  5. local table = table
  6. local assert = assert
  7. local tostring = tostring
  8. local type = type
  9. local random = math.random
  10. module "irc"
  11. --protocol parsing
  12. function parse(line)
  13. local prefix
  14. local lineStart = 1
  15. if line:sub(1,1) == ":" then
  16. local space = line:find(" ")
  17. prefix = line:sub(2, space-1)
  18. lineStart = space
  19. end
  20. local _, trailToken = line:find("%s+:", lineStart)
  21. local lineStop = line:len()
  22. local trailing
  23. if trailToken then
  24. trailing = line:sub(trailToken + 1)
  25. lineStop = trailToken - 2
  26. end
  27. local params = {}
  28. local _, cmdEnd, cmd = line:find("(%S+)", lineStart)
  29. local pos = cmdEnd + 1
  30. while true do
  31. local _, stop, param = line:find("(%S+)", pos)
  32. if not param or stop > lineStop then
  33. break
  34. end
  35. pos = stop + 1
  36. params[#params + 1] = param
  37. end
  38. if trailing then
  39. params[#params + 1] = trailing
  40. end
  41. return prefix, cmd, params
  42. end
  43. function parseNick(nick)
  44. local access, name = nick:match("^([%+@]*)(.+)$")
  45. return parseAccess(access or ""), name
  46. end
  47. function parsePrefix(prefix)
  48. local user = {}
  49. if prefix then
  50. user.access, user.nick, user.username, user.host = prefix:match("^([%+@]*)(.+)!(.+)@(.+)$")
  51. end
  52. user.access = parseAccess(user.access or "")
  53. return user
  54. end
  55. function parseAccess(accessString)
  56. local access = {op = false, halfop = false, voice = false}
  57. for c in accessString:gmatch(".") do
  58. if c == "@" then access.op = true
  59. elseif c == "%" then access.halfop = true
  60. elseif c == "+" then access.voice = true
  61. end
  62. end
  63. return access
  64. end
  65. --mIRC markup scheme (de-facto standard)
  66. color = {
  67. black = 1,
  68. blue = 2,
  69. green = 3,
  70. red = 4,
  71. lightred = 5,
  72. purple = 6,
  73. brown = 7,
  74. yellow = 8,
  75. lightgreen = 9,
  76. navy = 10,
  77. cyan = 11,
  78. lightblue = 12,
  79. violet = 13,
  80. gray = 14,
  81. lightgray = 15,
  82. white = 16
  83. }
  84. local colByte = char(3)
  85. setmetatable(color, {__call = function(_, text, colornum)
  86. colornum = type(colornum) == "string" and assert(color[colornum], "Invalid color '"..colornum.."'") or colornum
  87. return table.concat{colByte, tostring(colornum), text, colByte}
  88. end})
  89. local boldByte = char(2)
  90. function bold(text)
  91. return boldByte..text..boldByte
  92. end
  93. local underlineByte = char(31)
  94. function underline(text)
  95. return underlineByte..text..underlineByte
  96. end
  97. function checkNick(nick)
  98. return nick:find("^[a-zA-Z_%-%[|%]%^{|}`][a-zA-Z0-9_%-%[|%]%^{|}`]*$") ~= nil
  99. end
  100. function defaultNickGenerator(nick)
  101. -- LuaBot -> LuaCot -> LuaCou -> ...
  102. -- We change a random charachter rather than appending to the
  103. -- nickname as otherwise the new nick could exceed the ircd's
  104. -- maximum nickname length.
  105. local randindex = random(1, #nick)
  106. local randchar = sub(nick, randindex, randindex)
  107. local b = byte(randchar)
  108. b = b + 1
  109. if b < 65 or b > 125 then
  110. b = 65
  111. end
  112. -- Get the halves before and after the changed character
  113. local first = sub(nick, 1, randindex - 1)
  114. local last = sub(nick, randindex + 1, #nick)
  115. nick = first..char(b)..last -- Insert the new charachter
  116. return nick
  117. end