Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zh
Created December 10, 2008 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zh/34260 to your computer and use it in GitHub Desktop.
Save zh/34260 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/
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
#Rack::Handler::CGI.run ToXMPP.new
#Rack::Handler::WEBrick.run ToXMPP.new, :Port => 9292
Rack::Handler::Mongrel.run ToXMPP.new, :Port => 9292
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment