Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gemification #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ruby-notifier/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
1 change: 1 addition & 0 deletions ruby-notifier/.rvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvm use 1.9.2@irccloud --create
4 changes: 4 additions & 0 deletions ruby-notifier/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in irccloud-notifier.gemspec
gemspec
1 change: 1 addition & 0 deletions ruby-notifier/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'bundler/gem_tasks'
23 changes: 23 additions & 0 deletions ruby-notifier/bin/irccloud-notifier
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> --password <your_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
96 changes: 0 additions & 96 deletions ruby-notifier/growler.rb

This file was deleted.

23 changes: 23 additions & 0 deletions ruby-notifier/irccloud-notifier.gemspec
Original file line number Diff line number Diff line change
@@ -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 = ["[email protected]"]
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
7 changes: 7 additions & 0 deletions ruby-notifier/lib/irccloud-notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "irccloud-notifier/version"
require "irccloud-notifier/growler"

module Irccloud
module Notifier
end
end
95 changes: 95 additions & 0 deletions ruby-notifier/lib/irccloud-notifier/growler.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions ruby-notifier/lib/irccloud-notifier/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Irccloud
module Notifier
VERSION = "0.0.1"
end
end