2008-02-27 22:50:27 -07:00
|
|
|
open Irc
|
|
|
|
|
|
|
|
(* ==========================================
|
|
|
|
* Client stuff
|
|
|
|
*)
|
2008-03-04 21:38:19 -07:00
|
|
|
type t = {iobuf: Iobuf.t;
|
2008-02-28 22:00:24 -07:00
|
|
|
nick: string ref;
|
2008-03-18 22:50:23 -06:00
|
|
|
away: string option ref;
|
2008-03-04 21:38:19 -07:00
|
|
|
username: string;
|
|
|
|
realname: string}
|
|
|
|
|
|
|
|
exception Error of Command.t
|
2008-02-28 22:00:24 -07:00
|
|
|
|
2008-03-02 21:30:37 -07:00
|
|
|
let modes = "l"
|
|
|
|
|
2008-03-01 21:38:05 -07:00
|
|
|
let dbg msg a = prerr_endline msg; a
|
2008-02-28 22:00:24 -07:00
|
|
|
|
|
|
|
let by_nick = Hashtbl.create 25
|
|
|
|
|
2008-03-07 12:00:40 -07:00
|
|
|
let lookup nick =
|
|
|
|
Hashtbl.find by_nick nick
|
|
|
|
|
2008-03-04 21:38:19 -07:00
|
|
|
let error num args text =
|
|
|
|
Error (Command.create (Some !(Irc.name)) num args (Some text))
|
2008-02-28 22:00:24 -07:00
|
|
|
|
2008-05-10 14:19:14 -06:00
|
|
|
let nuhost cli = (!(cli.nick), cli.username, (Iobuf.addr cli.iobuf))
|
2008-03-07 12:00:40 -07:00
|
|
|
|
2008-03-19 16:19:24 -06:00
|
|
|
let kill cli message =
|
|
|
|
Iobuf.close cli.iobuf ("Killed: " ^ message)
|
2008-02-27 22:50:27 -07:00
|
|
|
|
2008-03-18 17:20:44 -06:00
|
|
|
let write_command cli cmd =
|
2008-03-04 21:38:19 -07:00
|
|
|
Iobuf.write cli.iobuf cmd
|
2008-02-27 22:50:27 -07:00
|
|
|
|
2008-03-18 17:20:44 -06:00
|
|
|
let write cli sender name args text =
|
|
|
|
write_command cli (Command.create sender name args text)
|
|
|
|
|
2008-03-06 13:54:16 -07:00
|
|
|
let reply cli num ?(args=[]) text =
|
2008-05-09 08:01:11 -06:00
|
|
|
write cli (Some !(Irc.name)) num (!(cli.nick) :: args) (Some text)
|
2008-02-28 22:00:24 -07:00
|
|
|
|
2008-03-18 17:20:44 -06:00
|
|
|
let handle_error cli iobuf message =
|
2008-03-06 21:30:49 -07:00
|
|
|
Hashtbl.remove by_nick !(cli.nick)
|
|
|
|
|
2008-03-04 21:38:19 -07:00
|
|
|
let handle_command cli iobuf cmd =
|
2008-03-06 13:54:16 -07:00
|
|
|
match (Command.as_tuple cmd) with
|
|
|
|
| (None, "OPER", [name; password], None) ->
|
|
|
|
()
|
|
|
|
| (None, "MODE", target :: args, None) ->
|
|
|
|
()
|
|
|
|
| (None, "SERVICE", [nickname; _; distribution; svctype; _], Some info) ->
|
|
|
|
()
|
2008-03-18 17:20:44 -06:00
|
|
|
| (None, "QUIT", [], None) ->
|
|
|
|
write cli (Some !(Irc.name)) "ERROR" [] (Some "So long");
|
|
|
|
Iobuf.close iobuf "No reason provided"
|
|
|
|
| (None, "QUIT", [], Some message) ->
|
|
|
|
write cli (Some !(Irc.name)) "ERROR" [] (Some "So long");
|
|
|
|
Iobuf.close iobuf message
|
2008-03-06 13:54:16 -07:00
|
|
|
| (None, "JOIN", ["0"], None) ->
|
|
|
|
()
|
|
|
|
| (None, "JOIN", [channels], None) ->
|
2008-05-10 14:19:14 -06:00
|
|
|
Channel.handle_command cli.iobuf (nuhost cli) cmd
|
2008-03-06 13:54:16 -07:00
|
|
|
| (None, "JOIN", [channels; keys], None) ->
|
|
|
|
()
|
|
|
|
| (None, "PART", [channels], message) ->
|
|
|
|
()
|
|
|
|
| (None, "TOPIC", [channel], None) ->
|
|
|
|
()
|
|
|
|
| (None, "TOPIC", [channel], Some topic) ->
|
|
|
|
()
|
|
|
|
| (None, "NAMES", [channels], None) ->
|
|
|
|
()
|
|
|
|
| (None, "LIST", [channels], None) ->
|
|
|
|
()
|
|
|
|
| (None, "INVITE", [nickname; channel], None) ->
|
|
|
|
()
|
|
|
|
| (None, "KICK", [channels; users], comment) ->
|
|
|
|
()
|
2008-03-07 12:00:40 -07:00
|
|
|
| (None, ("PRIVMSG" as command), [target], Some text)
|
|
|
|
| (None, ("NOTICE" as command), [target], Some text) ->
|
|
|
|
if Channel.is_channel_name target then
|
2008-05-10 14:19:14 -06:00
|
|
|
Channel.handle_command cli.iobuf (nuhost cli) cmd
|
2008-03-07 12:00:40 -07:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
let peer = lookup target in
|
2008-05-10 14:19:14 -06:00
|
|
|
write peer
|
|
|
|
(Some (Irc.string_of_nuhost (nuhost cli)))
|
|
|
|
command [target] (Some text)
|
2008-03-07 12:00:40 -07:00
|
|
|
with Not_found ->
|
|
|
|
reply cli "401" ~args:[target] "No such nick/channel"
|
|
|
|
end
|
2008-03-06 13:54:16 -07:00
|
|
|
| (None, "MOTD", [], None) ->
|
|
|
|
reply cli "422" "MOTD File is missing"
|
|
|
|
| (None, "LUSERS", [], None) ->
|
|
|
|
()
|
|
|
|
| (None, "VERSION", [], None) ->
|
|
|
|
reply cli "351" ~args:[Irc.version; !(Irc.name)] ""
|
|
|
|
| (None, "STATS", [], None) ->
|
|
|
|
()
|
|
|
|
| (None, "TIME", [], None) ->
|
|
|
|
let now = Unix.gmtime (Unix.time ()) in
|
|
|
|
let timestr =
|
|
|
|
Printf.sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ"
|
|
|
|
(now.Unix.tm_year + 1900)
|
|
|
|
now.Unix.tm_mday
|
|
|
|
(match now.Unix.tm_mon with
|
|
|
|
| 0 -> 12
|
|
|
|
| mon -> mon)
|
|
|
|
now.Unix.tm_hour
|
|
|
|
now.Unix.tm_min
|
|
|
|
now.Unix.tm_sec
|
|
|
|
in
|
|
|
|
reply cli "391" ~args:[!(Irc.name)] timestr;
|
|
|
|
| (None, "ADMIN", [], None) ->
|
|
|
|
()
|
|
|
|
| (None, "INFO", [], None) ->
|
2008-03-18 22:50:23 -06:00
|
|
|
reply cli "371" (Printf.sprintf "pgircd v%s" Irc.version);
|
|
|
|
reply cli "371" (Printf.sprintf "Running since %f" Irc.start_time);
|
|
|
|
reply cli "374" "End of INFO list"
|
2008-03-06 13:54:16 -07:00
|
|
|
| (None, "SERVLIST", [], None) ->
|
|
|
|
()
|
|
|
|
| (None, "SQUERY", [servicename], Some text) ->
|
|
|
|
()
|
|
|
|
| (None, "WHO", [], None) ->
|
|
|
|
()
|
|
|
|
| (None, "WHO", [mask], None) ->
|
|
|
|
()
|
|
|
|
| (None, "WHO", [mask; "o"], None) ->
|
|
|
|
()
|
|
|
|
| (None, "WHIOS", [masks], None) ->
|
|
|
|
()
|
|
|
|
| (None, "KILL", [nickname; comment], None) ->
|
|
|
|
()
|
2008-03-06 16:11:57 -07:00
|
|
|
| (None, "PING", [], Some text)
|
|
|
|
| (None, "PING", [text], None) ->
|
2008-03-18 17:20:44 -06:00
|
|
|
write cli (Some !(Irc.name)) "PONG" [!(Irc.name)] (Some text)
|
2008-03-06 16:11:57 -07:00
|
|
|
| (None, "PONG", [payload], None) ->
|
2008-03-18 22:50:23 -06:00
|
|
|
(* We do nothing. *)
|
2008-03-06 13:54:16 -07:00
|
|
|
()
|
|
|
|
| (None, "ERROR", [], Some message) ->
|
2008-03-18 22:50:23 -06:00
|
|
|
write cli (Some !(Irc.name)) "NOTICE" [!(cli.nick)] (Some "Bummer.")
|
2008-03-06 13:54:16 -07:00
|
|
|
| (None, "AWAY", [], None) ->
|
2008-03-18 22:50:23 -06:00
|
|
|
cli.away := None;
|
|
|
|
reply cli "305" "You are no longer marked as being away"
|
2008-03-06 13:54:16 -07:00
|
|
|
| (None, "AWAY", [], Some message) ->
|
2008-03-18 22:50:23 -06:00
|
|
|
cli.away := Some message;
|
|
|
|
reply cli "306" "You have been marked as being away"
|
2008-03-06 13:54:16 -07:00
|
|
|
| (None, "REHASH", [], None) ->
|
|
|
|
()
|
|
|
|
| (None, "WALLOPS", [], Some text) ->
|
|
|
|
()
|
|
|
|
| (None, "ISON", nicks, None) ->
|
2008-03-06 16:51:35 -07:00
|
|
|
let ison = List.filter (Hashtbl.mem by_nick) nicks in
|
|
|
|
reply cli "303" (String.concat " " ison)
|
2008-03-06 13:54:16 -07:00
|
|
|
| (_, name, _, _) ->
|
|
|
|
reply cli "421" ~args:[name] "Unknown or misconstructed command"
|
2008-02-27 22:50:27 -07:00
|
|
|
|
2008-03-04 21:38:19 -07:00
|
|
|
let set_nick cli nick =
|
|
|
|
if Hashtbl.mem by_nick nick then
|
|
|
|
raise (error "433" [nick] "Nickname in use");
|
|
|
|
Hashtbl.remove by_nick !(cli.nick);
|
|
|
|
Hashtbl.replace by_nick nick cli;
|
|
|
|
cli.nick := nick
|
|
|
|
|
2008-03-18 15:27:03 -06:00
|
|
|
let rec handle_command_prereg (nick, username, realname, password) iobuf cmd =
|
2008-02-27 22:50:27 -07:00
|
|
|
(* Handle a command during the login phase *)
|
2008-03-04 21:38:19 -07:00
|
|
|
let acc =
|
2008-03-02 21:30:37 -07:00
|
|
|
match (Command.as_tuple cmd) with
|
2008-03-18 15:27:03 -06:00
|
|
|
| (None, "PASS", [password'], None) ->
|
|
|
|
(nick, username, realname, Some password')
|
|
|
|
| (None, "USER", [username'; _; _], Some realname') ->
|
|
|
|
(nick, Some username', Some (Irc.truncate realname' 40), password)
|
|
|
|
| (None, "NICK", [nick'], None) ->
|
|
|
|
(Some nick', username, realname, password)
|
2008-03-02 21:30:37 -07:00
|
|
|
| _ ->
|
2008-03-04 21:38:19 -07:00
|
|
|
Iobuf.write iobuf (Command.create
|
2008-03-06 13:54:16 -07:00
|
|
|
(Some !(Irc.name))
|
|
|
|
"451" ["*"]
|
|
|
|
(Some "Register first."));
|
2008-03-18 15:27:03 -06:00
|
|
|
(nick, username, realname, password)
|
2008-02-29 17:16:00 -07:00
|
|
|
in
|
2008-03-04 21:38:19 -07:00
|
|
|
let welcome cli =
|
|
|
|
try
|
|
|
|
set_nick cli !(cli.nick);
|
|
|
|
reply cli "001" "Welcome to IRC.";
|
|
|
|
reply cli "002" ("I am " ^ !(Irc.name) ^
|
2008-03-06 13:54:16 -07:00
|
|
|
" Running version " ^ Irc.version);
|
2008-03-18 22:50:23 -06:00
|
|
|
reply cli "003" ("This server was created " ^
|
|
|
|
(string_of_float Irc.start_time));
|
2008-03-04 21:38:19 -07:00
|
|
|
reply cli "004" (!(Irc.name) ^
|
2008-03-06 13:54:16 -07:00
|
|
|
" " ^ Irc.version ^
|
|
|
|
" " ^ modes ^
|
|
|
|
" " ^ Channel.modes);
|
2008-03-18 17:20:44 -06:00
|
|
|
Iobuf.bind iobuf (handle_command cli) (handle_error cli)
|
2008-03-04 21:38:19 -07:00
|
|
|
with Error cmd ->
|
|
|
|
Iobuf.write iobuf cmd
|
2008-02-28 22:00:24 -07:00
|
|
|
in
|
2008-03-04 21:38:19 -07:00
|
|
|
match acc with
|
|
|
|
| (Some nick, Some username, Some realname, None) ->
|
2008-03-06 13:54:16 -07:00
|
|
|
welcome {iobuf = iobuf;
|
|
|
|
nick = ref nick;
|
2008-03-18 22:50:23 -06:00
|
|
|
away = ref None;
|
2008-03-06 13:54:16 -07:00
|
|
|
username = username;
|
|
|
|
realname = realname}
|
2008-03-04 21:38:19 -07:00
|
|
|
| (Some nick, Some username, Some realname, Some password) ->
|
2008-03-06 13:54:16 -07:00
|
|
|
Iobuf.write iobuf (Command.create
|
|
|
|
(Some !(Irc.name))
|
|
|
|
"NOTICE" ["AUTH"]
|
|
|
|
(Some "*** Authentication unimplemented"));
|
|
|
|
welcome {iobuf = iobuf;
|
|
|
|
nick = ref nick;
|
2008-03-18 22:50:23 -06:00
|
|
|
away = ref None;
|
2008-03-06 13:54:16 -07:00
|
|
|
username = username;
|
|
|
|
realname = realname}
|
2008-03-04 21:38:19 -07:00
|
|
|
| _ ->
|
2008-03-18 17:20:44 -06:00
|
|
|
Iobuf.bind iobuf (handle_command_prereg acc) (fun _ _ -> ())
|
2008-03-04 21:38:19 -07:00
|
|
|
|
2008-03-18 15:27:03 -06:00
|
|
|
let handle_connection d fd addr =
|
2008-03-18 17:20:44 -06:00
|
|
|
let handle_command = handle_command_prereg (None, None, None, None) in
|
|
|
|
Iobuf.create d fd addr handle_command (fun _ _ -> ())
|
2008-03-06 21:30:49 -07:00
|
|
|
|