Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AJAX Chat > PtokaX script
- -- version 1.0.4
- -- by deseven, 2010-2011
- -- main settings
- MysqlHost = "localhost"
- MysqlUser = "ajaxchat"
- MysqlPass = "ajaxchatpwd"
- MysqlDB = "ajaxchat"
- MsgData = "/mnt/ram/recievechat.data"
- IDFile = "/serv/ptokax/scripts/recievechat.id"
- CheckMsgTimer = 3000
- -- codepage and convertion settings
- UseIconv = true
- IconvFrom = "utf8"
- IconvTo = "cp1251//TRANSLIT"
- UseSetNames = true
- ReplaceWhat = { "%[b]","%[/b]","%[u]","%[/u]","%[i]","%[/i]","%[code]","%[/code]","%[quote]","%[/quote]","%[color.-]","%[/color]","%[url.-]","%[/url]","\\n", "|" }
- ReplaceWith = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "\r\n","|" }
- -- split function taken from lua-users wiki
- function Split(str, delim, maxNb)
- -- Eliminate bad cases...
- if string.find(str, delim) == nil then
- return { str }
- end
- if maxNb == nil or maxNb < 1 then
- maxNb = 0 -- No limit
- end
- local result = {}
- local pat = "(.-)" .. delim .. "()"
- local nb = 0
- local lastPos
- for part, pos in string.gfind(str, pat) do
- nb = nb + 1
- result[nb] = part
- lastPos = pos
- if nb == maxNb then break end
- end
- -- Handle the last field
- if nb ~= maxNb then
- result[nb + 1] = string.sub(str, lastPos)
- end
- return result
- end
- function OnStartup()
- -- setting timer
- AJAXChatTimer = TmrMan.AddTimer(CheckMsgTimer,"RecieveMsg")
- -- reading file to get LastMsgID after restart (or first start)
- local IDFileR = assert(io.open(IDFile,"r"))
- LastMsgID = IDFileR:read("*line")
- IDFileR:close()
- end
- RecieveMsg = function()
- -- we do not want to send all messages, right?
- if LastMsgID == nil then else
- -- getting new messages
- if UseSetNames == true then
- 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
- else
- 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
- end
- if UseIconv == true then
- GetMsg = GetMsg.." | iconv -f "..IconvFrom.." -t "..IconvTo.." > "..MsgData
- else
- GetMsg = GetMsg.." > "..MsgData
- end
- os.execute(GetMsg)
- -- opening file with new messages
- local MsgDataR = assert(io.open(MsgData,"r"))
- -- checking if there are no new messages
- if MsgDataR:read("*line") == nil then
- MsgDataR:close()
- else
- -- reading new messages
- MsgDataR:close()
- MsgDataR = assert(io.open(MsgData,"r"))
- local msgs = {}
- for line in MsgDataR:lines() do
- table.insert(msgs, line)
- end
- MsgDataR:close()
- -- replacing bb-codes and sending messages to chat
- local CurrentMsg = {}
- for i,v in ipairs(msgs) do
- CurrentMsg = Split(msgs[i],"\t",3)
- for r,w in ipairs(ReplaceWhat) do
- CurrentMsg[3] = CurrentMsg[3]:gsub(w,ReplaceWith[r])
- end
- if string.find(CurrentMsg[3],"/privmsg") == 1 then
- -- do nothing
- else
- Core.SendToAll("<"..CurrentMsg[2].."> "..CurrentMsg[3])
- end
- end
- LastMsgID = CurrentMsg[1]
- end
- end
- end
- function OnExit()
- -- writing LastMsgID to file
- if LastMsgID == nil then else
- local IDFileW = assert(io.open(IDFile,"w"))
- IDFileW:write(LastMsgID)
- IDFileW:close()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement