Changeset 1357
- Timestamp:
- 03/03/07 12:28:13 (2 years ago)
- Files:
-
- feedmelinks/lib/FeedMeLinks/Bot.pm (modified) (11 diffs)
- feedmelinks/lib/FeedMeLinks/Environment-template.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
feedmelinks/lib/FeedMeLinks/Bot.pm
r1355 r1357 15 15 16 16 my $www = get_proxy( FeedMeLinks::Environment::get_bot_user(), FeedMeLinks::Environment::get_bot_password()); 17 18 sub h { 19 return "<HTML>" . @_[0] . "</HTML>"; 20 } 21 17 my $site = "http://www." . FeedMeLinks::Environment::get_domain(); 18 19 # init 22 20 sub handle_response { 23 21 my ($aim, $sender, $message, $away, $sessions_ref ) = @_; 24 22 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 31 $sessions_ref->{ "$sender" } = { 32 "last_link" => "none yet", 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 23 # AIM often wraps seemingly plaintext messages with shouty <HTML><FONT>...</FONT></HTML> crap. this strips that 46 24 $message = strip_html( $message ); 47 25 26 # init user 27 28 my $fml_username = is_user_validated( $sender ); 29 $fml_username .= ""; # type coerce 30 print "fml_username: $fml_username\n"; 31 if( ! $fml_username ) { 32 print "bulding FAKE session for $sender\n"; 33 $sessions_ref->{"$sender"} = build_empty_session(); 34 if( ! $sessions_ref->{ $sender }->{ "seen_welcome" } ) { 35 $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='$site/set-up/IM?aim=$sender'>activate</a></b> to set up your account.") ); 36 $sessions_ref->{ $sender }->{ "seen_welcome" } = 1; 37 } 38 } else { 39 if( $sessions_ref->{ $sender }->{ "seen_welcome" } == 0 ) { 40 $aim->send_im( $sender, "Welcome back, " . get_dude() . "!" ); 41 $sessions_ref->{ $sender }->{ "seen_welcome" } = 1; 42 } 43 print "bulding real session for $sender\n"; 44 $sessions_ref->{"$sender"} = build_authenticated_session( $fml_username ); 45 } 46 48 47 # handle commands 49 if( $message =~ m/(ftp|gopher|https?):\/\// || $message =~ m/www\./ ) { 48 if( 49 $message =~ m/(ftp|gopher|https?):\/\// 50 || $message =~ m/www\./ 51 || $message =~ m/\.(com|org|net|biz|info|gov|mil|edu|uk|es|jp|mx|it|am|ca|de)\/?$/ 52 ) { 50 53 if( $sessions_ref->{ $sender }->{ "validated" } ) { 51 54 my $url = $message; 55 print "orig url is $url\n"; 56 $url =~ s/^add //; 57 $url = add_prefix( $url ); 58 52 59 my $name = get_title_for_url( $url ); 53 60 my $name = $name ? $name : $url; 54 print "$name\n";55 $aim->send_im($sender, "<b>Adding</b>:\n" . $name );56 61 my $encoded_name = $name; 57 62 $encoded_name =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; 58 63 my $encoded_url = $url; 59 64 $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", []); 65 print "$sender adding $url as $name\n"; 66 67 my $response = $sessions_ref->{$sender}->{"proxy"}->get( "$site/api/add?url=$encoded_url&name=$encoded_name", []); 64 68 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"; 69 my $info = $xp->getNodeText( "/result" ); 70 71 print "raw: " . Dumper( $response->content ) . "\n"; 72 print "got $info back from FML\n"; 73 if( $info =~ m/http/ ) { 74 my $link_id = $info; 75 $link_id =~ s/.*com\///; 76 print "link_id: $link_id\n"; 77 $sessions_ref->{$sender}->{"last_link"} = $link_id; 78 $sessions_ref->{$sender}->{"last_action"} = "added_link"; 79 $aim->send_im($sender, h("<b>Added</b>:\n<a href='$info'>$name</a>" )); 80 $aim->send_im($sender, h("(coming soon) Want to tag it? Just start typing; separate tags with commas." )); 81 } else { 82 $aim->send_im($sender, h("<b>Oops, the server screwed something up!</b>\n$info" )); 83 } 84 85 # FIXME: next action: tag / rename 68 86 69 87 } else { 70 88 # 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." ));89 $aim->send_im($sender, h("That looks like a link! To add links via IM, just <b><a href='$site/set-up/IM?aim=$sender'>activate your account</a></b> by clicking." )); 72 90 } 73 91 … … 83 101 print "aim_validation_phrase: $aim_validation_phrase\n"; 84 102 85 print " http://www.feedmelinks.com/api/backroom/get_im_validation_phrase_for_user&user=$claimed_fml_username\n";103 print "$site/api/backroom/get_im_validation_phrase_for_user&user=$claimed_fml_username\n"; 86 104 my $response = $www->get( 87 " http://www.feedmelinks.com/api/backroom/get_im_validation_phrase_for_user&user=$claimed_fml_username",105 "$site/api/backroom/get_im_validation_phrase_for_user&user=$claimed_fml_username", 88 106 []); 89 107 my $xp = XML::XPath->new(xml => $response->content); … … 93 111 if( $fml_validation_phrase eq $aim_validation_phrase ) { 94 112 my $response = $www->get( 95 " http://www.feedmelinks.com/api/backroom/validate_users_im_name&user=$claimed_fml_username",113 "$site/api/backroom/validate_users_im_name&user=$claimed_fml_username", 96 114 []); 97 115 $xp = XML::XPath->new(xml => $response->content); … … 99 117 print "validation_response: $validation_response\n"; 100 118 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 ); 119 $sessions_ref->{"$sender"} = build_authenticated_session( $claimed_fml_username ); 120 #$aim->send_im($sender, "<b>Rebuilt an authenticated session for you</b>:\n" . Dumper( $sessions_ref )); 110 121 111 122 $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 123 } 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>.") );124 $aim->send_im($sender, h("Darn, that doesn't match our records. Please try again, or <a href='$site/contact-us?subject=AIM+issues:+'>contact us</a>.") ); 114 125 } 115 126 … … 117 128 my $users = ""; 118 129 for my $user ( keys %$sessions_ref ) { 119 print "who: $user\n";120 130 $users .= 121 "<a href=' http://feedmelinks.com/u/"131 "<a href='$site/" 122 132 . $sessions_ref->{ $user } -> { "fml_username" } . "'>" 123 133 . $sessions_ref->{ $user } -> { "fml_username" } … … 129 139 130 140 } elsif( $message =~ m/peeps/ ) { 131 $aim->send_im($sender, "<b>what are you, SNACK FOOD?</b>\n" ); 141 $aim->send_im($sender, "<b>what are you, SNACK FOOD?</b> (sorry, unfinished)\n" ); 142 my $u = $sessions_ref->{ $sender } -> { "fml_username" }; 143 144 my $response = $www->get( "$site/usr/" . substr( $u, 0, 1 ) . "/$u/contacts.xml", []); 145 $xp = XML::XPath->new(xml => $response->content); 146 my $peeps = $xp->find("/fml"); 147 # FIXME: peeps list here for sharing 132 148 133 149 } elsif( $message =~ m/version/ ) { … … 135 151 136 152 } elsif( $message =~ m/(links|recent)/ ) { 137 $aim->send_im($sender, " FIXME: recent links here! (not finished yet, sorry");153 $aim->send_im($sender, "recent links here, soon! (not finished yet, sorry"); 138 154 139 155 } elsif( $message =~ m/(recent|new)/ ) { 140 $aim->send_im($sender, " FIXME:show the last 10 links");156 $aim->send_im($sender, "(soon!) show the last 10 links"); 141 157 142 158 } elsif( $message =~ m/help/ || $message =~ m/commands/ || $message =~ m/\?/ ) { 143 159 144 $aim->send_im($sender, "<b>commands</b>: 145 who 146 peeps 147 recent 148 status 149 version 150 help" ); 160 $aim->send_im($sender, "<b>Instructions</b>: 161 <b>paste or type any link</b> (or try: who, version, credits, or help)" ); 151 162 152 163 } elsif( $message =~ m/status/ ) { 153 164 $aim->send_im($sender, "<b>DEBUG DUMP</b>:\nsender: $sender\nmessage: $message" ); 154 165 $aim->send_im($sender, Dumper( $sessions_ref )); 155 } elsif( 0 == "check_validation" ) { 156 157 } elsif( 0 == "waiting_for_validation_phrase" ) { 158 $aim->send_im($sender, "within for this validation, butchy" ); 166 print Dumper( $sessions_ref ) . "\n"; 167 168 } elsif( $message =~ m/credits/ ) { 169 $aim->send_im($sender, h("by <b><a href='http://jm3.net'>John Manoogian III / jm3</a></b>")); 170 171 } elsif( $sessions_ref->{$sender}->{"last_link"} && $sessions_ref->{$sender}->{"last_action"} eq "added_link" ) { 172 $sessions_ref->{$sender}->{"last_action"} = "added_tags"; 173 $aim->send_im($sender, "Mmm, looks like tagz!"); 174 175 } else { 176 $aim->send_im($sender, "I'm up in your AIM, feed me some links. (type <b>?</b> for help)"); 177 159 178 } 179 160 180 return $sessions_ref; 161 181 } … … 163 183 sub is_user_validated { 164 184 my $sender = shift; 165 my $response = $www->get( " http://www.feedmelinks.com/api/backroom/is_im_account_known&user=$sender" , []);185 my $response = $www->get( "$site/api/backroom/is_im_account_known&user=$sender" , []); 166 186 my $xp = XML::XPath->new(xml => $response->content); 167 187 my $is_valid = $xp->findnodes('/rest/user/@valid'); 168 188 $is_valid .= ""; 169 189 my $fml_user = $xp->getNodeText("/rest/user"); 190 print "trying to validate user $sender; server said: account $fml_user, valid=$is_valid\n"; 170 191 171 192 if( $is_valid ) { … … 176 197 return 0; 177 198 } 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 " 199 # "Welcome to the Feed Me Links bot. To link your AIM account to your feed me links account, just click this link: $site/set-up/IM?im_name=$sender " 200 } 201 202 sub build_empty_session { 203 return { 204 'last_link' => 'none yet', 205 'last_action' => '', 206 'fml_username' => "", 207 'fml_password' => "", 208 'validated' => 0, 209 'seen_welcome' => 0 210 }; 211 } 212 213 # FIXME: rename ; ONLY call this on validated users, as this will give them a fully locked and loaded session to link with 214 sub build_authenticated_session { 215 #user and password 216 my $u = $_[0]; 217 my $p; 218 219 print "> passing user: $u\n"; 220 print "> using url: $site/api/backroom/getPasswdForUser&user=$u\n"; 221 my $response = $www->get( "$site/api/backroom/getPasswdForUser&user=$u", []); 222 $xp = XML::XPath->new(xml => $response->content); 223 my $p = "" . $xp->getNodeText("/rest/response"); 224 print "< got password: '$p'\n"; 225 226 my $session = { 227 'last_link' => 'none yet', 228 'last_action' => '', 229 'fml_username' => $u, 230 'fml_password' => $p, 231 'proxy' => get_proxy( $u, $p ), 232 'validated' => 1, 233 'seen_welcome' => 0 234 }; 235 236 return $session; 179 237 } 180 238 … … 207 265 } 208 266 267 sub add_prefix { 268 my $url = $_[0]; 269 if( index( $url, "http" ) != 0 270 && index( $url, "https" ) != 0 271 && index( $url, "ftp" ) != 0 272 ) { 273 return "http://$url"; 274 } else { 275 return $url; 276 } 277 } 278 279 sub h { 280 return "<HTML>" . @_[0] . "</HTML>"; 281 } 282 209 283 sub strip_html { 210 284 my $html = $_[0]; … … 214 288 } 215 289 290 sub get_dude { 291 my @dudewords = ( 292 "dude", 293 "bro", 294 "bro-ham" 295 ); 296 return $dudewords[ int(rand( scalar( @dudewords ))) ]; 297 } 298 216 299 sub print_version { print get_version() . "\n"; } 217 sub get_version { return "version 0.1.4!"; } 300 301 sub get_version { 302 $^T = time; 303 my $d = -M "/home/fml/public_html/lib/FeedMeLinks/Bot.pm"; 304 $d = int( $d * 1440 ); 305 return "You are speaking with Feed Me Links bot v0.1.4!\n(code last baked $d minutes ago)\n"; 306 } 218 307 219 308 1; # modules must return a true value feedmelinks/lib/FeedMeLinks/Environment-template.pm
r1314 r1357 1 1 #!/usr/local/bin/perl 2 package FeedMeLinks::Environ ement;2 package FeedMeLinks::Environment; 3 3 use strict; # for warnings: 4 4 # jm3