Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --// dctoirc.lua: A simple passthrough for IRC to dc++ chat
- debug = false
- --// if this is intended for multiple users at the IRC end then set this true
- multiuser = false
- --//IRC details
- server = "irc.freenode.net"
- port = 6667
- --// you may use a nick instead of a channel to have it /msg straight to that nick
- channel = "#channel"
- --// nick for IRC, the DC++ nick is configured in the client
- nick = "ZBot"
- --// show DC++ joins in irc
- showjoins = false
- local passthrough = {}
- socket = require("socket")
- client = socket.tcp()
- client:settimeout(1)
- res,err = client:connect(server, port)
- DC():PrintDebug(" **dctoirc.lua: connect",res,err)
- function send(txt)
- local res = client:send(txt.."\r\n")
- if not res then
- print("send","error")
- else
- print("send",txt)
- end
- end
- function recv()
- local res, err = client:receive('*l')
- if err == 'timeout' then
- return res, false, true
- end
- return res, err, false
- end
- function IRCEvents()
- local events = {}
- local event =
- function(trigger, callback)
- local cb = callback
- return
- function()
- if string.match(res, trigger) then
- cb()
- return true
- else
- return false
- end
- end
- end
- return {
- setListener =
- function(trigger, callback)
- table.insert(events,event(trigger,callback))
- end,
- check =
- function()
- local res
- for k,v in pairs(events) do
- v()
- end
- end
- }
- end
- IRC = IRCEvents()
- --// set up our timer to check the IRC channel 5 times a second:
- dcpp:setListener( "timer", "Pings",
- function()
- --// Timer only calls once a second, not really enough for our needs so we loop within it
- for variable = 0, 5, 1 do
- res, err, timeout = recv()
- if not err and not timeout then
- if debug then
- DC():PrintDebug(" **dctoirc.lua: "..res )
- end
- IRC.check()
- print("RECV", res)
- end
- end
- end
- )
- IRC.setListener("PING ",
- function()
- --// normal PING requests from the server
- if string.sub(res,1,4)=="PING" then
- response = string.gsub(res,"PING","PONG",1)
- --// PING requests from other users
- else
- from, timestamp = string.match(res, "^:([%a%d]+)!.* PRIVMSG "..nick.." :.PING ([%d]+).")
- if from and timestamp then
- response = "NOTICE "..from.." :\001PING "..timestamp.."\001"
- end
- end
- send(response)
- end
- )
- IRC.setListener("NOTICE Auth",
- function()
- --// Only join a channel if channel is a channel rather than user
- if string.sub(channel,1,1)=="#" then
- send("JOIN "..channel)
- end
- end
- )
- IRC.setListener("PRIVMSG",
- function()
- --//would ideally use this regex but lua doesn't like it
- --if string.find(res,"^:[%a%d]+!.* PRIVMSG "..channel.."|"..nick.." :.*$") then
- if string.find(res,"^:[%a%d]+!.* PRIVMSG [#%a%d]+ :.*$") then
- user = string.match(res, "^:([%a%d]+)!")
- message = string.match(res, "^:[%a%d]+!.* PRIVMSG [#%a%d]+ :(.*)$")
- --// make /me work
- if string.match(message, "^\001ACTION ") then
- message = "/me "..string.match(message, "^\001ACTION (.*)\001")
- end
- if debug then
- DC():PrintDebug(" **dctoirc.lua: user="..user.." message="..message )
- end
- local hubs = dcpp:getHubs()
- table.foreach(hubs,
- function(_,shub)
- if multiuser then
- shub:sendChat( "<"..user..">"..message )
- else
- shub:sendChat( message )
- end
- end
- )
- end
- end
- )
- dcpp:setListener( "chat", "passthrough",
- function( hub, user, text )
- send("PRIVMSG "..channel.." :<"..user:getNick()..">"..text)
- end
- )
- --// you probably don't want it repeating everything you it says on irc
- --dcpp:setListener( "ownChat", "passthrough",
- -- function( hub, text )
- -- send("PRIVMSG "..channel.." :<"..hub:getOwnNick()..">"..text)
- -- end
- --)
- dcpp:setListener( "unknownChat", "passthrough",
- function( hub, text )
- send("PRIVMSG "..channel.." :**"..text)
- end
- )
- if showjoins then
- --//user join, userConnected doesn't seem to work properly
- dcpp:setListener( "userMyInfo", "passthrough",
- function( hub, user, stuff )
- send("PRIVMSG "..channel.." :** "..user:getNick().." connected")
- end
- )
- dcpp:setListener( "userQuit", "passthrough",
- function( hub, nick )
- send("PRIVMSG "..channel.." :** "..nick.." quit")
- end
- )
- end
- --// Identify
- send("NICK "..nick)
- send("USER "..nick.." 0 * : DC++/IRC passthrough by zed0")
- DC():PrintDebug( " ** Loaded dctoirc.lua **" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement