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..890eea9 --- /dev/null +++ b/ruby-notifier/bin/irccloud-notifier @@ -0,0 +1,23 @@ +#!/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]) + +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(opt['email'], opt['password']) +growler.run 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 -} diff --git a/ruby-notifier/irccloud-notifier.gemspec b/ruby-notifier/irccloud-notifier.gemspec new file mode 100644 index 0000000..e8fa39c --- /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 = ["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} + + 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..2ace36b --- /dev/null +++ b/ruby-notifier/lib/irccloud-notifier/growler.rb @@ -0,0 +1,95 @@ +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 + buffer.each_line 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']}" + + when 'end_of_backlog' + @eob[ev['cid']] = true + + else + if @eob[ev['cid']] == true and ev['highlight'] == true + if buf = @buffers[ev['bid']] + 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