Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 192 lines (175 sloc) 5.909 kB
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
1 (*
08aac6a @ermine Remove email
authored
2 * (c) 2004-2010 Anastasia Gornostaeva
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
3 *)
cc68359 Added copyright
Anastasia Gornostaeva authored
4
19debed Big rework, added many new plugins
Anastasia Gornostaeva authored
5 open Pcre
f95240c Adapted to use new xml parser
Anastasia Gornostaeva authored
6 open Light_xml
19debed Big rework, added many new plugins
Anastasia Gornostaeva authored
7 open Common
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
8 open Hooks
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
9 open Plugin_command
34c9f2c initial import
Anastasia Gornostaeva authored
10
11 exception DictError of string
12
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
13 let dictd_server = ""
aea977c bugfixes
Anastasia Gornostaeva authored
14
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
15 let dictd_port = 1
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
16
34c9f2c initial import
Anastasia Gornostaeva authored
17 let connect server port =
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
18 let inet_addr =
19 try Unix.inet_addr_of_string server with Failure("inet_addr_of_string") ->
20 (Unix.gethostbyname server).Unix.h_addr_list.(0) in
21 let sock_addr = Unix.ADDR_INET (inet_addr, port) in
22 try
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
23 let pair = Unix.open_connection sock_addr in
ff64596 * sulci/logger.ml: Removed
Anastasia Gornostaeva authored
24 log#info "plugin_dict.ml: %s:%d connected" server port;
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
25 pair
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
26 with
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
27 | Unix.Unix_error ((Unix.EINTR|Unix.EAGAIN), "connect", _) ->
28 let rec cycle () =
ff64596 * sulci/logger.ml: Removed
Anastasia Gornostaeva authored
29 log#info "plugin_dict.ml: attempting to connect [%s][%d]"
30 server port;
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
31 try
32 let pair = Unix.open_connection sock_addr in
ff64596 * sulci/logger.ml: Removed
Anastasia Gornostaeva authored
33 log#info "plugin_dict.ml: again: %s:%d" server port;
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
34 pair
35 with
36 | Unix.Unix_error ((Unix.EINTR|Unix.EAGAIN), "connect",_) ->
37 cycle ()
38 | _ ->
39 raise (DictError "unable to connect")
40 in
41 cycle ()
42 | _ ->
43 raise (DictError "unable to connect")
44
34c9f2c initial import
Anastasia Gornostaeva authored
45 exception NoStatus
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
46
19debed Big rework, added many new plugins
Anastasia Gornostaeva authored
47 let status = regexp "([0-9][0-9][0-9]) (.*)"
34c9f2c initial import
Anastasia Gornostaeva authored
48
49 let get_status in_dict =
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
50 let line = input_line in_dict in
51 try
19debed Big rework, added many new plugins
Anastasia Gornostaeva authored
52 let r = Pcre.exec ~rex:status line in
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
53 Pcre.get_substring r 1, Pcre.get_substring r 2
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
54 with Not_found ->
aea977c bugfixes
Anastasia Gornostaeva authored
55 raise
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
56 (DictError "Hmm.. It seems i was connectod to inproper dictd server")
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
57
34c9f2c initial import
Anastasia Gornostaeva authored
58 let read_text in_dict =
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
59 let rec cycle acc =
60 let line = input_line in_dict in
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
61 if line = ".\r" then
62 acc
63 else
64 cycle (acc ^ line ^ "\n")
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
65 in
66 cycle ""
67
19debed Big rework, added many new plugins
Anastasia Gornostaeva authored
68 let cmdlist = ["-list"]
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
69
34c9f2c initial import
Anastasia Gornostaeva authored
70 let process_cmd_dict cmd =
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
71 if List.mem cmd cmdlist then
72 let in_dict, out_dict = connect dictd_server dictd_port in
73 let reply =
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
74 (match get_status in_dict with
75 | "220", _ ->
76 (match cmd with
77 | "-list" ->
78 output_string out_dict "SHOW DB\r\n";
79 | _ -> raise (DictError "unknown command")
80 );
81 flush out_dict;
82 (match get_status in_dict with
83 | "110", rsp ->
84 let piece = read_text in_dict in
85 let _ = get_status in_dict in
86 rsp ^ "\n" ^ piece
87 | "554", rsp ->
88 rsp
89 | code, rsp ->
90 raise (DictError ("Unknown status code: " ^
91 code ^ " " ^ rsp))
92 );
93 | _ ->
94 raise (DictError "Cannot work!")
95 ) in
96 close_in in_dict;
97 reply
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
98 else
99 "Unknown command"
100
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
101 let process_dict db word env =
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
102 let in_dict, out_dict = connect dictd_server dictd_port in
103 let reply = match get_status in_dict with
104 | "220", _ ->
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
105 output_string out_dict
106 (Printf.sprintf "DEFINE %s %s\r\n" db word);
107 flush out_dict;
108 (match get_status in_dict with
0be804c removed compiler warnings and setuped full ocamlbuild support
Anastasia Gornostaeva authored
109 | "550", _err ->
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
110 Lang.get_msg env.env_lang "plugin_dict_db_not_found" []
0be804c removed compiler warnings and setuped full ocamlbuild support
Anastasia Gornostaeva authored
111 | "552", _err ->
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
112 Lang.get_msg env.env_lang "plugin_dict_word_not_found" []
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
113 | "150", rsp ->
114 let rec cycle acc =
115 match get_status in_dict with
116 | "151", text ->
117 let piece = read_text in_dict in
118 cycle (acc ^ "\n" ^ text ^ "\n" ^ piece)
119 | "250", _ ->
120 acc
121 | code, txt ->
122 raise (DictError ("unknown dict status " ^
123 code ^ " " ^ txt))
124 in
125 cycle rsp;
126 | "151", rsp ->
127 let piece = read_text in_dict in
128 let _ = get_status in_dict in
129 rsp ^ "\n" ^ piece
130 | code, rsp ->
131 raise (DictError ("Unknown status code " ^
132 code ^ " " ^ rsp))
133 )
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
134 | _ -> "Cannot work!"
135 in
136 close_in in_dict;
137 reply
138
aea977c bugfixes
Anastasia Gornostaeva authored
139 let rex1 = Pcre.regexp ~iflags:(cflags [`UTF8]) "([^\\s]+)[\\s]*$"
140 let rex2 = Pcre.regexp "(!|\\*|[a-z]+)\\s+([a-z]+)"
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
141
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
142 let dict xmpp env kind jid_from text =
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
143 if text = "" then
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
144 env.env_message xmpp kind jid_from (Lang.get_msg env.env_lang "plugin_dict_invalid_syntax" [])
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
145 else
146 if String.get text 0 = '-' then
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
147 let proc () =
148 let response = try
149 process_cmd_dict text
150 with DictError error -> error
151 in
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
152 env.env_message xmpp kind jid_from response
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
153 in
154 ignore (Thread.create proc ())
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
155 else
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
156 try
157 let r = Pcre.exec ~rex:rex2 text in
158 let db = Pcre.get_substring r 1 in
159 let word = Pcre.get_substring r 2 in
160 let proc () =
161 let response = try
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
162 process_dict db word env
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
163 with (DictError error) -> error
164 in
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
165 env.env_message xmpp kind jid_from response
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
166 in
167 ignore (Thread.create proc ())
168 with Not_found ->
169 try
170 let r = Pcre.exec ~rex:rex1 text in
171 let word = Pcre.get_substring r 1 in
172 let proc () =
173 let response = try
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
174 process_dict "*" word env
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
175 with (DictError error) -> error
176 in
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
177 env.env_message xmpp kind jid_from response
556b48a * sulci/*: Updated to use 'jid' module
Anastasia Gornostaeva authored
178 in
179 ignore (Thread.create proc ())
180 with Not_found ->
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
181 env.env_message xmpp kind jid_from
d166fe2 * sulci/*: Now MUC related API splitted from main API to have a
Anastasia Gornostaeva authored
182 (Lang.get_msg env.env_lang "plugin_dict_invalid_syntax" [])
3b6e8ec * sulci/common.ml: Now function make_msg controls a message length and
Anastasia Gornostaeva authored
183
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
184 let plugin opts =
f80f254 More or less working scheme for hooks, not completed
Anastasia Gornostaeva authored
185 add_for_token
186 (fun _opts xmpp ->
187 add_commands xmpp [("dict", dict)] opts
188 )
ec66144 Ontermediate commit
Anastasia Gornostaeva authored
189
34c9f2c initial import
Anastasia Gornostaeva authored
190 let _ =
f80f254 More or less working scheme for hooks, not completed
Anastasia Gornostaeva authored
191 Plugin.add_plugin "dict" plugin
Something went wrong with that request. Please try again.