-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms_handlers.php
431 lines (331 loc) · 9.92 KB
/
sms_handlers.php
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
function sms_handler($usr_id, $rx_msg, $rx_id, $usr_context) {
// Based on the user context, invoke a handler function.
if ($usr_context == 'handler') {
die("Bailing out before we hit infinite recursion.");
}
$handler_func = "sms_{$usr_context}";
if (DEBUG) {
echo "Handler: $handler_func\n";
}
if (function_exists($handler_func)) {
$rsp = call_user_func($handler_func, $usr_id, $rx_msg, $rx_id);
} else {
return array(
'ok' => 0,
'error' => "No handler function found for '$usr_context' context.\n"
);
}
$rsp['rx_id'] = $rx_id;
return $rsp;
}
function sms_intro($usr_id, $rx_msg, $rx_id) {
// 'intro' is the default context for new users, assigned after they've
// sent their first message.
$rsp = usr_get_by_id($usr_id);
if (! $rsp['ok']) {
if (DEBUG) {
print_r($rsp);
}
exit;
}
$usr = $rsp['usr'];
// Send the first incoming message out to admins for
// moderation.
$admin_options = "[/hold {$rx_id} /ban id$usr->id]";
$admin_msg = "$rx_msg\n$admin_options";
$signed_msg = msg_signed_format($usr, $admin_msg);
msg_mod_tx($usr_id, $signed_msg, $rx_id);
// Transition to name context.
usr_set_context($usr_id, 'name');
$tx_msg = xo('ctx_intro');
return array(
'ok' => 1,
'tx_msg' => $tx_msg
);
}
function sms_send_invite($usr_id, $rx_msg, $rx_id) {
// 'send_invite' is the context for when the user just initiated an
// /invite [phone] command. The response should be Y, N, or the message
// they want to send out as the invitation.
$rsp = usr_get_last_invite($usr_id);
if (! $rsp['ok']) {
return array(
'ok' => 0,
'tx_msg' => xo($rsp['xo'])
);
}
$invite = $rsp['invite'];
$yes_no = strtolower($rx_msg);
$yes_no = trim($yes_no);
$yes_no = mb_substr($yes_no, 0, 1);
if ($yes_no == 'y') {
// Send the invitation as-is!
$rsp = usr_get_by_phone($invite->phone);
$invited = $rsp['usr'];
usr_set_context($usr_id, 'chat');
$rsp = db_update('usr', array(
'context' => 'invited',
'invited_by' => $usr_id,
'joined' => null
), "id = $invited->id");
if (! $rsp['ok']) {
return array(
'ok' => 0,
'tx_msg' => xo('err_db')
);
}
$rsp = db_update('usr_invite', array(
'sent' => date('Y-m-d H:i:s')
), "id = $invite->id");
if (! $rsp['ok']) {
return array(
'ok' => 0,
'tx_msg' => xo('err_db')
);
}
// (This is the part where we actually send out the invite.)
msg_tx($invited->id, xo('cmd_invite_hello', $invite->invitation), $rx_id);
return array(
'ok' => 1,
'tx_msg' => xo('cmd_invite_sent')
);
} else if ($yes_no == 'n') {
// Cancel the invitation
usr_set_context($usr_id, 'chat');
$tx_msg = xo('cmd_invite_cancel');
} else {
// Update the invitation text
db_update('usr_invite', array(
'invitation' => $rx_msg
), "id = $invite->id");
// And ask one more time before we send it out.
return array(
'ok' => 1,
'tx_msg' => xo('cmd_invite_preview', $rx_msg)
);
}
}
function sms_invited($usr_id, $rx_msg, $rx_id) {
// 'invited' is the status a user gets assigned when their friend sends
// an "/invite [phone]" command.
$msg = strtolower($rx_msg);
$msg = trim($msg);
if (mb_substr($msg, 0, 2) == 'ok') {
// Great, a new user accepted an invite!
$usr = usr_get("id$usr_id");
// Transition to name context.
usr_set_context($usr_id, 'name');
$phone = addslashes($usr->phone);
// Update the usr_invite record
db_update('usr_invite', array(
'accepted' => date('Y-m-d H:i:s')
), "phone = '$phone'");
// Update the usr record
db_update('usr', array(
'joined' => date('Y-m-d H:i:s')
), "id = $usr_id");
$rsp = db_single("
SELECT *
FROM usr_invite
WHERE phone = ?
ORDER BY sent DESC
", array($usr->phone));
if ($rsp['row']) {
$invite = $rsp['row'];
msg_tx($invite->usr_id, xo('cmd_invite_accepted', $usr->phone), $rx_id);
}
// Send intro text out
$tx_msg = xo('ctx_intro');
} else {
$tx_msg = xo('ctx_invite_sorry');
}
return array(
'ok' => 1,
'tx_msg' => $tx_msg
);
}
function sms_name($usr_id, $rx_msg, $rx_id) {
// 'name' context is when we're prompting the user for theif first
// username.
include(__DIR__ . '/config.php');
$rsp = usr_set_name($usr_id, $rx_msg, $rx_id);
if ($rsp['ok']) {
$name = $rsp['new_name'];
$rsp = usr_get_by_id($usr_id);
util_ensure_rsp($rsp);
$usr = $rsp['usr'];
// Transition to coc
usr_set_context($usr_id, 'coc');
$coc_url = "$website_url/conduct";
$tx_msg = xo('ctx_name', $name, $coc_url);
} else {
// Invalid name
$tx_msg = xo($rsp['xo']);
$tx_msg .= "\nPlease try again.";
}
return array(
'ok' => 1,
'tx_msg' => $tx_msg
);
}
function sms_coc($usr_id, $rx_msg, $rx_id) {
// 'coc' is the Code of Conduct context. The user has to reply 'ok' in
// order to proceed.
include(__DIR__ . '/config.php');
$usr = usr_get("id$usr_id");
$msg = strtolower($rx_msg);
$msg = trim($msg);
if (mb_substr($msg, 0, 2) == 'ok') {
// What happens next depends on whether the user was invited.
if (! empty($usr->invited_by)) {
// If the user was *invited*, just jump into the chat.
usr_set_context($usr_id, 'chat');
$count = xo_chat_count($usr_id);
$mod = xo('ctx_coc_mod');
$tx_msg = xo('ctx_first_msg', "$mod $count", $website_url);
// Announce that the user has joined the chat
$announcement = xo('cmd_start_announce', $usr->name);
msg_mod_tx($usr_id, "[$announcement]", $rx_id);
} else {
$rsp = usr_get_first_msg($usr_id, 'formatted');
util_ensure_rsp($rsp);
$first_msg = $rsp['first_msg'];
$first_msg_held = $rsp['first_msg_held'];
if ($first_msg_held) {
// First message is held: just jump into to chat
usr_set_context($usr_id, 'chat');
$mod = xo('ctx_coc_mod');
$count = xo_chat_count($usr_id, $usr->channel);
$tx_msg = xo('ctx_first_msg', "$mod $count", $website_url);
// Announce that the user has joined the chat
$announcement = xo('cmd_start_announce', $usr->name);
msg_mod_tx($usr_id, "[$announcement]", $rx_id);
} else {
// Ask to send out the first message.
usr_set_context($usr_id, 'first_msg');
$mod = xo('ctx_coc_mod');
$tx_msg = xo('ctx_coc_first_msg', $mod, $first_msg);
}
}
} else {
$coc_url = "$website_url/conduct";
$tx_msg = xo('ctx_coc_nope', $coc_url);
}
return array(
'ok' => 1,
'tx_msg' => $tx_msg
);
}
function sms_first_msg($usr_id, $rx_msg, $rx_id) {
// 'first_msg' context is when the user is deciding whether they want
// to send out their first message.
// This is also where we transition into the chat, so we try to offer as
// much context as possible.
// For example:
// Message sent! You are now chatting with 17 others.
// Reply /stop to leave, or /help for more commands...
include(__DIR__ . '/config.php');
$usr = usr_get("id$usr_id");
if (! $usr) {
return array(
'ok' => 0,
'error' => "Couldn't find user $usr_id!\n"
);
}
// Transition into chat
usr_set_context($usr_id, 'chat');
// Announce that the user has joined the chat
$announcement = xo('cmd_start_announce', $usr->name);
msg_mod_tx($usr_id, "[$announcement]", $rx_id);
// First, figure out $count: how many people are in the chat?
$count = xo_chat_count($usr_id);
// Next: send or not send the message? Result is stored in $sent.
$yes_no = strtolower($rx_msg);
$yes_no = trim($yes_no);
$yes_no = mb_substr($yes_no, 0, 1);
if ($yes_no == 'y') {
// Ok, send it!!
$rsp = usr_get_first_msg($usr_id);
util_ensure_rsp($rsp);
$first_msg = $rsp['first_msg'];
$first_msg_id = $rsp['first_msg_id'];
$first_msg_held = $rsp['first_msg_held'];
if ($first_msg_held) {
// On second thought, the message was held, so I guess
// we will just bail out now.
// Instead, join the chat!
usr_set_context($usr_id, 'chat');
// Announce that the user has joined the chat
$announcement = xo('cmd_start_announce', $usr->name);
msg_mod_tx($usr_id, "[$announcement]", $rx_id);
$tx_msg = xo_first_msg_held($usr_id);
return array(
'ok' => 1,
'tx_msg' => $tx_msg
);
}
// This is the part where we actually send the message: *whoosh*
msg_chat($usr_id, $first_msg, $first_msg_id);
// Yep, "we sent it."
if ($count == xo('ctx_chat_first')) {
$sent = xo('ctx_first_msg_saved'); // "saved." (archived)
} else {
$sent = xo('ctx_first_msg_sent'); // vs. "sent!"
}
} else if ($yes_no == 'n') {
// Nope, "didn't send."
$sent = xo('ctx_first_msg_drop');
} else {
// Huh? "Didn't send."
$sent = xo('ctx_first_msg_huh');
}
// Send the response message
$tx_msg = xo('ctx_first_msg', "$sent $count", $website_url);
// Is this the first user? If so, make 'em the admin.
if (usr_is_first()) {
// TODO: figure out if this could work better via web service
$rsp = usr_set_status("id$usr_id", 'admin');
if ($rsp['ok']) {
msg_tx($usr_id, xo('ctx_intro_admin'), $rx_id);
} else if (DEBUG) {
echo "Could not set user status:\n";
print_r($rsp);
}
}
return array(
'ok' => 1,
'tx_msg' => $tx_msg
);
}
function sms_stopped($usr_id, $rx_msg, $rx_id) {
// 'stopped' is when the user has left the chat with the "/stop"
// command. We will send their message and transition to 'chat' context.
$rsp = msg_chat($usr_id, $rx_msg, $rx_id);
$rsp['tx_msg'] = xo('ctx_stopped');
return $rsp;
}
function sms_chat($usr_id, $rx_msg, $rx_id) {
// 'chat' is when the user is sending messages to other users.
return msg_chat($usr_id, $rx_msg, $rx_id);
}
function sms_mod_request($usr_id, $rx_msg, $rx_id) {
// 'mod_request' is when a user has sent "/mod" to request help, but
// still needs to confirm that they want help.
$usr = usr_get("id$usr_id");
$msg = strtolower($rx_msg);
$msg = trim($msg);
if (mb_substr($msg, 0, 2) == 'ok') {
usr_set_context($usr_id, 'stopped');
$announcement = xo('cmd_mod_request', $usr->name, $usr->name);
msg_mod_tx($usr_id, "[$announcement]", $rx_id);
$tx_msg = xo('cmd_mod_sent');
} else {
usr_set_context($usr_id, 'chat');
$tx_msg = xo('cmd_mod_cancel');
}
return array(
'ok' => 1,
'tx_msg' => $tx_msg
);
}