Advertisement
deseven

receivechat.lua

Mar 18th, 2011
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.48 KB | None | 0 0
  1. -- AJAX Chat > PtokaX script
  2. -- version 1.0.4
  3. -- by deseven, 2010-2011
  4.  
  5. -- main 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.  
  14. -- codepage and convertion settings
  15. UseIconv = true
  16. IconvFrom = "utf8"
  17. IconvTo = "cp1251//TRANSLIT"
  18. UseSetNames = true
  19. ReplaceWhat = { "%[b]","%[/b]","%[u]","%[/u]","%[i]","%[/i]","%[code]","%[/code]","%[quote]","%[/quote]","%[color.-]","%[/color]","%[url.-]","%[/url]","\\n", "|"      }
  20. ReplaceWith = { "",    "",     "",    "",     "",    "",     "",       "",        "",        "",         "",         "",         "",         "",       "\r\n","|" }
  21.  
  22. -- split function taken from lua-users wiki
  23. function Split(str, delim, maxNb)
  24.     -- Eliminate bad cases...
  25.     if string.find(str, delim) == nil then
  26.         return { str }
  27.     end
  28.     if maxNb == nil or maxNb < 1 then
  29.         maxNb = 0    -- No limit
  30.     end
  31.     local result = {}
  32.     local pat = "(.-)" .. delim .. "()"
  33.     local nb = 0
  34.     local lastPos
  35.     for part, pos in string.gfind(str, pat) do
  36.         nb = nb + 1
  37.         result[nb] = part
  38.         lastPos = pos
  39.         if nb == maxNb then break end
  40.     end
  41.     -- Handle the last field
  42.     if nb ~= maxNb then
  43.         result[nb + 1] = string.sub(str, lastPos)
  44.     end
  45.     return result
  46. end
  47.  
  48. function OnStartup()
  49.     -- setting timer
  50.     AJAXChatTimer = TmrMan.AddTimer(CheckMsgTimer,"RecieveMsg")
  51.  
  52.     -- reading file to get LastMsgID after restart (or first start)
  53.     local IDFileR = assert(io.open(IDFile,"r"))
  54.     LastMsgID = IDFileR:read("*line")
  55.     IDFileR:close()
  56. end
  57.  
  58. RecieveMsg = function()
  59.     -- we do not want to send all messages, right?
  60.     if LastMsgID == nil then else
  61.         -- getting new messages
  62.         if UseSetNames == true then
  63.             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
  64.         else
  65.             GetMsg = "echo '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
  66.         end
  67.         if UseIconv == true then
  68.             GetMsg = GetMsg.." | iconv -f "..IconvFrom.." -t "..IconvTo.." > "..MsgData
  69.         else
  70.             GetMsg = GetMsg.." > "..MsgData
  71.         end
  72.         os.execute(GetMsg)
  73.  
  74.         -- opening file with new messages
  75.         local MsgDataR = assert(io.open(MsgData,"r"))
  76.    
  77.         -- checking if there are no new messages
  78.         if MsgDataR:read("*line") == nil then
  79.             MsgDataR:close()
  80.         else
  81.             -- reading new messages
  82.             MsgDataR:close()
  83.             MsgDataR = assert(io.open(MsgData,"r"))
  84.             local msgs = {}
  85.             for line in MsgDataR:lines() do
  86.                 table.insert(msgs, line)
  87.             end
  88.             MsgDataR:close()
  89.    
  90.             -- replacing bb-codes and sending messages to chat
  91.             local CurrentMsg = {}
  92.             for i,v in ipairs(msgs) do
  93.                 CurrentMsg = Split(msgs[i],"\t",3)
  94.                 for r,w in ipairs(ReplaceWhat) do
  95.                     CurrentMsg[3] = CurrentMsg[3]:gsub(w,ReplaceWith[r])
  96.                 end
  97.                 if string.find(CurrentMsg[3],"/privmsg") == 1 then
  98.                     -- do nothing
  99.                 else
  100.                     Core.SendToAll("<"..CurrentMsg[2].."> "..CurrentMsg[3])
  101.                 end
  102.             end
  103.             LastMsgID = CurrentMsg[1]
  104.         end
  105.     end
  106. end
  107.  
  108. function OnExit()
  109.     -- writing LastMsgID to file
  110.     if LastMsgID == nil then else
  111.         local IDFileW = assert(io.open(IDFile,"w"))
  112.         IDFileW:write(LastMsgID)
  113.         IDFileW:close()
  114.     end
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement