irc-bot/infobot.ml

71 lines
1.4 KiB
OCaml
Raw Normal View History

2010-12-08 17:18:07 -07:00
type t = {
filename: string;
mutable db: Cdb.cdb_file;
}
2009-11-08 22:18:18 -07:00
let _ = Random.self_init ()
2010-12-08 17:18:07 -07:00
let create filename =
{
filename = filename;
db = Cdb.open_cdb_in filename;
}
2009-11-08 22:18:18 -07:00
let choice l =
let n = Random.int (List.length l) in
List.nth l n
2010-12-08 17:18:07 -07:00
let strip s =
let rec lastchar n =
match s.[n-1] with
| '.'
| '!'
| '?'
| ' ' ->
lastchar (n - 1)
| _ ->
n
in
let len = lastchar (String.length s) in
if (len = String.length s) then
None
else
Some (String.sub s 0 len)
let choose_one ib key =
match (Cdb.get_matches ib.db key) with
| [] ->
raise Not_found
| keys ->
choice keys
2009-11-08 22:18:18 -07:00
2010-12-09 08:22:44 -07:00
let lookup store text =
2009-11-08 22:18:18 -07:00
try
2010-12-08 17:18:07 -07:00
let text, factoid =
try
(text, choose_one store text)
with Not_found ->
match (strip text) with
| None ->
raise Not_found
| Some stext ->
(stext, choose_one store stext)
in
2010-12-09 08:22:44 -07:00
match factoid.[0] with
| ':' ->
Some ("\001ACTION " ^ (Str.string_after factoid 1) ^ "\001")
| '\\' ->
Some (Str.string_after factoid 1)
| _ ->
Some (Printf.sprintf "I overheard that %s is %s" text factoid)
2009-11-08 22:18:18 -07:00
with Not_found ->
2010-12-09 08:22:44 -07:00
None
2010-12-10 17:03:24 -07:00
let handle_privmsg store msg sender forum text =
match (lookup store text) with
| Some reply ->
msg reply
| None ->
()