Prepare to add callouts

This commit is contained in:
Neale Pickett 2010-12-09 08:22:44 -07:00
parent 607e53f417
commit d850b2cbbe
5 changed files with 54 additions and 26 deletions

32
bot.ml
View File

@ -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

View File

@ -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 [];

View File

@ -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. *)

View File

@ -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

15
process.ml Normal file
View File

@ -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 ->