-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.rb
179 lines (151 loc) · 5.86 KB
/
helpers.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
module UserInterface; end
module UserInterface::Helpers
# defaults to this user, otherwise, supply their openid
def userdir(user = false)
require 'json'
@userdir_cache ||= {}
return @userdir_cache[user] if @userdir_cache[user]
@state ||= Camping::H[{}]; @cookies ||= {}
user = @state.identity unless user
folder = "users/#{userhash(user)}"
unless File.exist?(folder)
profile = {
'name' => @state.default_username || ((@cookies['idp-username'].to_s.length > 0)?@cookies['idp-username']:nil) || 'Anonymous',
'desc' => 'You should fill out a name above and a little profiley thing here. :)'
}
Dir.mkdir(folder, 0777)
File.open("#{folder}/openid", 'w') { |w| w.write(user) }
File.open("#{folder}/profile", 'w') { |w| w.write(JSON.generate(profile)) }
end
return (@userdir_cache[user] = folder)
end
def userhash(user = false)
require 'digest/sha1'
folder = Digest::SHA1.hexdigest(user || @state.identity)
end
def talkie_user
dir = userdir('http://talkie.me/')
alter_json("#{dir}/profile") do |o|
o['name'] = "#{AppName}"
o['desc'] = "#{AppName} Bot"
end
'http://talkie.me/'
end
# discovers pavatars, or favicons
def get_remote_avatar(from, type = :pavatar)
require 'open-uri'; require 'nokogiri'
from = URI.parse(from.to_s) unless from.is_a?(URI)
raise StandardError.new('Evil is afoot!') unless ['http', 'https'].include?(from.scheme.downcase)
source = open(from)
if type == :pavatar
return nil if source.meta['x-pavatar'] == 'none'
if source.meta['x-pavatar']
file = source.base_uri.merge(source.meta['x-pavatar']).read
return file if file.status.first == '200'
end
end
page = Nokogiri::HTML(source)
rels = {:pavatar => 'pavatar', :favicon => 'icon'}
link = page.css("*[rel~='#{rels[type]}']").first
return source.base_uri.merge(link['href']).read if link
extension = {:favicon => '.ico', :pavatar => '.png'}[type]
if type == :pavatar
tested = source.base_uri.merge("#{type}#{extension}").read
return tested unless tested.status.first == '200'
end
tested = source.base_uri.merge("/#{type}#{extension}").read
return tested unless tested.status.first == '200'
return nil
end
# imports some avatar image, doing the resizing, in to the place
def import_avatar(place, filename)
[16, 80].each do |size|
system('convert', filename, '-resize', "x#{size*2}", '-resize', "#{size*2}x<", '-resize', '50%', '-gravity', 'center', '-background', 'white', '-flatten', '+repage', '-crop', "#{size}x#{size}+0+0", '-quality','95', "PNG24:#{place}/avatar-#{size}.png")
end
File.delete(filename)
end
# simple question!
def user_owns_room?(room = nil)
return false unless @state.identity
return true if ['http://creativepony.com/', 'https://me.yahoo.com/jennathepony#0fc3b'].include?(@state.identity)
require 'json'
@settings = JSON.parse(IO.read("rooms/#{room || @room}/settings")) unless @settings
return @settings['owners'].include?(@state.identity)
end
# pick a path
def room_or_default(room, path)
if File.exist?("rooms/#{room}/#{path}")
"rooms/#{room}/#{path}"
else
"default/room/#{path}"
end
end
# open, lock, alter a text/json file, write, unlock
def alter_text(filename, &blk)
File.open(filename, 'r+') do |source|
source.flock(File::LOCK_SH)
text = source.read
text = (blk.call(text) || text).to_s
source.flock(File::LOCK_EX)
source.truncate(0); source.rewind; source.write(text)
end
end
def alter_json(filename, &blk)
require 'json'
alter_text(filename) do |text|
object = JSON.parse(text)
blk.call(object)
JSON.generate(object)
end
end
def flock_read(filename)
File.open(filename, 'r') do |file|
file.flock File::LOCK_SH
file.read
end
end
# # invent a new unique room folder, to create a room with!
# def new_room_folder(title)
# folder = digitless_folder = 'room/' + title.gsub(/[^A-Za-z0-9]+/, '-').sub(/^(-?)(.+?)(-?)$/, '\2')
# digit = 1
# folder = "#{digitless_folder}-#{digit}" and digit += 1 while File.exist?(folder)
# folder
# end
# return a login screen if user needs to auth to use controller, usage: "require_login or return"
def require_login
@cookies.delete('once_logged_in')
return true if @state.identity
@cookies.once_logged_in = URL(@env.PATH_INFO).to_s
@error = 'You need to login to view this page'
@body = render(:login)
return false
end
# attempt to drastically reduce the risk of cross site request forgery
def safeguard_referer
our_domain = URI.new(@request.url).host.to_s rescue false
their_domain = URI.new(@request['HTTP_REFERRER']).host.to_s rescue false
if their_domain && our_domain.downcase != their_domain.downcase
@status = 403
@body = render(:generic_error, 'Cross Site Link Error', "In order to protect the security of user accounts, this request has been blocked, due to the referrer provided with it specifying a different domain to #{our_domain}")
return false
else
return true
end
end
# add a message to a room
def send_message(room, message)
require 'json'
meta = {'timestamp' => Time.now.to_i}
msgid = nil
File.open("rooms/#{room}/message-counter", 'r+') do |counter|
counter.flock File::LOCK_EX # exclusive lock, make everyone else wait till we're done
msgid = counter.read.to_i + 1
File.open("rooms/#{room}/temp-#{msgid}", 'w') do |msg|
msg.write(JSON.generate(meta.merge(message)))
end
File.rename("rooms/#{room}/temp-#{msgid}", "rooms/#{room}/message-#{msgid}")
counter.truncate(0); counter.rewind; counter.write(msgid.to_s)
end
return msgid
end
end