-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircbot.rb
95 lines (87 loc) · 2.43 KB
/
ircbot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'rubygems'
require 'yaml'
require 'cinch'
require 'logger'
require 'socket'
## Load custom logger
begin
$lh = "IRCBot -"
$logger = Logger.new(STDERR)
$logger.level = Logger::INFO
$logger.info "#{$lh} Logger initialized successfully"
rescue
$logger.error "#{$lh} Logger initialization failed"
raise
end
## Load configuration file
begin
$conf = YAML.load(File.open('./ircbot.yml'))
$logger.info "#{$lh} Configuration properties loaded successfully"
rescue
$logger.error "#{$lh} Configuration properties load failed"
raise
end
class IRCMessenger
include Cinch::Plugin
listen_to :monitor_msg, :method => :send_msg
def send_msg(m, msg)
Channel($conf['channels'].first).send "#{msg}"
end
end
class IRCPing
include Cinch::Plugin
listen_to :ping_msg, :method => :send_msg
def send_msg(m, msg)
Channel($conf['nickname']).send "#{msg}"
end
end
class IRCBot
def initialize
begin
@bot = Cinch::Bot.new do
configure do |c|
c.server = $conf['server']
c.channels = $conf['schannels']
c.nick = $conf['nickname']
c.plugins.plugins = [IRCMessenger , IRCPing]
end
end
$logger.info "#{$lh} Bot initialized successfully"
rescue
$logger.error "#{$lh} Bot initialization failed"
raise
end
end
def server(bot)
begin
$logger.info "#{$lh} Starting IRCMessenger socket at #{$conf['socket_server']}:#{$conf['socket_port']}"
server = TCPServer.new $conf['socket_server'], $conf['socket_port']
loop do
Thread.start(server.accept) do |client|
listener,message = client.gets.split('*',2)
$logger.info "New socket message TYPE: #{listener} - MESSAGE: #{message}"
case listener
when 'PING'
bot.handlers.dispatch(:ping_msg, nil, message)
when 'MSG'
bot.handlers.dispatch(:monitor_msg, nil, message) unless message.nil?
end
client.close
end #Thread.Start
end #loop
rescue
$logger.error "#{$lh} An error ocurred when trying to start IRCMessenger socket at #{$conf['socket_server']}:#{$conf['socket_port']}"
end
end
def start
begin
Thread.new { server(@bot) }
@bot.start
$logger.info "#{$lh} Connected to #{$conf['server']} successfully"
return true
rescue
$logger.error "#{$lh} Connection to #{$conf['server']} failed"
raise
end
end
end