-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnet.lua
More file actions
200 lines (175 loc) · 4.89 KB
/
net.lua
File metadata and controls
200 lines (175 loc) · 4.89 KB
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
--- net.lua
--
-- The net module manages client-to-facebook and client-to-client connections.
--
-- @author Paul Moore
--
-- Copyright (c) 2012 Strange Ideas Software
--
-- This file is part of Factor Friends.
--
-- Factor Friends is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- Factor Friends is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with Factor Friends. If not, see <http://www.gnu.org/licenses/>.
require "pubnub"
local facebook = require "facebook"
local json = require "json"
local net = {}
-- The registered Facebook AppId.
local FB_APP_ID = "223466991100849"
-- Pubnub keys.
local SUB_KEY = "demo"
local PUB_KEY = "demo"
local SEC_KEY = nil
-- Encryption key, not yet used.
local AES_KEY = string.char(
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B,
0x0C, 0x0D, 0x0E, 0x0F
)
-- Important while using Pubnub demo keys.
local CHANNEL_PREFIX = "factorfriends_"
local pn
local fbToken, fbUser, fbFriends
--- Dispatches a net event through the Runtime object.
local function dispatch (type, args)
args = args or {}
args.name = "net"
args.type = type
Runtime:dispatchEvent(args)
end
--- Opens a connection to the active user's channel, to begin receiving messages.
local function join ()
pn:subscribe({
channel = CHANNEL_PREFIX..fbUser.id,
errorback = function (reason)
dispatch("error", {details = reason})
end,
connect = function ()
dispatch("join")
end,
callback = function (message)
dispatch("receive", {message = message})
end
})
end
--- Closes the active user's channel.
local function unjoin ()
pn:unsubscribe({
channel = CHANNEL_PREFIX..fbUser.id
})
end
local function onFacebookEvent (event)
if "session" == event.type then
if "login" == event.phase then
-- Logged in, now we need our data and our friends data.
fbToken = event.token
facebook.request("me", "GET")
facebook.request("me/friends", "GET")
elseif "loginFailed" == event.phase then
dispatch("error", {details = "Can't login to Facebook!"})
elseif "loginCancelled" == event.phase then
dispatch("error", {details = "Login cancelled."})
elseif "logout" == event.phase then
unjoin()
fbToken = nil
fbUser = nil
fbFriends = nil
dispatch("logout")
end
elseif "request" == event.type then
if event.isError then
dispatch("error", {details = "Can't load Facebook data!"})
net.logout()
else
local response = json.decode(event.response)
if response.id then
fbUser = response
elseif response.data then
fbFriends = response.data
end
-- Dispatch the login event only once we have all the data we need (me + friends).
if fbUser and fbFriends then
join()
dispatch("login")
end
end
end
end
--- Initializes the net module.
function net.init ()
pn = pubnub.new({
subscribe_key = SUB_KEY,
publish_key = PUB_KEY,
secret_key = SEC_KEY,
cipher_key = nil,
ssl = false,
origin = "pubsub.pubnub.com"
})
end
--- Deinitializes the net module.
function net.deinit ()
net.logout()
pn = nil
end
--- Begins the login procedure to Facebook.
function net.login ()
facebook.login(FB_APP_ID, onFacebookEvent)
end
--- Begins the logout procedure to Facebook.
function net.logout ()
facebook.logout()
end
--- Returns an indexed table of the active user's friends.
function net.friends ()
return fbFriends
end
--- Returns the corrosponding friend table for a given Facebook id.
function net.friendForId (id)
for i, f in ipairs(net.friends()) do
if f.id == id then
return f
end
end
end
--- Returns the active user's table.
function net.user ()
return fbUser
end
--- Returns true if there is an active user, false otherwise.
function net.isLoggedIn ()
return fbToken ~= nil
end
--- Adds an event listener (table or function) for Runtime 'net' events.
function net.listen (callback)
Runtime:addEventListener("net", callback)
end
--- Removes an event listener (table or function) for Runtime 'net' events.
function net.unlisten (callback)
Runtime:removeEventListener("net", callback)
end
--- Sends a message to another user with this App.
--
-- @param fbId The Facebook id of the user.
-- @param message The message to send. No need to include your id, as it is already included as a senderId.
function net.send (fbId, message)
message.senderId = fbUser.id
pn:publish({
channel = CHANNEL_PREFIX..fbId,
message = message,
callback = function (result)
dispatch("send", {result = result})
end
})
end
return net