normalize.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. --[[
  2. return the field normalized (comma separated, single space)
  3. and add individual player names to recipient list
  4. --]]
  5. function mail.normalize_players_and_add_recipients(field, recipients)
  6. local order = mail.parse_player_list(field)
  7. for _, c in ipairs(order) do
  8. if recipients[string.lower(c)] == nil then
  9. recipients[string.lower(c)] = c
  10. end
  11. end
  12. return mail.concat_player_list(order)
  13. end
  14. function mail.parse_player_list(field)
  15. if not field then
  16. return {}
  17. end
  18. local separator = ", "
  19. local pattern = "([^" .. separator .. "]+)"
  20. -- get individual players
  21. local player_set = {}
  22. local order = {}
  23. field:gsub(pattern, function(c)
  24. if player_set[string.lower(c)] == nil then
  25. player_set[string.lower(c)] = c
  26. order[#order+1] = c
  27. end
  28. end)
  29. return order
  30. end
  31. function mail.concat_player_list(order)
  32. -- turn list of players back into normalized string
  33. return table.concat(order, ", ")
  34. end
  35. function mail.player_in_list(name, list)
  36. list = list or {}
  37. if type(list) == "string" then
  38. list = mail.parse_player_list(list)
  39. end
  40. for _, c in pairs(list) do
  41. if name == c then
  42. return true
  43. end
  44. end
  45. return false
  46. end
  47. function mail.ensure_new_format(message, name)
  48. if message.to == nil then
  49. message.to = name
  50. end
  51. end