Advertisement
Guest User

HC Admin Script FIX

a guest
Sep 4th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.86 KB | None | 0 0
  1. ------------------------------------------------------------------------------
  2. -- Module API
  3. ------------------------------------------------------------------------------
  4.  
  5. function hc.messaging.init()
  6.     hc.add_menu_command("Messages", hc.messaging.messages_command, hc.MESSAGES_LEVEL, hc.COMMAND_MENU_KEY)
  7.  
  8.     hc.add_say_command("vm", hc.messaging.vm_command, hc.VM_LEVEL, "[<message>]", "Send a message to all VIP's online.", true)
  9.     hc.add_say_command("mm", hc.messaging.mm_command, hc.MM_LEVEL, "[<message>]", "Send a message to all moderators online.", true)
  10.     hc.add_say_command("<id>[,<id>...]", hc.messaging.pm_command, hc.PM_LEVEL, "[<message>]", "Send a private message to a few specified players.", true, "[%d,]+")
  11.     hc.add_say_command("om", hc.messaging.om_command, hc.OM_LEVEL, "[<message>]", "Send an offline message to a user.", true)
  12.     hc.add_say_command("ombc", hc.messaging.ombc_command, hc.OMBC_LEVEL, "[<message>]", "Send an offline message to all or a group of users.", true)
  13.     hc.add_say_command("bc", hc.messaging.bc_command, hc.BC_LEVEL, "[<message>]", "Send a broadcast message.", true)
  14.  
  15.     addhook("init_player", "hc.messaging.init_player_hook")
  16.     addhook("team", "hc.messaging.team_hook")
  17. end
  18.  
  19.  
  20. ------------------------------------------------------------------------------
  21. -- Internal functions
  22. ------------------------------------------------------------------------------
  23.  
  24. function hc.messaging.send_pm(p, idstring, msg, cmd, tag, title)
  25.     local ids = {}
  26.  
  27.     for id in string.gmatch(idstring, "%d+") do
  28.         table.insert(ids, tonumber(id))
  29.     end
  30.  
  31.     local cmd = "<id>[,<id>...]"
  32.  
  33.     if #ids > 0 then
  34.         local error = false
  35.  
  36.         for _,i in ipairs(ids) do
  37.             if i == p then
  38.                 hc.cmd_error(p, cmd, "You can't send a private message to yourself.")
  39.                 error = true
  40.             elseif not hc.player_exists(i) then
  41.                 hc.cmd_error(p, cmd, "Player #" .. i .. " does not exist.")
  42.                 error = true
  43.             end
  44.         end
  45.         if not error then
  46.             hc.players[p].messaging.recipients = ids
  47.             if msg ~= nil and msg ~= "" then
  48.                 hc.messaging.send_pm_cb(p, { msg })
  49.             else
  50.                 local rec_string
  51.  
  52.                 if #ids > 1 then
  53.                     rec_string = hc.messaging.get_receiver_strings(ids, 10000, true)[1]
  54.                 else
  55.                     rec_string = player(ids[1], "name")
  56.                 end
  57.                 hc.messaging.compose_message(p, hc.messaging.send_pm_cb, "Private Message to " .. rec_string)
  58.             end
  59.         end
  60.     else
  61.         hc.cmd_error(p, cmd, "Illegal/missing ID.")
  62.     end
  63. end
  64.  
  65. function hc.messaging.send_pm_cb(p, msg)
  66.     local colour_rec = hc.LIGHT_GREY
  67.     local colour_snd = hc.MEDIUM_GREY
  68.     local ids = hc.players[p].messaging.recipients
  69.     local tag = "PM"
  70.     local sender_name = player(p, "name")
  71.  
  72.     local rec_prefix = colour_rec .. tag .. ": " .. p .. ". " .. sender_name
  73.  
  74.     -- Show to receiver.
  75.     for _,id in ipairs(ids) do
  76.         if #ids > 1 then
  77.             local rec_list = {}
  78.  
  79.             for _,i in ipairs(ids) do
  80.                 if i ~= id then
  81.                     table.insert(rec_list, i)
  82.                 end
  83.             end
  84.             local rec_string = hc.messaging.get_receiver_strings(rec_list, 60 - rec_prefix:len())
  85.  
  86.             hc.msgs2(id, rec_string, rec_prefix .. " -> ")
  87.         end
  88.        
  89.         local count = 0
  90.         for _,line in ipairs(msg) do
  91.             local text = hc.censor_text(line)
  92.  
  93.             msg2(id, rec_prefix .. ": " .. text)
  94.            
  95.             count = count + 1
  96.             if count >= 10 then
  97.                 return
  98.             end
  99.         end
  100.     end
  101.  
  102.     -- Show to sender.
  103.     if #ids > 1 then
  104.         local rec_string = hc.messaging.get_receiver_strings(ids, 60)
  105.  
  106.         hc.msgs2(p, rec_string, colour_snd .. tag .. ": -> ")
  107.  
  108.         for _,line in ipairs(msg) do
  109.             local text = hc.censor_text(line)
  110.  
  111.             hc.msgs2(p, text, colour_snd .. tag .. ": ")
  112.         end
  113.     else
  114.         local recip_name = player(ids[1], "name")
  115.         for _,line in ipairs(msg) do
  116.             local text = hc.censor_text(line)
  117.  
  118.             msg2(p, colour_snd .. tag .. ": -> " .. ids[1] .. ". " .. recip_name .. ": " .. text)
  119.         end
  120.     end
  121. end
  122.  
  123. function hc.messaging.get_receiver_strings(receivers, width, ommit_names)
  124.     local strings = {}
  125.     local str = ""
  126.  
  127.     for _,id in ipairs(receivers) do
  128.         if str:len() > width then
  129.             table.insert(strings, str)
  130.             str = ""
  131.         end
  132.         if str ~= "" then
  133.             str = str .. ", "
  134.         end
  135.         str = str .. id
  136.         if not ommit_names then
  137.             str = str .. ". " .. player(id, "name")
  138.         end
  139.     end
  140.     if str ~= "" then
  141.         table.insert(strings, str)
  142.     end
  143.     return strings
  144. end
  145.  
  146.  
  147. function hc.messaging.send_mm(p, msg)
  148.     for _,line in ipairs(msg) do
  149.         local text = hc.censor_text(line)
  150.  
  151.         for i=1,hc.SLOTS do
  152.             if hc.player_exists(i) and hc.is_moderator(i) and i ~= p then
  153.                 msg2(i, hc.LIGHT_GREY .. "MM: " .. p .. ". " .. player(p, "name") .. ": " .. text)
  154.             end
  155.         end
  156.         msg2(p, hc.MEDIUM_GREY .. "MM: " .. text)
  157.     end
  158. end
  159.  
  160. function hc.messaging.send_vm(p, msg)
  161.     for _,line in ipairs(msg) do
  162.         local text = hc.censor_text(line)
  163.  
  164.         for i=1,hc.SLOTS do
  165.             if hc.player_exists(i) and hc.is_vip(i) and not hc.is_moderator(i) and i ~= p then
  166.                 msg2(i, hc.LIGHT_GREY .. "VM: " .. p .. ". " .. player(p, "name") .. ": " .. text)
  167.             end
  168.         end
  169.         msg2(p, hc.MEDIUM_GREY .. "VM: " .. text)
  170.     end
  171. end
  172.  
  173. function hc.messaging.get_messages_fn(usgn)
  174.     return hc.MESSAGES_DIR_PATH .. "/" .. usgn .. ".hcm"
  175. end
  176.  
  177. function hc.messaging.send_om(p, recipient, message)
  178.     local filename = hc.messaging.get_messages_fn(recipient)
  179.     local sender_usgn = hc.get_usgn(p)
  180.     local t = { { sender_usgn, os.time(), unpack(message) } }
  181.  
  182.     hc.write_file(filename, t, true)
  183.  
  184.     local i = hc.get_player_id(recipient)
  185.     if i ~= nil then
  186.         hc.info(i, "You have received a new message from " .. hc.users[sender_usgn].name .. ".")
  187.     end
  188. end
  189.  
  190. function hc.messaging.compose_message(p, callback, title, message)
  191.     hc.players[p].messaging.send_cb = callback
  192.  
  193.     hc.open_editor(p, message, "Composing " .. title,
  194.         {
  195.             "",
  196.             { title = "Send Message", func = hc.messaging.send_cb },
  197.             { title = "Print to Log", func = hc.messaging.print_om },
  198.             { title = "Cancel Message", func = hc.messaging.cancel_cb }
  199.         })
  200. end
  201.  
  202. function hc.messaging.view_message(p, from, usgn, time, title, message)
  203.     local header = "From: " .. from .. ", " .. os.date("%d %B %Y %X", time)
  204.     local msg = { header, unpack(message) }
  205.  
  206.     hc.players[p].messaging.current_om = { from = from, usgn = usgn, time = time, header = header, message = message }
  207.  
  208.     hc.open_editor(p, msg, "Viewing " .. title,
  209.         {
  210.             { title = "Previous Message", func = hc.messaging.previous_om },
  211.             { title = "Next Message", func = hc.messaging.next_om },
  212.             "",
  213.             { title = "Reply", func = hc.messaging.reply_om },
  214.             { title = "Forward", func = hc.messaging.forward_om },
  215.             { title = "Print to Log", func = hc.messaging.print_om },
  216.             { title = "Delete", func = hc.messaging.delete_om },
  217.             {
  218.                 title = "Back to Messages",
  219.                 func = function(p)
  220.                     hc.close_editor(p)
  221.                     hc.messaging.messages_command(p)
  222.                 end
  223.             },
  224.             {
  225.                 title = "Close Messages",
  226.                 func = function(p)
  227.                     hc.close_editor(p)
  228.                 end
  229.             }
  230.         },
  231.         true)
  232. end
  233.  
  234. function hc.messaging.select_recipient_and_send_om(p, msg)
  235.     hc.players[p].messaging.om = msg
  236.     hc.show_menu(p, "Recipient", hc.get_users(),
  237.         function(p, id, item)
  238.             local name = hc.users[item.usgn].name
  239.             local yes = 1
  240.             local yes_new = 2
  241.             local no_new = 3
  242.             local no = 4
  243.             local usgn = item.usgn
  244.  
  245.             hc.show_menu(p, "Send message to " .. name .. "?",
  246.                 {
  247.                     { title = "Yes", value = yes },
  248.                     { title = "Yes and to another recipient", value = yes_new },
  249.                     { title = "No. Select a new recipient", value = no_new },
  250.                     { title = "No. Scratch it.", value = no }
  251.                 },
  252.                 function(p, id, item)
  253.                     if item.value == yes or item.value == yes_new then
  254.                         hc.messaging.send_om(p, usgn, hc.players[p].messaging.om)
  255.                         hc.event(p, "Message sent to " .. name .. ".")
  256.                     end
  257.                     if item.value == yes_new or item.value == no_new then
  258.                         hc.messaging.select_recipient_and_send_om(p, msg)
  259.                     end
  260.                 end)
  261.         end)
  262. end
  263.  
  264.  
  265. -------------------------------------------------------------------------------
  266. -- Menu callbacks
  267. -------------------------------------------------------------------------------
  268.  
  269. function hc.messaging.send_cb(p, message, current_line)
  270.     if #message > 0 then
  271.         hc.close_editor(p)
  272.         hc.players[p].messaging.send_cb(p, message)
  273.     else
  274.         hc.info(p, "Nothing to send!")
  275.     end
  276. end
  277.  
  278. function hc.messaging.cancel_cb(p, message, current_line)
  279.     if #message > 0 then
  280.         local entries = {
  281.             { title = "Yes: Message will be lost!", value = true },
  282.             { title = "No: Continue composing!", value = false }
  283.         }
  284.         hc.show_menu(p, "Cancel Composing?", entries,
  285.             function(id, _, item)
  286.                 if item.value then
  287.                     hc.close_editor(p)
  288.                 end
  289.             end)
  290.     else
  291.         hc.close_editor(p)
  292.     end
  293. end
  294.  
  295. function hc.messaging.next_om(p)
  296.     local om = hc.players[p].messaging.current_om
  297.  
  298.     local filename = hc.messaging.get_messages_fn(hc.get_usgn(p))
  299.     local messages = hc.read_file(filename)
  300.  
  301.     for i=#messages,1,-1 do
  302.         local row = messages[i]
  303.         local from, usgn, time, message = hc.messaging.parse_message_item(row)
  304.  
  305.         if time < om.time then
  306.             hc.close_editor(p)
  307.             hc.messaging.view_message(p, from, usgn, time, "Offline Message", message)
  308.             return
  309.         end
  310.     end
  311.     hc.event(p, "No more messages.")
  312. end
  313.  
  314. function hc.messaging.previous_om(p)
  315.     local om = hc.players[p].messaging.current_om
  316.  
  317.     local filename = hc.messaging.get_messages_fn(hc.get_usgn(p))
  318.     local messages = hc.read_file(filename)
  319.  
  320.     for i=1,#messages do
  321.         local row = messages[i]
  322.         local from, usgn, time, message = hc.messaging.parse_message_item(row)
  323.  
  324.         if time > om.time then
  325.             hc.close_editor(p)
  326.             hc.messaging.view_message(p, from, usgn, time, "Offline Message", message)
  327.             return
  328.         end
  329.     end
  330.     hc.event(p, "No more messages.")
  331. end
  332.  
  333. function hc.messaging.print_om(p)
  334.     local om = hc.players[p].messaging.current_om
  335.  
  336.     hc.event(p, om.header)
  337.     for _,line in ipairs(om.message) do
  338.         hc.event(p, line)
  339.     end
  340. end
  341.  
  342. function hc.messaging.reply_om(p)
  343.     hc.close_editor(p)
  344.  
  345.     local om = hc.players[p].messaging.current_om
  346.     local message = { "" }
  347.  
  348.     table.insert(message, "> " .. om.header)
  349.     for _,line in ipairs(om.message) do
  350.         table.insert(message, "> " .. line)
  351.     end
  352.  
  353.     hc.messaging.compose_message(p, function(p, m, cl)
  354.         hc.messaging.send_om(p, om.usgn, m)
  355.         hc.messaging.view_message(p, om.from, om.usgn, om.time,
  356.             "Offline Message", om.message)
  357.     end, "OM Reply", message)
  358. end
  359.  
  360. function hc.messaging.forward_om(p)
  361.     hc.close_editor(p)
  362.  
  363.     local om = hc.players[p].messaging.current_om
  364.     local message = { "" }
  365.  
  366.     table.insert(message, "> " .. om.header)
  367.     for _,line in ipairs(om.message) do
  368.         table.insert(message, "> " .. line)
  369.     end
  370.  
  371.     hc.messaging.compose_message(p, function(p, m, cl)
  372.         hc.messaging.select_recipient_and_send_om(p, m)
  373.         hc.messaging.view_message(p, om.from, om.usgn, om.time,
  374.             "Offline Message", om.message)
  375.     end, "OM Forward", message)
  376. end
  377.  
  378. function hc.messaging.delete_om(p)
  379.     hc.close_editor(p)
  380.  
  381.     local om = hc.players[p].messaging.current_om
  382.     local t = {}
  383.  
  384.     local filename = hc.messaging.get_messages_fn(hc.get_usgn(p))
  385.     local messages = hc.read_file(filename)
  386.  
  387.     for _,row in ipairs(messages) do
  388.         local from, usgn, time, message = hc.messaging.parse_message_item(row)
  389.  
  390.         if om.usgn ~= usgn or om.time ~= time then
  391.             table.insert(t, row)
  392.         else
  393.             hc.event(p, "Message deleted.")
  394.         end
  395.     end
  396.  
  397.     if #t == 0 then
  398.         os.remove(filename)
  399.     else
  400.         hc.write_file(filename, t)
  401.     end
  402.     hc.messaging.messages_command(p)
  403. end
  404.  
  405.  
  406. -------------------------------------------------------------------------------
  407. -- Say commands
  408. -------------------------------------------------------------------------------
  409.  
  410. function hc.messaging.vm_command(p, arg)
  411.     if arg then
  412.         hc.messaging.send_vm(p, { arg })
  413.     else
  414.         hc.messaging.compose_message(p, hc.messaging.send_vm, "VIP Message")
  415.     end
  416. end
  417.  
  418. function hc.messaging.mm_command(p, arg)
  419.     if arg then
  420.         hc.messaging.send_mm(p, { arg })
  421.     else
  422.         hc.messaging.compose_message(p, hc.messaging.send_mm, "Moderator Message")
  423.     end
  424. end
  425.  
  426. function hc.messaging.pm_command(p, cmd, arg)
  427.     hc.messaging.send_pm(p, cmd, arg)
  428. end
  429.  
  430. function hc.messaging.om_command(p, arg)
  431.     if arg then
  432.         hc.messaging.select_recipient_and_send_om(p, { arg })
  433.     else
  434.         hc.messaging.compose_message(p, hc.messaging.select_recipient_and_send_om, "Offline Message")
  435.     end
  436. end
  437.  
  438. function hc.messaging.ombc_command(p, arg)
  439.     local function send_ombc(p, msg)
  440.         local menu = {
  441.             { title = "All", value = { hc.VIP, hc.MODERATOR1, hc.MODERATOR2, hc.ADMINISTRATOR } },
  442.             { title = "VIP's", value = { hc.VIP } },
  443.             { title = "Moderators level 1", value = { hc.MODERATOR1 } },
  444.             { title = "Moderators level 2", value = { hc.MODERATOR2 } },
  445.             { title = "Moderators", value = { hc.MODERATOR1, hc.MODERATOR2 } },
  446.             { title = "Administrators", value = { hc.ADMINISTRATOR } }
  447.         }
  448.  
  449.         hc.show_menu(p, "Recipients", menu,
  450.             function(p, id, item)
  451.                 for usgn,user in pairs(hc.users) do
  452.                     for _,level in ipairs(item.value) do
  453.                         if user.level == level then
  454.                             hc.messaging.send_om(p, usgn, { "*** Broadcast to " .. item.title, unpack(msg) })
  455.                             break
  456.                         end
  457.                     end
  458.                 end
  459.                 hc.event(p, "Message sent to: " .. item.title .. ".")
  460.             end)
  461.     end
  462.  
  463.     if arg then
  464.         send_ombc(p, { arg })
  465.     else
  466.         hc.messaging.compose_message(p, send_ombc, "Offline Broadcast")
  467.     end
  468. end
  469.  
  470. function hc.messaging.bc_command(p, arg)
  471.     local function send_bc(p, msg)
  472.         for _,line in ipairs(msg) do
  473.             local text = hc.censor_text(line)
  474.  
  475.             text = hc.strip_end(text, "[©@]C", 2)
  476.             hc.info(player(p, "name") .. ": " .. text)
  477.         end
  478.     end
  479.  
  480.     if arg then
  481.         send_bc(p, { arg })
  482.     else
  483.         hc.messaging.compose_message(p, send_bc, "Broadcast")
  484.     end
  485. end
  486.  
  487.  
  488. -------------------------------------------------------------------------------
  489. -- Menu commands
  490. -------------------------------------------------------------------------------
  491.  
  492. function hc.messaging.messages_command(p)
  493.     local t = {}
  494.  
  495.     local filename = hc.messaging.get_messages_fn(hc.get_usgn(p))
  496.     local tab = hc.read_file(filename)
  497.  
  498.     for i=#tab,1,-1 do
  499.         local row = tab[i]
  500.         local from, usgn, time, message = hc.messaging.parse_message_item(row)
  501.  
  502.         table.insert(t, {
  503.             title = from .. "|" .. os.date("%d/%m %H:%M", time),
  504.             from = from,
  505.             usgn = usgn,
  506.             time = time,
  507.             message = message
  508.         })
  509.     end
  510.  
  511.     if #t > 0 then
  512.         hc.show_menu(p, "Messages", t,
  513.             function(p, _, item)
  514.                 hc.messaging.view_message(p, item.from, item.usgn,
  515.                     item.time,
  516.                     "Offline Message",
  517.                     item.message)
  518.             end)
  519.     else
  520.         hc.info(p, "You have no messages. Use '!om' to send a message.")
  521.     end
  522. end
  523.  
  524. function hc.messaging.parse_message_item(row)
  525.     local usgn = tonumber(row[1])
  526.     local time = tonumber(row[2])
  527.  
  528.     row[1] = usgn
  529.     row[2] = time
  530.  
  531.     local from
  532.     local sender = hc.users[usgn]
  533.  
  534.     if sender then
  535.         from = sender.name
  536.     else
  537.         from = usgn
  538.     end
  539.  
  540.     local message = {}
  541.  
  542.     for i=3,#row do
  543.         table.insert(message, row[i])
  544.     end
  545.     return from, usgn, time, message
  546. end
  547.  
  548.  
  549. -------------------------------------------------------------------------------
  550. -- Hooks
  551. -------------------------------------------------------------------------------
  552.  
  553. function hc.messaging.init_player_hook(p, reason)
  554.     hc.players[p].messaging = {}
  555. end
  556.  
  557. function hc.messaging.team_hook(p)
  558.     local filename = hc.messaging.get_messages_fn(hc.get_usgn(p))
  559.     local f, error = io.open(filename)
  560.     if f == nil then
  561.         return
  562.     end
  563.  
  564.     if f:read(0) ~= nil then
  565.         hc.info(p, "You have messages! Press " .. hc.DEFAULT_KEYS[hc.COMMAND_MENU_KEY] .. " to read.")
  566.     end
  567.     f:close()
  568. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement