Changeset 1355

Show
Ignore:
Timestamp:
03/02/07 07:45:00 (2 years ago)
Author:
jm3
Message:

preliminary bot

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • feedmelinks/.htaccess-global

    r1350 r1355  
    5151RewriteRule    ^xml/set/comment   /api/comment 
    5252RewriteRule    ^xml/peeps$ /peeps-as-xml [L] 
    53 RewriteRule    ^api/backroom/(.*)$ /api/backroom/secure-call.php?call=$1 [L] 
     53RewriteRule    ^api/backroom/(.*)$ /api/restricted/secure-call.php?call=$1 [L] 
    5454 
    5555# JSON API 
  • feedmelinks/_config/groups

    r1350 r1355  
    3939cover 
    4040covey 
    41 cowardice 
    4241crash 
    4342cruft 
     
    210209cover 
    211210covey 
    212 cowardice 
    213211crash 
    214212cruft 
  • feedmelinks/aim/bot/klaus.pl

    r1354 r1355  
    3333        # has to happen at the main klaus level so we can force a reload of all the other commands in the module 
    3434        if( $message =~ m/reload/ ) { 
    35                 if( $sender eq "jm3theDestroyer" ) { 
     35                if( $sender eq "jm3theDestroyer" || $sender eq "jm3bottester" ) { 
    3636                        Module::Reload->check; 
    3737                        $sessions_ref = {}; 
    3838                        $aim->send_im($sender, "reloaded " . FeedMeLinks::Bot::get_version()); 
    3939                } else { 
    40                         $aim->send_im($sender, "(you are not jm3theDestroyer)" ); 
     40                        $aim->send_im($sender, "(you are not a super-user)" ); 
    4141                } 
    4242        } else { 
  • feedmelinks/api/add.php

    r1352 r1355  
    88        $tok =  $authenticated = $_COOKIE["c_pass_token"]; 
    99 
     10                header( "Content-type: text/xml"); 
    1011        if( $u && ($pass && $pass == getPasswdForUser( $u ) || ($tok && $tok == md5( getPasswdForUser( $u ))))) { 
    11                 header( "Content-type: text/xml"); 
    1212 
    1313                # support the del.icio.us api naming as well for name(description) and private(shared): 
     
    2727                } 
    2828 
    29                 echo "<result code='done' info='New link added as http://" . get_domain() . "/$new_link_ID'/>"; 
     29                echo "<result code='done' new_link='http://" . get_domain() . "/$new_link_ID'/>"; 
    3030 
    3131                if( $tags ) { 
     
    5656                header('WWW-Authenticate: Basic realm="Feed_Me_Links_XML_API"'); 
    5757                header('HTTP/1.0 401 Unauthorized'); 
    58                 echo "login first, please."; 
     58                echo "<result code='Log in first, please.'/>"; 
    5959        } 
    6060?> 
  • feedmelinks/export/index.php

    r1354 r1355  
    3434                <option value="html">HTML</option> 
    3535                <option value="json">JSON</option> 
    36                 <option value="rss">RSS - coming soon</option> 
    3736        </select> 
    3837        <select name="select"> 
  • feedmelinks/lib/FeedMeLinks/Bot.pm

    r1354 r1355  
    11#!/usr/local/bin/perl 
    22package FeedMeLinks::Bot; 
    3  
     3#use strict; 
     4 
     5use Data::Dumper; 
     6use Date::Format; 
     7use FeedMeLinks::Environment; 
     8use File::Temp qw/ :mktemp  /; 
     9use HTML::Strip; 
     10use HTML::TokeParser; 
     11use LWP::UserAgent; 
    412use MIME::Base64; 
    5 use LWP; 
    613use XML::XPath; 
    7 use Date::Format; 
    8 use Data::Dumper; 
    9 use FeedMeLinks::Environment; 
    10  
    11 my $www = LWP::UserAgent->new; 
    12 # FIXME: get the username and passwd from env.pm 
    13 $www->credentials( 
    14         "www." . FeedMeLinks::Environment::get_domain() . ":80", 
    15         'Feed_Me_Links_XML_API', 
    16         FeedMeLinks::Environment::get_bot_user() => FeedMeLinks::Environment::get_bot_password()  
    17 ); 
     14require Encode; 
     15 
     16my $www = get_proxy( FeedMeLinks::Environment::get_bot_user(), FeedMeLinks::Environment::get_bot_password()); 
    1817 
    1918sub h { 
     
    2423        my ($aim, $sender, $message, $away, $sessions_ref ) = @_; 
    2524        my $user; 
     25 
     26        my $result = is_user_validated( $sender ); 
     27        my $fml_username = $result ? $result : ""; 
     28 
     29        # FIXME: bust this out into an init method so we can call it equally when loading or when validating 
     30        # FIXME: and add the proxy. then we are rockin 
    2631        $sessions_ref->{ "$sender" } = {  
    2732                "last_link"    => "none yet", 
    28                 "fml_username" => "not_yet_associated_with_FML_account", 
    29                 "validated"    => undef 
    30                 }; 
     33                "validated"    => ($result ? 1 : 0), 
     34                "fml_username" => $fml_username, 
     35                "last_action"  => "", 
     36                "seen_welcome" => 0 
     37                } unless exists $sessions_ref->{ "$sender" }; 
     38 
     39         if( ! $sessions_ref->{ $sender }->{ "validated" } ) { 
     40                 if( ! $sessions_ref->{ $sender }->{ "seen_welcome" } ) { 
     41                                $aim->send_im($sender, h("<b>Hi! I am the Feed Me Links bot!</b>\nTo get started, you can type <b>links</b>, <b>help</b>, or click <b><a href='http://feedmelinks.com/set-up/IM?aim=$sender'>activate</a></b> to set up your account.") ); 
     42                         $sessions_ref->{ $sender }->{ "seen_welcome" } = 1; 
     43                 } 
     44         } 
     45         
     46        $message = strip_html( $message ); 
    3147 
    3248        # handle commands 
    33  
    3449        if( $message =~ m/(ftp|gopher|https?):\/\// || $message =~ m/www\./ ) { 
    3550                if( $sessions_ref->{ $sender }->{ "validated" } ) { 
    36                         $aim->send_im($sender, "add this link: next action: load and deref the title" ); 
     51                        my $url = $message; 
     52                        my $name = get_title_for_url( $url ); 
     53                        my $name = $name ? $name : $url; 
     54                        print "$name\n"; 
     55                        $aim->send_im($sender, "<b>Adding</b>:\n" . $name ); 
     56                        my $encoded_name = $name; 
     57                        $encoded_name =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; 
     58                        my $encoded_url = $url; 
     59                        $encoded_url =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; 
     60 
     61                        # FIXME: authenticate so that this works 
     62 
     63                        my $response = $www->get( "http://www.feedmelinks.com/api/add?url=$encoded_url&name=$encoded_name", []); 
     64                        my $xp = XML::XPath->new(xml => $response->content); 
     65                        print Dumper( $response->content ); 
     66                        my $code = $xp->findnodes("/result/@code"); 
     67                        #print "code: $code\n"; 
     68 
    3769                } else { 
    38                         $aim->send_im($sender, "i WANT to add that link, but you are NOT YET VALID (ez: next action, link)" ); 
     70                        # FIXME: should search and say "NN people have added this link -- click activate to add it" 
     71                        $aim->send_im($sender, h("That looks like a link! To add links via IM, just <b><a href='http://feedmelinks.com/set-up/IM?aim=$sender'>activate your account</a></b> by clicking." )); 
    3972                } 
    40  
    4173                 
     74        } elsif( $message =~ m/validate:/ ) { 
     75 
     76                        my $claimed_fml_username = $message; 
     77                        $claimed_fml_username =~ s/validate: //; 
     78                        my $aim_validation_phrase = $claimed_fml_username; # ordering of these lines is significant! 
     79                        $aim_validation_phrase =~ s/^.+, //; 
     80                        $aim_validation_phrase =~ s/<.*$//; 
     81                        $claimed_fml_username =~ s/,.+$//; 
     82                        print "claimed_fml_username: $claimed_fml_username\n"; 
     83                        print "aim_validation_phrase: $aim_validation_phrase\n"; 
     84 
     85                        print "http://www.feedmelinks.com/api/backroom/get_im_validation_phrase_for_user&user=$claimed_fml_username\n"; 
     86                my $response = $www->get(  
     87                        "http://www.feedmelinks.com/api/backroom/get_im_validation_phrase_for_user&user=$claimed_fml_username", 
     88                        []); 
     89                my $xp = XML::XPath->new(xml => $response->content); 
     90                my $fml_validation_phrase = $xp->getNodeText("/rest/response"); 
     91                print "fml_validation_phrase: $fml_validation_phrase\n"; 
     92 
     93                if( $fml_validation_phrase eq $aim_validation_phrase ) { 
     94                        my $response = $www->get(  
     95                                "http://www.feedmelinks.com/api/backroom/validate_users_im_name&user=$claimed_fml_username", 
     96                                []); 
     97                        $xp = XML::XPath->new(xml => $response->content); 
     98                        my $validation_response = $xp->getNodeText("/rest/response"); 
     99                        print "validation_response: $validation_response\n"; 
     100 
     101                        # update the sessions table 
     102                        $sessions_ref->{ "$sender" }->{ "validated" } = 1; 
     103                        $sessions_ref->{ "$sender" }->{ "fml_username" } = $claimed_fml_username; 
     104 
     105                        my $response = $www->get( "http://feedmelinks.com/api/backroom/getPasswdForUser&user=$claimed_fml_username", []); 
     106                        $xp = XML::XPath->new(xml => $response->content); 
     107                        my $fml_password = $xp->getNodeText("/rest/response"); 
     108                        print "fml_password: $fml_password\n"; 
     109                        $sessions_ref->{ "$sender" }->{ "proxy" } = get_proxy( $claimed_fml_username, $fml_password ); 
     110 
     111                        $aim->send_im($sender, "<b>All done -- that was easy</b>!\nNow your Feed Me Links account is linked to your AIM and you can start using Feed Me Links via AIM. To get started, paste any URL into this window." ); 
     112                } else { 
     113                        $aim->send_im($sender, h("Darn, that doesn't match our records. Please try again, or <a href='http://feedmelinks.com/contact-us?subject=AIM+issues:+'>contact us</a>.") ); 
     114                } 
     115 
    42116        } elsif( $message =~ m/who/ || $message =~ m/lusers/ ) { 
    43117                my $users = ""; 
    44118    for my $user ( keys %$sessions_ref ) { 
     119                        print "who: $user\n"; 
    45120                        $users .=  
    46121                        "<a href='http://feedmelinks.com/u/"  
     
    59134                $aim->send_im($sender, get_version()); 
    60135         
     136        } elsif( $message =~ m/(links|recent)/ ) { 
     137                $aim->send_im($sender, "FIXME: recent links here! (not finished yet, sorry"); 
     138         
    61139        } elsif( $message =~ m/(recent|new)/ ) { 
    62140                $aim->send_im($sender, "FIXME: show the last 10 links"); 
     
    73151 
    74152        } elsif( $message =~ m/status/ ) { 
    75                 $aim->send_im($sender, "<b>DEBUG DUMP</b>:\nsender: $sender, \nurl: $url, \nrequest: $request, \nresponse: $response, \nusers_with_this_im_name: " . $users_with_this_im_name . ", \nsingle_user: $single_user, \nis_valid: $is_valid, \nmessage: $message, $away " ); 
    76                 if( $user_is_logged_in ) { 
    77                         $aim->send_im($sender, "we know you from this session! welcome!" ); 
    78                 } 
     153                $aim->send_im($sender, "<b>DEBUG DUMP</b>:\nsender: $sender\nmessage: $message" ); 
    79154                $aim->send_im($sender, Dumper( $sessions_ref )); 
    80155        } elsif( 0 == "check_validation" ) { 
    81         if( 0 ) { 
    82                 $session_user{"last_step"} = "waiting_for_validation_phrase"; 
    83                 my $url = "http://www.feedmelinks.com/api/backroom/is_im_account_known&user=$sender"; 
    84                 my $parameters = [ ]; 
    85                 my $response = $www->get($url, $parameters); 
    86                 my $xp = XML::XPath->new(xml => $response->content); 
    87                 my $response = $xp->getNodeText("/rest/response"); 
    88                 my $request = $xp->getNodeText("/rest/request"); 
    89                 my $is_valid = $xp->getNodeText("/rest/user/\@valid"); 
    90                 my $users_with_this_im_name = $xp->findnodes("/rest/user")->size(); 
    91                 my $single_user = $xp->getNodeText("/rest/user"); 
    92  
    93                 if( $users_with_this_im_name == 0 ) { 
    94                         $aim->send_im($sender, "Welcome to the Feed Me Links bot. To link your AIM account to your feed me links account, just click this link: http://feedmelinks.com/set-up/IM?im_name=$sender " ); 
    95                 } 
    96         } 
     156 
    97157        } elsif( 0 == "waiting_for_validation_phrase" ) { 
    98158                $aim->send_im($sender, "within for this validation, butchy" ); 
    99         } else { 
    100  
    101                 # no op 
    102159        } 
    103160        return $sessions_ref; 
    104161} 
    105162 
     163sub is_user_validated { 
     164        my $sender = shift; 
     165        my $response = $www->get( "http://www.feedmelinks.com/api/backroom/is_im_account_known&user=$sender" , []); 
     166        my $xp = XML::XPath->new(xml => $response->content); 
     167        my $is_valid = $xp->findnodes('/rest/user/@valid'); 
     168        $is_valid .= ""; 
     169        my $fml_user = $xp->getNodeText("/rest/user"); 
     170 
     171        if( $is_valid ) { 
     172                print "this AIM account is associated with FML account $fml_user and has been validated\n"; 
     173                return "" . $fml_user; # stupid string conversion 
     174        } else { 
     175                print "(not showing username $fml_user cause it hasn't been validated -- could be anyone)\n"; 
     176                return 0; 
     177        } 
     178        # "Welcome to the Feed Me Links bot. To link your AIM account to your feed me links account, just click this link: http://feedmelinks.com/set-up/IM?im_name=$sender " 
     179} 
     180 
     181sub get_proxy { 
     182        my( $u, $p ) = @_; 
     183        my $www = LWP::UserAgent->new; 
     184        $www->credentials( 
     185                "www." . FeedMeLinks::Environment::get_domain() . ":80", 
     186                'Feed_Me_Links_XML_API', 
     187                $u => $p 
     188        ); 
     189        return $www; 
     190} 
     191 
     192sub get_title_for_url { 
     193        my $url = $_[0]; 
     194        ($fh, $file) = mkstemp( "fml_aim_pagetitle_url_XXXX" ); 
     195 
     196        my $ua = LWP::UserAgent->new(); 
     197        my $response = $ua->get($url, ':content_file' => $file); 
     198 
     199        my $p = HTML::TokeParser->new($file); 
     200        my $title = ""; 
     201        if ($p->get_tag("title")) { 
     202                $title = $p->get_trimmed_text; 
     203        } 
     204        unlink( $file ); 
     205        return Encode::encode_utf8( $title ); 
     206        #return $title; 
     207} 
     208 
     209sub strip_html { 
     210        my $html = $_[0]; 
     211        my $hs = HTML::Strip->new(); 
     212        my $clean_text = $hs->parse( $html ); 
     213        return $clean_text; 
     214} 
     215 
    106216sub print_version { print get_version() . "\n"; } 
    107 sub get_version { return "version 0.1.3!"; } 
     217sub get_version { return "version 0.1.4!"; } 
    108218 
    1092191; # modules must return a true value 
  • feedmelinks/modules/main-interface.inc.php

    r1334 r1355  
    1616<big style='margin-left: 6px;'> 
    1717        You have <?= number_format( $num_links ) ?> links and <?= number_format( $num_tags ) ?> tags. 
     18        <p/> 
     19        <a href="/export">Export a copy of your links</a> 
    1820</big> 
    1921<?php 
  • feedmelinks/modules/utils.inc.php

    r1354 r1355  
    115115        $q = run_query( $qs ); 
    116116        export_driver( $u, $select, $format, $q ); 
    117  
    118         $data = ob_get_clean(); 
    119         $data = preg_replace( "/,]/", "]", $data ); # FIXME: braindead why won't this fucking replace the # },]}} comma??????? 
     117        #$data = preg_replace( "/,]/", "]", $data ); # FIXME: braindead why won't this fucking replace the # },]}} comma??????? 
     118        $data = ($format == "rss" || $format == "xml_delicious") ? force_valid_xml( ob_get_clean()) : ob_get_clean(); 
    120119        return $data; 
    121         $data = force_valid_xml( $data ); 
    122120} 
    123121 
  • feedmelinks/set-up/IM.php

    r1352 r1355  
    1111<?php 
    1212        if( $tok && $tok == md5( getPasswdForUser( $u )) ) { 
     13                $stored_aim = get_IM_address_for_user( $u ); 
     14                if( $stored_aim && $stored_aim == $aim ) { 
     15                        echo bounceTo( "/validate/IM" ); 
     16                        return; 
     17                } 
     18 
    1319    $con_account = mysql_connect(); 
    1420    mysql_selectdb( getDBName() ); 
     
    2632<div id="challenge"> 
    2733        <h1>Success! Your Validation Code is:</h1> 
    28         <input class="copyable" name="secret" value="validate me: <?= get_im_validation_phrase_for_user( $u ) ?>" size="32" /> 
     34        <input class="copyable" name="secret" value="validate: <?= $u . ", " . get_im_validation_phrase_for_user( $u ) ?>" size="32" /> 
    2935        <small>Paste the above text into your AIM window to finish the setup process</small> 
    3036        <br /><br /><br /><br /> 
    31         <form> 
    32                 <input type="button" class="bigbutton" value="All done, close window" onClick="window.close();" /> 
    33         </form> 
     37        (All done, you can close this window now) 
    3438</div> 
    3539 
     
    5256                                        <input type="hidden" name="op" value="update-im" /> 
    5357                                        My AIM name: 
    54                                         <input type="text" name="im" size="24" value="<?= $im_name ?>" /> 
     58                                        <input type="text" name="im" size="24" value="<?= $aim ? $aim : $im_name ?>" /> 
    5559                                        &nbsp; <input type="submit" class="default button" style="padding: 0.4em; font-size: 14px;" value="Add AIM Account" /> 
    5660                                        <br /> 
  • feedmelinks/style/validation.css

    r1349 r1355  
    33.copyable { 
    44        font-size: 24px; 
    5         padding: 1.0em; 
     5        padding: 0.5em; 
    66        margin: 10px; 
    77        border: 1px dotted gray; 
     
    1616#challenge h1 { 
    1717        margin-bottom: 0px; 
     18 
    1819} 
    1920#challenge { 
    2021        text-align: center; 
    21         width: 500px; 
     22        width: 600px; 
    2223} 
  • feedmelinks/validate/IM.php

    r1350 r1355  
    1616                <div id="challenge"> 
    1717                        <h1>Your Validation Code is:</h1> 
    18                         <input class="copyable" name="secret" value="<?= get_im_validation_phrase_for_user( $u ) ?>" size="22" /> 
    19                         <small>Copy &amp; paste the above phrase into AIM when asked for your challenge code</small> 
     18                        <input class="copyable" name="secret" value="validate: <?= "$u, " . get_im_validation_phrase_for_user( $u ) ?>" size="32" /> 
     19                        <small>Copy &amp; paste the phrase from the above box into your AIM window.</small> 
    2020                </div> 
    2121