This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
forked from eebrah/zipper-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseen.rb
94 lines (79 loc) · 2.41 KB
/
seen.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
# -*- coding: utf-8 -*-
#
# = Cinch Seen plugin
#
# == Configuration
# Add the following to your bot’s configure.do stanza:
#
# config.plugins.options[Cinch::History] = {
# :file => "/var/cache/seenlog.dat"
# }
#
# [file]
# Where to store the message log. This is a required
# argument.
#
# == Author
# Marvin Gülker (Quintus)
#
# == License
# An seen info plugin for Cinch.
# Copyright © 2014 Marvin Gülker
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class Seen
include Cinch::Plugin
SeenInfo = Struct.new(:time, :nick, :message)
match /seen (.*)/
listen_to :connect, :method => :on_connect
listen_to :channel, :method => :on_channel
def on_connect(*)
@filepath = config[:file] || raise("Missing required argument: :file")
@file = File.open(@filepath, "a+")
@file.seek(0, File::SEEK_END) # Work around Ruby bug https://bugs.ruby-lang.org/issues/10039
@filemutex = Mutex.new
at_exit{@file.close}
end
def on_channel(msg)
return if msg.message.start_with?("\u0001") # ACTION
@filemutex.synchronize do
@file.puts("#{msg.time.to_i}\0#{msg.user.nick}\0#{msg.message.strip}")
end
end
def execute(msg, nick)
if nick == bot.nick
msg.reply "Self-reference err err tilt BOOOOM"
return
end
if info = find_last_message(nick)
msg.reply("#{nick}: was last seen on #{info.time} saying: #{info.message}")
else
msg.reply("I have not yet seen #{nick} saying something.")
end
end
private
def find_last_message(nick)
@filemutex.synchronize do
@file.rewind
@file.each_line.reverse_each do |line|
parts = line.split("\0")
if parts[1] == nick
return SeenInfo.new(Time.at(parts[0].to_i), parts[1], parts[2])
end
end
@file.seek(0, File::SEEK_END)
end
nil
end
end