(* copyright 2004 - David MENTRE This program is under GNU GPL license *) open Messages_aux open Messages_clnt open Format let string_of_return_code code = match code with | v when v = rt_ok -> "Ok" | v when v = rt_generic_client_error -> "Generic client error" | v when v = rt_not_enough_rights -> "Not enough rights" | v when v = rt_already_exists -> "Already exists" | v when v = rt_not_found -> "Not found" | v when v = rt_request_too_much_ids -> "Request too much ids" | v when v = rt_generic_server_error -> "Generic server error" | v when v = rt_vote_choice_not_found -> "Vote choice not found" | v when v = rt_duplicate_vote_choice -> "Duplicate vote choice" | v when v = rt_anonymous_cannot_vote -> "Anonymous cannot vote" | _ -> failwith "string_of_return_code: should never happen" let update_tags_hash h user_msg client cookie = Hashtbl.clear h; try let ret = Demexp.V1.max_tag_id client cookie in if ret.max_tag_id_rc <> rt_ok then failwith (Printf.sprintf "unable to get max_tag_id (%s)" (string_of_return_code ret.max_tag_id_rc)); let number = 1 (* fixme: should be Rtypes.int_of_uint4 max_number_ids Keep 1 for debug *) in let rec get_some_tags base max_id = if base <= max_id then ( let ret = Demexp.V1.tag_info client (cookie, base, number) in if ret.tag_info_rc <> rt_ok then failwith (Printf.sprintf "unable to get info for tag %d to %d (%s)" base (base + number) (string_of_return_code ret.tag_info_rc)); Array.iter (fun elt -> Hashtbl.add h elt.a_tag_id elt.a_tag_label) ret.tag_info; get_some_tags (base + number) max_id ) in get_some_tags 0 ret.max_tag_id with Failure str -> user_msg str let display_questions client cookie user_msg = let add_question q = if q.q_info_status = public then ( let q_tags = Demexp.V1.get_question_tags client (cookie, q.q_id) in printf "question: (%d)%s\n" q.q_id q.q_desc; printf " |author: %s\n" q.q_info_author; printf " |limit_date: %s\n" q.q_info_limit_date; printf " |status: %s\n" (if q.q_info_status = public then "public" else "tagging_only"); let print_response i r = printf " |response: (%d)%s (by %s) [%s]\n" i r.r_info_desc r.r_info_author r.r_info_link in Array.iteri print_response q.q_info_responses; printf " |winning_responses: "; Array.iter (fun i -> printf "%d " i) q.q_info_elected_responses; printf "\n" ) in try let ret = Demexp.V1.max_question_id client cookie in if ret.max_question_id_rc <> rt_ok then failwith (Printf.sprintf "unable to get max_question_id (%s)" (string_of_return_code ret.max_question_id_rc)); let number = 1 (* fixme: should be Rtypes.int_of_uint4 max_number_ids Keep 1 for debug *) in let rec get_some_questions base max_id = if base <= max_id then ( let ret = Demexp.V1.question_info client (cookie, base, number) in if ret.question_info_rc <> rt_ok then failwith (Printf.sprintf "unable to get info for question %d to %d (%s)" base (base + number) (string_of_return_code ret.question_info_rc)); Array.iter add_question ret.question_info; get_some_questions (base + number) max_id ) in get_some_questions 0 ret.max_question_id with Failure str -> user_msg str let dislay_users client cookie user_msg = let max_id = let ret = Demexp.V1.max_participant_id client cookie in if ret.max_participant_id_rc = rt_ok then ret.max_participant_id else ( user_msg ("cannot get max_participant_id: " ^ (string_of_return_code ret.max_participant_id_rc)); -1 ) in let print_one info = printf "user: (%d) %s [" info.info_id info.info_login; Array.iter (fun s -> printf "%s," s) info.info_groups; printf "]\n" in let number = 1 (* fixme: should be Rtypes.int_of_uint4 max_number_ids Keep 1 for debug *) in let rec print_users i = if i <= max_id then ( let ret = Demexp.V1.participant_info client (cookie, i, number) in if ret.participant_info_rc = rt_ok then ( Array.iter print_one ret.participant_info; print_users (i + number) ) else user_msg ("cannot get participant_info: " ^ (string_of_return_code ret.participant_info_rc)) ) in print_users 0 let connect_to_server user_msg server port login pass = user_msg "Connecting to server..."; let client = Demexp.V1.create_client (Rpc_client.Inet (server, port)) Rpc.Tcp in user_msg (Printf.sprintf "Connected. Login with login \"%s\"..." login); let cookie = Demexp.V1.login client (login, pass) in user_msg "Logged in."; (client, cookie) let _ = if Array.length Sys.argv <> 5 then ( printf "usage: demexp-pump server port login password\n"; exit 1 ); let server = Sys.argv.(1) and port = int_of_string Sys.argv.(2) and login = Sys.argv.(3) and passwd = Sys.argv.(4) in let user_msg str = eprintf "address@hidden" str in let client, cookie = connect_to_server user_msg server port login passwd in user_msg "Getting tags..."; let tags = Hashtbl.create 3 in update_tags_hash tags user_msg client cookie; Hashtbl.iter (fun id label -> printf "tag: (%d)%s\n" id label) tags; user_msg "Getting questions..."; display_questions client cookie user_msg; user_msg "Getting users..."; dislay_users client cookie user_msg; Demexp.V1.goodbye client cookie