Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use DBI;
- use POSIX;
- use Digest::MD5 qw(md5_hex);
- use HTML::Entities;
- use strict;
- my $dsn = "DBI:mysql:database=enigma_toolbus:host=localhost:port=3006";
- my $user = "enigma_toolbus";
- my $pass = "alpha345";
- my $db = DBI->connect($dsn,$user,$pass);
- $db->{RaiseError} = 1;
- sub dbg {
- my $dbg = shift;
- return $db->do("INSERT INTO debug (time, info, project) VALUES (".time.",".$db->quote($dbg).",'txt')") or -255;
- }
- sub change {
- my ($who, $what, $to) = (shift, shift, shift);
- return $db->do("UPDATE txt_main SET $what=".$db->quote($to)." WHERE email='$who'") or &dbg("Could not change $what to $to where email=$who ($DBI::errstr)");
- }
- sub getHelp {
- my $what = shift;
- my $ret = "";
- my $h = "cinfo-change your info.|cname-change name on account|cnick-change your nickname|fortune-get a fortune|info-get usrs info (use like info userid)|lusers-list usrs and id|msg-msg usr (use like msg userid text)|status-get status (use like status id)|recent-get most recent update|help-alone lists cmds, w/ cmd name gives help (i.e. help help)|menu-lists cmds|follow-follow user (use like follow id)|unfollow-unfollow user (use like unfollow id)";
- my %list = split /[\-\|]/,$h;
- if(exists($list{$what})) {
- $ret = $list{$what};
- } else {
- $ret = "The command you provided, $what, was not found.";
- }
- return $ret;
- }
- sub getInfo {
- my $ret = "";
- my $id = shift;
- my $q = $db->prepare("SELECT info FROM txt_main WHERE id=?");
- $q->execute($id) or &dbg("Error getting info ($DBI::errstr)");
- if($q->rows != 1) {
- $ret = "Couldn't find a user with id: $id";
- } else {
- my ($info) = $q->fetchrow_array();
- my $name = &getName($id);
- $ret = "${name}'s info: $info";
- }
- return $ret;
- $q->finish;
- }
- sub mail {
- my $to = shift;
- my $subject = shift;
- $subject = "Subject: ".$subject."\n";
- my $content = shift;
- my $headers = shift || "From: txt\@sirmxe.info\nReply-to: txt\@sirmxe.info";
- return if $content eq "";
- &dbg("sending mail: $content to $to");
- my $sendmail = "/usr/sbin/sendmail -t";
- open SENDMAIL,"|$sendmail";
- print SENDMAIL $headers;
- print SENDMAIL $subject;
- print SENDMAIL "To: $to\n";
- print SENDMAIL "\n";
- print SENDMAIL $content;
- print SENDMAIL "\n.\n";
- close SENDMAIL;
- }
- sub wordwrap {
- my $word = shift;
- my $amount = shift || 80;
- my $w = $word;
- $amount -= 1;
- $w =~ s/(.{1,$amount}\S|\S+)\s+/$1\n/mg;
- return $w;
- }
- sub sendResponse {
- my ($from, $response) = (shift, shift);
- if($response eq "") {
- return;
- }
- $response =~ s/\n/ /ig;
- my @responses = split /\n/,&wordwrap($response,150);
- my $i = 0;
- my $num = $#responses+1;
- foreach my $response (@responses) {
- $response .= " (".($i+1)."/$num)" if $num > 1;
- &mail($from, "txt-bus", $response);
- $i++;
- }
- }
- sub lusers {
- my $ret = "Userlist\n";
- my $q = $db->prepare("SELECT id,name,nick FROM txt_main" or $ret = "Error getting users ($DBI::errstr)");
- $q->execute or &dbg("Could not list users ($DBI::errstr)");
- if($q->rows == 0) {
- $ret = "No users registered.";
- } else {
- while(my ($id, $name, $nick) = $q->fetchrow_array()) {
- &dbg("id: $id, name: $name, nick: $nick\n");
- if($nick == "") {
- $name = $nick;
- } else {
- my @n = split / /,$name;
- $name = $n[0];
- }
- $ret .= "$id. $name\n";
- }
- }
- $q->finish;
- return $ret;
- }
- sub ago {
- my $tm = shift;
- my $rcs = shift or 0;
- my $cur_tm = time;
- my $no;
- my $v;
- my $dif = $cur_tm - $tm;
- my @pds = ("second","minute","hour","day","week","month","year","decade");
- my @lngh = array(1,60,3600,8400,604800,2630880,31570560,315705600);
- for($v = $#lngh-1; ($v >= 0) && (($no = $dif/$lngh[$v]) <= 1); $v--) { }
- if($v < 0) {
- $v = 0;
- }
- my $_tm = $cur_tm - ($dif%$lngh[$v]);
- $no = floor($no);
- if($no != 1) {
- $pds[$v] .= 's';
- }
- my $x = sprintf "%d %s", $no, $pds[$v];
- if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) {
- $x .= &ago($_tm);
- }
- return $x;
- }
- sub getStatus {
- my $id = shift;
- my $ret = "";
- my $q = $db->prepare("SELECT `status`,`when` FROM txt_main_status WHERE userid=? ORDER BY `status` DESC LIMIT 1");
- $q->execute($id) or &dbg("Couldn't get status for userid $id ($DBI::errstr)");
- if($q->rows == 0) {
- $ret = "No status updates from that person.";
- } else {
- my ($status, $when) = $q->fetchrow_array();
- my $who = &getName($id);
- $when = &ago($when);
- $ret = "${who}'s status: $status (posted $when)";
- }
- return $ret;
- }
- sub getName {
- my $id = shift;
- my $field = shift || "id";
- my $ret = "";
- my $q = $db->prepare("SELECT username,name,nick FROM txt_main WHERE $field=?");
- $q->execute($id) or &dbg("Could not look up user $id ($DBI::errstr)");
- if($q->rows == 0) {
- $ret = "Could not find a user with that id.";
- } else {
- my ($username,$name,$nick) = $q->fetchrow_array();
- if($nick != "") {
- $ret = $nick;
- } elsif ($name != "") {
- my @n = split(/ /, $name);
- $ret = $n[0];
- } else {
- $ret = $username;
- }
- }
- return $ret;
- }
- sub getMostRecent {
- my $id = shift;
- my $ret = "";
- my $q = $db->prepare("SELECT userid,`when` FROM txt_main_status ORDER BY `when` DESC LIMIT 1");
- $q->execute or &dbg("Couldn't get most recent: ($DBI::errstr)");
- my $uid = $q->fetchrow_array();
- $ret = "Most recent update\n".&getStatus($uid);
- return $ret;
- }
- sub getID {
- my $value = shift;
- my $field = shift;
- return &get("id","txt_main",$field,$value);
- }
- sub get {
- my $ret = "";
- my $what = shift;
- my $table = shift;
- my $from = shift;
- my $value = shift;
- my $q = $db->prepare("SELECT $what FROM $table WHERE $from=?");
- $q->execute($value) or $ret=-1;
- if($q->rows == 0) {
- $ret = 0;
- } else {
- $ret = $q->fetchrow_array();
- }
- return $ret;
- }
- sub updateStatus {
- my ($from,$stat) = (shift, shift);
- $stat =~ s/\n//g;
- my $id = &getID($from,"email");
- &dbg("updateStatus, from = $from\n");
- my $ret = "";
- if($id == 0) {
- $ret = "Couldn't update status.";
- &dbg("Couldn't update status because user sent from an email address that doesn't exist.\n");
- } elsif ($id == -1) {
- $ret = "Couldn't update status.";
- &dbg("Couldn't update status because of mysql error.\n");
- } else {
- my $time = time;
- $db->do("INSERT INTO txt_status (`status`,`when`,`userid`) VALUES (".$db->quote($stat).",$time,$id)") or &dbg("Couldn't insert into txt_status ($DBI::errstr)");
- $db->do("UPDATE txt_main SET lastupdate=".$db->{'mysql_insertid'}." WHERE id=$id") or &dbg("Couldn't insert into txt_main ($DBI::errstr)");
- $db->do("UPDATE txt_main SET lastupdatetxt=".$db->quote($stat)." WHERE id=$id") or &dbg("Couldn't insert last txt into txt_main ($DBI::errstr)");
- my $q = $db->prepare("SELECT followerid FROM txt_main_followers WHERE userid=$id");
- my $fid;
- while($fid = $q->fetchrow_array()) {
- my $em = &get("email","id",$fid);
- my $n = &getName($fid);
- my $msg = "New update from $n: $stat";
- &sendResponse($em,$msg);
- }
- }
- }
- sub isFollowing {
- my ($user,$follower) = (shift, shift);
- my $q = $db->prepare("SELECT userid,followerid FROM txt_main_followers WHERE userid=? AND followerid=?");
- $q->execute($user,$follower);
- return $q->rows > 0;
- }
- sub exists {
- my $id = shift;
- my $field = shift || "id";
- return !(!(&get("id","txt_main",$field,$id)));
- }
- sub follow {
- my $from = shift;
- my $who = shift;
- my $n = &getName($who);
- my $q = $db->prepare("SELECT id FROM txt_main WHERE email=?");
- my $ret = "";
- $q->execute($from) or &dbg("Error following: $DBI::errstr");
- if($q->rows == 0) {
- $ret = "You aren't a user with us, are you?"
- } elsif(!&exists($who)) {
- $ret = "That user doesn't exist.";
- } else {
- my $id = $q->fetchrow_array();
- if(&isFollowing($id,$who)) {
- $ret = "You are already following this user!";
- } elsif($who == $id) {
- $ret = "You can't follow yourself.";
- } else {
- $q = $db->do("INSERT INTO txt_followers (userid,followerid,time) VALUES ($who, $id, ".time.")") or &dbg("Could not add userid $id as follower to userid $who ($DBI::errstr)");
- if($q == -255) {
- $ret = "Could not follow $n";
- } else {
- $ret = "You are now following $n."
- }
- }
- }
- return $ret;
- }
- sub unfollow {
- my $from = shift;
- my $who = shift;
- my $n = &getName($who);
- my $q = $db->prepare("SELECT id FROM txt_main WHERE email=?");
- my $ret = "";
- $q->execute($from) or &dbg("Error unfollowing: $DBI::errstr");
- if($q->rows == 0) {
- $ret = "You aren't a user with us, are you?."
- } elsif(!&exists($who)) {
- $ret = "That user doesn't exist.";
- } else {
- my $id = $q->fetchrow_array();
- if(!&isFollowing($id,$who)) {
- $ret = "You aren't following this user!";
- } elsif($who == $id) {
- $ret = "You can't unfollow yourself.";
- } else {
- $q = $db->do("DELETE FROM txt_followers WHERE userid=$id AND followerid=$who") or &dbg("Could not remove userid $id as follower to userid $who ($DBI::errstr)");
- if($q == -255) {
- $ret = "Could not unfollow $n";
- } else {
- $ret = "You are no longer following $n."
- }
- }
- }
- return $ret;
- }
- sub msg {
- my ($from, $usr, $txt) = (shift, shift, shift);
- my $name;
- my $nm;
- my $em;
- my $msg;
- my $q = $db->prepare("SELECT * FROM txt WHERE id=?");
- my $ret;
- $q=>execute($usr) or &dbg("Error messaging a user ($DBI::errstr)");
- if($q == -255) {
- $ret = "Sorry, couldn't message that user.";
- } else {
- my %hash = $q->fetchrow_hash();
- $name = &getName($usr);
- $nm = &getName($from,"email");
- $em = $hash{"email"};
- $msg = "Hi $name! You have received a message from $nm: $txt\n To reply, please message back with msg $usr: your message here";
- &sendResponse($em,$msg);
- $ret = "Message sent to $name.";
- }
- return $ret;
- }
- sub addUser {
- my ($email, $username, $password, $created) = (shift, shift, shift, time);
- return $db->do("INSERT INTO txt_main (email, username, password, created) VALUES (".$db->quote($email).",".$db->quote($username).",'".md5_hex($password)."',$created)") or &dbg("Could not created user: $username, with email: $email, password: $password, at $created ($DBI::errstr)");
- }
- my $term = $/;
- undef $/;
- my $content = <STDIN>;
- $/ = $term;
- my $tm = time();
- &dbg("Body content: $content");
- my @arr = split /\n\n/,$content;
- my @headers = split /\n/,$arr[0];
- my @body = splice(@arr,1);
- my $phone;
- my $from;
- my $subject;
- foreach my $cur (@headers) {
- if($cur =~ /From: .+ \<(.+)\>/i) {
- $phone = $from = $1;
- } elsif($cur =~ /From: (.+)/i) {
- $phone = $from = $1;
- } elsif ($cur =~ /Subject: (.+)/i) {
- $subject = $1;
- }
- }
- my $img = "";
- my $noText = !(join("\n",@body) =~ /Content-Type: text\/plain/i);
- my $noImg = !(join("\n",@body) =~ /img src/i);
- my $body;
- if(ref(@body) == "ARRAY") {
- for(my $i=0;$i<$#body+1;$i++) {
- if(!$noText && $noImg) {
- if($body[$i] =~ /Content-type: text\/plain/i) {
- $body = join("\n",@body[$i+1..$#body]);
- $body =~ s/--[\w\d\-\s\!\@\#\$\%\^\&\*\(\)\_\+\=\[\]\{\}\;\:\'\"\,\.\/\<\>\?]+//ig;
- last;
- }
- } else {
- if($body[$i] =~ /Content-type: text\/html/i) {
- $body = $body[$i+1];
- $body =~ s/<title>.+<\/title>//ig;
- $body =~ s/<\!\-\-.+?\-\->//is;
- if($noImg) {
- $body =~ s/\n\n//g;
- $body =~ s/<.*?>//ig;
- $body =~ s/[\r\t\n ]{2,}/ /ig;
- } else {
- #this is where we parse messages from sprint.
- #not important right now.
- }
- }
- }
- }
- }
- $body =~ s/On .+,.+ (wrote|said)://ig;
- $body =~ s/\n{2,}/ /ig;
- $body =~ s/^\s+//ig;
- $db->do("INSERT INTO incoming (body,time) VALUES (".$db->quote("TXT_DEBUG\nparsed body content: $body\n\n").",".time.")") or &dbg("Error: failed inserting into incoming ($DBI::errstr)");
- $body =~ s/>\s?.+\n//ig;
- if($phone =~ /(.+)@.+/) {
- $phone = $1;
- }
- my $email = $from;
- my $response = "";
- my $time = time;
- if(!&exists($from,"email")) {
- if($body =~ /reg ([\w\d\.]+) (.+)/i) {
- my $username = $1;
- my $password = $2;
- if(&addUser($email,$username,$password) == -255) {
- $response = "We couldn't register you. Please try again later.";
- } else {
- $response = "Welcome to txtbus, $username! Use cname to change your name, cnick to change your nick, cinfo to change your info, help for commands.";
- }
- } else {
- $response = "Hello $phone. You are not currently registered. To register, please reply back: 'reg username password'";
- }
- } else {
- $body = encode_entities($body);
- my $name = &getName(&get("id","txt_main","email",$email));
- $body =~ s/^\s*//ig;
- if($body =~ /^c(\w+) (.+)/) { #change info
- my $field = $1;
- if($field == "nick") {
- $field = "nickname";
- }
- if($field !~ /nick|name|info/) {
- $response = "Sorry, but you can't change that information.";
- } else {
- if(&change($from,$2,$1) != -255) {
- $response = "$1 changed successfully.";
- } else {
- $response = "We couldn't change your $1, sorry.";
- }
- }
- } elsif($body =~ /^fortune/) { #get a fortune
- #crappy workaround for fortune from mysql db, for simplicity
- #open FORTUNE,"|fortune";
- #$response = <FORTUNE>;
- #close FORTUNE;
- $response = "go to a chinese restaurant";
- #fortune isn't installed on this host :(
- } elsif($body =~ /^info (\d+)/i) { #geti nfo on a user
- $response = &getInfo($1);
- } elsif($body =~ /^lusers/i) { #List USERS
- $response = &lusers()
- } elsif($body =~ /^follow (\d+)/i) { #follow a user
- $response = &follow($from,$1);
- } elsif($body =~ /^unfollow (\d+)/i) { #unfollow a user
- $response = &unfollow($from,$1);
- } elsif($body =~ /^help ([a-z]+)/i) { #detailed help
- $response = &getHelp($1);
- } elsif($body =~ /^(help|menu)/i) { #get some help?
- $response = "cinfo|cname|cnick|fortune|info|lusers|msg|status|recent|menu|no cmd 2 update status|help cmd|follow|unfollow";
- } elsif($body =~ /^msg (\d+) (.+)/i) { #msg user
- $response = &msg($from,$1,$2);
- } elsif($body =~ /^recent/) { #get most recent post
- $response = &getMostRecent();
- } elsif($body =~ /^status (\d+)/i) { #check status for user
- $response = &getStatus($1);
- } else {
- $response = &updateStatus($from,$body);
- }
- }
- &sendResponse($email,$response);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement