2014-12-07 23:13:22 +01:00
|
|
|
open Lwt
|
2014-03-09 18:28:02 +01:00
|
|
|
module C = Irc_client_lwt.Client
|
|
|
|
|
2014-12-07 23:13:22 +01:00
|
|
|
let host = "irc.freenode.net"
|
|
|
|
let port = 6667
|
|
|
|
let realname = "CheeseBot"
|
2014-12-12 23:20:24 +01:00
|
|
|
let nick = "cheesebot"
|
2014-12-07 23:13:22 +01:00
|
|
|
let username = nick
|
|
|
|
let channel = "#cheeseburger"
|
2014-03-09 18:28:02 +01:00
|
|
|
|
|
|
|
let string_opt_to_string = function
|
|
|
|
| None -> "None"
|
|
|
|
| Some s -> Printf.sprintf "Some %s" s
|
|
|
|
|
|
|
|
let string_list_to_string string_list =
|
|
|
|
Printf.sprintf "[%s]" (String.concat "; " string_list)
|
|
|
|
|
2014-12-08 23:48:41 +01:00
|
|
|
let evaluate_string_opt = function
|
|
|
|
| None -> None
|
|
|
|
| Some s -> Evaluate.evaluate s
|
|
|
|
|
2014-12-07 23:13:22 +01:00
|
|
|
let callback ~connection ~result =
|
2014-03-09 18:28:02 +01:00
|
|
|
let open Irc_message in
|
2014-12-07 23:13:22 +01:00
|
|
|
match result with
|
2014-03-09 18:28:02 +01:00
|
|
|
| Message {prefix=prefix; command=command; params=params; trail=trail} ->
|
2014-12-08 23:48:41 +01:00
|
|
|
begin
|
|
|
|
Lwt_io.printf "Got message: prefix=%s; command=%s; params=%s; trail=%s\n"
|
|
|
|
(string_opt_to_string prefix)
|
|
|
|
command
|
|
|
|
(string_list_to_string params)
|
|
|
|
(string_opt_to_string trail);
|
|
|
|
match command with
|
|
|
|
| "PRIVMSG" ->
|
|
|
|
begin
|
|
|
|
let response = evaluate_string_opt trail in
|
|
|
|
match response with
|
|
|
|
| None -> Lwt.return_unit
|
|
|
|
| Some s ->
|
|
|
|
C.send_privmsg ~connection ~target:channel ~message:s
|
|
|
|
end
|
|
|
|
| _ -> Lwt.return_unit
|
|
|
|
end
|
2014-03-09 18:28:02 +01:00
|
|
|
| Parse_error (raw, error) ->
|
|
|
|
Lwt_io.printf "Failed to parse \"%s\" because: %s" raw error
|
|
|
|
|
|
|
|
let lwt_main =
|
2014-12-07 23:13:22 +01:00
|
|
|
Lwt_unix.gethostbyname host
|
|
|
|
>>= fun he -> C.connect ~addr:(he.Lwt_unix.h_addr_list.(0))
|
|
|
|
~port ~username ~mode:0 ~realname ~nick ~password:"cheese" ()
|
|
|
|
>>= fun connection -> Lwt_io.printl "Connected"
|
|
|
|
>>= fun () -> C.send_join ~connection ~channel
|
|
|
|
>>= fun () -> C.listen ~connection ~callback
|
|
|
|
>>= fun () -> C.send_quit ~connection
|
2014-03-09 18:28:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Lwt_main.run lwt_main
|