package require Tcl 8.4

#
# offlineReply.tcl --
#
#       Tcl Script for shroudBNC IRC Proxy
#       Respond to public "highlighting" or private messages/notices with an user-defined reply if the bouncer-user is offline
#
# Copyright (c) 2006, Markus Knöpfle, avilon87---gmail---com, http://avilon.eu/
# All rights reserved.
#
# Modified by Khobbits
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * The name of the author may not be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# $Id: offlineReply.tcl,v 0.5 2006/07/28 01:56 Markus Knöpfle Exp $
#

# Changelog: 0.1: Initial release (2006/01/08)
#            0.2: Several (Bug-)fixes, proper script header and contact information (2006/02/27)
#            0.3: Important bugfix, layout changes (2006/06/09)
#            0.4: Important bugfixes, layout changes
#            0.5: Added log-feature for pubm 'highlights'
#	     0.55kh: Khobbits edited misc stuff.

if { [namespace exists ::offlineReply] } {
  namespace delete ::offlineReply
}

catch {putmainlog "Loading offlineReply 0.55, Filename: [info script]"}

namespace eval ::offlineReply {

  # Which Bouncer users does this script apply to?
  # replace '[bncuserlist]' with '[list username1 username 2 etc...]' if you don't want to activate this script for all users

  set users [bncuserlist]

  # What message should one get when highlighting us? available parameters: %nick%

  variable notcstyle "Hello %nick%, but I'm currently not online. Please try again later, thank you."


  foreach user $users {
    setctx $user
    bind msgm - * [namespace current]::msgm
    bind pubm - * [namespace current]::pubm
    bind notc - * [namespace current]::notc

    proc msgm {nick args} {
          if { [string first [getcurrentnick] $args] eq "-1" } { return }
      if { [getbncuser [getctx] hasclient] || ![onchan $nick] || ([string length $nick] < "2") } {
         return
      }
      if { [info exists [getns]::lastofflineReply] && ([clock seconds] - [set [getns]::lastofflineReply] < "15") } {
        return
      }
      replyo $nick
      set [getns]::lastofflineReply [clock seconds]
      return
    }

    proc pubm {nick host hand chan arg} {
      if { [string first [getcurrentnick] $arg] eq "-1" } { return }
      if { ![getbncuser [getctx] hasclient] } {
        if { [info exists [getns]::lastofflineReply] && ([clock seconds] - [set [getns]::lastofflineReply] < "15") } {
          return
        }
        replyo $nick
        putlog "$nick ($host) mentioned your nickname on $chan at [clock format [clock sec]] when writing '$arg'."
        set [getns]::lastofflineReply [clock seconds]
      }

      return
    }

    proc notc {nick args} {
          if { [string first [getcurrentnick] $args] eq "-1" } { return }
      if { [getbncuser [getctx] hasclient] || ![onchan $nick] || ([string length $nick] < "2") } {
        return
      }
      if { [info exists [getns]::lastofflineReply] && ([clock seconds] - [set [getns]::lastofflineReply] < "15") } {
        return
      }
      replyo $nick
      set [getns]::lastofflineReply [clock seconds]

      return
    }

    proc replyo {nick} {
      variable notcstyle
      set msg [string map [list %nick% $nick] $notcstyle]
      puthelp "NOTICE $nick :$msg"
      return
    }

  }
};
