-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathirclogger.rb
197 lines (160 loc) · 4.8 KB
/
irclogger.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Wire up the paths
Dir[File.dirname(__FILE__) + '/vendor/*/lib'].each { |d| $:.unshift d }
$:.unshift File.dirname(__FILE__) + '/lib'
require 'sinatra'
require 'date'
require 'helpers'
require 'partials'
require 'json'
class Irclogger < Sinatra::Base
set :run, false
set :env, :production
enable :static
set :public, File.dirname(__FILE__) + "/public"
# set :raise_errors, true
# set :show_exceptions, true
## DB ###########################
require 'sequel'
DB = Sequel.connect 'mysql://root@localhost/irclogs'
class Message < Sequel::Model(:irclog)
def message_type
return "msg" if msg?
return "info" if info?
""
end
def msg?
! nick.empty?
end
def info?
! msg?
end
def self.find_by_channel_and_date(channel, date)
# Channels look like: .sinatra, or ..Paws. Sub it so it finds in the db right.
channel = channel.gsub(/\./, "#")
day_after = date + 1
filter(:timestamp > Time.local(date.year, date.month, date.day).to_i).
filter(:timestamp < Time.local(day_after.year, day_after.month, day_after.day).to_i).
filter(:channel => "#{channel}").
order(:timestamp)
end
end
## Helpers ###########################
helpers do
include IRCLogger::Helpers
include Sinatra::Partials
include Rack::Utils
alias_method :h, :escape_html
def status_line(text)
text.sub(/#.*$/, "")
end
def calendar(channel, date, links=true)
cal = `cal #{date.month} #{date.year}`
if links
cal.gsub!(/\b(\d{1,2})\b/) do
d = date.strftime("%Y-%m-#{$1.rjust 2, "0"}")
current = "current" if date.to_s == d
%Q{<a class="#{current}" href="/#{channel}/#{d}">#{$1}</a>}
end
end
next_date = date >> 1
prev_date = date >> -1
%Q{<a href="/#{channel}/#{prev_date}"><</a>#{date.strftime("%B %Y").center(18)}<a href="/#{channel}/#{next_date}">></a>\n#{cal.split("\n")[1..-1].join("\n")}}
end
def plain_entry(message)
buffer = [Time.at(message.timestamp).strftime("%H:%M")]
if message.msg?
buffer << "<#{message.nick}>"
buffer << message.line
else
buffer << status_line(message.line)
end
buffer.join(" ")
end
end
## Web ##########################
before do
@channels = DB["SELECT channel FROM irclog GROUP BY channel"].inject([]) { |arr, row|
arr << row[:channel] if (row[:channel] =~ /^#/ && row[:channel] != "#datamapper http://datamapper.")
arr
}
end
get '/' do
@date = Date.today
@link_calendar = false
haml :index
end
get "/styles.css" do
response["Content-Type"] = "text/css"
sass :styles
end
get '/:channel' do
redirect "/#{params[:channel]}/"
end
get '/:channel/' do
@channel = params[:channel]
redirect "/#{@channel}/#{relative_day('today')}"
end
get '/:channel/:date' do
if params[:date] == "today"
redirect "/#{params[:channel]}/#{relative_day('today')}"
else
@date = Date.parse(params[:date])
end
@channel = params[:channel]
@link_calendar = true
@messages = Message.find_by_channel_and_date(@channel, @date)
if request.html?
haml :channel
else
response["Content-Type"] = "text/plain"
@messages.map do |message|
plain_entry(message)
end.join("\n")
end
end
get '/:channel/slice/:from/:to' do
@channel = params[:channel]
@channel = @channel.gsub(/\./, "#")
@messages = Message.filter(:timestamp > params[:from]).
filter(:timestamp < params[:to]).
filter(:channel => "#{@channel}").
order(:timestamp).collect do |m|
{
:id => m.id ,
:channel => m.channel ,
:day => m.day ,
:nick => m.nick ,
:timestamp => m.timestamp ,
:line => m.line ,
:spam => m.spam ,
:permalink => "http://irclogger.com/#{@channel}/#{Time.at(m.timestamp).strftime("%Y-%m-%d")}#msg_#{m.timestamp}"
}
end
@messages.to_json
end
get '/channels.json' do
@channels = DB["SELECT channel FROM irclog GROUP BY channel"].inject([]) { |arr, row|
arr << row[:channel] if (row[:channel] =~ /^#/ && row[:channel] != "#datamapper http://datamapper.")
arr
}.collect do |channel|
channel = channel.gsub(/\./, "#")
{
:channel => channel,
:permalink => "http://irclogger.com/#{channel[1..-1]}"
}
end
@channels.to_json
end
end
## Monkey Patching #############
class Fixnum
def minutes
self * 60
end
end
class NilClass; def empty?; true; end; end
class Rack::Request
def html?
accept.any? { |content_type| content_type =~ /html/ }
end
end