2 (*************************************************************************)
3 (* This program is free software: you can redistribute it and/or modify *)
4 (* it under the terms of the version 3 of the GNU General Public License *)
5 (* as published by the Free Software Foundation. *)
7 (* This program is distributed in the hope that it will be useful, but *)
8 (* WITHOUT ANY WARRANTY; without even the implied warranty of *)
9 (* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
10 (* General Public License for more details. *)
12 (* You should have received a copy of the GNU General Public License *)
13 (* along with this program. If not, see <http://www.gnu.org/licenses/>. *)
15 (* Written and (C) by Francois Fleuret *)
16 (* Contact <francois.fleuret@idiap.ch> for comments & bug reports *)
17 (*************************************************************************)
21 (* I removed all the dcc stuff between v0.25 and v0.5 because it was a
22 biiiig pile of crap. Feel free to write it ! *)
24 let version = "IRCML v0.5";;
26 let term_bold = ref "
\e[1m"
27 and term_underline = ref "
\e[4m"
28 and term_inverse = ref "
\e[7m"
29 and term_reset = ref "
\e[m";;
31 (*****************************************************************************)
33 (*****************************************************************************)
35 let keys h = Hashtbl.fold (fun key data accu -> key :: accu) h []
37 let verbose = ref false;;
39 (* Return the element after the given one, or the first of the list if
40 the given one is the last *)
44 let rec next_rec x = function
46 | h::t -> if h = x then List.hd t else next_rec x t
47 in try next_rec x l with Failure(e) -> List.hd l;;
49 let string_of_size size =
50 if size > 1048576 then (string_of_int (size / 1048576))^"Mb"
51 else if size > 1024 then (string_of_int (size / 1024))^"Kb"
52 else (string_of_int size) ^ "b";;
54 let duration_of_seconds duration =
55 (if duration >= 3600 then (string_of_int (duration/3600))^"h" else "") ^
56 (if (duration mod 3600) >= 60 then (string_of_int ((duration mod 3600)/60))^"min" else "") ^
57 (if (duration mod 60) > 0 then (string_of_int (duration mod 60))^"s" else "");;
59 (*****************************************************************************)
60 (* The global variables *)
61 (*****************************************************************************)
63 type user_status = { mutable operator: bool }
64 let string_of_user = fun u -> (if u.operator then "@" else "")
66 type channel = { name: string; people: (string, user_status) Hashtbl.t ; modes: (char, string) Hashtbl.t };;
72 and desired_nick = ref "b0z0"
73 and desired_server = ref "localhost"
74 and desired_port = ref 6667
76 and current_nick = ref ""
77 and (current_modes: (char, string) Hashtbl.t) = Hashtbl.create 10
78 and default_channel = ref ""
80 and login = ref "c4mlp0w4"
81 and name = ref ("Using " ^ version ^ ", the light IRC client in CAML")
83 and alive = ref true;;
85 (* Print something with a 'header character' à la sirc *)
86 (* '<' Leaving channel (part, kick, signoff, etc.) *)
87 (* '>' Joining a channel *)
90 (* 'X' Internal error, should never meet one *)
91 let printh h s = output_string Pervasives.stdout
94 "E" -> (!term_bold ^ "*" ^ h ^ "*" ^ " " ^ s ^ !term_reset ^ "\n")
95 | _ -> (!term_bold ^ "*" ^ h ^ "*" ^ !term_reset ^ " " ^ s ^ "\n")
98 (* Simply print something in the console *)
99 let print s = output_string Pervasives.stdout (s ^ "\n");;
101 type ssfe_cmd = Status of string | Tabulable of string;;
103 let current_status = ref "";;
108 Status(s) -> if not (s = !current_status) then (current_status := s; print ("`#ssfe#s" ^ s))
109 | Tabulable(s) -> print ("`#ssfe#t" ^ s)
112 (*****************************************************************************)
113 (* Command line parameter parsing *)
114 (*****************************************************************************)
116 Arg.parse [ ("-nick", Arg.String(fun n -> desired_nick := n), "Set the desired nick");
117 ("-server", Arg.String(fun s -> desired_server := s), "Set the desired server");
118 ("-port", Arg.Int(fun p -> desired_port := p), "Set the desired port");
119 ("-login", Arg.String(fun s -> login := s), "Set the login");
120 ("-name", Arg.String(fun s -> name := s), "Set the name") ]
121 (fun s -> ()) "Unknown option";;
123 (*****************************************************************************)
124 (* The connection with the server *)
125 (*****************************************************************************)
128 Established of Unix.file_descr
131 let last_server_msg = ref 0;;
133 let server_connection = ref Broken;;
135 (* This is the list of file descriptors active for reading. Each one
136 corresponds to a reader which will be called when something is
137 readable and a exception-handler, called if the connection is lost,
140 type fd_machine = { reader: unit -> unit; error_handler: string -> unit };;
142 let (fd_active: (Unix.file_descr, fd_machine) Hashtbl.t) = Hashtbl.create 10;;
144 (*****************************************************************************)
145 (* Handling of the channel list *)
146 (*****************************************************************************)
148 let (joined_channels: (string, channel) Hashtbl.t) = Hashtbl.create 10;;
150 let find_joined c = Hashtbl.find joined_channels (String.uppercase c)
151 and mem_joined c = Hashtbl.mem joined_channels (String.uppercase c)
152 and set_joined c x = Hashtbl.replace joined_channels (String.uppercase c) x
153 and remove_joined c = Hashtbl.remove joined_channels (String.uppercase c)
154 and same_channel c1 c2 = (String.uppercase c1) = (String.uppercase c2);;
156 let part_channel channel =
157 remove_joined channel;
158 if !default_channel = channel
159 then match (keys joined_channels) with
160 [] -> default_channel := ""; if not !ssfe then printh "I" "Not in a channel anymore"
161 | c::t -> default_channel := c; printh "I" ("Talking to " ^ !default_channel ^ " now");;
163 (*****************************************************************************)
164 (* Takes a string and returns an array of strings corresponding to the *)
165 (* tokens in the IRC protocole syntax. I.e. tokens are separated by white *)
166 (* spaces except the tail which is after a ':' *)
167 (*****************************************************************************)
169 let (irc_tokenize: string -> string array) = fun s ->
170 let l = (String.length s) in
171 let rec irc_tokenize s n =
172 if String.get s n == ':' then [String.sub s (n + 1) (l - n - 2)] else
174 let m = String.index_from s n ' ' in
175 let h = String.sub s n (m - n) in
176 h::irc_tokenize s (m+1)
178 Not_found -> if n < l then [String.sub s n (l - n - 1)] else []
180 (* Ignore the prefix if there is one *)
181 if (String.get s 0) == ':' then
182 let n = (String.index s ' ') in Array.of_list ((String.sub s 1 (n - 1))::(irc_tokenize s (n+1)))
183 else Array.of_list (""::(irc_tokenize s 0));;
185 (* Function to split the "nick!login@host" *)
187 let (split_prefix: string -> string * string * string) = fun prefix ->
188 let n1 = (String.index prefix '!') in
189 let n2 = (String.index_from prefix n1 '@') in
190 (String.sub prefix 0 n1,
191 String.sub prefix (n1 + 1) (n2 - (n1 + 1)),
192 String.sub prefix (n2 + 1) ((String.length prefix) - (n2 + 1)));;
194 (*****************************************************************************)
196 (*****************************************************************************)
198 let (cmd_aliases: (string, string) Hashtbl.t) = Hashtbl.create 100;;
200 List.iter (fun (a, b) -> Hashtbl.replace cmd_aliases a b) [
201 ("J", "JOIN"); ("M", "MSG"); ("K", "KICK"); ("L", "CHANINFO");
202 ("N", "NICK"); ("Q", "QUIT"); ("T", "TOPIC"); ("W", "WHOIS");
203 ("DC", "DISCONNECT"); ("S", "SERVER"); ("H", "HELP")
206 (*****************************************************************************)
207 (* The managers will be filled later *)
208 (*****************************************************************************)
210 let (protocole_managers: (string, string array -> unit) Hashtbl.t) = Hashtbl.create 100;;
211 let (cmd_managers: (string, string * ((unit -> string) -> (unit -> string) -> unit)) Hashtbl.t) = Hashtbl.create 100;;
212 let (ctcp_managers: (string, (unit -> string) -> (unit -> string) -> string -> string -> unit) Hashtbl.t) = Hashtbl.create 10;;
214 (*****************************************************************************)
215 (* The routines handling lines from the server or the user *)
216 (*****************************************************************************)
218 let print_unknown_protocole = fun tokens ->
219 let buffer = Buffer.create 100 in
220 Buffer.add_string buffer "Unknown protocole";
221 for i = 0 to ((Array.length tokens) - 1) do
222 Buffer.add_string buffer (" " ^ (string_of_int i) ^ ":(" ^ tokens.(i) ^ ")");
224 printh "X" (Buffer.contents buffer);;
226 let server_line_handler = fun line ->
227 if !verbose then (print_string ("SERVER [" ^ line ^ "]\n"); flush stdout);
228 last_server_msg := int_of_float (Unix.time ());
229 let tokens = (irc_tokenize line) in
231 let f = Hashtbl.find protocole_managers tokens.(1) in
232 try f tokens with Not_found -> printh "E" ("Protocole " ^ tokens.(1) ^ " raised Not_found!")
234 Not_found -> print_unknown_protocole tokens;;
236 let kill_server_connection error_message =
237 match !server_connection with
240 Hashtbl.remove fd_active fd;
241 server_connection := Broken;
242 printh "E" ("Connection lost (" ^ error_message ^ ")");
243 default_channel := "";
244 Hashtbl.clear joined_channels
248 let tell_server line =
249 match !server_connection with
253 if (Unix.write fd line 0 (String.length line)) <= 0 then kill_server_connection ("write error")
255 Unix.Unix_error(e, fname, fparam) -> kill_server_connection (fname ^ ": " ^ (Unix.error_message e))
257 | Broken -> printh "E" "No connection";;
259 let establish_connection () =
260 match !server_connection with
261 Established(_) -> printh "E" "Connection already established (try /DISCONNECT first)"
263 printh "I" ("Attempting connection to " ^ !desired_server ^
264 ":" ^ (string_of_int !desired_port) ^
265 " with nick " ^ !desired_nick);
267 let fd = Connection.connect !desired_server !desired_port in
269 match Unix.getsockname fd with
270 Unix.ADDR_INET(ai, _) -> my_host := Connection.dot_to_number (Unix.string_of_inet_addr ai)
271 | _ -> printh "E" "Internal error #0";
273 server_connection := Established(fd);
274 Hashtbl.replace fd_active fd { reader = linizer fd server_line_handler; error_handler = kill_server_connection };
275 tell_server ("USER " ^ !login ^ " caml rulez :" ^ !name ^ "\n");
276 tell_server ("NICK " ^ !desired_nick ^ "\n");
277 last_server_msg := int_of_float (Unix.time ());
278 printh "I" ("Connection established, waiting for server checkings.");
280 Network_error(e) -> printh "E" ("Can not connect to host: " ^ e);;
282 let stdin_line_handler = fun line ->
283 (*print_string ("STDIN [" ^ line ^ "]\n"); flush stdout;*)
284 if String.length line > 0 then
285 match String.get line 0 with
288 let (cl_token, cl_tail) = tokenizer line ' ' in
289 let cmd = String.uppercase (cl_token ()) in let cmd = String.sub cmd 1 ((String.length cmd) - 1) in
290 let cmd = try Hashtbl.find cmd_aliases cmd with Not_found -> cmd in
292 let (_, f) = (Hashtbl.find cmd_managers cmd) in
297 Not_found -> printh "E" ("Command " ^ cmd ^ " raised Not_found!")
300 Not_found -> printh "E" ("Unknown command '" ^ cmd ^ "'");
305 let (cl_token, cl_tail) = tokenizer line ' ' in
306 match cl_token () with
310 printh "I" "You are using ssfe";
312 term_underline := "
\1f";
313 term_inverse := "
\16";
320 if !default_channel = ""
321 then printh "E" "Not in a channel"
324 print ("<" ^ !current_nick ^ "> " ^ line);
325 tell_server ("PRIVMSG " ^ !default_channel ^ " :" ^ line ^ "\n")
328 let console_reader = linizer Unix.stdin stdin_line_handler
329 and console_error = fun error_message -> printh "E" ("Stdin error (" ^ error_message ^ ")"); exit 1 in
330 Hashtbl.replace fd_active Unix.stdin { reader = console_reader; error_handler = console_error };;
332 (*****************************************************************************)
333 (* Functions to handle scripts *)
334 (*****************************************************************************)
336 Hashtbl.add cmd_managers
339 "<module>: Loads the given bytecode object",
342 let module_name = token () in
343 printh "I" ("loading module " ^ module_name);
345 Dynlink.loadfile(module_name)
347 Dynlink.Error error -> printh "E" ("Module loading error (" ^ (Dynlink.error_message error) ^ ")");
348 | Sys_error error_message -> printh "E" ("Module loading error (" ^ error_message ^ ")");
350 Not_found -> printh "E" "Missing argument"
353 (*****************************************************************************)
354 (* Functions to handle the various user commands *)
355 (*****************************************************************************)
357 (* Each manager gets two (unit -> string), the first one to get tokens
358 one after another and the second one to get the tail when required *)
360 Hashtbl.add cmd_managers
369 (fun nick user -> print ("on " ^ name ^ " : [" ^ (string_of_user user) ^ "] " ^ nick))
375 Hashtbl.add cmd_managers
378 "[<cmd>]: Shows the help for the given command, or for all of them",
381 let cmd = String.uppercase (token ()) in
383 let (h, f) = (Hashtbl.find cmd_managers cmd) in
384 printh "H" ("/" ^ cmd ^ " " ^ h)
386 Not_found -> printh "H" ("Unknown command " ^ cmd)
388 Not_found -> Hashtbl.iter (fun cmd (help, f) -> (printh "H" ("/" ^ cmd ^ " " ^ help))) cmd_managers
391 Hashtbl.add cmd_managers
394 "[<action description>]: Describes an action in the default channel",
396 if !default_channel = ""
397 then printh "E" "Not in a channel"
398 else let msg = try (tail ()) with Not_found -> "" in
399 tell_server ("PRIVMSG " ^ !default_channel ^ " :
\ 1ACTION " ^ msg ^ "
\ 1\n");
400 print ("* " ^ !current_nick ^ " " ^ msg)
403 Hashtbl.add cmd_managers
406 "[<message>]: Sets yourself away",
408 try tell_server ("AWAY :" ^ (tail ()) ^ "\n")
409 with Not_found -> tell_server "AWAY\n";
412 Hashtbl.add cmd_managers
415 "<nick|#channel> <message>: Sends a message to a user, or a channel",
418 let dest = (token ()) and msg = (tail ()) in
419 tell_ssfe (Tabulable ("/msg " ^ dest ^ " "));
420 match String.get dest 0 with
423 print ("<" ^ !current_nick ^ (if same_channel !default_channel dest then "" else "/" ^ dest) ^ "> " ^ msg);
424 tell_server ("PRIVMSG " ^ dest ^ " :" ^ msg ^ "\n")
429 print (">" ^ dest ^ "< " ^ msg);
430 tell_server ("PRIVMSG " ^ dest ^ " :" ^ msg ^ "\n")
433 with Not_found -> printh "E" "Missing parameter"
436 Hashtbl.add cmd_managers
439 "<nick|#channel> <ctcp string>: Sends a CTCP (string quoted between ^A) to a user or a channel",
442 let dest = (token ()) and msg = (tail ()) in
443 tell_ssfe (Tabulable ("/msg " ^ dest ^ " "));
444 printh "I" ("Sending a CTCP " ^ msg ^ " to " ^ dest);
445 tell_server ("PRIVMSG " ^ dest ^ " :
\ 1" ^ msg ^ "
\ 1\n")
446 with Not_found -> printh "E" "Missing parameter"
449 Hashtbl.add cmd_managers
452 "<nick|#channel> <message>: Sends a notice to a user or a channel",
455 let dest = (token ()) and msg = (tail ()) in
456 print ("-> -" ^ dest ^ "- " ^ msg); tell_server ("PRIVMSG " ^ dest ^ " :" ^ msg ^ "\n")
457 with Not_found -> printh "E" "Missing parameter"
460 Hashtbl.add cmd_managers
463 "[<nick>]: Requests information about a user",
465 let nick = try token () with Not_found -> !current_nick in
467 then printh "E" "Missing argument"
468 else tell_server ("WHOIS " ^ nick ^ "\n")
471 Hashtbl.add cmd_managers
474 "[<nick>]: Requests information about a user on his server (get the idle)",
476 let nick = try token () with Not_found -> !current_nick in
478 then printh "E" "Missing argument"
479 else tell_server ("WHOIS " ^ nick ^ " " ^ nick ^ "\n")
482 Hashtbl.add cmd_managers
485 "[<server>[:<port>]]: Establishes a connection to a new server",
489 let (stoken, stail) = tokenizer (token ()) ':' in
490 desired_server := stoken ();
491 desired_port := int_of_string (stoken ())
494 | Failure(_) -> printh "E" "Syntax error"
496 establish_connection ()
499 Hashtbl.add cmd_managers
502 ": Selects the next joined channel as default",
505 default_channel := (find_joined (circular_succ (String.uppercase !default_channel) (keys joined_channels))).name;
506 if not !ssfe then printh "I" ("Talking to " ^ !default_channel ^ " now")
508 Not_found -> printh "E" "You have to join a channel first"
511 Hashtbl.add cmd_managers
514 "[<#channel> [<key>]]: Joins a channel or tells what is the default channel",
517 let channel = token () in let chan = match String.get channel 0 with '#' | '&' -> channel | _ -> "#" ^ channel in
518 let key = try " " ^ (token ()) with Not_found -> "" in
519 if mem_joined channel
520 then (default_channel := chan; if not !ssfe then printh "I" ("Talking to " ^ chan ^ " now"))
521 else tell_server ("JOIN " ^ chan ^ key ^ "\n")
523 if !default_channel = ""
524 then printh "I" "You are not in a channel"
525 else printh "I" ("You current channel is " ^ !default_channel)
528 Hashtbl.add cmd_managers
531 "[<topic>]: Sets or requests the topic in the current channel",
533 if !default_channel = ""
534 then printh "E" "Not in a channel"
536 try let topic = tail () in tell_server ("TOPIC " ^ !default_channel ^ " :" ^ topic ^ "\n")
537 with Not_found -> tell_server ("TOPIC " ^ !default_channel ^ "\n")
540 Hashtbl.add cmd_managers
543 "[<mode change>]: Changes mode or shows the mode of the current channel",
545 let chan = try token () with Not_found -> !default_channel in
547 then printh "E" "Need to specify a channel"
549 let chan = match String.get chan 0 with '#' | '&' -> chan | _ -> "#" ^ chan in
550 try tell_server ("MODE " ^ chan ^ " " ^ (tail ()) ^ "\n")
551 with Not_found -> tell_server ("MODE " ^ chan ^ "\n")
554 Hashtbl.add cmd_managers
557 "[<#channel>] <nick> [<comment>]: Kicks the given user from the given (or current) channel",
559 if !default_channel = ""
560 then printh "E" "Not in a channel"
563 let (channel, nick) = match String.get t 0 with '#' | '&' -> (t, token ()) | _ -> (!default_channel, t)
564 and comment = try tail () with Not_found -> "B1G d0Rk5 SuCk M00z b4LLz" in
565 tell_server ("KICK " ^ !default_channel ^ " " ^ nick ^ " :" ^ comment ^ "\n")
566 with Not_found -> printh "E" "Missing parameter"
569 Hashtbl.add cmd_managers
572 "[<#channel>]: Leaves the current or the specified channel",
574 let chan = try token () with Not_found -> !default_channel in
576 then printh "E" "Need to specify a channel"
577 else let chan = match String.get chan 0 with '#' | '&' -> chan | _ -> "#" ^ chan in
578 tell_server ("PART " ^ chan ^ "\n")
581 Hashtbl.add cmd_managers
584 "[<#channel>]: Gets the user list for the given channel",
586 let chan = try token () with Not_found -> !default_channel in
587 if chan = "" then printh "E" "Need to specify a channel"
589 let chan = match String.get chan 0 with '#' | '&' -> chan | _ -> "#" ^ chan in
590 tell_server ("LIST " ^ chan ^ "\n");
591 tell_server ("NAMES " ^ chan ^ "\n")
594 Hashtbl.add cmd_managers
597 "[<nick>]: Changes or requests the current nick",
599 try let nick = token () in tell_server ("NICK " ^ nick ^ "\n")
600 with Not_found -> printh "I" ("You current nick is " ^ !current_nick)
603 Hashtbl.add cmd_managers
606 "[<message>]: Signoffs on the current server with the given quit message",
608 let msg = try tail () with Not_found -> (version ^ ", the light IRC client in CAML") in
609 tell_server ("QUIT :" ^ msg ^ "\n");
612 Hashtbl.add cmd_managers
615 "[<message>]: Signoffs on the current server if connected, and terminates operations",
617 if !server_connection != Broken then begin
618 let msg = try tail () with Not_found -> (version ^ ", the light IRC client in CAML") in
619 tell_server ("QUIT :" ^ msg ^ "\n");
624 Hashtbl.add cmd_managers
627 "<line>: Sends the line to the server, as it is",
629 try tell_server (tail () ^ "\n")
630 with Not_found -> printh "E" "Missing parameter"
633 Hashtbl.add cmd_managers
636 ": Set the verbose mode (everything form the server will be printed)",
638 verbose := not !verbose;
639 if !verbose then printh "I" "Verbose mode on" else printh "I" "Verbose mode off"
642 (*****************************************************************************)
643 (* Function to handle the various CTCPs *)
644 (*****************************************************************************)
646 Hashtbl.add ctcp_managers
649 fun token tail from dest -> tell_server ("NOTICE " ^ from ^ " :" ^ version ^ ", the light IRC client in CAML\n")
652 Hashtbl.add ctcp_managers
655 fun token tail from dest -> try print ("* " ^ from ^ " " ^ (tail())) with Not_found -> print ("* " ^ from)
658 (*****************************************************************************)
659 (* Functions to handle the mode changes (that part of the protocole sucks) *)
660 (*****************************************************************************)
662 let rec flatten_modes = fun polarity mode_string mode_indice param_strings param_indice ->
663 if mode_indice < (String.length mode_string)
665 match (String.get mode_string mode_indice) with
666 '+' -> flatten_modes 1 mode_string (mode_indice+1) param_strings param_indice
667 | '-' -> flatten_modes (-1) mode_string (mode_indice+1) param_strings param_indice
668 | 'k' | 'o' | 'v' | 'b' as c ->
669 (c, polarity, param_strings.(param_indice))::(flatten_modes polarity mode_string (mode_indice+1) param_strings (param_indice+1))
670 | 'm' | 'i' | 's' | 't' | 'n' as c ->
671 (c, polarity, "")::(flatten_modes polarity mode_string (mode_indice+1) param_strings param_indice)
674 then ('l', polarity, param_strings.(param_indice))::(flatten_modes polarity mode_string (mode_indice+1) param_strings (param_indice+1))
675 else ('l', polarity, "")::(flatten_modes polarity mode_string (mode_indice+1) param_strings param_indice)
676 | other -> (other, polarity, "")::(flatten_modes polarity mode_string (mode_indice+1) param_strings param_indice)
681 let rec string_of_modelist = function
684 (if l > 0 then " +" else " -") ^
686 (if p = "" then "" else " " ^ p) ^
687 (string_of_modelist t)
689 let set_modes channel l =
690 let chan = find_joined channel in
694 | ('b', pol, pattern) -> ()
695 | ('v', pol, nick) -> ()
696 | ('o', pol, nick) -> (Hashtbl.find chan.people nick).operator <- (pol > 0)
697 | (c, 1, param) -> Hashtbl.replace chan.modes c param
698 | (c, -1, param) -> Hashtbl.remove chan.modes c
703 let get_status = fun () ->
704 " [" ^ version ^ "] " ^
705 (try string_of_user (Hashtbl.find (find_joined !default_channel).people !current_nick)
706 with Not_found -> "")
709 (if Hashtbl.mem current_modes 'i' then "i" else "") ^
710 (if Hashtbl.mem current_modes 'O' then "i" else "") ^
711 (if Hashtbl.mem current_modes 'o' then "i" else "") ^
712 (if Hashtbl.mem current_modes 'w' then "w" else "") ^
713 (if Hashtbl.mem current_modes 'a' then "a" else "") ^
715 if !default_channel = "" then ""
717 " on " ^ !default_channel ^
718 let modes = (find_joined !default_channel).modes in
720 (if Hashtbl.mem modes 'm' then "m" else "") ^
721 (if Hashtbl.mem modes 'i' then "i" else "") ^
722 (if Hashtbl.mem modes 's' then "s" else "") ^
723 (if Hashtbl.mem modes 'n' then "n" else "") ^
724 (if Hashtbl.mem modes 't' then "t" else "") ^
725 (try " key=" ^ (Hashtbl.find modes 'k') with Not_found -> "") ^
726 (try " limit=" ^ (Hashtbl.find modes 'l') with Not_found -> "") ^
730 (*****************************************************************************)
731 (* Function to handle the various commands received from the server *)
732 (*****************************************************************************)
734 (* This one just write a certain token as it is *)
736 let print_information i = fun tokens -> printh "I" tokens.(i);;
738 (* Here is the serious stuff ... each entry is a fun able to
739 interprete the tokens given as parameters *)
741 Hashtbl.add protocole_managers "001"
743 fun tokens -> current_nick := tokens.(2);
744 printh "I" ("You are now connected as " ^ tokens.(2))
747 Hashtbl.add protocole_managers "PING"
749 fun tokens -> tell_server ("PONG " ^ tokens.(2) ^ "\n")
752 Hashtbl.add protocole_managers "JOIN"
755 let (nick, login, host) = split_prefix tokens.(0) in
756 if nick = !current_nick
758 printh ">" ("You have joined " ^ tokens.(2));
759 set_joined tokens.(2) { name=tokens.(2); people= Hashtbl.create 10; modes = Hashtbl.create 10 };
760 default_channel := tokens.(2);
761 tell_server ("MODE " ^ tokens.(2) ^ "\n")
763 else printh ">" (nick ^ " (" ^ login ^ "@" ^ host ^ ") has joined " ^ tokens.(2));
764 Hashtbl.replace (find_joined tokens.(2)).people nick { operator = false }
767 Hashtbl.add protocole_managers "PART"
770 let (nick, login, host) = split_prefix tokens.(0) in
771 if nick = !current_nick
773 printh "<" ("You have left " ^ tokens.(2));
774 part_channel tokens.(2)
777 printh "<" (nick ^ " (" ^ login ^ "@" ^ host ^ ") has left " ^ tokens.(2));
778 Hashtbl.remove (find_joined tokens.(2)).people nick;
782 Hashtbl.add protocole_managers "QUIT"
784 fun tokens -> let (nick, login, host) = split_prefix tokens.(0) in
787 (fun name data s -> if Hashtbl.mem data.people nick then s ^ " " ^ data.name else s)
788 joined_channels "" in
789 printh "<" ("Signoff: " ^ nick ^ " (" ^ tokens.(2) ^ ") from" ^ channels);
790 Hashtbl.iter (fun name data -> Hashtbl.remove data.people nick) joined_channels;
793 Hashtbl.add protocole_managers "KICK"
796 let (nick, login, host) = split_prefix tokens.(0) in
797 if tokens.(3) = !current_nick
799 printh "<" ("You have been kicked from " ^ tokens.(2) ^ " by " ^ nick ^ " (" ^ tokens.(4)^ ")");
800 part_channel tokens.(2)
802 else printh "<" (tokens.(3) ^ " has been kicked from " ^ tokens.(2) ^ " by " ^ nick ^ " (" ^ tokens.(4) ^ ")");
803 Hashtbl.remove (find_joined tokens.(2)).people tokens.(3);
806 Hashtbl.add protocole_managers "NICK"
808 fun tokens -> let (nick, login, host) = split_prefix tokens.(0) in
809 if nick = !current_nick
811 printh "I" ("You are now known as " ^ tokens.(2));
812 current_nick := tokens.(2)
814 else printh "I" (nick ^ " is now known as " ^ tokens.(2));
817 let u = Hashtbl.find data.people nick in
819 Hashtbl.remove data.people nick;
820 Hashtbl.add data.people tokens.(2) u
824 Hashtbl.iter rename joined_channels;
827 Hashtbl.add protocole_managers "MODE"
830 let (nick, login, host) = try split_prefix tokens.(0) with Not_found -> (tokens.(0), "", "") in
831 let modes = flatten_modes 1 tokens.(3) 0 tokens 4 in
832 if match String.get tokens.(2) 0 with '#' | '&' -> true | _ -> false
835 set_modes (String.uppercase tokens.(2)) modes;
836 printh "I" ("Mode change by " ^ nick ^ " on channel " ^ tokens.(2) ^ (string_of_modelist modes))
841 (function (c, 1, x) -> Hashtbl.replace current_modes c x | (c, -1, x) -> Hashtbl.remove current_modes c | _ -> ())
843 printh "I" ("Mode change" ^ (string_of_modelist modes) ^ " by " ^ nick ^ " for user " ^ tokens.(2))
847 Hashtbl.add protocole_managers "TOPIC"
850 let (nick, login, host) = split_prefix tokens.(0) in
851 printh "I" (nick ^ " has changed the topic on " ^ tokens.(2) ^ " to \"" ^ tokens.(3) ^ "\"")
854 Hashtbl.add protocole_managers "ERROR"
856 fun tokens -> printh "E" tokens.(2)
859 Hashtbl.add protocole_managers "PRIVMSG"
862 let (nick, login, host) = split_prefix tokens.(0) in
864 let n1 = String.index tokens.(3) '
\ 1' in
865 let n2 = String.index_from tokens.(3) (n1 + 1) '
\ 1' in
866 let ctcp_line = (String.sub tokens.(3) (n1 + 1) (n2 - n1 - 1)) in
867 let (ctcp_token, ctcp_tail) = tokenizer ctcp_line ' ' in
868 let cmd = ctcp_token () in
870 (Hashtbl.find ctcp_managers cmd) ctcp_token ctcp_tail nick tokens.(2)
872 Not_found -> printh "E" ("Unknown CTCP [" ^ ctcp_line ^ "] from " ^ nick);
873 tell_server ("NOTICE " ^ nick ^ " :Unknown CTCP " ^ cmd ^ "'\n")
877 if (String.get tokens.(2) 0) = '#'
878 then print ("<" ^ nick ^ (if same_channel tokens.(2) !default_channel then "" else ("/" ^ tokens.(2))) ^ "> " ^
881 tell_ssfe (Tabulable ("/msg " ^ nick ^ " "));
882 print ("[" ^ nick ^ "] " ^ tokens.(3))
887 Hashtbl.add protocole_managers "NOTICE"
890 let (nick, login, host) = split_prefix tokens.(0) in
891 print ("-" ^ nick ^ (if tokens.(2) = !current_nick then "" else "/" ^ tokens.(2)) ^ "- " ^ tokens.(3))
894 Hashtbl.add protocole_managers "301"
896 fun tokens -> printh "I" (tokens.(3) ^ " is away (" ^ tokens.(4) ^ ")")
899 Hashtbl.add protocole_managers "311"
901 fun tokens -> printh "I" (tokens.(3) ^ " is " ^ tokens.(4) ^ "@" ^ tokens.(5) ^ " (" ^ tokens.(7) ^ ")")
904 Hashtbl.add protocole_managers "312"
906 fun tokens -> printh "I" ("on IRC via server " ^ tokens.(4) ^ " (" ^ tokens.(5) ^ ")")
909 Hashtbl.add protocole_managers "317"
912 let duration = int_of_string tokens.(4) in
913 printh "I" (tokens.(3) ^ " has been idle for " ^ (duration_of_seconds duration))
916 Hashtbl.add protocole_managers "319"
918 fun tokens -> printh "I" ("on channels: " ^ tokens.(4))
921 Hashtbl.add protocole_managers "324"
923 fun tokens -> set_modes tokens.(3) (flatten_modes 1 tokens.(4) 0 tokens 5)
926 Hashtbl.add protocole_managers "322"
928 fun tokens -> printh "I" ("There are " ^ tokens.(4) ^ " users on " ^ tokens.(3))
931 Hashtbl.add protocole_managers "331"
933 fun tokens -> printh "I" ("No topic is set on " ^ tokens.(3))
936 Hashtbl.add protocole_managers "333"
938 fun tokens -> printh "I" ("Topic for " ^ tokens.(3) ^ " set by " ^ tokens.(2))
943 fun s -> Hashtbl.add protocole_managers s (print_information 3)
945 [ "002"; "003"; "005"; "020"; "042";
946 "251"; "252"; "253"; "255"; "265"; "266";
950 Hashtbl.add protocole_managers "305"
952 fun tokens -> printh "I" tokens.(3); Hashtbl.remove current_modes 'a'
955 Hashtbl.add protocole_managers "306"
957 fun tokens -> printh "I" tokens.(3); Hashtbl.replace current_modes 'a' ""
960 Hashtbl.add protocole_managers "403" (print_information 4);
964 fun s -> Hashtbl.add protocole_managers s (fun tokens -> ())
968 Hashtbl.add protocole_managers "004"
970 fun tokens -> printh "I" (tokens.(3) ^ " " ^ tokens.(4) ^ " " ^ tokens.(5) ^ " " ^ tokens.(6))
973 Hashtbl.add protocole_managers "254"
975 fun tokens -> printh "I" ("There are " ^ tokens.(3) ^ " " ^ tokens.(4))
978 Hashtbl.add protocole_managers "332"
980 fun tokens -> printh "I" ("Topic for " ^ tokens.(3) ^ " is '" ^ tokens.(4) ^ "'")
983 Hashtbl.add protocole_managers "353"
986 printh "I" ("Users on " ^ tokens.(4) ^ ": " ^ tokens.(5));
987 let (n_token, n_tail) = tokenizer tokens.(5) ' '
988 and people_table = (find_joined tokens.(4)).people in
989 let rec add_nick = fun () ->
991 (let nick = n_token () in match String.get nick 0 with
992 '@' -> Hashtbl.replace people_table (String.sub nick 1 ((String.length nick) - 1)) { operator = true }
993 | _ -> Hashtbl.replace people_table nick { operator = false });
999 Hashtbl.add protocole_managers "366"
1002 (*let users = Hashtbl.fold (fun nick status s -> " " ^ nick ^ s) (find_joined tokens.(3)).people "" in
1003 printh "I" ("Users on " ^ tokens.(3) ^ ":" ^ users)*)
1006 Hashtbl.add protocole_managers "401"
1008 fun tokens -> printh "E" ("Can not find " ^ tokens.(3) ^ " on IRC")
1011 Hashtbl.add protocole_managers "433"
1015 then printh "E" ("Nickname " ^ tokens.(3) ^ " already in used, type /nick <nick> to choose another one")
1016 else printh "E" ("Nickname " ^ tokens.(3) ^ " already in used, you keep " ^ tokens.(2))
1019 Hashtbl.add protocole_managers "441"
1021 fun tokens -> printh "E" (tokens.(3) ^ " is not on channel " ^ tokens.(4))
1024 Hashtbl.add protocole_managers "461"
1026 fun tokens -> printh "E" ("Not enough parameters for " ^ tokens.(3))
1029 Hashtbl.add protocole_managers "472"
1031 fun tokens -> printh "E" ("Mode '" ^ tokens.(3) ^ "' " ^ tokens.(4))
1034 Hashtbl.add protocole_managers "474"
1036 fun tokens -> printh "E" ("Can not join " ^ tokens.(3) ^ " (banned)")
1039 Hashtbl.add protocole_managers "475"
1041 fun tokens -> printh "E" (tokens.(3) ^ ": " ^ tokens.(4))
1044 Hashtbl.add protocole_managers "482"
1046 fun tokens -> printh "E" ("You are not an operator on " ^ tokens.(3))
1049 (*****************************************************************************)
1050 (* The module initialization *)
1051 (*****************************************************************************)
1056 Dynlink.add_interfaces
1057 [ "Ircml"; "Buffer"; "List"; "Arg"; "String"; "Digest"; "Dynlink"; "Hashtbl"; "Sys";
1058 "Connection"; "Unix"; "Pervasives"; "Array" ]
1059 [ Sys.getcwd(); "/home/fleuret/local/lib/ocaml" ];
1062 Dynlink.Error error -> printh "E" ("Module loading error (" ^ (Dynlink.error_message error) ^ ")");;
1064 try ignore (Unix.tcgetattr Unix.stdout) with Unix.Unix_error _ -> print "`#ssfe#i";;
1066 (*****************************************************************************)
1068 (*****************************************************************************)
1070 print "-----------------------------------------------------------------------------";
1071 print (" " ^ version ^ ", the IRC client in OCAML");
1072 print " Written and (c) by <francois.fleuret@epfl.ch>";
1073 print "-----------------------------------------------------------------------------";
1075 establish_connection ();
1081 let (reading, _, _) = Unix.select (keys fd_active) [] [] 1.0 in
1086 let h = Hashtbl.find fd_active fd in
1087 try h.reader () with Network_error(e) -> h.error_handler e
1091 tell_ssfe (Status (get_status ()))
1095 printh "I" "You'll be back, ho yes ... you'll be back"