mirror of https://github.com/nealey/irc-bot
Prepare to add callouts
This commit is contained in:
parent
607e53f417
commit
d850b2cbbe
32
bot.ml
32
bot.ml
|
@ -7,16 +7,40 @@ let write iobuf command args text =
|
||||||
print_endline ("--> " ^ (Command.as_string cmd));
|
print_endline ("--> " ^ (Command.as_string cmd));
|
||||||
Iobuf.write iobuf 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 =
|
let handle_command bot iobuf cmd =
|
||||||
print_endline ("<-- " ^ (Command.as_string 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) ->
|
| (_, "PING", _, text) ->
|
||||||
write iobuf "PONG" [] text
|
write iobuf "PONG" [] text
|
||||||
| (_, "001", _, _) ->
|
| (_, "001", _, _) ->
|
||||||
write iobuf "JOIN" ["#bot"] None
|
write iobuf "JOIN" ["#bot"] None
|
||||||
| (Some sender, "JOIN", [], Some chan) ->
|
| (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 main () =
|
||||||
let host = Unix.gethostbyname "woozle.org" in
|
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 conn = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
|
||||||
let bot = {store = Infobot.create "info.cdb"} in
|
let bot = {store = Infobot.create "info.cdb"} in
|
||||||
let _ = Unix.connect conn (Unix.ADDR_INET (host.Unix.h_addr_list.(0), 6667)) in
|
let _ = Unix.connect conn (Unix.ADDR_INET (host.Unix.h_addr_list.(0), 6667)) in
|
||||||
|
|
|
@ -22,7 +22,7 @@ type t = {
|
||||||
timers : Timer.t ref;
|
timers : Timer.t ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
let create size =
|
let create ?(size=5) () =
|
||||||
{read_fds = ref [];
|
{read_fds = ref [];
|
||||||
write_fds = ref [];
|
write_fds = ref [];
|
||||||
except_fds = ref [];
|
except_fds = ref [];
|
||||||
|
|
|
@ -10,7 +10,7 @@ type fd_handler = Unix.file_descr -> event -> unit
|
||||||
type timer_handler = float -> unit
|
type timer_handler = float -> unit
|
||||||
(** [timer_handler d when] is called at or after [when] *)
|
(** [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.
|
(** Create a new event dispatcher, preallocating [size] fd events.
|
||||||
[size] is just a hint, the fd list will grow on demand. *)
|
[size] is just a hint, the fd list will grow on demand. *)
|
||||||
|
|
||||||
|
|
21
infobot.ml
21
infobot.ml
|
@ -39,7 +39,7 @@ let choose_one ib key =
|
||||||
| keys ->
|
| keys ->
|
||||||
choice keys
|
choice keys
|
||||||
|
|
||||||
let handle_privmsg store iobuf sender target text =
|
let lookup store text =
|
||||||
try
|
try
|
||||||
let text, factoid =
|
let text, factoid =
|
||||||
try
|
try
|
||||||
|
@ -51,23 +51,12 @@ let handle_privmsg store iobuf sender target text =
|
||||||
| Some stext ->
|
| Some stext ->
|
||||||
(stext, choose_one store stext)
|
(stext, choose_one store stext)
|
||||||
in
|
in
|
||||||
let response =
|
|
||||||
match factoid.[0] with
|
match factoid.[0] with
|
||||||
| ':' ->
|
| ':' ->
|
||||||
"\001ACTION " ^ (Str.string_after factoid 1) ^ "\001"
|
Some ("\001ACTION " ^ (Str.string_after factoid 1) ^ "\001")
|
||||||
| '\\' ->
|
| '\\' ->
|
||||||
Str.string_after factoid 1
|
Some (Str.string_after factoid 1)
|
||||||
| _ ->
|
| _ ->
|
||||||
Printf.sprintf "I overheard that %s is %s" text factoid
|
Some (Printf.sprintf "I overheard that %s is %s" text factoid)
|
||||||
in
|
|
||||||
Iobuf.write iobuf (Command.create None "PRIVMSG" [target] (Some response))
|
|
||||||
with Not_found ->
|
with Not_found ->
|
||||||
()
|
None
|
||||||
|
|
||||||
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
|
|
||||||
| _ ->
|
|
||||||
()
|
|
||||||
|
|
|
@ -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 ->
|
||||||
|
|
Loading…
Reference in New Issue