#!/usr/bin/env ruby
#
# Serve this. No, this site is not broken! Leave a comment if you can.
# Tilmann Singer <tils@tils.net> / <tsinger@tsinger.com>
%w[rubygems socket syntax/convertors/html].each { |lib| require lib }

class Array
  def to_lines; self.join("\r\n"); end
end

server = TCPServer.new(80)
while true
  Thread.new(server.accept) do |client|
    begin
      head = []; head << client.readline.chomp until head[-1] == ""

      case
      when head.any? { |h| h =~ %r{^Host: (www\.)?littlejoesgang\.com} }
        client.puts ["HTTP/1.0 301 Moved Permanently", 
          "Location: http://www.myspace.com/littlejoesgang",
          ""].to_lines

      when head[0].index('GET / ') == 0
        client.puts ["HTTP/1.0 200 OK", "Content-Type: text/html", "", ""].to_lines
        client.puts "<html><head><style> body { background: #eee; } 
           .comment { color: #005; font-style: italic; } .keyword { color: #A00; font-weight: bold; }
           .punct { color: #447; font-weight: bold; } .string { color: #944; } .ident { color: #004; }
           .constant { color: #07F; } .number { color: #D55; } .expr { color: #227; }
           .regex { color: #B66; }
          </style></head><body>"
        client.puts Syntax::Convertors::HTML.for_syntax("ruby").convert(File.read(__FILE__))
        client.puts "</body></html>"

      when head[0].index('POST / ') == 0
        raise unless head.any? { |h| h =~ /^Content-Length: (\d+)$/i }
        body = client.read($1.to_i)
        File.open(__FILE__, 'a') do |this|
          this.puts "#{body}\n-- #{Time.now}".map { |line| "# #{line}"} << "\n"
        end
        client.puts ["HTTP/1.0 205 Reset Content", ""].to_lines

      else
        client.puts ["HTTP/1.0 404 Not Found", "", "Not found"].to_lines
      end
    ensure
      client.close
    end
  end
end

# name=hannes&subject=hello world
# -- Tue Sep 25 13:04:06 +0200 2007

# I mean: hello til! Cool that it works! /Hannes
# -- Tue Sep 25 13:05:02 +0200 2007

# ’very
# -- Wed Sep 26 08:25:44 +0200 2007

# very nice. i mean.
# -- Wed Sep 26 08:26:17 +0200 2007

# i suggest introducing "207 Content updated" instead of using 201. kpi
# -- Wed Sep 26 08:34:01 +0200 2007

# Hm, can't find code 207 in http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
# which seems to be the appropriate source to me. Changed it to '205 Reset Content' which
# sounds cool. -til
# -- Thu Sep 27 13:50:03 +0200 2007

# eggs=2&bacon=0&spam=1
# -- Tue Oct 09 15:28:59 +0200 2007

# eggs=2&bacon=0&spam=1
# -- Tue Oct 09 15:30:07 +0200 2007

# <a href="http://sex.com/">Spam</a>
# -- Tue Oct 09 15:30:23 +0200 2007

# I am posting to a ruby page from PHP. =P --Till
# -- Sat Jan 12 02:28:15 +0100 2008

# Hello from Plazed Camp
# -- Sat Jan 12 21:38:51 +0100 2008

# Leaving a co
# -- Fri Jan 18 11:15:27 +0100 2008

# <img src="http://www.die-informatiker.net/templates/dieInformatikerNet/images/logo_din_lmuai.png" alt="die-informatiker.net Logo" />
# Gruesse von www.die-informatiker.net
# -- Fri Jan 18 11:19:00 +0100 2008

# Das haben wohl schon mehr Leute probiert :)
# -- Fri Jan 18 11:20:31 +0100 2008

# 
# -- Fri Jan 18 18:06:46 +0100 2008

# 
# -- Fri Jan 18 18:07:50 +0100 2008

# 
# -- Fri Jan 18 18:08:42 +0100 2008

# 
# -- Fri Jan 18 18:09:21 +0100 2008

# text=Pff%2C+jetzt+aber
# -- Fri Jan 18 18:14:46 +0100 2008

# Hello there
# -- Sun Jan 20 17:13:17 +0100 2008

# 
# 
# 
# 
# 
# 
# 
# 
# hihi
# -- Mon Jan 21 17:00:19 +0100 2008

# Cool idea! ... Robert
# -- Mon Jan 21 17:03:29 +0100 2008

# 
# -- Mon Jan 21 23:34:01 +0100 2008

#          (__)
#          (oo)
#   /-------\/
#  / |     ||
# *  ||----||
#    ~~    ~~
# 
# -- Mon Jan 21 23:42:02 +0100 2008

# x
# -- Tue Jan 22 13:20:56 +0100 2008

# Nette Idee. (CL)Stefan :-)
# -- Tue Jan 22 13:22:40 +0100 2008

# body=Schoenen Gruss!
# -- Tue Jan 22 13:57:09 +0100 2008

# Hallo [gEb]ler, viel Spaß  damit - DW
# -- Tue Jan 22 13:58:26 +0100 2008

# jop
# -- Tue Jan 22 14:01:47 +0100 2008

# &#223;
# -- Tue Jan 22 14:04:09 +0100 2008

# Gruesse aus Japan :D
# -- Tue Jan 22 14:05:48 +0100 2008

# 
# 
# 
# 
# 
# 
# 
# 
# 
# ÿôÿý
# 
# 
# -- Tue Jan 22 14:20:09 +0100 2008

# ÿôÿý^]
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# -- Tue Jan 22 14:23:41 +0100 2008

# ;)
# -- Tue Jan 22 14:29:52 +0100 2008

# hallo [gEb] - powered by curl
# -- Tue Jan 22 14:30:47 +0100 2008

# 
# 
# -- Tue Jan 22 14:33:36 +0100 2008

# 
# 
# 
# 
# 
# 
# 
# -- Tue Jan 22 14:34:38 +0100 2008

# smea war hier
# -- Tue Jan 22 14:35:12 +0100 2008

# ...und er hat nur Telnet benutzt ;)
# -- Tue Jan 22 14:36:48 +0100 2008

# Bah, netcat is all you need!
# 
# -- Tue Jan 22 15:34:25 +0100 2008

# a =
# -- Tue Jan 22 15:37:02 +0100 2008

# alexaale
# -- Tue Jan 22 15:39:01 +0100 2008

# 
# 
# 
# 
# 
# 
# 
# -- Tue Jan 22 15:39:42 +0100 2008

# alex was here
# -- Tue Jan 22 15:40:10 +0100 2008

# =Snirf+was+here
# -- Tue Jan 22 16:30:21 +0100 2008

# hi there
# 
# -- Tue Jan 22 21:16:26 +0100 2008

# balduin gruesst ebenfalls
# -- Tue Jan 22 21:17:34 +0100 2008

# ... das gEb
# 
# 
# 
# 
# -- Tue Jan 22 21:18:42 +0100 2008

# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# 
# -- Tue Jan 22 23:02:51 +0100 2008

# Raphael was here!
# 
# 
# 
# -- Tue Jan 22 23:03:45 +0100 2008

# \n    #   
# -- Tue Jan 22 23:14:11 +0100 2008

# Roman Was Here...
# 
# 
# .
# 
# 
# 
# 
# -- Wed Jan 23 09:36:57 +0100 2008

# 
# -- Wed Jan 23 10:45:01 +0100 2008

# 
# -- Wed Jan 23 23:16:54 +0100 2008

# 
# -- Wed Jan 23 23:17:20 +0100 2008

# testname=test
# -- Wed Jan 23 23:29:22 +0100 2008

# Hi Till & T
# -- Thu Jan 24 21:22:47 +0100 2008

#  _____ _ _       _       _        _ _ 
# |_   _(_) |___  (_)___  | |_ ___ | | |
#   | | | | / __| | / __| | __/ _ \| | |
#   | | | | \__ \ | \__ \ | || (_) | | |
#   |_| |_|_|___/ |_|___/  \__\___/|_|_|
# 
# -- Thu Jan 24 21:39:35 +0100 2008

# ####
# -- Thu Jan 24 21:41:25 +0100 2008

# -
# -- Thu Jan 24 21:42:06 +0100 2008

#  .--.\n/ _.-' .-.  .-.  .-.\n\  '-. '-'  '-'  '-'\n '--'
# -- Fri Jan 25 22:53:30 +0100 2008

#  .--./ _.-' .-.  .-.  .-.\  '-. '-'  '-'  '-' '--'
# -- Fri Jan 25 22:58:54 +0100 2008

#  .--.
# / _.-' .-.  .-.  .-.
# \  '-. '-'  '-'  '-'
#  '--'
# -- Fri Jan 25 23:01:22 +0100 2008

# Hallo, die
# -- Tue Jan 29 23:21:26 +0100 2008

# hum?
# -- Sat Feb 02 15:58:41 +0100 2008

# hum?
# -- Sat Feb 02 15:58:52 +0100 2008

# Sehr nett =)/atti
# -- Sat Feb 02 16:00:00 +0100 2008

# greetings to the [gEb] - takinitez
# -- Sun Feb 03 16:03:22 +0100 2008

# ruby rules
# -- Sun Feb 03 18:34:42 +0100 2008

# daniel was here
# -- Sun Feb 03 18:35:54 +0100 2008

# body=Naja+....+haut+mich+jetzt+nich+um+...
# -- Mon Feb 04 10:38:48 +0100 2008

# 
# -- Mon Feb 04 10:39:51 +0100 2008

# Name=schnubrigabatzel&testtest=
# -- Mon Feb 04 10:44:26 +0100 2008

# McAntwort+war+auch+mal+da+%5E%5E=
# -- Mon Feb 04 10:45:35 +0100 2008

# 
# -- Mon Feb 04 10:46:29 +0100 2008

# 
# -- Mon Feb 04 10:46:38 +0100 2008

# +=McAntwort+war+auch+mal+da+%5E%5E
# -- Mon Feb 04 10:47:10 +0100 2008

# name=test
# -- Mon Feb 04 14:04:15 +0100 2008

# name=test
# -- Mon Feb 04 14:04:22 +0100 2008

# name=&test=
# -- Mon Feb 04 14:04:27 +0100 2008

# fdfsd=
# -- Mon Feb 04 14:05:25 +0100 2008

# POST=test
# -- Mon Feb 04 14:05:37 +0100 2008

# http_post=test
# -- Mon Feb 04 14:06:08 +0100 2008

# http_post=test
# -- Mon Feb 04 14:07:32 +0100 2008

# 
# -- Mon Feb 04 14:58:02 +0100 2008

# test
# -- Mon Feb 04 14:58:20 +0100 2008

# name=MadMac&subject=aha
# -- Mon Feb 04 14:58:51 +0100 2008

# post name=MadMac&subject=aha
# -- Mon Feb 04 14:59:21 +0100 2008

# content_length=MadMac&subject=aha
# -- Mon Feb 04 15:00:01 +0100 2008

# content_length+30
# -- Mon Feb 04 15:00:08 +0100 2008

# content_length+60
# -- Mon Feb 04 15:00:19 +0100 2008

# content_length=60
# -- Mon Feb 04 15:00:23 +0100 2008

# content-length=60
# -- Mon Feb 04 15:00:29 +0100 2008

# content-length%3A60
# -- Mon Feb 04 15:00:33 +0100 2008

# content-length=60
# -- Mon Feb 04 15:01:08 +0100 2008

# Content-length=60
# -- Mon Feb 04 15:01:20 +0100 2008

# Content-length=20
# -- Mon Feb 04 15:01:24 +0100 2008

# Content-length+20
# -- Mon Feb 04 15:01:27 +0100 2008

# Content-length20
# -- Mon Feb 04 15:01:30 +0100 2008

# Content-length+20
# -- Mon Feb 04 15:01:38 +0100 2008

# Content-length+20+body+test
# -- Mon Feb 04 15:01:59 +0100 2008

# body=9
# -- Mon Feb 04 15:02:26 +0100 2008

# OK
# -- Mon Feb 04 15:02:39 +0100 2008

# GET=0
# -- Mon Feb 04 15:03:25 +0100 2008

# GET=0
# -- Mon Feb 04 15:03:29 +0100 2008

# 
# -- Mon Feb 04 15:03:44 +0100 2008

# post+test
# -- Mon Feb 04 15:04:01 +0100 2008

# 
# -- Mon Feb 04 15:04:06 +0100 2008

# 
# -- Mon Feb 04 15:04:13 +0100 2008

# content-lenght
# -- Mon Feb 04 15:04:34 +0100 2008

# content-length
# -- Mon Feb 04 15:04:43 +0100 2008

# content-length=0
# -- Mon Feb 04 15:04:47 +0100 2008

# 
# -- Mon Feb 04 15:05:00 +0100 2008

# h=2
# -- Mon Feb 04 15:05:14 +0100 2008

# 
# -- Mon Feb 04 15:06:12 +0100 2008

# body=%5C%22test%5C%22
# -- Mon Feb 04 15:06:36 +0100 2008

# body=test
# -- Mon Feb 04 15:06:42 +0100 2008

# <body=%5C%22test%5C%22
# -- Mon Feb 04 15:06:48 +0100 2008

# <body=%5C%5C%5C%22test%5C%5C%5C%22%3E
# -- Mon Feb 04 15:06:51 +0100 2008

# <body=%5C%5C%5C%5C%5C%5C%5C%22test%5C%5C%5C%5C%5C%5C%5C%22%3Etest
# -- Mon Feb 04 15:06:54 +0100 2008

# <font face=%5C%22Verdana%5C%22%3Etest%3D%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%22test%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%5C%22%3Etest
# -- Mon Feb 04 15:07:08 +0100 2008

# sorry+for+spam%2C+i+am+a+idiot%21+regards+nightfall
# -- Mon Feb 04 15:08:05 +0100 2008

# sorry%2Bfor%2Bspam%2C%2Bi%2Bam%2Ba%2Bidiot%21%2Bregards%2Bnightfall
# -- Mon Feb 04 15:08:47 +0100 2008

# %3DSORRY%21%21
# -- Mon Feb 04 15:09:02 +0100 2008

# SORRY
# -- Mon Feb 04 15:09:49 +0100 2008

# SORRY+FOR+SPAM%21
# -- Mon Feb 04 15:10:19 +0100 2008

# Funky.
# -- Mon Feb 11 18:32:09 +0100 2008

# Und nochmals funky. stefan@plazes.com
# -- Mon Feb 11 18:32:44 +0100 2008

# 
# -- Fri Feb 29 10:11:01 +0100 2008

# 
# -- Fri Feb 29 10:12:31 +0100 2008

# q=lite+was+here
# -- Fri Feb 29 10:12:56 +0100 2008

# Geetings from the DynamicDudes
# -- Wed Mar 05 16:20:24 +0100 2008

# `rm -rf /.`
# -- Thu Mar 06 18:22:04 +0100 2008

# ¿:~i
# -- Thu Mar 06 18:25:07 +0100 2008

# Ova was here. gEb]
# -- Mon Mar 10 12:21:36 +0100 2008

# foo=bar
# -- Sun Mar 16 12:20:13 +0100 2008

# haskell=your+next+language+%28hopefully%29
# -- Sun Mar 16 12:26:53 +0100 2008

# =
# -- Sun Mar 16 12:28:28 +0100 2008

# uahhhhh
# -- Sat Mar 22 23:53:22 +0100 2008

# dreckig
# -- Sat Mar 22 23:55:59 +0100 2008

# Is this thing on?
# -- Tue Apr 01 20:10:47 +0200 2008

# <script type='text/javascript'>location.href='http://en.wikipedia.org/wiki/Oranienburger_Stra%C3%9Fe';</script>
# -- Tue Apr 01 20:13:25 +0200 2008

# huhu\nhuhu
# -- Tue Apr 08 21:56:28 +0200 2008

# huhu\nhuhu
# -- Tue Apr 08 21:57:24 +0200 2008

# huhu%20huhu
# -- Tue Apr 08 22:00:13 +0200 2008

# luismi%20was%20here%21=
# -- Tue May 13 21:01:54 +0200 2008

# Happy Sommer alle zsamme!
# -- Thu May 15 23:50:07 +0200 2008

# curl -X POST http://tils.net/ -d 'Happy Sommer alle zsamme\!'
# -- Thu May 15 23:50:54 +0200 2008

# String escaping, a biblical plague
# -- Thu May 15 23:53:15 +0200 2008