Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 17.89 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use DBI;
  3. use POSIX;
  4. use Digest::MD5 qw(md5_hex);
  5. use HTML::Entities;
  6.  
  7. use strict;
  8. my $dsn = "DBI:mysql:database=enigma_toolbus:host=localhost:port=3006";
  9. my $user = "enigma_toolbus";
  10. my $pass = "alpha345";
  11. my $db = DBI->connect($dsn,$user,$pass);
  12. $db->{RaiseError} = 1;
  13.  
  14. sub dbg {
  15.         my $dbg = shift;
  16.         return $db->do("INSERT INTO debug (time, info, project) VALUES (".time.",".$db->quote($dbg).",'txt')") or -255;
  17. }
  18.  
  19. sub change {
  20.         my ($who, $what, $to) = (shift, shift, shift);
  21.         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)");
  22. }
  23.  
  24. sub getHelp {
  25.         my $what = shift;
  26.         my $ret = "";
  27.         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)";
  28.         my %list = split /[\-\|]/,$h;
  29.         if(exists($list{$what})) {
  30.                 $ret = $list{$what};
  31.         } else {
  32.                 $ret = "The command you provided, $what, was not found.";
  33.         }
  34.         return $ret;
  35. }
  36.  
  37. sub getInfo {
  38.         my $ret = "";
  39.         my $id = shift;
  40.         my $q = $db->prepare("SELECT info FROM txt_main WHERE id=?");
  41.         $q->execute($id) or &dbg("Error getting info ($DBI::errstr)");
  42.         if($q->rows != 1) {
  43.                 $ret = "Couldn't find a user with id: $id";
  44.         } else {
  45.                 my ($info) = $q->fetchrow_array();
  46.                 my $name = &getName($id);
  47.                 $ret = "${name}'s info: $info";
  48.         }
  49.         return $ret;
  50.         $q->finish;
  51. }
  52.  
  53. sub mail {
  54.         my $to = shift;
  55.         my $subject = shift;
  56.         $subject = "Subject: ".$subject."\n";
  57.         my $content = shift;
  58.         my $headers = shift || "From: txt\@sirmxe.info\nReply-to: txt\@sirmxe.info";
  59.         return if $content eq "";
  60.         &dbg("sending mail: $content to $to");
  61.         my $sendmail = "/usr/sbin/sendmail -t";
  62.         open SENDMAIL,"|$sendmail";
  63.         print SENDMAIL $headers;
  64.         print SENDMAIL $subject;
  65.         print SENDMAIL "To: $to\n";
  66.         print SENDMAIL "\n";
  67.         print SENDMAIL $content;
  68.         print SENDMAIL "\n.\n";
  69.         close SENDMAIL;
  70. }
  71.  
  72. sub wordwrap {
  73.         my $word = shift;
  74.         my $amount = shift || 80;
  75.         my $w = $word;
  76.         $amount -= 1;
  77.         $w =~ s/(.{1,$amount}\S|\S+)\s+/$1\n/mg;
  78.         return $w;
  79. }
  80.  
  81. sub sendResponse {
  82.         my ($from, $response) = (shift, shift);
  83.         if($response eq "") {
  84.                 return;
  85.         }
  86.         $response =~ s/\n/ /ig;
  87.         my @responses = split /\n/,&wordwrap($response,150);
  88.         my $i = 0;
  89.         my $num = $#responses+1;
  90.         foreach my $response (@responses) {
  91.                 $response .= " (".($i+1)."/$num)" if $num > 1;
  92.                 &mail($from, "txt-bus", $response);
  93.                 $i++;
  94.         }
  95. }
  96.  
  97. sub lusers {
  98.         my $ret = "Userlist\n";
  99.         my $q = $db->prepare("SELECT id,name,nick FROM txt_main" or $ret = "Error getting users ($DBI::errstr)");
  100.         $q->execute or &dbg("Could not list users ($DBI::errstr)");
  101.         if($q->rows == 0) {
  102.                 $ret = "No users registered.";
  103.         } else {
  104.                 while(my ($id, $name, $nick) = $q->fetchrow_array()) {
  105.                         &dbg("id: $id, name: $name, nick: $nick\n");
  106.                         if($nick == "") {
  107.                                 $name = $nick;
  108.                         } else {
  109.                                 my @n = split / /,$name;
  110.                                 $name = $n[0];
  111.                         }
  112.                         $ret .= "$id. $name\n";
  113.                 }
  114.         }
  115.         $q->finish;
  116.         return $ret;
  117. }
  118.  
  119. sub ago {
  120.         my $tm = shift;
  121.         my $rcs = shift or 0;
  122.         my $cur_tm = time;
  123.         my $no;
  124.         my $v;
  125.         my $dif = $cur_tm - $tm;
  126.         my @pds = ("second","minute","hour","day","week","month","year","decade");
  127.         my @lngh = array(1,60,3600,8400,604800,2630880,31570560,315705600);
  128.         for($v = $#lngh-1; ($v >= 0) && (($no = $dif/$lngh[$v]) <= 1); $v--) { }
  129.         if($v < 0) {
  130.                 $v = 0;
  131.         }
  132.         my $_tm = $cur_tm - ($dif%$lngh[$v]);
  133.         $no = floor($no);
  134.         if($no != 1) {
  135.                 $pds[$v] .= 's';
  136.         }
  137.         my $x = sprintf "%d %s", $no, $pds[$v];
  138.         if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) {
  139.                 $x .= &ago($_tm);
  140.         }
  141.         return $x;
  142. }
  143.  
  144. sub getStatus {
  145.         my $id = shift;
  146.         my $ret = "";
  147.         my $q = $db->prepare("SELECT `status`,`when` FROM txt_main_status WHERE userid=? ORDER BY `status` DESC LIMIT 1");
  148.         $q->execute($id) or &dbg("Couldn't get status for userid $id ($DBI::errstr)");
  149.         if($q->rows == 0) {
  150.                 $ret = "No status updates from that person.";
  151.         } else {
  152.                 my ($status, $when) = $q->fetchrow_array();
  153.                 my $who = &getName($id);
  154.                 $when = &ago($when);
  155.                 $ret = "${who}'s status: $status (posted $when)";
  156.         }
  157.         return $ret;
  158. }
  159.  
  160. sub getName {
  161.         my $id = shift;
  162.         my $field = shift || "id";
  163.         my $ret = "";
  164.         my $q = $db->prepare("SELECT username,name,nick FROM txt_main WHERE $field=?");
  165.         $q->execute($id) or &dbg("Could not look up user $id ($DBI::errstr)");
  166.         if($q->rows == 0) {
  167.                 $ret = "Could not find a user with that id.";
  168.         } else {
  169.                 my ($username,$name,$nick) = $q->fetchrow_array();
  170.                 if($nick != "") {
  171.                         $ret = $nick;
  172.                 } elsif ($name != "") {
  173.                         my @n = split(/ /, $name);
  174.                         $ret = $n[0];
  175.                 } else {
  176.                         $ret =  $username;
  177.                 }
  178.         }
  179.         return $ret;
  180. }
  181.  
  182. sub getMostRecent {
  183.         my $id = shift;
  184.         my $ret = "";
  185.         my $q = $db->prepare("SELECT userid,`when` FROM txt_main_status ORDER BY `when` DESC LIMIT 1");
  186.         $q->execute or &dbg("Couldn't get most recent: ($DBI::errstr)");
  187.         my $uid = $q->fetchrow_array();
  188.         $ret = "Most recent update\n".&getStatus($uid);
  189.         return $ret;
  190. }
  191.  
  192. sub getID {
  193.         my $value = shift;
  194.         my $field = shift;
  195.         return &get("id","txt_main",$field,$value);
  196. }
  197.  
  198. sub get {
  199.         my $ret = "";
  200.         my $what = shift;
  201.         my $table = shift;
  202.         my $from = shift;
  203.         my $value = shift;
  204.         my $q = $db->prepare("SELECT $what FROM $table WHERE $from=?");
  205.         $q->execute($value) or $ret=-1;
  206.         if($q->rows == 0) {
  207.                 $ret = 0;
  208.         } else {
  209.                 $ret = $q->fetchrow_array();
  210.         }
  211.         return $ret;
  212. }
  213.  
  214. sub updateStatus {
  215.         my ($from,$stat) = (shift, shift);
  216.         $stat =~ s/\n//g;
  217.         my $id = &getID($from,"email");
  218.         &dbg("updateStatus, from = $from\n");
  219.         my $ret = "";
  220.         if($id == 0) {
  221.                 $ret = "Couldn't update status.";
  222.                 &dbg("Couldn't update status because user sent from an email address that doesn't exist.\n");
  223.         } elsif ($id == -1) {
  224.                 $ret = "Couldn't update status.";
  225.                 &dbg("Couldn't update status because of mysql error.\n");
  226.         } else {
  227.                 my $time = time;
  228.                 $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)");
  229.                 $db->do("UPDATE txt_main SET lastupdate=".$db->{'mysql_insertid'}." WHERE id=$id") or &dbg("Couldn't insert into txt_main ($DBI::errstr)");
  230.                 $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)");
  231.                 my $q = $db->prepare("SELECT followerid FROM txt_main_followers WHERE userid=$id");
  232.                 my $fid;
  233.                 while($fid = $q->fetchrow_array()) {
  234.                         my $em = &get("email","id",$fid);
  235.                         my $n = &getName($fid);
  236.                         my $msg = "New update from $n: $stat";
  237.                         &sendResponse($em,$msg);
  238.                 }
  239.         }
  240. }
  241.  
  242. sub isFollowing {
  243.         my ($user,$follower) = (shift, shift);
  244.         my $q = $db->prepare("SELECT userid,followerid FROM txt_main_followers WHERE userid=? AND followerid=?");
  245.         $q->execute($user,$follower);
  246.         return $q->rows > 0;
  247. }
  248.  
  249. sub exists {
  250.         my $id = shift;
  251.         my $field = shift || "id";
  252.         return !(!(&get("id","txt_main",$field,$id)));
  253. }
  254.  
  255. sub follow {
  256.         my $from = shift;
  257.         my $who = shift;
  258.         my $n = &getName($who);
  259.         my $q = $db->prepare("SELECT id FROM txt_main WHERE email=?");
  260.         my $ret = "";
  261.         $q->execute($from) or &dbg("Error following: $DBI::errstr");
  262.         if($q->rows == 0) {
  263.                 $ret = "You aren't a user with us, are you?"
  264.         } elsif(!&exists($who)) {
  265.                 $ret = "That user doesn't exist.";
  266.         } else {
  267.                 my $id = $q->fetchrow_array();
  268.                 if(&isFollowing($id,$who)) {
  269.                         $ret = "You are already following this user!";
  270.                 } elsif($who == $id) {
  271.                         $ret = "You can't follow yourself.";
  272.                 } else {
  273.                         $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)");
  274.                         if($q == -255) {
  275.                                 $ret = "Could not follow $n";
  276.                         } else {
  277.                                 $ret = "You are now following $n."
  278.                         }
  279.                 }
  280.         }
  281.         return $ret;
  282. }
  283.  
  284. sub unfollow {
  285.         my $from = shift;
  286.         my $who = shift;
  287.         my $n = &getName($who);
  288.         my $q = $db->prepare("SELECT id FROM txt_main WHERE email=?");
  289.         my $ret = "";
  290.         $q->execute($from) or &dbg("Error unfollowing: $DBI::errstr");
  291.         if($q->rows == 0) {
  292.                 $ret = "You aren't a user with us, are you?."
  293.         } elsif(!&exists($who)) {
  294.                 $ret = "That user doesn't exist.";
  295.         } else {
  296.                 my $id = $q->fetchrow_array();
  297.                 if(!&isFollowing($id,$who)) {
  298.                         $ret = "You aren't following this user!";
  299.                 } elsif($who == $id) {
  300.                         $ret = "You can't unfollow yourself.";
  301.                 } else {
  302.                         $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)");
  303.                         if($q == -255) {
  304.                                 $ret = "Could not unfollow $n";
  305.                         } else {
  306.                                 $ret = "You are no longer following $n."
  307.                         }
  308.                 }
  309.         }
  310.         return $ret;
  311. }
  312.  
  313. sub msg {
  314.         my ($from, $usr, $txt) = (shift, shift, shift);
  315.         my $name;
  316.         my $nm;
  317.         my $em;
  318.         my $msg;
  319.         my $q = $db->prepare("SELECT * FROM txt WHERE id=?");
  320.         my $ret;
  321.         $q=>execute($usr) or &dbg("Error messaging a user ($DBI::errstr)");
  322.         if($q == -255) {
  323.                 $ret = "Sorry, couldn't message that user.";
  324.         } else {
  325.                 my %hash = $q->fetchrow_hash();
  326.                 $name = &getName($usr);
  327.                 $nm = &getName($from,"email");
  328.                 $em = $hash{"email"};
  329.                 $msg = "Hi $name! You have received a message from $nm: $txt\n To reply, please message back with msg $usr: your message here";
  330.                 &sendResponse($em,$msg);
  331.                 $ret = "Message sent to $name.";
  332.         }
  333.         return $ret;
  334. }
  335.  
  336.  
  337. sub addUser {
  338.         my ($email, $username, $password, $created) = (shift, shift, shift, time);
  339.         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)");
  340. }
  341. my $term = $/;
  342. undef $/;
  343. my $content = <STDIN>;
  344. $/ = $term;
  345. my $tm = time();
  346. &dbg("Body content: $content");
  347. my @arr = split /\n\n/,$content;
  348. my @headers = split /\n/,$arr[0];
  349. my @body = splice(@arr,1);
  350. my $phone;
  351. my $from;
  352. my $subject;
  353. foreach my $cur (@headers) {
  354.         if($cur =~ /From: .+ \<(.+)\>/i) {
  355.                 $phone = $from = $1;
  356.         } elsif($cur =~ /From: (.+)/i) {
  357.                 $phone = $from = $1;
  358.         } elsif ($cur =~ /Subject: (.+)/i) {
  359.                 $subject = $1;
  360.         }
  361. }
  362. my $img = "";
  363. my $noText = !(join("\n",@body) =~ /Content-Type: text\/plain/i);
  364. my $noImg = !(join("\n",@body) =~ /img src/i);
  365. my $body;
  366. if(ref(@body) == "ARRAY") {
  367.         for(my $i=0;$i<$#body+1;$i++) {
  368.                 if(!$noText && $noImg) {
  369.                         if($body[$i] =~ /Content-type: text\/plain/i) {
  370.                                 $body = join("\n",@body[$i+1..$#body]);
  371.                                 $body =~ s/--[\w\d\-\s\!\@\#\$\%\^\&\*\(\)\_\+\=\[\]\{\}\;\:\'\"\,\.\/\<\>\?]+//ig;
  372.                                 last;
  373.                         }
  374.                 } else {
  375.                         if($body[$i] =~ /Content-type: text\/html/i) {
  376.                                 $body = $body[$i+1];
  377.                                 $body =~ s/<title>.+<\/title>//ig;
  378.                                 $body =~ s/<\!\-\-.+?\-\->//is;
  379.                                 if($noImg) {
  380.                                         $body =~ s/\n\n//g;
  381.                                         $body =~ s/<.*?>//ig;
  382.                                         $body =~ s/[\r\t\n ]{2,}/ /ig;
  383.                                 } else {
  384.                                         #this is where we parse messages from sprint.
  385.                                         #not important right now.
  386.                                 }
  387.                         }
  388.                 }
  389.         }
  390. }
  391. $body =~ s/On .+,.+ (wrote|said)://ig;
  392. $body =~ s/\n{2,}/ /ig;
  393. $body =~ s/^\s+//ig;
  394. $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)");
  395. $body =~ s/>\s?.+\n//ig;
  396. if($phone =~ /(.+)@.+/) {
  397.         $phone = $1;
  398. }
  399. my $email = $from;
  400. my $response = "";
  401. my $time = time;
  402. if(!&exists($from,"email")) {
  403.         if($body =~ /reg ([\w\d\.]+) (.+)/i) {
  404.                 my $username = $1;
  405.                 my $password = $2;
  406.                 if(&addUser($email,$username,$password) == -255) {
  407.                         $response = "We couldn't register you. Please try again later.";
  408.                 } else {
  409.                         $response = "Welcome to txtbus, $username! Use cname to change your name, cnick to change your nick, cinfo to change your info, help for commands.";
  410.                 }
  411.         } else {
  412.                 $response = "Hello $phone. You are not currently registered. To register, please reply back: 'reg username password'";
  413.         }
  414. } else {
  415.         $body = encode_entities($body);
  416.         my $name = &getName(&get("id","txt_main","email",$email));
  417.         $body =~ s/^\s*//ig;
  418.         if($body =~ /^c(\w+) (.+)/) { #change info
  419.                 my $field = $1;
  420.                 if($field == "nick") {
  421.                         $field = "nickname";
  422.                 }
  423.                 if($field !~ /nick|name|info/) {
  424.                         $response = "Sorry, but you can't change that information.";
  425.                 } else {
  426.                         if(&change($from,$2,$1) != -255) {
  427.                                 $response = "$1 changed successfully.";
  428.                         } else {
  429.                                 $response = "We couldn't change your $1, sorry.";
  430.                         }
  431.                 }
  432.         } elsif($body =~ /^fortune/) { #get a fortune
  433.                 #crappy workaround for fortune from mysql db, for simplicity
  434.                 #open FORTUNE,"|fortune";
  435.                 #$response = <FORTUNE>;
  436.                 #close FORTUNE;
  437.                 $response = "go to a chinese restaurant";
  438.                 #fortune isn't installed on this host :(
  439.         } elsif($body =~ /^info (\d+)/i) { #geti nfo on a user
  440.                 $response = &getInfo($1);
  441.         } elsif($body =~ /^lusers/i) { #List USERS
  442.                 $response = &lusers()
  443.         } elsif($body =~ /^follow (\d+)/i) { #follow a user
  444.                 $response = &follow($from,$1);
  445.         } elsif($body =~ /^unfollow (\d+)/i) { #unfollow a user
  446.                 $response = &unfollow($from,$1);
  447.         } elsif($body =~ /^help ([a-z]+)/i) { #detailed help
  448.                 $response = &getHelp($1);
  449.         } elsif($body =~ /^(help|menu)/i) { #get some help?
  450.                 $response = "cinfo|cname|cnick|fortune|info|lusers|msg|status|recent|menu|no cmd 2 update status|help cmd|follow|unfollow";
  451.         } elsif($body =~ /^msg (\d+) (.+)/i) { #msg user
  452.                 $response = &msg($from,$1,$2);
  453.         } elsif($body =~ /^recent/) { #get most recent post
  454.                 $response = &getMostRecent();
  455.         } elsif($body =~ /^status (\d+)/i) { #check status for user
  456.                 $response = &getStatus($1);
  457.         } else {
  458.                 $response = &updateStatus($from,$body);
  459.         }
  460. }
  461. &sendResponse($email,$response);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement