2009-02-09 22:06:14 -07:00
|
|
|
let info_db = Cdb.open_cdb_in "/home/neale/src/firebot/info.cdb"
|
|
|
|
let _ = Random.self_init ()
|
|
|
|
|
|
|
|
let choice l =
|
|
|
|
let n = Random.int (List.length l) in
|
|
|
|
List.nth l n
|
|
|
|
|
|
|
|
let get_one key =
|
|
|
|
let matches = Cdb.get_matches info_db key in
|
|
|
|
match Stream.npeek 120 matches with
|
|
|
|
| [] -> raise Not_found
|
|
|
|
| keys -> choice keys
|
|
|
|
|
2009-02-08 20:25:27 -07:00
|
|
|
let write iobuf command args text =
|
|
|
|
let cmd = Command.create None command args text in
|
|
|
|
print_endline ("--> " ^ (Command.as_string cmd));
|
|
|
|
Iobuf.write iobuf cmd
|
|
|
|
|
2009-03-02 23:26:04 -07:00
|
|
|
let scheme_eval str =
|
|
|
|
let thread = Ocs_top.make_thread () in
|
|
|
|
let env = Ocs_top.make_env () in
|
2009-03-03 19:48:22 -07:00
|
|
|
let inport = Ocs_port.open_input_string str in
|
|
|
|
let outport = Ocs_port.open_output_string () in
|
2009-03-02 23:26:04 -07:00
|
|
|
let lexer = Ocs_lex.make_lexer inport "interactive" in
|
|
|
|
let v = Ocs_read.read_expr lexer in
|
|
|
|
let c = Ocs_compile.compile env v in
|
|
|
|
try
|
2009-03-03 19:48:22 -07:00
|
|
|
Ocs_eval.eval thread (Ocs_print.print outport false) c;
|
|
|
|
Ocs_port.get_output_string outport
|
2009-03-02 23:26:04 -07:00
|
|
|
with Ocs_error.Error msg ->
|
|
|
|
msg
|
|
|
|
|
|
|
|
|
|
|
|
let handle_privmsg iobuf sender target text =
|
|
|
|
try
|
|
|
|
let factoid = get_one text in
|
|
|
|
let response =
|
|
|
|
match factoid.[0] with
|
|
|
|
| ':' ->
|
|
|
|
"\001ACTION " ^ (Str.string_after factoid 1) ^ "\001"
|
|
|
|
| '\\' ->
|
|
|
|
Str.string_after factoid 1
|
|
|
|
| _ ->
|
|
|
|
Printf.sprintf "Gosh, %s, I think %s is %s" sender text factoid
|
|
|
|
in
|
|
|
|
write iobuf "PRIVMSG" [target] (Some response)
|
|
|
|
with Not_found ->
|
|
|
|
if text.[0] == '(' then
|
|
|
|
let result = scheme_eval text in
|
|
|
|
write iobuf "PRIVMSG" [target] (Some result)
|
|
|
|
|
|
|
|
|
2009-02-08 20:25:27 -07:00
|
|
|
let handle_command iobuf cmd =
|
|
|
|
print_endline ("<-- " ^ (Command.as_string cmd));
|
|
|
|
match Command.as_tuple cmd with
|
|
|
|
| (_, "PING", _, text) ->
|
|
|
|
write iobuf "PONG" [] text
|
|
|
|
| (_, "001", _, _) ->
|
2009-03-03 19:48:22 -07:00
|
|
|
write iobuf "JOIN" ["#bot"] None
|
2009-03-02 23:26:04 -07:00
|
|
|
| (Some sender, "JOIN", [], Some chan) ->
|
2009-02-08 20:25:27 -07:00
|
|
|
write iobuf "PRIVMSG" [chan] (Some "hi asl")
|
2009-03-02 23:26:04 -07:00
|
|
|
| (Some sender, "PRIVMSG", [target], Some text) ->
|
2009-03-03 17:56:07 -07:00
|
|
|
if Irc.is_channel target then
|
2009-03-02 23:26:04 -07:00
|
|
|
handle_privmsg iobuf sender target text
|
2009-02-08 20:25:27 -07:00
|
|
|
| _ ->
|
|
|
|
()
|
|
|
|
|
|
|
|
let handle_error iobuf str =
|
|
|
|
print_endline str
|
|
|
|
|
|
|
|
let main () =
|
|
|
|
let host = Unix.gethostbyname "woozle.org" in
|
|
|
|
let dispatcher = Dispatch.create 5 in
|
|
|
|
let conn = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
|
|
|
|
let _ = Unix.connect conn (Unix.ADDR_INET (host.Unix.h_addr_list.(0), 6667)) in
|
|
|
|
let iobuf = Iobuf.create dispatcher conn "woozle" handle_command handle_error in
|
|
|
|
write iobuf "NICK" ["bot"] None;
|
2009-03-03 19:48:22 -07:00
|
|
|
write iobuf "USER" ["bot"; "bot"; "bot"] (Some "A Bot");
|
2009-02-08 20:25:27 -07:00
|
|
|
Dispatch.run dispatcher
|
|
|
|
|
|
|
|
|
|
|
|
let _ =
|
|
|
|
main ()
|