-
Notifications
You must be signed in to change notification settings - Fork 0
/
doormsg_to_notification
executable file
·100 lines (95 loc) · 3.52 KB
/
doormsg_to_notification
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
#!/usr/bin/ruby
# Door Message to Notification
# Takes door entry messages and turns them into text notifications.
# Input: Exchange 'door.entry'
# Output: Exchange 'notification.general'
require 'rubygems'
require 'amqp'
require 'net/http'
require 'cgi'
require 'json'
require 'inifile'
cfg = IniFile.load(File.expand_path("~/.labamqprc"))
amqp_host = cfg['broker']['BrokerHost']
amqp_user = cfg['ledsign']['BrokerUsername']
amqp_pass = cfg['ledsign']['BrokerPassword']
AMQP.logging = true
AMQP.start(:host => amqp_host, :user => amqp_user, :pass => amqp_pass) do
amq = AMQP::Channel.new
door_exchange = amq.fanout('door.entry', :durable => false, :auto_delete => false);
door_queue = amq.queue('door.entry.notification', :durable => false, :auto_delete => true);
door_queue.bind(door_exchange);
sign_exchange = amq.fanout('notification.general', :durable => false, :auto_delete => false);
door_queue.subscribe do |header, msg|
json = JSON.load(msg)
p json
door = json['door']
direction = "entered"
public = false
case door
when "Front Door"
door = "1266 Queen"
when "Unit 6"
door = "HackLab"
public = true
when "Unit 12"
door = "HackLab Classroom"
public = true
when "Unit 6 Exit"
door = "HackLab"
direction = "left"
public = true
when "Hacklab Member Closet" || "Hacklab Darkroom"
next # no notifications for internal doors
end
if(json['allowed']) then
nickname = json['nickname']
realname = json['realname']
image = nil
if(/^[a-zA-Z0-9\ ]+$/.match(nickname) != nil) then
homedir = `getent passwd '#{nickname}' | cut -d: -f6`
homedir.rstrip!
if(homedir == "") then
homedir = `getent passwd | cut -d: -f5,6 | grep '#{realname}:' | cut -d: -f2`
homedir.rstrip!
if homedir then
puts "found home directory using real name '#{realname}': #{homedir}"
else
puts "couldn't find home directory for '#{nickname}'"
end
else
puts "found home directory using nickname '#{nickname}': #{homedir}"
end
signname = image = nil
if File.exist?("#{homedir}/.ledsign") then
signname = File.read("#{homedir}/.ledsign")
puts "found sign name '#{signname.rstrip}'"
end
if File.exist?("#{homedir}/.ledsign.png") then
image = "#{homedir}/.ledsign.png"
puts "found sign image '#{image}'"
end
end
signname = nickname if !signname
signname.rstrip!
notify_msg = Hash.new
notify_msg['text'] = "#{signname} has #{direction} #{door}."
msg_type = "Get Ready For"
msg_type = "Now Entering" if door == "HackLab" || door == "HackLab Classroom"
msg_type = "Goodbye" if direction == "left"
notify_msg['long_text'] = "#{msg_type}\n#{signname}"
notify_msg['public'] = public
notify_msg['image'] = image if image
puts notify_msg.to_json
sign_exchange.publish(notify_msg.to_json)
else
nickname = json['nickname']
if(nickname == "") then
notify_msg = {:text => "I denied someone access to #{door} because I don't recognise their card.", :long_text => "ACCESS DENIED"}.to_json
else
notify_msg = {:text => "I denied #{nickname} access to #{door} because nobody has introduced them to me yet or someone told me they shouldn't be in there.", :long_text => "ACCESS DENIED"}.to_json
end
sign_exchange.publish(notify_msg)
end
end
end