Commit 44ade2c8 authored by Andreas Nedbal's avatar Andreas Nedbal

Merge pull request #7 from nilsding/master

Automatically join the channel with the most users in it
parents 5595cb75 aec02f9c
.directory
config.yml
Gemfile.lock
certs/
GEM
remote: https://rubygems.org/
specs:
Ruby-IRC (1.0.13)
activesupport (4.2.3)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
ffi (1.9.10)
hashie (3.4.2)
i18n (0.7.0)
json (1.8.3)
minitest (5.7.0)
mumble-ruby (1.1.2)
activesupport
hashie
opus-ruby
ruby_protobuf
wavefile
opus-ruby (1.0.1)
ffi
ruby_protobuf (0.4.11)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
wavefile (0.6.0)
PLATFORMS
ruby
DEPENDENCIES
Ruby-IRC
mumble-ruby
require_relative './mumble-ruby/client'
\ No newline at end of file
module Mumble
class Client
alias super_init initialize
# ./lib/mumble-ruby/client.rb:14
def initialize(host, port=64738, username="RubyClient", password="")
@after_callbacks = Hash.new { |h, k| h[k] = [] }
super_init host, port, username, password
yield(@config) if block_given?
end
# Joins the channel with the most users
# @param default_channel [String] The channel to join if there are no other users on the Mumble server.
def join_channel_with_most_users(default_channel = APP_CONFIG['mumble']['channel'])
# this is probably the best one-liner I've ever written in Ruby so far; and it even works! --nilsding
chans_with_users = users.values.map{ |x| channels[x.channel_id] unless x == me}.compact.inject(Hash.new(0)){ |h, c| h[c] += 1; h }.sort_by{ |_k, v| v }.reverse
if chans_with_users.empty?
writeln! "[Mumble] Looks like we're alone, joining channel \033[1m#{default_channel}\033[0m."
join_channel default_channel
return
end
chan_with_most_users = chans_with_users.first
writeln! "[Mumble] Joining channel \033[1m#{chan_with_most_users[0].name}\033[0m, as there are #{chan_with_most_users[1]} users in it."
join_channel chan_with_most_users[0]
end
# Returns the current channel the bot is in.
# @return [Mumble::Channel] The current channel.
def current_channel
channels[me.channel_id]
end
Messages.all_types.each do |msg_type|
# I needed a way to guarantee that a block runs after a specific callback finished.
define_method "after_#{msg_type}" do |&block|
@after_callbacks[msg_type] << block
end
end
private
# ./lib/mumble-ruby/client.rb:138
def run_callbacks(sym, *args)
@callbacks[sym].each { |c| c.call *args }
@after_callbacks[sym].each { |c| c.call *args }
end
# small hack to not spam the console output
def writeln!(msg)
puts msg unless msg == @last_message
@last_message = msg
end
end
end
\ No newline at end of file
......@@ -7,6 +7,7 @@ require "mumble-ruby"
$LOAD_PATH.unshift File.expand_path './lib', File.dirname(__FILE__)
require "irc_colors"
require "ext/mumble-ruby"
VERSION = "0.2.4"
......@@ -33,52 +34,58 @@ irc = IRC.new(APP_CONFIG['irc']['nickname'],
def start(irc, mumble)
#IRC
@irc_thread ||= Thread.new do
IRCEvent.add_callback('whoreply') { |event|
IRCEvent.add_callback('whoreply') do |event|
unless event.stats[7] == APP_CONFIG['irc']['nickname']
realname = event.message
realname.slice! "0 "
$irc_channel = event.channel
$irc_users << {nickname: event.stats[7], realname: realname }
end
}
IRCEvent.add_callback('endofwho') { |event|
end # add_callback 'whoreply'
IRCEvent.add_callback 'endofwho' do |event|
sleep(2)
user_msg = "There are currently #{$irc_users.count} users connected to #{$irc_channel} on #{APP_CONFIG['irc']['server']}<br/>"
$irc_users.each do |user|
user_msg += "<b>#{user[:nickname]}</b> (#{user[:realname]}) <br/>"
end
mumble.text_channel(APP_CONFIG['mumble']['channel'], user_msg)
mumble.text_channel(mumble.current_channel, user_msg)
user_msg = ""
$irc_users = []
$irc_channel = ""
}
IRCEvent.add_callback('join') { |event|
end # add_callback 'endofwho'
IRCEvent.add_callback 'join' do |event|
next unless APP_CONFIG['irc']['announce_joins']
unless event.from == APP_CONFIG['irc']['nickname']
puts "[IRC] #{event.from} joined #{event.channel}"
mumble.text_channel(APP_CONFIG['mumble']['channel'], "<b>#{event.from}</b> joined #{event.channel}")
mumble.text_channel(mumble.current_channel, "<b>#{event.from}</b> joined #{event.channel}")
end
}
IRCEvent.add_callback('part') { |event|
end # add_callback 'join'
IRCEvent.add_callback 'part' do |event|
next unless APP_CONFIG['irc']['announce_joins']
unless event.from == APP_CONFIG['irc']['nickname']
puts "[IRC] #{event.from} left #{event.channel}"
mumble.text_channel(APP_CONFIG['mumble']['channel'], "<b>#{event.from}</b> left #{event.channel}")
mumble.text_channel(mumble.current_channel, "<b>#{event.from}</b> left #{event.channel}")
end
}
IRCEvent.add_callback('quit') { |event|
end # add_callback 'part'
IRCEvent.add_callback 'quit' do |event|
next unless APP_CONFIG['irc']['announce_joins']
unless event.from == APP_CONFIG['irc']['nickname']
puts "[IRC] #{event.from} disconnected from the server"
mumble.text_channel(APP_CONFIG['mumble']['channel'], "<b>#{event.from}</b> disconnected from #{APP_CONFIG['irc']['server']}")
mumble.text_channel(mumble.current_channel, "<b>#{event.from}</b> disconnected from #{APP_CONFIG['irc']['server']}")
end
}
IRCEvent.add_callback('nick') { |event|
end # add_callback 'quit'
IRCEvent.add_callback 'nick' do |event|
next unless APP_CONFIG['irc']['announce_nicks']
puts "[IRC] #{event.from} is now known as #{event.channel}"
mumble.text_channel(APP_CONFIG['mumble']['channel'], "<i>#{event.from}</i> is now known as <b>#{event.channel}</b>")
}
IRCEvent.add_callback('privmsg') { |event|
mumble.text_channel(mumble.current_channel, "<i>#{event.from}</i> is now known as <b>#{event.channel}</b>")
end # add_callback 'nick'
IRCEvent.add_callback 'privmsg' do |event|
message = event.message.gsub(/\s+/m, ' ').strip.split(" ")
command = message[0]
next if command.nil?
......@@ -110,19 +117,21 @@ def start(irc, mumble)
end
when 'mumble'
unless content.empty?
mumble.text_channel(APP_CONFIG['mumble']['channel'], "<b>#{event.from}(#{event.channel}):</b> #{content}".irc_colors)
mumble.text_channel(mumble.current_channel, "<b>#{event.from}(#{event.channel}):</b> #{content}".irc_colors)
puts "[IRC->Mumble] #{event.from}[#{event.channel}]: #{content}"
end
end
end
}
IRCEvent.add_callback('endofmotd') { |event|
end # add_callback 'privmsg'
IRCEvent.add_callback 'endofmotd' do |event|
puts "[IRC] Connected to server #{APP_CONFIG['irc']['server']}"
APP_CONFIG['irc']['channel'].each do |channel|
irc.add_channel(channel)
puts "[IRC] Joined channel: #{channel}"
end
}
end # add_callback 'endofmotd'
irc.connect
end
......@@ -144,7 +153,7 @@ def start(irc, mumble)
APP_CONFIG['help'].each do |cmd|
help_msg += "<b>#{prefix_config}#{cmd['command']}</b> - #{cmd['description']}<br/>"
end
mumble.text_channel(APP_CONFIG['mumble']['channel'], help_msg)
mumble.text_channel(mumble.current_channel, help_msg)
when 'irc'
mumble_msg = content.gsub(/\s+/m, ' ').split(" ")
mumble_chl = mumble_msg[0]
......@@ -166,19 +175,26 @@ def start(irc, mumble)
end
end
end
end
#end of on_text_message
end # on_text_message
mumble.on_user_state do |state|
next unless state.include? "name"
next unless mumble.connected?
next unless APP_CONFIG['mumble']['announce_joins']
name = state['name'].gsub("\n", '')
puts "[Mumble] #{name} connected"
APP_CONFIG['irc']['channel'].each do |channel|
irc.send_message(channel, "\x039[+]\x0F \x02#{name}\x0f connected to #{APP_CONFIG['mumble']['server']}")
next if mumble.users[state['actor']] == mumble.me
if state.include? "name"
next unless APP_CONFIG['mumble']['announce_joins']
name = state['name'].gsub("\n", '')
puts "[Mumble] #{name} connected"
APP_CONFIG['irc']['channel'].each do |channel|
irc.send_message(channel, "\x039[+]\x0F \x02#{name}\x0f connected to #{APP_CONFIG['mumble']['server']}")
end
end
end
end # on_user_state
mumble.after_user_state do |state|
next unless mumble.connected?
next if mumble.users[state['actor']] == mumble.me
mumble.join_channel_with_most_users
end # after_user_state
mumble.on_user_remove do |x|
next unless APP_CONFIG['mumble']['announce_joins']
......@@ -189,19 +205,23 @@ def start(irc, mumble)
APP_CONFIG['irc']['channel'].each do |channel|
irc.send_message(channel, "\x034[-]\x0F \x02#{name}\x0f disconnected from #{APP_CONFIG['mumble']['server']}")
end
end
mumble.connect
puts "[Mumble] Connected to server #{APP_CONFIG['mumble']['server']}"
end # on_user_remove
mumble.after_user_remove do
mumble.join_channel_with_most_users
end # after_user_remove
mumble.on_connected do
mumble.me.mute
mumble.me.deafen
end
end # on_connected
sleep(2)
mumble.join_channel(APP_CONFIG['mumble']['channel'])
puts "[Mumble] Joined channel: #{APP_CONFIG['mumble']['channel']}"
mumble.on_server_sync do
puts "[Mumble] Connected to server #{APP_CONFIG['mumble']['server']}"
mumble.join_channel_with_most_users
end # on_server_sync
mumble.connect
end
@irc_thread.join
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment