mirror of https://github.com/nealey/irc-bot
Setup working, now there's a bug in command parsing (again).
This commit is contained in:
parent
4f6ba7f271
commit
b55baf1e92
|
@ -1,5 +1,7 @@
|
||||||
type t = {name: string}
|
type t = {name: string}
|
||||||
|
|
||||||
|
let modes = "t"
|
||||||
|
|
||||||
let by_name = Hashtbl.create 25
|
let by_name = Hashtbl.create 25
|
||||||
|
|
||||||
let lookup name =
|
let lookup name =
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
type t
|
type t
|
||||||
|
|
||||||
|
val modes : string
|
||||||
|
|
||||||
val create : string -> t
|
val create : string -> t
|
||||||
val lookup : string -> t
|
val lookup : string -> t
|
||||||
|
|
64
client.ml
64
client.ml
|
@ -8,11 +8,13 @@ type t = {outq: Command.t Queue.t;
|
||||||
ibuf: string;
|
ibuf: string;
|
||||||
ibuf_len: int ref;
|
ibuf_len: int ref;
|
||||||
output_ready: unit -> unit;
|
output_ready: unit -> unit;
|
||||||
handle_command: t -> Command.t -> unit;
|
handle_command: (t -> Command.t -> unit) ref;
|
||||||
nick: string ref;
|
nick: string ref;
|
||||||
username: string ref;
|
username: string ref;
|
||||||
realname: string ref}
|
realname: string ref}
|
||||||
|
|
||||||
|
let modes = "l"
|
||||||
|
|
||||||
let dbg msg a = prerr_endline msg; a
|
let dbg msg a = prerr_endline msg; a
|
||||||
|
|
||||||
let by_file_descr = Hashtbl.create 25
|
let by_file_descr = Hashtbl.create 25
|
||||||
|
@ -47,31 +49,43 @@ let write cli cmd =
|
||||||
let handle_close cli =
|
let handle_close cli =
|
||||||
()
|
()
|
||||||
|
|
||||||
let handle_command cli command =
|
let handle_command cli cmd =
|
||||||
()
|
write cli cmd
|
||||||
|
|
||||||
let handle_command_login cli cmd =
|
let handle_command_login cli cmd =
|
||||||
(* Handle a command during the login phase *)
|
(* Handle a command during the login phase *)
|
||||||
(match (Command.as_tuple cmd) with
|
begin
|
||||||
| (None, "USER", [username; _; _], Some realname) ->
|
match (Command.as_tuple cmd) with
|
||||||
cli.username := username;
|
| (None, "USER", [username; _; _], Some realname) ->
|
||||||
cli.realname := Irc.truncate realname 40
|
cli.username := username;
|
||||||
| (None, "NICK", [nick], None) ->
|
cli.realname := Irc.truncate realname 40
|
||||||
cli.nick := nick;
|
| (None, "NICK", [nick], None) ->
|
||||||
| _ ->
|
cli.nick := nick;
|
||||||
write cli (Command.create
|
| _ ->
|
||||||
~sender:(Some !Irc.name)
|
write cli (Command.create
|
||||||
~text:(Some "Register first.")
|
(Some !Irc.name) "451" ["*"] (Some "Register first."))
|
||||||
"451" ["*"]));
|
end;
|
||||||
(match (!(cli.username), !(cli.nick)) with
|
match (!(cli.username), !(cli.nick)) with
|
||||||
| ("", _)
|
| ("", _)
|
||||||
| (_, "") ->
|
| (_, "") ->
|
||||||
()
|
()
|
||||||
| (_, nick) ->
|
| (_, nick) ->
|
||||||
write cli (Command.create
|
cli.handle_command := handle_command;
|
||||||
~sender:(Some !Irc.name)
|
let command name text =
|
||||||
~text:(Some "Welcome to IRC.")
|
write cli (Command.create
|
||||||
"001" [nick]))
|
(Some !(Irc.name))
|
||||||
|
name
|
||||||
|
[nick]
|
||||||
|
(Some text))
|
||||||
|
in
|
||||||
|
command "001" "Welcome to IRC.";
|
||||||
|
command "002" ("I am " ^ !(Irc.name) ^
|
||||||
|
" running version " ^ Irc.version);
|
||||||
|
command "003" "This server was created sometime";
|
||||||
|
command "004" (!(Irc.name) ^
|
||||||
|
" " ^ Irc.version ^
|
||||||
|
" " ^ modes ^
|
||||||
|
" " ^ Channel.modes)
|
||||||
|
|
||||||
let crlf = Str.regexp "\r?\n"
|
let crlf = Str.regexp "\r?\n"
|
||||||
|
|
||||||
|
@ -86,7 +100,7 @@ let handle_input cli =
|
||||||
String.blit leftover 0 cli.ibuf 0 (String.length leftover)
|
String.blit leftover 0 cli.ibuf 0 (String.length leftover)
|
||||||
| line :: tl ->
|
| line :: tl ->
|
||||||
let parsed = Command.from_string line in
|
let parsed = Command.from_string line in
|
||||||
cli.handle_command cli parsed;
|
!(cli.handle_command) cli parsed;
|
||||||
loop tl
|
loop tl
|
||||||
in
|
in
|
||||||
loop lines
|
loop lines
|
||||||
|
@ -145,7 +159,7 @@ let create ues g fd =
|
||||||
begin
|
begin
|
||||||
fun () -> Unixqueue.add_resource ues g (Unixqueue.Wait_out fd, -.1.0)
|
fun () -> Unixqueue.add_resource ues g (Unixqueue.Wait_out fd, -.1.0)
|
||||||
end;
|
end;
|
||||||
handle_command = handle_command_login;
|
handle_command = ref handle_command_login;
|
||||||
nick = ref "";
|
nick = ref "";
|
||||||
username = ref "";
|
username = ref "";
|
||||||
realname = ref ""}
|
realname = ref ""}
|
||||||
|
|
|
@ -3,12 +3,14 @@ type t = {sender: string option;
|
||||||
args: string list;
|
args: string list;
|
||||||
text: string option}
|
text: string option}
|
||||||
|
|
||||||
let create ?(sender=None) ?(text=None) name args =
|
let create sender name args text =
|
||||||
{sender = sender;
|
{sender = sender;
|
||||||
name = name;
|
name = name;
|
||||||
args = args;
|
args = args;
|
||||||
text = text}
|
text = text}
|
||||||
|
|
||||||
|
let anon = create None
|
||||||
|
|
||||||
let as_string cmd =
|
let as_string cmd =
|
||||||
let ret = Buffer.create 120 in
|
let ret = Buffer.create 120 in
|
||||||
(match cmd.sender with
|
(match cmd.sender with
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
type t
|
type t
|
||||||
|
|
||||||
val create : ?sender:string option -> ?text:string option -> string -> string list -> t
|
val create : string option -> string -> string list -> string option -> t
|
||||||
val from_string : string -> t
|
val from_string : string -> t
|
||||||
val as_string : t -> string
|
val as_string : t -> string
|
||||||
val as_tuple : t -> (string option * string * string list * string option)
|
val as_tuple : t -> (string option * string * string list * string option)
|
||||||
|
|
1
irc.ml
1
irc.ml
|
@ -1,4 +1,5 @@
|
||||||
let name = ref "irc.test"
|
let name = ref "irc.test"
|
||||||
|
let version = "0.1"
|
||||||
|
|
||||||
let newline_re = Pcre.regexp "\n\r?"
|
let newline_re = Pcre.regexp "\n\r?"
|
||||||
let argsep_re = Pcre.regexp " :"
|
let argsep_re = Pcre.regexp " :"
|
||||||
|
|
2
irc.mli
2
irc.mli
|
@ -1,4 +1,6 @@
|
||||||
val name : string ref
|
val name : string ref
|
||||||
|
val version : string
|
||||||
|
|
||||||
val uppercase : string -> string
|
val uppercase : string -> string
|
||||||
val lowercase : string -> string
|
val lowercase : string -> string
|
||||||
val truncate : string -> int -> string
|
val truncate : string -> int -> string
|
||||||
|
|
35
tests.ml
35
tests.ml
|
@ -19,36 +19,41 @@ let unit_tests =
|
||||||
(fun () ->
|
(fun () ->
|
||||||
assert_equal
|
assert_equal
|
||||||
~printer:Command.as_string
|
~printer:Command.as_string
|
||||||
(Command.create "NICK" ["name"])
|
(Command.create None "NICK" ["name"] None)
|
||||||
(Command.from_string "NICK name");
|
(Command.from_string "NICK name");
|
||||||
assert_equal
|
assert_equal
|
||||||
~printer:Command.as_string
|
~printer:Command.as_string
|
||||||
(Command.create ~sender:(Some "foo") "NICK" ["name"])
|
(Command.create (Some "foo") "NICK" ["name"] None)
|
||||||
(Command.from_string ":foo NICK name");
|
(Command.from_string ":foo NICK name");
|
||||||
assert_equal
|
assert_equal
|
||||||
~printer:Command.as_string
|
~printer:Command.as_string
|
||||||
(Command.create
|
(Command.create
|
||||||
~sender:(Some "foo.bar")
|
(Some "foo.bar")
|
||||||
~text:(Some "ta ta")
|
"PART" ["#foo"; "#bar"]
|
||||||
"PART" ["#foo"; "#bar"])
|
(Some "ta ta"))
|
||||||
(Command.from_string ":foo.bar PART #foo #bar :ta ta");
|
(Command.from_string ":foo.bar PART #foo #bar :ta ta");
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
let do_login nick =
|
||||||
|
[
|
||||||
|
Send ("USER " ^ nick ^ " +iw " ^ nick ^ " :gecos\r\n");
|
||||||
|
Send ("NICK " ^ nick ^ "\r\n");
|
||||||
|
Recv (":testserver.test 001 " ^ nick ^ " :Welcome to IRC.\r\n");
|
||||||
|
Recv (":testserver.test 002 " ^ nick ^ " :I am testserver.test running version " ^ Irc.version ^ "\r\n");
|
||||||
|
Recv (":testserver.test 003 " ^ nick ^ " :This server was created sometime\r\n");
|
||||||
|
Recv (":testserver.test 004 " ^ nick ^ " :testserver.test 0.1 l t\r\n");
|
||||||
|
]
|
||||||
|
|
||||||
let regression_tests =
|
let regression_tests =
|
||||||
let login_script nick =
|
"Regression tests" >:::
|
||||||
[
|
[
|
||||||
Send ("USER " ^ nick ^ " +iw " ^ nick ^ " :gecos\r\n");
|
"Simple connection" >::
|
||||||
Send ("NICK " ^ nick ^ "\r\n");
|
(do_chat ((do_login "nick") @
|
||||||
Recv (":testserver.test 001 " ^ nick ^ " :Welcome to IRC.\r\n");
|
[Send "WELCOME :datacomp\r\n";
|
||||||
|
Recv "WELCOME :datacomp\r\n"]));
|
||||||
]
|
]
|
||||||
in
|
|
||||||
"Regression tests" >:::
|
|
||||||
[
|
|
||||||
"Simple connection" >::
|
|
||||||
(do_chat (login_script "nick"));
|
|
||||||
]
|
|
||||||
|
|
||||||
let _ =
|
let _ =
|
||||||
Irc.name := "testserver.test";
|
Irc.name := "testserver.test";
|
||||||
|
|
Loading…
Reference in New Issue