forked from yuanying/WebIRC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebserver.rb
222 lines (190 loc) · 5.69 KB
/
webserver.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require "lib/webircconfig"
require "lib/connections"
require "lib/rss"
require "rubygems"
require "sinatra"
require "json"
mime :json, "application/json"
mime :rss, "application/rss+xml"
mime :raw, "application/octet-stream"
configure do
@@config = WebIRCConfig.new
@@rss_feed = RSSFeed.new
@@connections = Connections.new(@@rss_feed)
end
helpers do
def config(key)
@@config[key]
end
def protected?
@@config["web_user"] and @@config["web_password"]
end
def protected!
response["WWW-Authenticate"] = %(Basic realm="WebIRC") and throw(:halt, [401, "Not authorized\n"]) and return unless !protected? or authorized?
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == [@@config["web_user"], @@config["web_password"]]
end
def get_history_iphone(last_read)
history = Hash.new
@@connections.each_connection_with_id do |connection_id, connection|
history[connection_id] = connection.history_iphone(last_read.has_key?(connection_id) ? last_read[connection_id] : 0)
end
history
end
def get_history(last_read)
history = Hash.new
@@connections.each_connection_with_id do |connection_id, connection|
history[connection_id] = connection.history(last_read.has_key?(connection_id) ? last_read[connection_id] : 0)
end
history
end
def get_users
users = Hash.new
@@connections.each_connection_with_id do |connection_id, connection|
users[connection_id] = connection.users
end
users
end
def sync(open)
close = Hash.new
close_connections = Array.new
open.each_key do |connection_id|
if @@connections.has?(connection_id)
close[connection_id] = {:channels => @@connections[connection_id].sync_channels(open[connection_id]["channels"]), :privmsgs => @@connections[connection_id].sync_privmsgs(open[connection_id]["privmsgs"])}
else
close_connections << connection_id
end
end
{:targets => close, :connections => close_connections}
end
def get_update_iphone(json_object)
{:history => get_history_iphone(json_object["last_read"]), :sync => sync(json_object["sync"])}.to_json
end
def get_update(json_object)
{:history => get_history(json_object["last_read"]), :sync => sync(json_object["sync"])}.to_json
end
def json_request(request)
JSON.parse(request.env["rack.input"].read)
end
end
get "/", :agent => /Apple.*Mobile.*Safari/ do
protected!
erb :home_iphone
end
get "/" do
protected!
erb :home
end
get "/public/:filename" do
if @@config.exists?(params["filename"])
content_type :raw
@@config.get_file(params["filename"]) {|file| file.read}
else
raise Sinatra::NotFound
end
end
get "/upload" do
protected!
erb :upload
end
post "/upload" do
protected!
@filename = params["datafile"][:filename]
@@config.save_file(@filename) {|file| file.write(params["datafile"][:tempfile].read)}
erb :uploaded
end
post "/connect" do
protected!
content_type :json
command = json_request(request)
@@config["nickname"], @@config["user_name"], @@config["real_name"] = command["nickname"], command["user_name"], command["real_name"]
@@config.save!
@@connections.add(command)
sleep 1
get_update(command)
end
post "/close" do
protected!
content_type :json
command = json_request(request)
if command["target"]
@@connections[command["connection_id"]].close(command["target"]) if @@connections.has?(command["connection_id"])
else
@@connections.remove(command["connection_id"])
end
sleep 1
get_update(command)
end
post "/join" do
protected!
content_type :json
command = json_request(request)
@@connections[command["connection_id"]].join(command["channel"], command["param"]) if @@connections.has?(command["connection_id"])
sleep 1
get_update(command)
end
post "/part" do
protected!
content_type :json
command = json_request(request)
@@connections[command["connection_id"]].part(command["channel"], command["param"]) if @@connections.has?(command["connection_id"])
sleep 1
get_update(command)
end
post "/privmsg" do
protected!
content_type :json
command = json_request(request)
@@connections[command["connection_id"]].privmsg(command["target"], command["text"], command["action"]) if @@connections.has?(command["connection_id"])
get_update(command)
end
post "/notice" do
protected!
content_type :json
command = json_request(request)
@@connections[command["connection_id"]].notice(command["target"], command["text"]) if @@connections.has?(command["connection_id"])
get_update(command)
end
post "/new_window" do
protected!
content_type :json
command = json_request(request)
@@connections[command["connection_id"]].new_window(command["target"]) if @@connections.has?(command["connection_id"])
get_update(command)
end
post "/command" do
protected!
content_type :json
command = json_request(request)
@@connections[command["connection_id"]].send(command["command"]) if @@connections.has?(command["connection_id"])
sleep command["wait"] if command["wait"]
get_update(command)
end
get "/all" do
protected!
content_type :json
{:history => get_history({})}.to_json
end
post "/update", :agent => /Apple.*Mobile.*Safari/ do
protected!
content_type :json
get_update_iphone(JSON.parse(request.env["rack.input"].read))
end
post "/update" do
protected!
content_type :json
get_update(JSON.parse(request.env["rack.input"].read))
end
post "/password" do
protected!
command = json_request(request)
@@config["web_user"], @@config["web_password"] = command["web_user"], command["web_password"]
@@config.save!
"200"
end
get "/rss" do
content_type :rss
@@rss_feed.to_s
end