forked from vikassy/autobotz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc_bot.rb
101 lines (82 loc) · 3.01 KB
/
irc_bot.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
96
97
98
99
100
101
require 'cinch'
require 'redis'
require 'sanitize'
$channels_to_be_tracked = ["#nitk-autobotz"]
time = Time.now.localtime("+05:30")
class Logger
attr_accessor :redis
def initialize(ip,port)
# @redis = Redis.new(:host => ip, :port => port) #This is for development
@redis = Redis.new(:host => '127.4.45.1', :port => 15008)
# @redis = Redis::new(:path=>"#{ENV['OPENSHIFT_GEAR_DIR']}tmp/redis.sock") #This is for production
len = redis.LLEN "channels"
registered_channel = redis.lrange('channels',0,len)
puts "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll"
puts registered_channel
$channels_to_be_tracked.each do |f|
puts f
if not registered_channel.include?(f)
@redis.LPUSH "channels" , f
end
end
puts "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll"
end
def get_time
Time.now.localtime("+05:30")
end
def get_log_time
t = get_time
"#{t.hour}:#{t.min}:#{t.sec}"
end
def log(channel,user,msg)
puts "iiiiiiiinnnnnnnnnnnnnnnnnnnnnnnnnnnnnssssssssssssssssiiiiiiiiiiiiiddddddddddddddddeeeeeeeeeeeee"
puts "#{channel}:#{get_time.day}"
len = @redis.LLEN "#{channel}:#{get_time.day}"
if len.to_i == 0
puts "iiiiiiiinnnnnnnnnnnnnnnnnnnnnnnnnnnnnssssssssssssssssiiiiiiiiiiiiiddddddddddddddddeeeeeeeeeeeee"
@redis.LPUSH "#{channel}" , "#{get_time.day}"
end
puts "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll"
puts "#{channel}:#{get_time.day}"
nick = Sanitize.clean(user.nick)
@redis.LPUSH "#{channel}:#{get_time.day}", "<div style='color: green;display: inline'><#{get_log_time}></div>"+"<div style='color: red;display: inline'>#{nick}</div>"+": "+Sanitize.clean(msg)
end
def bot_log(channel,msg)
@redis.LPUSH "#{channel}:#{get_time.day}", "<div style='color: green;display: inline'><#{get_log_time}></div>"+"<div style='color: red;display: inline'>autobotz</div>"+": "+msg
end
end
bot = Cinch::Bot.new do
logger = Logger.new('127.0.0.1',6379)
configure do |c|
c.server = "irc.freenode.org"
c.channels = $channels_to_be_tracked
c.nick = 'autobotz'
end
on :message,"!users" do |m|
names = "Users: "
m.channel.users.each do |f|
names += f[0].to_s+" "
end
m.reply("#{names}")
logger.bot_log(m.channel,names)
end
on :message,"!user_count" do |m|
names = "#{m.user.nick}: Total_Users: #{m.channel.users.count}"
m.reply("#{names}")
logger.bot_log(m.channel,names)
end
on :message,"!hello" do |m|
msg = "Hello #{m.user.nick}"
m.reply(msg)
logger.bot_log(m.channel,msg)
end
on :message,"!log" do |m|
msg = "#{m.user.nick}: The log can be found in http://ircbot-run123.rhcloud.com/message?channel=#{(m.channel.to_s)[1,m.channel.to_s.size-1]}&date=#{logger.get_time.day}"
m.reply(msg)
logger.bot_log(m.channel,msg)
end
on :message do |m|
logger.log(m.channel,m.user,(m.params[1]).to_s)
end
end
bot.start