irc-bot/bot.ml

107 lines
2.8 KiB
OCaml
Raw Normal View History

2010-12-08 17:18:07 -07:00
type bot = {
store: Infobot.t;
}
2010-12-10 17:03:24 -07:00
let debug = prerr_endline
let file_descr_of_int (i:int) =
let blob = Marshal.to_string i [] in
(Marshal.from_string blob 0 : Unix.file_descr)
2009-02-08 20:25:27 -07:00
let write iobuf command args text =
let cmd = Command.create None command args text in
2010-12-10 17:03:24 -07:00
debug ("--> " ^ (Command.as_string cmd));
2009-02-08 20:25:27 -07:00
Iobuf.write iobuf cmd
2010-12-09 08:22:44 -07:00
let msg iobuf recip text =
write iobuf "PRIVMSG" [recip] (Some text)
2010-12-10 17:03:24 -07:00
let split = Str.split (Str.regexp "[ \t]*\r?\n")
(** Callback upon completion of the external command helper *)
let extern_callback iobuf sender forum text =
let lines = split text in
let nlines = List.length lines in
let recip =
if (nlines > 5) then begin
if (forum <> sender) then
msg iobuf forum (Format.sprintf "%d lines, sending privately" nlines);
sender
end else
forum
in
let rec f = function
| [] ->
()
| "" :: tl ->
f tl
| line :: tl ->
if line.[0] == ':' then
(* Interpret as raw IRC commands *)
let ine = Str.string_after line 1 in
let cmd = Command.from_string ine in
Iobuf.write iobuf cmd
else
(* Naive case: send to the recipient *)
msg iobuf recip line;
f tl
in
f lines
2010-12-09 08:22:44 -07:00
let handle_privmsg bot iobuf sender forum text =
2010-12-10 17:03:24 -07:00
if text.[0] == '.' then
Process.create_canned
(Iobuf.dispatcher iobuf)
text
(extern_callback iobuf sender forum)
"./helper"
[|"./helper"; sender; forum|]
else
Infobot.handle_privmsg bot.store (msg iobuf forum) sender forum text
2010-12-09 08:22:44 -07:00
2010-12-10 17:03:24 -07:00
let handle_command bot outbuf thisbuf cmd =
debug ("<-- " ^ (Command.as_string cmd));
2010-12-09 08:22:44 -07:00
match (Command.as_tuple cmd) with
2010-12-10 17:03:24 -07:00
| (Some suhost, "PRIVMSG", [target], Some text) ->
let sender = Irc.nick (Irc.nuhost_of_string suhost) in
let forum =
if Irc.is_channel target then
target
else
sender
in
handle_privmsg bot outbuf sender forum text
2009-02-08 20:25:27 -07:00
| (_, "PING", _, text) ->
2010-12-10 17:03:24 -07:00
write outbuf "PONG" [] text
2009-02-08 20:25:27 -07:00
| (_, "001", _, _) ->
2010-12-10 17:03:24 -07:00
write outbuf "JOIN" ["#bot"] None;
| (Some sender, "JOIN", [], Some chan) ->
2010-12-10 17:03:24 -07:00
msg outbuf chan "hi asl"
2009-02-08 20:25:27 -07:00
| _ ->
()
2010-12-10 17:03:24 -07:00
let discard_command iobuf cmd = ()
2009-02-08 20:25:27 -07:00
let handle_error iobuf str =
2010-12-10 17:03:24 -07:00
prerr_endline ("!!! " ^ str)
2009-02-08 20:25:27 -07:00
let main () =
2010-12-08 17:18:07 -07:00
let bot = {store = Infobot.create "info.cdb"} in
2010-12-10 17:03:24 -07:00
let dispatcher = Dispatch.create () in
let iobuf_out = Iobuf.create dispatcher Unix.stdout "collab_out"
discard_command
handle_error
in
let _ = Iobuf.create dispatcher Unix.stdin "collab_in"
(handle_command bot iobuf_out)
2010-12-08 17:18:07 -07:00
handle_error
in
2010-12-10 17:03:24 -07:00
write iobuf_out "NICK" ["zinc"] None;
write iobuf_out "USER" ["zinc"; "zinc"; "zinc"] (Some "I'm a little printf, short and stdout");
2009-02-08 20:25:27 -07:00
Dispatch.run dispatcher
let _ =
main ()