quakeforge/doc/config/gib/infobot.gib

236 lines
5.9 KiB
Text
Raw Normal View History

// infobot.gib
//
// Infobot for QuakeForge 0.5.3
//
// Copyright (C) 2003 Brian Koropoff
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to:
//
// Free Software Foundation, Inc.
// 59 Temple Place - Suite 330
// Boston, MA 02111-1307, USA
//
domain infobot
global rcsid = "$Id$"
global facts addr_cmds cmds current_file
function infobot::makeKey {
return $(regex::replace $args[1] "\." -- "\_")_
}
function infobot::hasAccess {
return $(equal $(client::getInfo $args[1] "infopass") $infobot_password)
}
function infobot::defineFactoid {
if (!$infobot_restrict_factoids || $(infobot::hasAccess $args[1])) {
key = $(infobot::makeKey $args[2])
// Filter out newlines/carriage returns
if ${facts.${key}[2]} {
return -2
}
facts.${key}[0] = $(regex::replace $args[3] "[\n\r]" g "")
facts.${key}[1] = @args[4:]
return 0
}
return -1
}
function infobot::factToString {
local out
for i in @args[1:] {
out = $out, $i, "\255"
}
return $out
}
function infobot::saveFactoids {
local output = ""
// Build up output to write to a file
for fact in %facts {
output = $output, $fact, "\255", $(infobot::factToString @facts.$fact), "\n"
}
file::write $args[1],".fct" $output
}
function infobot::loadFactoids {
delete facts
global facts
for line in $(split $(file::read $args[1],".fct") "\n") {
fact = $(split $line "\255")
facts.${fact[0]} = @fact[1:]
}
current_file = $args[1]
}
function infobot::chatEvent {
// Ignore teamtalk/spectalk
if $args[3] {
return
}
from = $(client::getInfo $args[1] "name")
mesg = $args[2]
local res
// see if we are being addressed by name
res = $(regex::extract $mesg $infobot_name, "[:,][[:space:]]+(.+)" i)
if #res {
mesg = $res[2]
local addr = 1
} else {
local addr = 0
}
res = $(regex::extract $mesg "([^[:space:]]+)[\?!]$" --)
if #res {
local key = $(infobot::makeKey $res[2])
if #{facts.$key} {
if $addr {
say $from, ": ", $res[2], " ", ${facts.${key}[1]}, " ", ${facts.$key}, "."
} else {
say "Well, ", $res[2], " ", ${facts.${key}[1]}, " ", ${facts.$key}, "."
}
} else if $addr { // Nothing found, and we were addressed
say $from, ": Sorry, I don't know anything about ", $res[2], "."
}
return
}
res = $(regex::extract $mesg "^([^[:space:]]+)[[:space:]]+(is|are)[[:space:]]+(.+)" i)
if #res {
res = $(infobot::defineFactoid $args[1] $res[2] $res[4] $res[3])
if $addr {
if ($res == -1) {
say $from, ": You do not have the privileges to alter factoids."
} else if ($res == -2) {
say $from, ": That factoid is locked."
} else {
say $from, ": Understood."
}
}
return
}
local cmd = $(split $mesg)
if $addr {
if #{addr_cmds.$cmd} {
${addr_cmds.$cmd} $args[1] $from @cmd[1:];
} else {
say $from, ": I'm sorry, I don't understand."
}
} else if #{cmds.$cmd} {
${cmds.$cmd} $args[1] $from @cmd[1:];
}
}
function infobot::cmdAddrRegister {
addr_cmds.$args[1] = $args[2]
}
function infobot::cmdRegister {
cmds.$args[1] = $args[2]
}
function infobot::forgetFactoid {
if ($infobot_restrict_factoids && !$(infobot::hasAccess $args[1])) {
say $args[2], ": You do not have the privileges to alter factoids."
return
}
fact = $(infobot::makeKey $args[3])
if #{facts.$fact} {
delete facts.$fact
say $args[2], ": I forgot about ", $args[3], "."
} else {
say $args[2], ": I don't know anything about ", $args[3], "."
}
}
function infobot::lockFactoid {
ifnot $(infobot::hasAccess $args[1]) {
say $args[2], ": You do not have the privileges to lock factoids."
return
}
fact = $(infobot::makeKey $args[3])
ifnot #{facts.$fact} {
say $args[2], ": No such factoid exists."
return
}
facts.${fact}[2] = 1
say $args[2], ": \"", $args[3], "\" is now locked."
}
function infobot::unlockFactoid {
ifnot $(infobot::hasAccess $args[1]) {
say $args[2], ": You do not have the privileges to unlock factoids."
return
}
fact = $(infobot::makeKey $args[3])
ifnot #{facts.$fact} {
say $args[2], ": No such factoid exists."
return
}
facts.${fact}[2] = 0
say $args[2], ": \"", $args[3], "\" is now unlocked."
}
function infobot::stats {
say $args[2], ": I currently reference ", $(count %facts), " factoid(s) and ", ($(count %cmds) + $(count %addr_cmds)), " command(s)."
}
function infobot::syncFactoids {
ifnot $(infobot::hasAccess $args[1]) {
say $args[2], ": You do not have syncing privileges."
return
}
if #args[3] {
file = $args[3]
} else {
file = $current_file
}
infobot::saveFactoids $file
say $args[2], ": Factoids successfully synced to file ", $file, ".fct."
}
// These are useful to the admin
function::export infobot::saveFactoids infobot::loadFactoids
event::register chat infobot::chatEvent
infobot::cmdAddrRegister "forget" infobot::forgetFactoid
infobot::cmdAddrRegister "lock" infobot::lockFactoid
infobot::cmdAddrRegister "unlock" infobot::unlockFactoid
infobot::cmdAddrRegister "infostats" infobot::stats
infobot::cmdAddrRegister "sync" infobot::syncFactoids
// Set default values
ifnot $(length $infobot_restrict_factoids) {
set infobot_restrict_factoids 0
}
ifnot $(length $infobot_password) {
set infobot_password "hackme"
}
ifnot $(length $infobot_name) {
set infobot_name "Console"
}
// Try to load a default factoids file
if $(contains "default.fct" $(file::find *.fct)) {
infobot::loadFactoids "default"
}