You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

319 lines
7.3 KiB

#!/usr/bin/ruby
$VERBOSE = nil
$BOTVERSION = "v0.0.2-alpha"
require 'rubygems'
require 'jabbot'
require 'ruby-mpd'
require 'yaml'
def configload ()
$CONF = YAML::load_file('conf/config.yml')
# Parsing of the general part of the yaml file
$SCNAME = $CONF["general"]["scriptname"]
# Parsing of the Jabber part of the yaml file
$JABB = $CONF["jabber"]
$NICK = $JABB["nick"]
$LOGIN = $JABB["login"]
$PASS = $JABB["password"]
$RESOURCE = $JABB["resource"]
$ROOM = $JABB["room"]
$CSERV = $JABB["confserver"]
$DROOM = $JABB["devroom"]
$DCSERV = $JABB["devconfserver"]
# Parsing of the MPD part of the yaml file
$MPDC = $CONF["mpd"]
$HOST = $MPDC["host"]
$PORT = $MPDC["port"]
$SITE = $MPDC["site"]
end
begin
configload()
rescue => error
puts "Error during loading configuration!\n" + error.message
exit
end
mpd = MPD.new $HOST, $PORT
mpd.connect
class MPD
def outputs
send_command 'outputs'
end
def pause
send_command 'pause'
end
def outputstatus(list)
outputs = outputrefresh()
if list.kind_of?(Array) then
conout = Hash.new
list.each {
|out|
conout[out.to_i] = outputs[out]
}
return conout
else
return outputs
end
end
def outputtoggle(out)
out = out.to_i
outputs = outputrefresh()
if outputs[out].status == 0 then
send_command "enableoutput #{out}"
else
send_command "disableoutput #{out}"
end
end
def outputrefresh
outarray = send_command 'outputs'
i = 0
outputs = Hash.new
Struct.new("Output", :name, :status)
until(outarray.length < 1)
line = outarray[0]
outid = line[:outputid].to_i
outname = line[:outputname]
outstatus = (line[:outputenabled] ? 1 : 0) # Conversion to int
outputs[outid] = Struct::Output.new(outname, outstatus)
outarray.shift
end
return outputs
end
def outputstatus(list)
outputs = outputrefresh()
if list.kind_of?(Array) then
conout = Hash.new
list.each {
|out|
conout[out.to_i] = outputs[out]
}
return conout
else
return outputs
end
end
def outputtoggle(out)
out = out.to_i
outputs = outputrefresh()
if outputs[out].status == 0 then
send_command "enableoutput #{out}"
else
send_command "disableoutput #{out}"
end
end
def nextsong
stat = send_command "status"
nsid = stat[:nextsongid]
return send_command "playlistid #{nsid}"
end
end
def mpdcontrol(mpd,request, mess, type)
arr = mess.text.split(/\W+/)
if arr[2].nil? == false then
if arr[2].chomp.strip != "" then
param = arr[2].chomp.strip
end
end
def outputsctl (outputs)
reply = ""
def site_state_translate (input)
if input == 0 then
return "off"
else
return "on"
end
end
outputs.each {
|key,var|
outfile = var[:name].split.last
weblink = ""
# This part needs to be modified to fit your setup.
case outfile
when "HQ"
file = "hi.ogg"
when "LQ"
file = "lo.ogg"
when "MP3"
file = "stream.mp3"
end
reply = reply+"#{key} - #{$SITE}/#{file} - #{site_state_translate(var.status)}\n"
}
return reply
end
status = mpd.status[:state].to_s.downcase
case request
when "status"
if status == "stop" then
sticon = ""
else
status == "play" ? sticon = "" : sticon = "▮▮"
end
song = mpd.current_song
if song.name != nil then
answer = "Stream: #{song.name} .:. #{song.title} @ #{song.file}"
else
answer = "#{song.artist} - #{song.title} [album #{song.album}]"
end
song = mpd.current_song
if song.name.nil? then
ns = mpd.nextsong
answer = "#{answer}\nNext song in the queue: #{ns[:artist]} - #{ns[:title]} [album #{ns[:album]}]"
end
if type == "0" then
post "#{sticon} #{answer}"
else
post "#{sticon} #{answer}" => mess.user
end
when "play"
mpd.play
when "pause"
mpd.pause
when "stop"
mpd.stop
when "prev"
mpd.previous
when "next"
mpd.next
when "nextsong"
song = mpd.current_song
if song.name != nil then
answer = "MPD is playing external stream. I can't get next song info."
else
ns = mpd.nextsong
answer = "Next song in queue: #{ns[:artist]} - #{ns[:title]} [album #{ns[:album]}]"
end
if type == "0" then
post answer
else
post answer => mess.user
end
when "toggle"
if param.nil? == false then
if param.to_i >= 0 and param.to_i <= 2 then
mpd.outputtoggle param.to_i
end
end
when "info"
if status == "stop" then
sticon = ""
else
status == "play" ? sticon = "" : sticon = "▮▮"
end
song = mpd.current_song
if song.name != nil then
answer = "Stream: #{song.name} .:. #{song.title} @ #{song.file}"
else
answer = "#{song.artist} - #{song.title} [album #{song.album}]"
end
if song.name != nil then
nsanswer = "MPD is playing external stream. I can't get next song info."
else
ns = mpd.nextsong
nsanswer = "Next song in queue: #{ns[:artist]} - #{ns[:title]} [album #{ns[:album]}]"
end
outputs = mpd.outputstatus([0, 1, 2])
outs = outputsctl(outputs)
if type == "0" then
post "#{sticon} #{answer}\n#{nsanswer}\nOutputs:\n#{outs}"
else
post "#{sticon} #{answer}\n#{nsanswer}\nOutputs:\n#{outs}" => mess.user
end
when "outputs"
outputs = mpd.outputstatus([0, 1, 2])
if type == "0" then
post outputsctl(outputs)
else
post outputsctl(outputs) => mess.user
end
when "version"
if type == "0" then
post "Pitriss MPD stream control bot, version #{$BOTVERSION}"
else
post "Pitriss MPD stream control bot, version #{$BOTVERSION}" => mess.user
end
when "help"
answer = "Stream is located at #{$SITE}/xyz.ext (see outputs)\n
Commands:\n
status - Info about currently played track
nestsong - Info about the next song in the queue
info - Verbose info about the most relevant aspects
outputs - Lists all outputs available and its status
toggle <output number> - Toggle state of the output
play - start the MPD
pause - pause the MPD
stop - stops the MPD
next - next song
prev - previous song\n\n
For more info ask in the room. (eg howto access playlist control)\n
Playlist control (Adding/Removing songs to/from the playlist) is not possible via this bot."
if type == "0" then
post answer
else
post answer => mess.user
end
else
if type == "0" then
post "Are you kiddin' me? That's complete bullshit! I don't believe single word you told me! Maybe you can try again with \"help\"?"
else
post "Are you kiddin' me? That's complete bullshit! I don't believe single word you told me! Maybe you can try again with \"help\"?" => mess.user
end
end
end
configure do |conf|
conf.login = $LOGIN
conf.password = $PASS
conf.nick = $NICK
conf.resource = $RESOURCE
if $SCNAME == File.basename($0) then
conf.channel = $ROOM
conf.server = $CSERV
else
conf.channel = $DROOM
conf.server = $DCSERV
end
end
message(/^#{$NICK}[,|:| ].*$/i) do |message, params|
arr = message.text.split(/\W+/)
if arr[1].nil? == false then
if arr[1].chomp.strip != "" then
mpdcontrol(mpd,arr[1].chomp.strip,message,"0")
else
mpdcontrol(mpd,"status",message,"0")
end
else
mpdcontrol(mpd,"status",message,"0")
end
end
query do |message, params|
arr = message.text.split(/\W+/)
if arr[0].nil? == false then
if arr[0].chomp.strip != "" then
mpdcontrol(mpd,arr[0].chomp.strip,message, "1")
else
mpdcontrol(mpd,"status",message,"1")
end
else
mpdcontrol(mpd,"status",message,"1")
end
end