<?page
title=>Send a Text Message
body<=
<?_code
{
    use strict;
    use vars qw(%FORM);

    LJ::set_active_crumb('textmessage');

    my $ret;

    my $user = $FORM{'user'};

    if ($FORM{'mode'} eq "details") 
    {
        $ret .= "<?h1 Service Providers h1?><?p The following text messaging service providers are supported. p?>";
        $ret .= "<UL>";
        foreach my $p (LJ::TextMessage::providers()) {
            my $info = LJ::TextMessage::provider_info($p);
            $ret .= "<P><B>$info->{'name'}</B> (from: $info->{'fromlimit'}, msg: $info->{'msglimit'}, total: $info->{'totlimit'})<BR>$info->{'notes'}";
        }
        $ret .= "</UL>";
        &add_footer();
        return $ret;
    } 

    unless ($user) {
        $ret .= "<?h1 Enter user h1?><?p Enter the LiveJournal username of the person you'd like to send a text message to: <div style='margin-left: 40px'><form method='get' action='textmessage.bml'>Username: <input type='text' size='15' maxlength='15' name='user' /> <input type='submit' value=\"Proceed...\" /></form></div> p?>";
        &add_footer();
        return $ret;
    }
    
    my $u = LJ::load_user($user);
    my $tminfo;
    
    if ($u->{'txtmsg_status'} eq "on") {
        my $dbr = LJ::get_db_reader();
        $tminfo = $dbr->selectrow_hashref("SELECT provider, number, security ".
                                          "FROM txtmsg WHERE userid=$u->{'userid'}")
        }
    
    unless (LJ::get_cap($u, "textmessaging")) {
        $ret .= "<?h1 Unavailable h1?><?p This user's account type does not permit text messaging. p?>";
        &add_footer();
        return $ret;
    }
    
    unless ($tminfo) {
        $ret .= "<?h1 Unavailable h1?><?p This user has not setup their text messaging ".
            "information at LiveJournal or they've turned it off. p?>";
        &add_footer();
        return $ret;
    }

    my $remote;
    if ($tminfo->{'security'} ne "all") {
        $remote = LJ::get_remote();
        my $andbefriend;
        if ($tminfo->{'security'} eq "friends") {
            $andbefriend = " and be listed as their friend";
        }
        unless ($remote) {
            $ret .= "<?h1 Not logged in h1?><?p To send a text message to this user, you must be <A HREF=\"/login.bml?ret=1\">logged in</A>$andbefriend. p?>";
            &add_footer();
            return $ret;
        }
        
        if ($tminfo->{'security'} eq "friends" && $u->{'userid'} != $remote->{'userid'}) {
            unless (LJ::is_friend($u->{'userid'}, $remote->{'userid'})) {
                $ret .= "<?h1 Unauthorized h1?><?p User <B>$u->{'user'}</B> has selected \"friends only\" as the security level required to send text messages to them.  p?>";
                &add_footer();
                return $ret;
            }
        }
    }

    ###### authorized.
    
    if ($FORM{'message'}) {
        my $message = $FORM{'message'};
        my $from = $tminfo->{'security'} eq "all" ? $FORM{'from'} : $remote->{'user'};
        
        my $phone = new LJ::TextMessage { 'provider' => $tminfo->{'provider'},
                                          'number' => $tminfo->{'number'}, 
                                          'mailcommand' => $LJ::SENDMAIL,
                                          'smtp' => $LJ::SMTP_SERVER,
                                      };
        my @errors;
        $phone->send({ 'from' => $from, 
                       'message' => $message, },
                     \@errors);

        # strip numbers from error messages
        s/(\d{3,})/'x'x length $1/eg foreach @errors;
        
        return LJ::bad_input(@errors) if @errors;
        
        $ret .= "<?h1 Success h1?><?p Your text message was sent. p?>";
        &add_footer();
        return $ret;
    }
    
    my $pinfo = LJ::TextMessage::provider_info($tminfo->{'provider'});

    $ret .= "<?h1 Send a message h1?><?p Fill out this form to send a text message to <B>$u->{'user'}</B>. p?>";
    $ret .= "<ul>";
    $ret .= "<form name='frmMsg' method='post' action='textmessage.bml'><input type='hidden' name='user' value=\"$u->{'user'}\" />";
    $ret .= "<b>From: </b> ";
    my $maxlen = $pinfo->{'totlimit'};
    if ($tminfo->{'security'} eq "all") {
        $ret .= "<INPUT NAME=from MAXLENGTH=15 SIZE=15 VALUE=\"$remote->{'user'}\">\n";
        $maxlen -= 15;
    } else {
        $ret .= "<INPUT NAME=from TYPE=HIDDEN VALUE=\"$remote->{'user'}\">\n";
        $ret .= "<TT>$remote->{'user'}</TT>";
        $maxlen -= length($remote->{'user'});
    }
    if ($pinfo->{'msglimit'} < $maxlen) { $maxlen = $pinfo->{'msglimit'}; }
    $ret .= "<P><B>Message:</B> (max <TT>$maxlen</TT> characters ... type until it stops you)<BR>";
    
    $ret .= "<input name='message' size='50' maxlength='$maxlen' />\n";
    $ret .= "<p><input type='submit' value=\"Send Message!\" /></p>";
    $ret .= "</form></ul>";
    
    &add_footer();
    return $ret;
    
    sub add_footer
    {
        my $howwork = $LJ::HELP_URL{'textmessaging_about'} ? "How does this feature work?  <a href=\"$LJ::HELP_URL{'textmessaging_about'}\">Read this</a>.<br />" : "";
        $ret .= "<p><hr /><font size='-1'>$howwork<B>Disclaimer:</B> The reliability of text messaging should not be trusted in dealing with emergencies.</font>";
    }

}
_code?>
<=body
page?><?_c <LJDEP>
lib: LJ::TextMessage
link: htdocs/login.bml, htdocs/support/faqbrowse.bml
form: htdocs/tools/textmessage.bml
post: htdocs/tools/textmessage.bml
</LJDEP> _c?>
