Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.27 KB | None | 0 0
  1. -- AJAX Chat > PtokaX script
  2. -- version 1
  3. -- by deseven, 2010
  4.  
  5. -- settings
  6. MysqlHost = "localhost"
  7. MysqlUser = "ajaxchat"
  8. MysqlPass = "ajaxchatpwd"
  9. MysqlDB = "ajaxchat"
  10. MsgData = "/mnt/ram/recievechat.data"
  11. IDFile = "/serv/ptokax/scripts/recievechat.id"
  12. CheckMsgTimer = 3000
  13. UseIconv = true
  14. IconvFrom = "utf8"
  15. IconvTo = "cp1251//TRANSLIT"
  16. ReplaceWhat = { "%[b]","%[/b]","%[u]","%[/u]","%[i]","%[/i]","%[code]","%[/code]","%[quote]","%[/quote]" }
  17. ReplaceWith = { "",    "",     "",    "",     "",    "",     "",       "",        "",        ""          }
  18.  
  19. -- split function taken from lua-users wiki
  20. function Split(str, delim, maxNb)
  21.     -- Eliminate bad cases...
  22.     if string.find(str, delim) == nil then
  23.         return { str }
  24.     end
  25.     if maxNb == nil or maxNb < 1 then
  26.         maxNb = 0    -- No limit
  27.     end
  28.     local result = {}
  29.     local pat = "(.-)" .. delim .. "()"
  30.     local nb = 0
  31.     local lastPos
  32.     for part, pos in string.gfind(str, pat) do
  33.         nb = nb + 1
  34.         result[nb] = part
  35.         lastPos = pos
  36.         if nb == maxNb then break end
  37.     end
  38.     -- Handle the last field
  39.     if nb ~= maxNb then
  40.         result[nb + 1] = string.sub(str, lastPos)
  41.     end
  42.     return result
  43. end
  44.  
  45. function OnStartup()
  46.     -- setting timer
  47.     AJAXChatTimer = TmrMan.AddTimer(CheckMsgTimer,"RecieveMsg")
  48.  
  49.     -- reading file to get LastMsgID after restart (or first start)
  50.     local IDFileR = assert(io.open(IDFile,"r"))
  51.     LastMsgID = IDFileR:read("*line")
  52.     IDFileR:close()
  53. end
  54.  
  55. RecieveMsg = function()
  56.     -- we do not want to send all messages, right?
  57.     if LastMsgID == nil then else
  58.         -- getting new messages
  59.         GetMsg = "echo 'SET NAMES utf8; SELECT id,userName,text FROM ajax_chat_messages WHERE id > "..LastMsgID.." and userID != 50000 and userID != 2147483647 ORDER BY id;' | mysql -B -N -s -h "..MysqlHost.." -u"..MysqlUser.." -p"..MysqlPass.." "..MysqlDB
  60.         if UseIconv == true then
  61.             GetMsg = GetMsg.." | iconv -f "..IconvFrom.." -t "..IconvTo.." > "..MsgData
  62.         end
  63.         os.execute(GetMsg)
  64.  
  65.         -- opening file with new messages
  66.         local MsgDataR = assert(io.open(MsgData,"r"))
  67.  
  68.         -- checking if there's no new messages
  69.         if MsgDataR:read("*line") == nil then
  70.             MsgDataR:close()
  71.         else
  72.             -- reading new messages
  73.             MsgDataR:close()
  74.             MsgDataR = assert(io.open(MsgData,"r"))
  75.             local msgs = {}
  76.             for line in MsgDataR:lines() do
  77.                 table.insert(msgs, line)
  78.             end
  79.             MsgDataR:close()
  80.  
  81.             -- replacing bb-codes and sending messages to chat
  82.             local CurrentMsg = {}
  83.             for i,v in ipairs(msgs) do
  84.                 CurrentMsg = Split(msgs[i],"\t",3)
  85.                 for r,w in ipairs(ReplaceWhat) do
  86.                     CurrentMsg[3] = CurrentMsg[3]:gsub(w,ReplaceWith[r])
  87.                 end
  88.                 Core.SendToAll("<"..CurrentMsg[2].."> "..CurrentMsg[3])
  89.             end
  90.             LastMsgID = CurrentMsg[1]
  91.         end
  92.     end
  93. end
  94.  
  95. function OnExit()
  96.     -- writing LastMsgID to file
  97.     if LastMsgID == nil then else
  98.         local IDFileW = assert(io.open(IDFile,"w"))
  99.         IDFileW:write(LastMsgID)
  100.         IDFileW:close()
  101.     end
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement