-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcampfireMonitor.rb
executable file
·133 lines (117 loc) · 4.12 KB
/
campfireMonitor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env ruby
# == Synopsis
# Program to monitor a campfire chat room
# Modified from code provided at: http://www.snailbyte.com/2007/09/13/campfire-activity-notifier-for-kde/
#
# == Usage
# campfireMonitor [roomName] [roomName] ...
#
# == Author
# Steven Pothoven and Snailbyte Ltd.
require 'rubygems'
require 'tinder'
require "cgi"
class App
VERSION = '0.2.0'
def initialize arguments, stdin
# default settings
campfireSubdomain = 'mySubdomain'
campfireToken = 'a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1'
roomNames = ['Room1', 'Room2']
if arguments.length > 0
roomNames = arguments
end
# define platform specific noficiation values such as icon path and
# your favorite audio player and audio file here for audio notifications
if RUBY_PLATFORM =~ /darwin/
# for OSX setup Growl
require 'growl'
@ui = 'mac'
@campfireIconPath = File.join File.expand_path(File.dirname(__FILE__)), 'campfireMonitor.png'
elsif RUBY_PLATFORM =~ /linux/
# for Linux default to Gnome and use MPlayer for audio
@ui = 'gnome'
@campfireIconPath = File.join File.expand_path(File.dirname(__FILE__)), 'campfire-logo.png'
@soundCommand = 'mplayer /usr/share/sounds/pop.wav'
elsif RUBY_PLATFORM =~ /mswin/
@ui = 'windows'
end
@campfire = Tinder::Campfire.new campfireSubdomain, :token => campfireToken
if @campfire.present?
alert nil, "CampfireMonitor", "Successfully logged in #{campfireSubdomain}"
@rooms = roomNames.collect { |roomName| @campfire.find_room_by_name roomName }
# remove any invalid rooms
@rooms.delete(nil);
@rooms.each do |room|
notify room, "CampfireMonitor", "Entered room."
notify room, "CampfireMonitor", "Topic is: #{room.topic}.".gsub("'","")
notify room, "CampfireMonitor", "Current users are: #{room.users.collect { |user| user.name}.join(', ')}."
end
else
alert nil, "CampfireMonitor", "Failed to log in #{campfireUsername}"
end
end
# display notifcation message
def notify room, user, msg
if msg and msg.size > 0
msg = CGI.unescapeHTML(msg);
if @ui == 'kde'
system "dcop knotify default notify eventname \'#{user}\' \'#{'<a href="'[email protected]_s+'/room/'+room.id+'">'+room.name+'</a>: ' unless room.nil?} #{msg}\' '' '' 16 2"
elsif @ui == 'gnome'
system "notify-send -i #{@campfireIconPath} '#{user}' '#{'<a href="'[email protected]_s+'/room/'+room.id+'">'+room.name+'</a>: ' unless room.nil?} #{msg}'"
elsif @ui == 'mac'
Growl.notify "#{user}: #{msg}", :title => "#{room.name unless room.nil?}", :icon => @campfireIconPath
else
puts "#{room.name+':' unless room.nil?}#{user} - #{msg}"
end
end
end
# notify with sound
def alert room, user, msg
notify room, user, msg
if @soundCommand and @soundCommand.length > 0
system @soundCommand
end
end
def run
# first get any missed messages for today
# threads = []
@rooms.each do |room|
# thread = Thread.new do
begin
room.transcript(Date.today).last(3).each do |m|
if !m.nil? and m[:message] and m[:message].size > 1
notify room, m[:person], m[:message].gsub("'","")
end
end
rescue
notify room, "CampfireMonitor", "Could not process transcript"
end
# end
# threads << thread
end
# threads.each { |thread| thread.join}
# listen for more messages
# threads = []
@rooms.each do |room|
# thread = Thread.new(room) do |room|
begin
notify room, "CampfireMonitor", "Waiting for messages..."
room.listen do |m|
if !m.nil? and m[:body].size > 1
unless m[:user][:name] == "Ad"
alert room, m[:user][:name], m[:body].gsub("'","")
end
end
end
rescue
notify room, "CampfireMonitor", "Problem starting monitor: " + $!
end
# end
# threads << thread
end
# threads.each { |thread| thread.join}
end
end
app = App.new(ARGV, STDIN)
app.run