main
 1#!/usr/bin/env ruby
 2## encoding: utf-8
 3
 4require "bunny"
 5
 6connection = Bunny.new(host: ENV.fetch('RABBIT_HOST', 'localhost'))
 7connection.start
 8
 9me = `whoami`.chomp!
10channel = connection.create_channel
11exchange = channel.topic("chitchat")
12queue = channel.queue(me, exclusive: true)
13
14ARGV.push('#') if ARGV.empty?
15ARGV.each do |username|
16  queue.bind(exchange, routing_key: username)
17end
18
19puts " [*] Waiting for gossip from #{ARGV}. To exit press CTRL+C"
20
21begin
22  queue.subscribe(block: true) do |delivery_info, properties, body|
23    puts " [x] #{delivery_info.routing_key}:#{body}"
24    message = "#{delivery_info.routing_key} says #{body}"
25    system("say '#{message}'")
26  end
27rescue Interrupt => _
28  channel.close
29  connection.close
30end