Skip to content

Instantly share code, notes, and snippets.

@zh
Created December 10, 2008 07:36
Show Gist options
  • Save zh/34262 to your computer and use it in GitHub Desktop.
Save zh/34262 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Purpose: Simple web form to send messages via XMPP to juick.com
# Usage: ruby rack2juick.rb and point your browser to
# http://127.0.0.1:9292/
# user: user, password: secret
require 'rubygems'
require 'rack'
require 'rack/request'
require 'rack/response'
require 'erb'
require 'xmpp4r'
WEB = 'me@jabber.ru/Web'.freeze
PASS = 'SECRET'.freeze
TO = 'juick@juick.com'.freeze
class ToXMPP
include Jabber
def call(env)
req = Rack::Request.new(env)
message = req.POST['message']
if message
c = Client::new(JID::new(BOT))
c.connect
c.auth(PASS)
c.send Message::new(TO, message.to_s).set_type(:normal).set_id('1').set_subject('message to juick')
sleep(1)
c.close
end
# Build the HTML template
html_template = ERB.new <<-EOL
<html>
<head><title>Send XMPP message</title>
<meta http-equiv=Content-Type content="text/html; charset=utf-8" />
</head>
<body><%= message %>
<br/><br/>
<form action="" method="post">
<textarea rows="3" cols="60" id="message" name="message"></textarea>
<br/><br/>
<input type="submit">
</form>
</body></html>
EOL
out = html_template.result binding
Rack::Response.new.finish do |res|
res.write out
end
end
end
app = Rack::Builder.new {
use Rack::ShowExceptions
use Rack::Auth::Basic do |username, password|
'user' == username && 'secret' == password
end
run ToXMPP.new
}
#Rack::Handler::CGI.run app
#Rack::Handler::WEBrick.run app, :Port => 9292
Rack::Handler::Mongrel.run app, :Port => 9292
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment