From 89fce1889d523c00533ba1d043ecacd6f1e84457 Mon Sep 17 00:00:00 2001 From: Christopher Giroir Date: Wed, 27 Jul 2011 21:36:20 -0400 Subject: [PATCH 1/4] Initial gemification --- ruby-notifier/.gitignore | 4 + ruby-notifier/.rvmrc | 1 + ruby-notifier/Gemfile | 4 + ruby-notifier/Rakefile | 1 + ruby-notifier/bin/irccloud-notifier | 28 ++++++ ruby-notifier/irccloud-notifier.gemspec | 23 +++++ ruby-notifier/lib/irccloud-notifier.rb | 7 ++ .../lib/irccloud-notifier/growler.rb | 96 +++++++++++++++++++ .../lib/irccloud-notifier/version.rb | 5 + 9 files changed, 169 insertions(+) create mode 100644 ruby-notifier/.gitignore create mode 100644 ruby-notifier/.rvmrc create mode 100644 ruby-notifier/Gemfile create mode 100644 ruby-notifier/Rakefile create mode 100755 ruby-notifier/bin/irccloud-notifier create mode 100644 ruby-notifier/irccloud-notifier.gemspec create mode 100644 ruby-notifier/lib/irccloud-notifier.rb create mode 100644 ruby-notifier/lib/irccloud-notifier/growler.rb create mode 100644 ruby-notifier/lib/irccloud-notifier/version.rb diff --git a/ruby-notifier/.gitignore b/ruby-notifier/.gitignore new file mode 100644 index 0000000..4040c6c --- /dev/null +++ b/ruby-notifier/.gitignore @@ -0,0 +1,4 @@ +*.gem +.bundle +Gemfile.lock +pkg/* diff --git a/ruby-notifier/.rvmrc b/ruby-notifier/.rvmrc new file mode 100644 index 0000000..98016de --- /dev/null +++ b/ruby-notifier/.rvmrc @@ -0,0 +1 @@ +rvm use 1.9.2@irccloud --create diff --git a/ruby-notifier/Gemfile b/ruby-notifier/Gemfile new file mode 100644 index 0000000..c059d96 --- /dev/null +++ b/ruby-notifier/Gemfile @@ -0,0 +1,4 @@ +source "http://rubygems.org" + +# Specify your gem's dependencies in irccloud-notifier.gemspec +gemspec diff --git a/ruby-notifier/Rakefile b/ruby-notifier/Rakefile new file mode 100644 index 0000000..c702cfc --- /dev/null +++ b/ruby-notifier/Rakefile @@ -0,0 +1 @@ +require 'bundler/gem_tasks' diff --git a/ruby-notifier/bin/irccloud-notifier b/ruby-notifier/bin/irccloud-notifier new file mode 100755 index 0000000..3184375 --- /dev/null +++ b/ruby-notifier/bin/irccloud-notifier @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby +require 'rubygems' +require 'bundler/setup' +require 'getopt/long' +require 'irccloud-notifier' + +lib = File.expand_path(File.dirname(__FILE__) + '/../lib') +$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib) + +require 'irccloud-notifier' + +opt = Getopt::Long.getopts( + ["--email", "-e", Getopt::REQUIRED], + ["--password", "-p", Getopt::REQUIRED] + ) + +email = opt['email'] +pass = opt['password'] + +if !email or !pass then + puts 'Usage: ' + $0 + ' --email --password ' + puts ' ' + puts '..and make sure growl is set to allow network connections/registrations, no pass' + exit +end + +growler = Irccloud::Notifier::Growler.new(email, pass) +growler.run diff --git a/ruby-notifier/irccloud-notifier.gemspec b/ruby-notifier/irccloud-notifier.gemspec new file mode 100644 index 0000000..e275cfd --- /dev/null +++ b/ruby-notifier/irccloud-notifier.gemspec @@ -0,0 +1,23 @@ +# -*- encoding: utf-8 -*- +$:.push File.expand_path("../lib", __FILE__) +require "irccloud-notifier/version" + +Gem::Specification.new do |s| + s.name = "irccloud-notifier" + s.version = Irccloud::Notifier::VERSION + s.authors = ["Evan Broder"] + s.email = ["evan@ebroder.net"] + s.homepage = "https://github.com/RJ/irccloud-tools" + s.summary = %q{Growl Notifier for IRCCloud} + s.description = %q{Growl Notifier for IRCCloud} + + s.rubyforge_project = "irccloud-notifier" + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] + + s.add_dependency('getopt') + s.add_dependency('ruby-growl') +end diff --git a/ruby-notifier/lib/irccloud-notifier.rb b/ruby-notifier/lib/irccloud-notifier.rb new file mode 100644 index 0000000..898f5f3 --- /dev/null +++ b/ruby-notifier/lib/irccloud-notifier.rb @@ -0,0 +1,7 @@ +require "irccloud-notifier/version" +require "irccloud-notifier/growler" + +module Irccloud + module Notifier + end +end diff --git a/ruby-notifier/lib/irccloud-notifier/growler.rb b/ruby-notifier/lib/irccloud-notifier/growler.rb new file mode 100644 index 0000000..f7c83fb --- /dev/null +++ b/ruby-notifier/lib/irccloud-notifier/growler.rb @@ -0,0 +1,96 @@ +require 'net/http' +require 'net/https' +require 'uri' +require 'json' + +require 'ruby-growl' + +module Irccloud + module Notifier + class Growler + URI_LOGIN = URI.parse('https://irccloud.com/chat/login') + URI_STREAM = URI.parse('https://irccloud.com/chat/stream') + + def initialize(email, pass) + @growl = Growl.new("127.0.0.1", "ruby-growl", ["irccloud-ruby"]) + + # do login to get session cookie: + puts 'Logging in...' + req = Net::HTTP::Post.new(URI_LOGIN.path) + req.set_form_data({'email' => email, 'password' => pass }) + http = Net::HTTP.new(URI_LOGIN.host, URI_LOGIN.port) + http.use_ssl = true + res = http.start {|http| http.request(req) } + + case res + when Net::HTTPSuccess, Net::HTTPRedirection + @session = res.response['set-cookie'].split(';')[0] + puts 'Session: ' + @session + else + res.error! + end + + @servers = {} + @eob = {} + @buffers = {} + end + + def run + buffer = '' + + # start stream + http = Net::HTTP.new(URI_STREAM.host, URI_STREAM.port) + http.use_ssl = true + http.request_get(URI_STREAM.path, 'cookie' => @session) do |response| + + p response['content-type'] + response.read_body do |str| + + buffer += str + lines = buffer.split("\n") + lines.each do |line| + begin + ev = JSON.parse line + case ev['type'] + when 'makeserver' + @servers[ev['cid']] = { + 'hostname' => ev['hostname'], + 'post' => ev['port'], + 'name' => ev['name'] } + puts 'Added server: ' + ev['name'] + + when 'channel_init', 'buffer_init' + name = ev['url'].split('/').last # hack :) + @buffers[ev['bid']] = { + 'url' => ev['url'], + 'cid' => ev['cid'], + 'name' => name } + puts 'Added buffer: ' + ev['url'] + ' ' + ev['bid'].to_s + + when 'end_of_backlog' + @eob[ev['cid']] = true + + else + if @eob[ev['cid']] == true and ev['highlight'] == true then + if buf = @buffers[ev['bid']] then + ser = servers[buf['cid']] + title = buf['name'] + ' (' + ser['name'] + ')' + msg = ev['msg'] + growl.notify("irccloud-ruby", title, msg) + else + puts 'LINE, UNKNOWN BUFFER: '+line + end + end + end + rescue JSON::JSONError => e + buffer = line + next + end + end + buffer = '' + end + end + end + end + end +end diff --git a/ruby-notifier/lib/irccloud-notifier/version.rb b/ruby-notifier/lib/irccloud-notifier/version.rb new file mode 100644 index 0000000..f82de62 --- /dev/null +++ b/ruby-notifier/lib/irccloud-notifier/version.rb @@ -0,0 +1,5 @@ +module Irccloud + module Notifier + VERSION = "0.0.1" + end +end From cf93710159f54dc122f851ffba513d8a565ca3d5 Mon Sep 17 00:00:00 2001 From: Christopher Giroir Date: Wed, 27 Jul 2011 21:50:09 -0400 Subject: [PATCH 2/4] Style changes --- ruby-notifier/bin/irccloud-notifier | 21 ++++++---------- .../lib/irccloud-notifier/growler.rb | 25 +++++++++---------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/ruby-notifier/bin/irccloud-notifier b/ruby-notifier/bin/irccloud-notifier index 3184375..890eea9 100755 --- a/ruby-notifier/bin/irccloud-notifier +++ b/ruby-notifier/bin/irccloud-notifier @@ -9,20 +9,15 @@ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib) require 'irccloud-notifier' -opt = Getopt::Long.getopts( - ["--email", "-e", Getopt::REQUIRED], - ["--password", "-p", Getopt::REQUIRED] - ) +opt = Getopt::Long.getopts(["--email", "-e", Getopt::REQUIRED], + ["--password", "-p", Getopt::REQUIRED]) -email = opt['email'] -pass = opt['password'] - -if !email or !pass then - puts 'Usage: ' + $0 + ' --email --password ' - puts ' ' - puts '..and make sure growl is set to allow network connections/registrations, no pass' - exit +if !opt['email'] or !opt['password'] + puts "Usage: #{$0} --email --password " + puts + puts '... and make sure growl is set to allow network connections/registrations, no pass' + exit end -growler = Irccloud::Notifier::Growler.new(email, pass) +growler = Irccloud::Notifier::Growler.new(opt['email'], opt['password']) growler.run diff --git a/ruby-notifier/lib/irccloud-notifier/growler.rb b/ruby-notifier/lib/irccloud-notifier/growler.rb index f7c83fb..2ace36b 100644 --- a/ruby-notifier/lib/irccloud-notifier/growler.rb +++ b/ruby-notifier/lib/irccloud-notifier/growler.rb @@ -17,15 +17,15 @@ def initialize(email, pass) # do login to get session cookie: puts 'Logging in...' req = Net::HTTP::Post.new(URI_LOGIN.path) - req.set_form_data({'email' => email, 'password' => pass }) + req.set_form_data('email' => email, 'password' => pass) http = Net::HTTP.new(URI_LOGIN.host, URI_LOGIN.port) http.use_ssl = true - res = http.start {|http| http.request(req) } + res = http.start {|http| http.request(req)} case res when Net::HTTPSuccess, Net::HTTPRedirection @session = res.response['set-cookie'].split(';')[0] - puts 'Session: ' + @session + puts "Session: #{@session}" else res.error! end @@ -44,11 +44,10 @@ def run http.request_get(URI_STREAM.path, 'cookie' => @session) do |response| p response['content-type'] - response.read_body do |str| + response.read_body do |str| buffer += str - lines = buffer.split("\n") - lines.each do |line| + buffer.each_line do |line| begin ev = JSON.parse line case ev['type'] @@ -57,7 +56,7 @@ def run 'hostname' => ev['hostname'], 'post' => ev['port'], 'name' => ev['name'] } - puts 'Added server: ' + ev['name'] + puts "Added server: #{ev['name']}" when 'channel_init', 'buffer_init' name = ev['url'].split('/').last # hack :) @@ -65,20 +64,20 @@ def run 'url' => ev['url'], 'cid' => ev['cid'], 'name' => name } - puts 'Added buffer: ' + ev['url'] + ' ' + ev['bid'].to_s + puts "Added buffer: #{ev['url']} #{ev['bid']}" when 'end_of_backlog' @eob[ev['cid']] = true else - if @eob[ev['cid']] == true and ev['highlight'] == true then - if buf = @buffers[ev['bid']] then + if @eob[ev['cid']] == true and ev['highlight'] == true + if buf = @buffers[ev['bid']] ser = servers[buf['cid']] - title = buf['name'] + ' (' + ser['name'] + ')' + title = "#{buf['name']} (#{ser['name']})" msg = ev['msg'] - growl.notify("irccloud-ruby", title, msg) + growl.notify('irccloud-ruby', title, msg) else - puts 'LINE, UNKNOWN BUFFER: '+line + puts "LINE, UNKNOWN BUFFER: #{line}" end end end From 0d6874e921cbe418930e93c13c18139adfacf783 Mon Sep 17 00:00:00 2001 From: Christopher Giroir Date: Wed, 27 Jul 2011 22:00:40 -0400 Subject: [PATCH 3/4] I got the name of the author wrong :) --- ruby-notifier/irccloud-notifier.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ruby-notifier/irccloud-notifier.gemspec b/ruby-notifier/irccloud-notifier.gemspec index e275cfd..e8fa39c 100644 --- a/ruby-notifier/irccloud-notifier.gemspec +++ b/ruby-notifier/irccloud-notifier.gemspec @@ -5,8 +5,8 @@ require "irccloud-notifier/version" Gem::Specification.new do |s| s.name = "irccloud-notifier" s.version = Irccloud::Notifier::VERSION - s.authors = ["Evan Broder"] - s.email = ["evan@ebroder.net"] + s.authors = ["Richard Jones"] + s.email = ["rj@metabrew.com"] s.homepage = "https://github.com/RJ/irccloud-tools" s.summary = %q{Growl Notifier for IRCCloud} s.description = %q{Growl Notifier for IRCCloud} From 40074be464bc2ad7cf85f229004afbd67d3b967a Mon Sep 17 00:00:00 2001 From: Christopher Giroir Date: Thu, 28 Jul 2011 00:05:16 -0400 Subject: [PATCH 4/4] Removed original growler.rb --- ruby-notifier/growler.rb | 96 ---------------------------------------- 1 file changed, 96 deletions(-) delete mode 100644 ruby-notifier/growler.rb diff --git a/ruby-notifier/growler.rb b/ruby-notifier/growler.rb deleted file mode 100644 index b877078..0000000 --- a/ruby-notifier/growler.rb +++ /dev/null @@ -1,96 +0,0 @@ -#!/usr/bin/ruby -require 'net/http' -require 'net/https' -require 'uri' -require 'rubygems' -require 'json' -require 'ruby-growl' -require "getopt/long" - -opt = Getopt::Long.getopts( - ["--email", "-e", Getopt::REQUIRED], - ["--password", "-p", Getopt::REQUIRED] - ) - -email = opt['email'] -pass = opt['password'] - -if !email or !pass then - puts 'Usage: ' + $0 + ' --email --password ' - puts ' ' - puts '..and make sure growl is set to allow network connections/registrations, no pass' - exit -end - -growl = Growl.new("127.0.0.1", "ruby-growl", ["irccloud-ruby"]) - -uri_login = URI.parse('https://irccloud.com/chat/login') -uri_stream = URI.parse('https://irccloud.com/chat/stream') - -# do login to get session cookie: -puts 'Logging in...' -req = Net::HTTP::Post.new(uri_login.path) -req.set_form_data({'email' => email, 'password' => pass }) -http = Net::HTTP.new(uri_login.host, uri_login.port) -http.use_ssl = true -res = http.start {|http| http.request(req) } -case res -when Net::HTTPSuccess, Net::HTTPRedirection - session = res.response['set-cookie'].split(';')[0] - puts 'Session: ' + session -else - res.error! -end - -eob = {} -servers = {} -buffers = {} -buffer = '' -# start stream -http = Net::HTTP.new(uri_stream.host, uri_stream.port) -http.use_ssl = true -http.request_get(uri_stream.path, {'cookie'=>session}) {|response| - p response['content-type'] - response.read_body do |str| - buffer += str - lines = buffer.split("\n") - lines.each { |line| - begin - ev = JSON.parse line - case ev['type'] - when 'makeserver' - servers[ev['cid']] = { 'hostname' => ev['hostname'], - 'post' => ev['port'], - 'name' => ev['name'] } - puts 'Added server: ' + ev['name'] - - when 'channel_init', 'buffer_init' - name = ev['url'].split('/').last # hack :) - buffers[ev['bid']] = { 'url' => ev['url'], - 'cid' => ev['cid'], - 'name' => name } - puts 'Added buffer: ' + ev['url'] + ' ' + ev['bid'].to_s - - when 'end_of_backlog' - eob[ev['cid']] = true - - else - if eob[ev['cid']] == true and ev['highlight'] == true then - if buf = buffers[ev['bid']] then - ser = servers[buf['cid']] - title = buf['name'] + ' (' + ser['name'] + ')' - msg = ev['msg'] - growl.notify("irccloud-ruby", title, msg) - else - puts 'LINE, UNKNOWN BUFFER: '+line - end - end - end - rescue JSON::JSONError => e - buffer = line - next - end - } - buffer = '' - end -}