commit e84f94c6c240e78f9a5f19e6cb4eccdcbe73e8b8
parent 09ece3009a8c397e34667381cf943d6f65e650b4
Author: Kyle Milz <kyle@getaddrinfo.net>
Date: Sat, 23 May 2015 23:58:45 -0600
sl: add new list message type
Diffstat:
M | sl | | | 64 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- |
1 file changed, 57 insertions(+), 7 deletions(-)
diff --git a/sl b/sl
@@ -5,6 +5,7 @@ use strict;
use DBI;
use IO::Socket;
+use Scalar::Util qw(looks_like_number);
my $dbh = DBI->connect(
"dbi:SQLite:dbname=db",
@@ -25,10 +26,15 @@ $dbh->do(qq{create table if not exists list_data(
status int not null default 0,
owner int not null,
primary key(list_id, position),
- foreign key(owner) references contacts(phone_num)
-)}) or die $DBI::errstr;
+ foreign key(owner) references contacts(phone_num))
+}) or die $DBI::errstr;
-# $dbh->do(qq{create table if not exists user_list(
+$dbh->do(qq{create table if not exists lists(
+ list_id int not null primary key,
+ phone_num int not null,
+ name text not null,
+ timestamp int not null)
+}) or die $DBI::errstr;
my $sock = new IO::Socket::INET (
LocalHost => '0.0.0.0',
@@ -40,11 +46,55 @@ my $sock = new IO::Socket::INET (
die "Could not create socket: $!\n" unless $sock;
-while (1) {
- my $new_sock = $sock->accept();
+srand;
+
+my $sql = qq{insert into lists (list_id, phone_num, name, timestamp)
+ values (?, ?, ?, ?)};
+my $new_list_sth = $dbh->prepare($sql);
+
+while (my $new_sock = $sock->accept()) {
+
+ read $new_sock, my $msg_type, 1;
+ if ($msg_type == 1) {
+ # new list
+
+ print "msg_type is new_list\n";
+ read $new_sock, my $new_list_size, 2;
+
+ if (!looks_like_number($new_list_size)) {
+ print "warn: $new_list_size is not a number, skipping\n";
+ next;
+ }
+ # we know this is safe
+ $new_list_size = int($new_list_size);
+
+ print "info: message size = $new_list_size\n";
+ read $new_sock, my $new_list, $new_list_size;
+
+ print "info: raw message: $new_list\n";
+ my ($phone_num, $name) = split("\0", $new_list);
+
+ unless ($name && $name ne "") {
+ print "info: name missing or empty, skipping\n";
+ next;
+ }
+ unless ($phone_num && $phone_num ne "") {
+ print "info: phone number missing, skipping\n";
+ next;
+ }
+
+ if (!looks_like_number($phone_num)) {
+ print "warn: $phone_num is not a number, skipping\n";
+ next;
+ }
+ print "info: new list: phone number = $phone_num\n";
+ print "info: new list: name = $name\n";
+ my $list_id = rand;
+ print "info: list id = $list_id";
- while(<$new_sock>) {
- print $_;
+ $new_list_sth->execute($list_id, $phone_num, $name, time);
}
+ close($new_sock);
}
+$dbh->disconnect();
close($sock);