2 # Scribe plugin for CyBot, which passes around notes.
5 class Scribe < PluginBase
8 @brief_help = 'Passes notes between people.'
11 @user_watch = method(:user_watch)
12 $user.user_watch_hooks << @user_watch
16 $user.user_watch_hooks.delete @user_watch
20 def user_watch(seen_or_lost, irc, user_nick)
22 if (n = @notes[sn = irc.server.name]) and (un = n[user_nick]) and !un.empty?
31 irc.from.notice("You have #{un.length} unread notes (#{count} of which I haven't told you about before). Use the 'notes' command to list them.")
40 Dir[file_name('*.notes')].each do |n|
41 Kernel.puts "Note file: #{n}"
44 @notes[bn[0...i]] = YAML.load_file(n)
48 Kernel.puts e.backtrace.join("\n")
54 open_file("#{k}.notes", 'w') do |f|
55 f.puts "# CyBot scribe plugin: Notes for server '#{k}'"
62 Kernel.puts e.backtrace.join("\n")
66 # Send a new note or read your next note.
68 # note Read the next unread note for you.
69 # note <num> Read note number <num>.
70 # note <to> <text> Write a new note to someone.
72 def cmd_note(irc, line)
73 u = $user.get_nick(irc) or return
74 nu = IRC::Address.normalize(u)
75 n = @notes[sn = irc.server.name] || (@notes[sn] = {})
77 if !line or line.empty?
78 if (e = n[nu]) and !e.empty?
79 from, date, txt = e.shift
80 n.delete(nu) if e.empty?
81 irc.reply "From #{from}, sent #{seconds_to_s(now - date, irc)} ago: #{txt}"
83 irc.reply "You have no unread notes."
86 to, txt = line.split(' ', 2)
88 if (t = $user.get_data(to, sn))
89 nto = IRC::Address.normalize(to)
90 nl = n[nto] || (n[nto] = [])
91 if (announced = $user.get_nick(to, sn))
92 as = (nn = irc.from.nick) == u ? '' : " (as #{nn.capitalize})"
93 irc.server.notice(to, "#{u.capitalize}#{as} has sent you a note. Use 'notes' to list your unread notes.")
95 nl << [u, now, txt, announced ? true : false]
96 irc.reply "Ok, note to #{to} added."
98 irc.reply "No such user, #{to}."
101 if !(e = n[nu]) or e.empty?
102 irc.reply "You have no unread notes."
106 if i >= 1 and i <= e.length
107 from, date, txt = e[i - 1]
108 irc.reply "Note #{i} from #{from}, sent #{seconds_to_s(now - date, irc)} ago: #{txt}"
110 irc.reply "Note number out of range. You have #{e.length} unread notes."
113 irc.reply "USAGE: note <number>"
119 help :note, "Use 'note <nick> <message>' to send a note, or 'note' to read and remove your next available note. You can also type 'note <n>' to read (wihtout removing) note number <n>. Use the 'notes' command to get a list."
122 def cmd_notes(irc, line)
123 u = $user.get_nick(irc) or return
124 if (s = @notes[irc.server.name]) and (s = s[IRC::Address.normalize u]) and !s.empty?
126 irc.reply "You have the following notes: #{s.map do |e|
128 "[#{i}] #{e[0]} (#{seconds_to_s(Time.now.to_i - e[1], irc)} ago)"
131 irc.reply "You have no unread notes."
134 help :notes, 'Displays a list of your currently unread notes.'