Permalink
Browse files

Broken, but halfway there.

  • Loading branch information...
1 parent 9196878 commit 7ac83971c06c07f86c8ddc2992b04129038cada6 @jbarnette committed Jun 3, 2008
Showing with 65 additions and 2 deletions.
  1. +1 −2 Rakefile
  2. +16 −0 bin/pastejour
  3. +48 −0 lib/pastejour.rb
View
@@ -20,8 +20,7 @@ pastejour_gemspec = Gem::Specification.new do |s|
s.autorequire = "pastejour"
s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
- # Uncomment this to add a dependency
- # s.add_dependency "foo"
+ s.add_dependency "dnssd", ">= 0.6.0"
end
Rake::GemPackageTask.new(pastejour_gemspec) do |pkg|
View
@@ -0,0 +1,16 @@
+#!/usr/bin/env ruby
+
+require "rubygems"
+require "pastejour"
+
+name = ARGV.shift
+
+if $stdin.tty?
+ name = /#{ENV["USER"]}$/ if name.nil? || name.empty?
+ $stdout.write(Pastejour.get(name))
+ $stdout.flush
+else
+ name = "paste" if name.nil? || name.empty?
+ name = [ENV["USER"], name].compact.join("-")
+ Pastejour.serve(name, $stdin.read)
+end
View
@@ -1 +1,49 @@
+require "dnssd"
+require "socket"
+require "webrick"
+
require "pastejour/version"
+
+module Pastejour
+ include Socket::Constants
+
+ Paste = Struct.new(:name, :host)
+ PORT = 42424
+ SERVICE = "_pastejour._tcp"
+
+ def self.get(name)
+ waiting = Thread.new { sleep 5 }
+ contents = nil
+
+ service = DNSSD.browse(SERVICE) do |reply|
+ # FIXME: fuck you, DNSSD.resolve. fuck you up your stupid ass.
+ DNSSD.resolve(reply.name, reply.type, reply.domain) { raise "FOO!" }
+ end
+
+ waiting.join
+ service.stop
+
+ # FIXME: actually read the paste contents
+ # TCPSocket.open("localhost", PORT).read
+
+ contents
+ end
+
+ def self.serve(name, contents)
+ DNSSD.register(name, SERVICE, "local", PORT) do |reply|
+ puts "Pasting #{name}..."
+ end
+
+ server = WEBrick::GenericServer.new(:Port => PORT)
+
+ trap "INT" do
+ server.shutdown
+ exit!
+ end
+
+ server.start do |socket|
+ socket.print(contents)
+ server.shutdown
+ end
+ end
+end

0 comments on commit 7ac8397

Please sign in to comment.