forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_hostip.ml
64 lines (59 loc) · 1.81 KB
/
plugin_hostip.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(*
* (c) 2006-2010 Anastasia Gornostaeva
*)
open Unix
open Netconversion
open Common
open Hooks
open Plugin_command
open Http_suck
let rex = Pcre.regexp "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"
let hostip xmpp env kind jid_from text =
let ip =
try
let h = gethostbyname text in
Some (string_of_inet_addr h.h_addr_list.(0))
with Not_found ->
if Pcre.pmatch ~rex text then
Some text
else
None
in
match ip with
| None ->
env.env_message xmpp kind jid_from
(Lang.get_msg env.env_lang "plugin_hostip_bad_syntax" []);
| Some ip ->
let url = Printf.sprintf
"http://api.hostip.info/get_html.php?ip=%s&position=true"
ip in
let callback data =
let response =
match data with
| OK (_media, charset, content) -> (
try
let enc =
match charset with
| None -> `Enc_iso88591
| Some v -> encoding_of_string v
in
let resp =
if enc <> `Enc_utf8 then
convert ~in_enc:enc ~out_enc:`Enc_utf8 content
else
content
in
"\n" ^ resp
with _exn ->
Lang.get_msg env.env_lang "conversation_trouble" []
)
| Exception _ ->
Lang.get_msg env.env_lang "plugin_hostip_failed" []
in
env.env_message xmpp kind jid_from response
in
Http_suck.http_get url callback
let plugin opts =
()
let _ =
Plugin.add_plugin "hostip" plugin