-
Notifications
You must be signed in to change notification settings - Fork 6
/
irc_log.rb
167 lines (139 loc) · 4.9 KB
/
irc_log.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require 'sinatra'
require 'redis'
require "erb"
require 'cinch'
require 'sanitize'
set :environment, :production #For ip:4567 , comment this to get localhost:4567
#redis = Redis.new(:host => '127.0.0.1', :port => 6379)
redis = Redis.new(:host => '127.4.45.1', :port => 15008) #This is for development
# redis = Redis::new(:path=>"#{ENV['OPENSHIFT_GEAR_DIR']}tmp/redis.sock") #This is for production
#================================================This is the code of the bot ===================================
$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 "-----------------------------------------------"
puts "The registered channels are : "
puts "-----------------------------------------------"
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 "-----------------------------------------------"
puts "End of initialize"
puts "-----------------------------------------------"
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 "-----------------------------------------------"
puts "Inside log....."
puts "#{channel}:#{get_time.strftime("%d-%m-%Y")}"
puts "-----------------------------------------------"
len = @redis.LLEN "#{channel}:#{get_time.strftime("%d-%m-%Y")}"
if len.to_i == 0
puts "-----------------------------------------------"
puts "Length of the list is zero"
puts "-----------------------------------------------"
@redis.LPUSH "#{channel}" , "#{get_time.strftime("%d-%m-%Y")}"
end
puts "-----------------------------------------------"
puts "#{channel}:#{get_time.strftime("%d-%m-%Y")}"
puts "-----------------------------------------------"
nick = Sanitize.clean(user.nick)
@redis.LPUSH "#{channel}:#{get_time.strftime("%d-%m-%Y")}", "<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.strftime("%d-%m-%Y")}", "<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.strftime("%d-%m-%Y")}"
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
job1 = fork do
bot.start
end
#================================================This is the code of the bot ===================================
get '/message' do
channel = "#"+params[:channel].to_s
date = params[:date].to_s
puts channel
puts date
len=redis.LLEN "#{channel}:#{date}"
data = ''
if len == 0
"<html><h3>Log not found</h3></html>"
else
data = redis.lrange("#{channel}:#{date}",0,len)
data = data.reverse.join("<br />")
end
data
end
get '/:channel' do
channel = params[:channel].to_s
len = redis.LLEN "\##{channel}"
puts "-----------------------------------------------"
puts "In the /:channel get"
puts "-----------------------------------------------"
@data=" "
c = redis.lrange("\##{channel}",0,len).each do |f|
puts f.to_s
@data+="<a href=\" /message?channel=#{channel}&date=#{f}\">#{f}</a><br /><br />"
end
puts "-----------------------------------------------"
erb :index
end
get '/' do
len = redis.LLEN "channels"
puts len
data=" "
redis.lrange("channels",0,len).each do |f|
data+="<a href=\" /#{f[1,f.length]}\">#{f}</a><br /><br />"
end
"<h3>Channels:</h3><br/><br/>"+data
end