Class: Firejab::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/firejab/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/firejab/connection.rb', line 12

def initialize(params)
  self.jabber = Jabber::Simple.new(params[:jabber][:username], params[:jabber][:password])

  self.campfire_options = {
    :path => "/room/#{params[:room_id]}/live.json",
    :host => 'streaming.campfirenow.com',
    :auth => "#{params[:token]}:x"
  }
  self.campfire_room_id = params[:room_id]
  self.campfire_domain = params[:domain]
  self.be_noisy = false

  # Load known tokens for our users from a "database"
  self.jabber_users = {}
  self.campfire_uids = {}
end

Instance Attribute Details

#be_noisyObject

Returns the value of attribute be_noisy.



8
9
10
# File 'lib/firejab/connection.rb', line 8

def be_noisy
  @be_noisy
end

#campfireObject

Returns the value of attribute campfire.



9
10
11
# File 'lib/firejab/connection.rb', line 9

def campfire
  @campfire
end

#campfire_domainObject

Returns the value of attribute campfire_domain.



9
10
11
# File 'lib/firejab/connection.rb', line 9

def campfire_domain
  @campfire_domain
end

#campfire_optionsObject

Returns the value of attribute campfire_options.



9
10
11
# File 'lib/firejab/connection.rb', line 9

def campfire_options
  @campfire_options
end

#campfire_room_idObject

Returns the value of attribute campfire_room_id.



9
10
11
# File 'lib/firejab/connection.rb', line 9

def campfire_room_id
  @campfire_room_id
end

#campfire_uidsObject

Returns the value of attribute campfire_uids.



9
10
11
# File 'lib/firejab/connection.rb', line 9

def campfire_uids
  @campfire_uids
end

#jabberObject

Returns the value of attribute jabber.



10
11
12
# File 'lib/firejab/connection.rb', line 10

def jabber
  @jabber
end

#jabber_usersObject

Returns the value of attribute jabber_users.



10
11
12
# File 'lib/firejab/connection.rb', line 10

def jabber_users
  @jabber_users
end

Instance Method Details

#add_token(jabber_username, campfire_token) ⇒ Object



29
30
31
32
# File 'lib/firejab/connection.rb', line 29

def add_token(jabber_username, campfire_token)
  self.jabber_users[jabber_username] ||= {}
  self.jabber_users[jabber_username][:campfire_token] = campfire_token
end

#runObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/firejab/connection.rb', line 34

def run
  EM.run do
    self.campfire = Twitter::JSONStream.connect(self.campfire_options)

    # Tell us what to do when we receive a campfire message
    self.campfire.each_item do |item|
      puts "Received a new message from campfire: #{item}"
      status = Yajl::Parser.parse(item)
      # For ppl that are connected and(?) we have tokens for, send the msg
      case status["type"]
      when "TextMessage", "PasteMessage"
        send_message_to_jabber_users(status['user_id'], status['body'])
      when "UploadMessage"
        #TODO: Retrieve full URL from `GET /room/#{id}/messages/#{upload_message_id}/upload.json`
        send_message_to_jabber_users(status['user_id'], "Uploaded #{status['body']}")
      when "EnterMessage"
        send_message_to_jabber_users(status['user_id'], "Has entered the room!") if self.be_noisy
      when "LeaveMessage", "KickMessage"
        send_message_to_jabber_users(status['user_id'], "Has left the room.") if self.be_noisy
      end
    end

    self.campfire.on_error do |message|
      puts "Received a campfire error: #{message}"
    end

    self.campfire.on_max_reconnects do |timeout, retries|
      puts "Fatal error with campfire, you'll need to restart"
    end

    EM::PeriodicTimer.new(1) do
      check_jabber_connection

      self.jabber.received_messages do |message|
        # Removes "/resource"
        jabber_username = message.from.strip

        if is_valid_user(jabber_username)
          send_message_to_campfire(jabber_username, message.body)
        elsif message.body.match(/^\w{40}$/)
          # We received an auth token
          add_token(jabber_username, message.body)
          #TODO: Verify token and lookup_campfire_uid
          send_jabber_message(jabber_username, "Heloooooo!")
        else
          # We don't know who this is, ask for their token
          send_jabber_message(jabber_username, "Hi! I don't know who you are, please send me your auth token from: https://#{self.campfire_domain}/member/edit")
        end
      end

      self.jabber.presence_updates do |friend, presence, message|
        # presence may be one of [:online, :unavailable, :away]
        # puts "Received presence update from #{friend.inspect}: #{presence.inspect} => #{message.inspect}"
        #TODO: Update room presence accordingly - POST /room/#{id}/[join,leave].json
        set_status(friend, presence)
      end

      self.jabber.new_subscriptions do |friend, presence|
        puts "New subscription request: #{friend.inspect}"
        # self.jabber.add(friend['jid'])
      end
    end
  end
end