From e680c0565bf3fd0fede9cc124f9a40484f309c56 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Sun, 2 Sep 2007 07:16:10 +0000 Subject: [PATCH] + cleaner support for CTCP commands --- lib/rbot/core/basics.rb | 6 ++---- lib/rbot/ircbot.rb | 37 +++++++++++-------------------------- lib/rbot/message.rb | 20 +++++++++++++++++--- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/lib/rbot/core/basics.rb b/lib/rbot/core/basics.rb index c770cda7..44b800f5 100644 --- a/lib/rbot/core/basics.rb +++ b/lib/rbot/core/basics.rb @@ -11,13 +11,11 @@ class BasicsModule < CoreBotModule def listen(m) return unless m.kind_of?(PrivMessage) - if m.message =~ /^\001PING\s+(.+)\001/ - ping_id = $1 + if m.ctcp == 'PING' + m.ctcp_reply m.ctcp, m.message if m.private? - @bot.notice m.source, "\001PING #{ping_id}\001" @bot.irclog "@ #{m.source} pinged me" else - @bot.notice m.source, "\001PING #{ping_id}\001" @bot.irclog "@ #{m.source} pinged #{m.target}" end end diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index 46d5e902..fd346670 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -953,34 +953,19 @@ class Bot sendmsg "PRIVMSG", where, message, options end + def ctcp_notice(where, command, message, options={}) + return if where.kind_of?(Channel) and quiet_on?(where) + sendmsg "NOTICE", where, "\001#{command} #{message}\001", options + end + + def ctcp_say(where, command, message, options={}) + return if where.kind_of?(Channel) and quiet_on?(where) + sendmsg "PRIVMSG", where, "\001#{command} #{message}\001", options + end + # perform a CTCP action with message +message+ to channel/nick +where+ def action(where, message, options={}) - return if where.kind_of?(Channel) and quiet_on?(where) - mchan = options.fetch(:queue_channel, nil) - mring = options.fetch(:queue_ring, nil) - if mchan - chan = mchan - else - chan = where - end - if mring - ring = mring - else - case where - when User - ring = 1 - else - ring = 2 - end - end - # FIXME doesn't check message length. Can we make this exploit sendmsg? - sendq "PRIVMSG #{where} :\001ACTION #{message}\001", chan, ring - case where - when Channel - irclog "* #{myself} #{message}", where - else - irclog "* #{myself}[#{where}] #{message}", where - end + ctcp_say(where, 'ACTION', message, options) end # quick way to say "okay" (or equivalent) to +where+ diff --git a/lib/rbot/message.rb b/lib/rbot/message.rb index c539843b..7527940a 100644 --- a/lib/rbot/message.rb +++ b/lib/rbot/message.rb @@ -208,6 +208,12 @@ module Irc # channel the message was in, nil for privately addressed messages attr_reader :channel + # for PRIVMSGs, false unless the message was a CTCP command, + # in which case it evaluates to the CTCP command itself + # (TIME, PING, VERSION, etc). The CTCP command parameters + # are then stored in the message. + attr_reader :ctcp + # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff # will be stripped from the message) attr_reader :action @@ -222,6 +228,7 @@ module Irc @target = target @private = false @plugin = nil + @ctcp = false @action = false if target == @bot.myself @@ -250,9 +257,11 @@ module Irc @address = true end - if(@message =~ /^\001ACTION\s(.+)\001/) - @message = $1 - @action = true + if(@message =~ /^\001(\S+)\s(.+)\001/) + @ctcp = $1 + @message = $2 + @action = @ctcp == 'ACTION' + debug "Received CTCP command #{@ctcp} with options #{@message} (action? #{@action})" end # free splitting for plugins @@ -315,6 +324,11 @@ module Irc @replied = true end + # send a CTCP response, i.e. a private notice to the sender + def ctcp_reply(command, string, options={}) + @bot.ctcp_notice @source, command, string, options + end + # convenience method to reply "okay" in the current language to the # message def plainokay -- 2.39.5