(* IRC functions Copyright (C) 2009 BODIN Antoine This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . *) type server = {nick:string ref; pass:string ref; user:(string * string * string * string) ref; cin:in_channel; cout:out_channel; sock:Unix.sockaddr} let sendstr (host:server) str = output_string host.cout (str^"\r\n"); flush host.cout let nick (host:server) nickname = host.nick:=nickname; sendstr host ("NICK "^nickname) let pass (host:server) password = host.pass:=password; if password <> "" then sendstr host ("PASS "^password) else () let user (host:server) (username,dn,sn,name) = host.user:=(username,dn,sn,name); sendstr host ("USER "^username^" "^dn^" "^sn^" "^name) let pong (host:server) (a,b) = sendstr host ("PONG "^a^(match b with "" -> "" | _ -> (" "^b))) let join (host:server) ?(key="") chan = sendstr host ("JOIN "^chan^" "^key) let privmsg (host:server) (target,msg) = sendstr host ("PRIVMSG "^target^" :"^msg) let names (host:server) chan = sendstr host ("NAMES "^chan) let quit (host:server) msg = sendstr host ("QUIT "^msg) let connect ?(nickname="guest") ?(password="") ?(username="guest") ?(dom="localhost") ?(ds="(null)") ?(name="unknown") ?(port=6667) dn = let sockaddr = Unix.ADDR_INET ((Unix.gethostbyname dn).Unix.h_addr_list.(0),port) in let (sin,sout) = Unix.open_connection sockaddr in let host = {nick = ref ""; pass = ref ""; user = ref ("","","",""); sock = sockaddr; cin = sin; cout = sout} in pass host password; nick host nickname; user host (username,dom,ds,name); host let disconnect (s:server) = Unix.shutdown_connection s.cin type event = PING of (string*string) | MODE of string | PART of (string*string*string) | QUIT of (string*string) | JOIN of (string*string) | NICK of (string*string) | TOPIC of (string*string*string) | PRIVMSG of (string*string*string) | UNKNOWN of string let evstr s = Str.string_after s 1 let exname s = List.nth (Str.split_delim (Str.regexp "!") s) 0 let event (host:server) = let cmd = input_line host.cin in match(Str.bounded_split (Str.regexp " ") cmd 4)with "PING"::a::b::[] -> PING (a,b) | "PING"::a::[] -> PING (a,"") | _::"MODE"::_::a::[] -> MODE (evstr a) | a::"NICK"::b::[] -> NICK (evstr (exname a),evstr b) | a::"PART"::b::c::[] -> PART (evstr (exname a), b,evstr c) | a::"QUIT"::b::[] -> QUIT (evstr (exname a), evstr b) | a::"JOIN"::b::_ -> JOIN (evstr (exname a), evstr b) | a::"TOPIC"::b::c::[] -> TOPIC (evstr (exname a), b,evstr c) | a::"PRIVMSG"::b::c::[] -> PRIVMSG (evstr (exname a), b,evstr c) | _ -> UNKNOWN cmd