-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.js
More file actions
219 lines (162 loc) · 5.35 KB
/
logic.js
File metadata and controls
219 lines (162 loc) · 5.35 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var redis = require('redis');
const sms = require('./main');
const REDISHOST = process.env.REDIS_HOST;
const REDISPORT = process.env.REDIS_PORT;
var client = redis.createClient({
host: REDISHOST,
port: REDISPORT
});
client.on('error', err => console.error('ERR:REDIS:', err));
client.on('connect', reply => console.log(reply));
["ping", "set", "get", "del", "hmset", "hdel", "hmget", "expire", "hgetall", "lrange"].forEach(eta => {
client["async_" + eta] = promisifyRedis(client, client[eta]);
});
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function promisifyRedis (client, func)
{
return function newFunction ()
{
return new Promise((resolve, reject) => {
var params = Array.from(arguments);
params.push((err, reply) => {
if (err) reject(err);
else resolve(reply);
});
console.log(params);
func.apply(client, params);
});
};
}
const optOuts = ["STOP", "STOPALL", "UNSUBSCRIBE", "CANCEL", "END", "QUIT"];
const optIns = ["START", "YES", "UNSTOP"];
const groupSpawns = ["NEW", "LFG", "GROUP"];
//async_hgetall(??)
exports.IncomingRawSMS =
async function (message, twilioPhoneNum, senderPhoneNum) {
var parced = message.match(/(\w+)(?:(?:\s+)(\w+)(?:(?:\s+)(\w+)|)|)/mg);
if ( groupSpawns.includes( parced[0] )) {
if( !parced[1] && !parced[2] )
{
var roomID = await createRoom(senderPhoneNum);
await sms.sendOutgoingSMS('"' + roomID + '" created at ' + await getRoomNum(roomID));
}
var reqRoomID = parced[1].trim();
//var existRoomID = async_hmget(twilioPhoneNum, senderPhoneNum);
if (await roomExists(reqRoomID) ) {
await sms.sendOutgoingSMS('"' + reqRoomID + '" is in use.');
return;
}
if( parced[2] == senderPhoneNum ) {
await sms.sendOutgoingSMS('Nickname cannot be your phonenumber.');
return;
}
await sms.sendOutgoingSMS('Hi ' + nickname + '! Welcome to "' + reqRoomID + '"!');
var nickname = parced[2];
await addUser( senderPhoneNum, nickname, reqRoomID );
}
else if ( optOuts.includes( parced[0] )) {
await killUser(senderPhoneNum);
}
else if ( optIns.includes( parced[0] )) {
// do nothing intentionally
}
await sendMessage(await getRoomID(twilioPhoneNum, senderPhoneNum),senderPhoneNum, message);
};
async function userExists (phone, roomID)
{
var reply = await client.async_hmget(roomID, phone);
if (reply) return true;
return false;
}
async function sendMessage(roomID, from, message) {
message = client.async_hmget(roomID, from) + ": " + message;
var members = await client.async_hgetall(roomID);
if (members)
{
var roomNum = members.phonenumber;
Object.keys(members)
.filter(key => key != from && key != "phonenumber")
.forEach(rho => sms.sendOutgoingSMS(message, rho, roomNum));
}
}
async function getRoomNum(roomID)
{
var members = await client.async_hgetall(roomID);
var roomNum = members.phonenumber;
return roomNum;
}
async function killUser (phone)
{
// get roomNumbers client is connected to
var connections = await client.async_lrange(phone, 0, -1);
connections.forEach( async conn => {
await removeUser(conn, phone);
});
}
async function removeUser (roomNum, phone)
{
// remove roomid entry from composite table
const roomid = client.async_get(roomNum + phone);
await client.async_del(roomNum + phone);
// remove phonenumber: nickname from roomid table
await client.async_hdel(roomid, phone);
}
const servicePool = [
"+19166178309",
"+19168350353",
"+19166719472",
"+19165381012"
];
async function addUser (phone, nickname, roomID)
{
const roomNum = await client.async_hmget(roomID, "phonenumber");
await client.async_hmset(roomID, [phone, nickname]);
await client.async_set(roomNum + phone, roomID);
}
async function getRoomID (roomNum, phone)
{
return await client.async_get(roomNum + phone);
}
async function createRoom(phone) {
var roomID;
console.log("Creating room");
do
{
roomID = getRandomInt(10000000);
console.log(`Generated room id: ${roomID}`);
} while (await roomExists(roomID));
if ( roomExists(roomID) )
throw('RoomID "' + roomID + '" Exists');
// get roomNumbers client is connected to
var connections = await client.async_lrange(phone, 0, -1);
console.log(`Open connections: ${connections}`);
// Throw error if no room for a new connection
if(connections)
{
if (connections.length >= servicePool.length)
throw Error("Max number of rooms taken");
}
// Pick an unused connection
var phoneNumCandidates = Array.from(servicePool);
for (a in connections)
{
phoneNumCandidates = phoneNumCandidates.filter(b => {
return a != b;
});
}
// add new connection to list
await client.async_lpush(phone, phoneNumCandidates[0]);
// expire in 48 hours
await client.async_hmset(roomID, ["phonenumber", phoneNumCandidates[0]]);
await client.async_expire(roomID, 60 * 60 * 48);
return roomID;
}
async function roomExists(roomID) {
if ( await client.async_hmget(roomID, "phonenumber") )
{
return true;
}
return false;
}