Saturday, October 23, 2010

An open relay for thimbl

I am experimenting with ideas in open social networking - thimbl has caught my attention the last few weeks. I have activated the finger daemon on my EC2 account. You can find the latest tweet by doing
   finger ossa@nummo.strangled.net

I have also devised an open relay for thimbl. Anyone can now make a microblog post, using only telnet. You can explore it right now (where now is defined as 23-Oct-2010, 18:19) as I have the open relay set up on my server. Here's what you do:
   telnet nummo.strangled.net 2993

You can type
   rece
to see the last 10 tweets. You can add your own tweet using the "say" command. For example:
   say hello from the internet
Type "rece" again, and you should see your post. When you've have enough, type
   quit

For further proof that it's worked, type
    finger ossa@nummo.strangled.net
and you should see your post.

The code behind it (VERY hacky):

#!/usr/bin/env tclsh
# Run this open relay server using
# retweet.tcl
# Example usage from the 'client' perspective:
# telnet localhost 2993
# C: OK Retweet says hello
# S: rece <-- obtain a list of recent posts
# C: blah blah 1
# C: ...
# C: OK Done
# C: say i don't care what i say <-- create a posting
# S: OK Done
# C: quit <-- quit the server
# socketry taken from
# http://www.tcl.tk/about/netserver.html
#fconfigure 0 -buffering line -blocking 0
#fconfigure 1 -buffering line -blocking 0
proc out { text } {
    puts 1 $text
    flush 1
}
# out "OK Retweet says hello"
proc in {} {
    gets 0 inp
    return $inp
}
proc handle_input {out inp} {
    if {[regexp {^say (.+)} $inp _ tweet]} {
# write the tweet to a file
set f1 [open "tweets" "a"]
        puts $f1 $tweet
        close $f1
# save the tweet in .plan
        set f1 [open ".plan" "w"]
        puts $f1 $tweet
        close $f1
puts $out "OK Done"
#flush stdout
return
    }
    if {[regexp {^rece} $inp]} {
set f1 [open "| tail tweets" "r"]
        puts $out [read $f1]
        close $f1
puts $out "OK Done"
#flush stdout
return
    }
    if {$inp == "help"} {
puts $out "Commands: help quit rece say"
puts $out "OK Done"
#flush stdout
return
    }
    if {$inp == "quit"} {
puts $out "OK Quitting"
#flush stdout
close $out
return
    }
    puts $out "FAIL Didn't understand command"
    #flush stdout
}
# Echo_Server --
#Open the server listening socket
#and enter the Tcl event loop
#
# Arguments:
#portThe server's port number
proc Echo_Server {port} {
    set s [socket -server EchoAccept $port]
    vwait forever
}
# Echo_Accept --
#Accept a connection from a new client.
#This is called after a new socket connection
#has been created by Tcl.
#
# Arguments:
#sockThe new socket connection to the client
#addrThe client's IP address
#portThe client's port number
proc EchoAccept {sock addr port} {
    global echo
    # Record the client's information
    puts "Accept $sock from $addr port $port"
    set echo(addr,$sock) [list $addr $port]
    # Ensure that each "puts" by the server
    # results in a network transmission
    fconfigure $sock -buffering line
    puts $sock "OK Greetings from fritter"
    # Set up a callback for when the client sends data
    fileevent $sock readable [list Echo $sock]
}
# Echo --
#This procedure is called when the server
#can read data from the client
#
# Arguments:
#sockThe socket connection to the client
proc Echo {sock} {
    global echo
    # Check end of file or abnormal connection drop,
    # then echo data back to the client.
    if {[eof $sock] || [catch {gets $sock line}]} {
close $sock
puts "Close $echo(addr,$sock)"
unset echo(addr,$sock)
    } else {
handle_input $sock $line
#puts $sock $line
    }
}
Echo_Server 2993
 
To run it, do the following:
cd ~
echo first post >~/.plan
echo first post >~/tweets
tclsh retweet.tcl
You may want to combine it with the nohup command so that it isn't killed.

The code is also avialabe for download .

2 comments:

Unknown said...

Hey Mark! I'm thrilled that your engaging with the ideas behind Thimbl, and realize how simple things could be. Why nut just join us? Making a console UI for Thimbl would both be much more difficult that what you've done above! Since we're a little backlogged on the the Web UI, your console UI may just be the first one released ;) -- Dmytri Kleiner

Unknown said...

Hmm, I mean why not just join us, and would not be much more difficult! ^DK