irc-bot/bot.ml

67 lines
1.8 KiB
OCaml
Raw Normal View History

2010-12-08 17:18:07 -07:00
type bot = {
store: Infobot.t;
}
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
2010-12-09 08:22:44 -07:00
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 ->
()
2010-12-08 17:18:07 -07:00
let handle_command bot iobuf cmd =
2009-02-08 20:25:27 -07:00
print_endline ("<-- " ^ (Command.as_string cmd));
2010-12-09 08:22:44 -07:00
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
2009-02-08 20:25:27 -07:00
| (_, "PING", _, text) ->
write iobuf "PONG" [] text
| (_, "001", _, _) ->
2009-03-03 19:48:22 -07:00
write iobuf "JOIN" ["#bot"] None
| (Some sender, "JOIN", [], Some chan) ->
2010-12-09 08:22:44 -07:00
msg iobuf chan "hi asl"
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
2010-12-09 08:22:44 -07:00
let dispatcher = Dispatch.create () in
2009-02-08 20:25:27 -07:00
let conn = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2010-12-08 17:18:07 -07:00
let bot = {store = Infobot.create "info.cdb"} in
2009-02-08 20:25:27 -07:00
let _ = Unix.connect conn (Unix.ADDR_INET (host.Unix.h_addr_list.(0), 6667)) in
2010-12-08 17:18:07 -07:00
let iobuf = Iobuf.create dispatcher conn "woozle"
(handle_command bot)
handle_error
in
2009-02-08 20:25:27 -07:00
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 ()