diff --git a/bot.ml b/bot.ml index 548f411..314cae6 100644 --- a/bot.ml +++ b/bot.ml @@ -7,16 +7,40 @@ let write iobuf command args text = print_endline ("--> " ^ (Command.as_string cmd)); Iobuf.write iobuf cmd +let msg iobuf recip text = + write iobuf "PRIVMSG" [recip] (Some text) + +let calc_re = Str.regexp "^calc \\(.*\\)" +let calc iobuf forum text = + if Str.string_match calc_re text 0 then + msg iobuf forum (Str.matched_group 1 text) + +let handle_privmsg bot iobuf sender forum text = + calc iobuf forum text; + match (Infobot.lookup bot.store text) with + | Some reply -> + msg iobuf forum reply + | None -> + () + + let handle_command bot iobuf cmd = print_endline ("<-- " ^ (Command.as_string cmd)); - Infobot.handle_command bot.store iobuf cmd; - match Command.as_tuple cmd with + match (Command.as_tuple cmd) with + | (Some sender, "PRIVMSG", [target], Some text) -> + let forum = + if Irc.is_channel target then + target + else + sender + in + handle_privmsg bot iobuf sender forum text | (_, "PING", _, text) -> write iobuf "PONG" [] text | (_, "001", _, _) -> write iobuf "JOIN" ["#bot"] None | (Some sender, "JOIN", [], Some chan) -> - write iobuf "PRIVMSG" [chan] (Some "hi asl") + msg iobuf chan "hi asl" | _ -> () @@ -25,7 +49,7 @@ let handle_error iobuf str = let main () = let host = Unix.gethostbyname "woozle.org" in - let dispatcher = Dispatch.create 5 in + let dispatcher = Dispatch.create () in let conn = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in let bot = {store = Infobot.create "info.cdb"} in let _ = Unix.connect conn (Unix.ADDR_INET (host.Unix.h_addr_list.(0), 6667)) in diff --git a/dispatch.ml b/dispatch.ml index 3b4d636..5a320a0 100644 --- a/dispatch.ml +++ b/dispatch.ml @@ -22,7 +22,7 @@ type t = { timers : Timer.t ref; } -let create size = +let create ?(size=5) () = {read_fds = ref []; write_fds = ref []; except_fds = ref []; diff --git a/dispatch.mli b/dispatch.mli index 2b1a2f0..cf89122 100644 --- a/dispatch.mli +++ b/dispatch.mli @@ -10,7 +10,7 @@ type fd_handler = Unix.file_descr -> event -> unit type timer_handler = float -> unit (** [timer_handler d when] is called at or after [when] *) -val create : int -> t +val create : ?size:int -> unit -> t (** Create a new event dispatcher, preallocating [size] fd events. [size] is just a hint, the fd list will grow on demand. *) diff --git a/infobot.ml b/infobot.ml index 3e73592..2dd9888 100644 --- a/infobot.ml +++ b/infobot.ml @@ -39,7 +39,7 @@ let choose_one ib key = | keys -> choice keys -let handle_privmsg store iobuf sender target text = +let lookup store text = try let text, factoid = try @@ -51,23 +51,12 @@ let handle_privmsg store iobuf sender target text = | Some stext -> (stext, choose_one store stext) in - let response = - match factoid.[0] with - | ':' -> - "\001ACTION " ^ (Str.string_after factoid 1) ^ "\001" - | '\\' -> - Str.string_after factoid 1 - | _ -> - Printf.sprintf "I overheard that %s is %s" text factoid - in - Iobuf.write iobuf (Command.create None "PRIVMSG" [target] (Some response)) + 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) with Not_found -> - () - -let handle_command store iobuf cmd = - match Command.as_tuple cmd with - | (Some sender, "PRIVMSG", [target], Some text) -> - if Irc.is_channel target then - handle_privmsg store iobuf sender target text - | _ -> - () + None diff --git a/process.ml b/process.ml new file mode 100644 index 0000000..3c6c4e0 --- /dev/null +++ b/process.ml @@ -0,0 +1,15 @@ +type t = { + +} + +let create iobuf prog args = + let fd0_exit, fd0_entr = Unix.pipe () in + let fd1_exit, fd1_entr = Unix.pipe () in + let fd2_exit, fd2_entr = Unix.pipe () in + let pid = Unix.create_process prog args fd0_exit fd1_entr fd2_entr in + + +let handle_event process fd event = + match event with + | Dispatch.Input -> +