2003-02-16 19:54:41 +00:00
|
|
|
// 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$"
|
|
|
|
|
2003-02-25 08:04:48 +00:00
|
|
|
global facts addr_cmds cmds current_file
|
2003-02-16 19:54:41 +00:00
|
|
|
|
|
|
|
function infobot::makeKey {
|
2003-02-25 08:04:48 +00:00
|
|
|
return $(regex::replace $args[1] "\." -- "\_")_
|
2003-02-16 19:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function infobot::hasAccess {
|
|
|
|
return $(equal $(client::getInfo $args[1] "infopass") $infobot_password)
|
|
|
|
}
|
|
|
|
|
|
|
|
function infobot::defineFactoid {
|
2003-02-25 08:04:48 +00:00
|
|
|
if (!$infobot_restrict_factoids || $(infobot::hasAccess $args[1])) {
|
2003-02-16 19:54:41 +00:00
|
|
|
key = $(infobot::makeKey $args[2])
|
2003-02-25 08:04:48 +00:00
|
|
|
// 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:]
|
2003-02-16 19:54:41 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2003-02-25 08:04:48 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2003-02-16 19:54:41 +00:00
|
|
|
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
|
2003-02-25 08:04:48 +00:00
|
|
|
res = $(regex::extract $mesg $infobot_name, "[:,][[:space:]]+(.+)" i)
|
2003-02-16 19:54:41 +00:00
|
|
|
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 {
|
2003-02-25 08:04:48 +00:00
|
|
|
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."
|
|
|
|
}
|
2003-02-16 19:54:41 +00:00
|
|
|
}
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2003-02-25 08:04:48 +00:00
|
|
|
function infobot::forgetFactoid {
|
|
|
|
if ($infobot_restrict_factoids && !$(infobot::hasAccess $args[1])) {
|
2003-02-16 21:27:18 +00:00
|
|
|
say $args[2], ": You do not have the privileges to alter factoids."
|
2003-02-16 19:54:41 +00:00
|
|
|
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], "."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-25 08:04:48 +00:00
|
|
|
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."
|
|
|
|
}
|
|
|
|
|
2003-02-16 19:54:41 +00:00
|
|
|
function infobot::stats {
|
|
|
|
say $args[2], ": I currently reference ", $(count %facts), " factoid(s) and ", ($(count %cmds) + $(count %addr_cmds)), " command(s)."
|
|
|
|
}
|
|
|
|
|
2003-02-25 08:04:48 +00:00
|
|
|
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
|
|
|
|
|
2003-02-16 19:54:41 +00:00
|
|
|
event::register chat infobot::chatEvent
|
2003-02-25 08:04:48 +00:00
|
|
|
infobot::cmdAddrRegister "forget" infobot::forgetFactoid
|
|
|
|
infobot::cmdAddrRegister "lock" infobot::lockFactoid
|
|
|
|
infobot::cmdAddrRegister "unlock" infobot::unlockFactoid
|
2003-02-16 19:54:41 +00:00
|
|
|
infobot::cmdAddrRegister "infostats" infobot::stats
|
2003-02-25 08:04:48 +00:00
|
|
|
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"
|
|
|
|
}
|