1# -*- coding: utf-8 -*-
2'''
3 calc.py: DNS-based czech-english dictionary
4
5 Copyright (c) 2009, Zdenek Vasicek (vasicek AT fit.vutbr.cz)
6                     Marek Vavrusa  (xvavru00 AT stud.fit.vutbr.cz)
7
8 This software is open source.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13
14    * Redistributions of source code must retain the above copyright notice,
15      this list of conditions and the following disclaimer.
16
17    * Redistributions in binary form must reproduce the above copyright notice,
18      this list of conditions and the following disclaimer in the documentation
19      and/or other materials provided with the distribution.
20
21    * Neither the name of the organization nor the names of its
22      contributors may be used to endorse or promote products derived from this
23      software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
36'''
37import os
38cz_dict = {}
39en_dict = {}
40
41def init(id, cfg):
42   log_info("pythonmod: dict init")
43   f = open("examples/dict_data.txt", "r")
44   try:
45      for line in f:
46         if line.startswith('#'):
47            continue
48         itm = line.split("\t", 3)
49         if len(itm) < 2:
50            continue
51         en,cs = itm[0:2]
52
53         if not (cs in cz_dict):
54            cz_dict[cs] = [en]     # [cs] = en
55         else:
56            cz_dict[cs].append(en) # [cs] = en
57
58         if not (en in en_dict):
59            en_dict[en] = [cs]     # [en] = cs
60         else:
61            en_dict[en].append(cs) # [en] = cs
62
63   finally:
64      f.close()
65   return True
66
67def deinit(id):
68   log_info("pythonmod: dict deinit")
69   return True
70
71def operate(id, event, qstate, qdata):
72    if (event == MODULE_EVENT_NEW) or (event == MODULE_EVENT_PASS):
73
74       if qstate.qinfo.qname_str.endswith("._dict_.cz."):
75
76         aword = ' '.join(qstate.qinfo.qname_list[0:-4])
77         adict = qstate.qinfo.qname_list[-4]
78
79         log_info("pythonmod: dictionary look up; word:%s dict:%s" % (aword,adict))
80
81         words = []
82         if (adict == "en") and (aword in en_dict):
83            words = en_dict[aword] # EN -> CS
84         if (adict == "cs") and (aword in cz_dict):
85            words = cz_dict[aword] # CS -> EN
86
87         if len(words) and ((qstate.qinfo.qtype == RR_TYPE_TXT) or (qstate.qinfo.qtype == RR_TYPE_ANY)):
88
89            msg = DNSMessage(qstate.qinfo.qname_str, RR_TYPE_TXT, RR_CLASS_IN, PKT_RD | PKT_RA | PKT_AA)
90            for w in words:
91                msg.answer.append("%s 300 IN TXT \"%s\"" % (qstate.qinfo.qname_str,w.replace("\"","\\\"")))
92
93            if not msg.set_return_msg(qstate):
94               qstate.ext_state[id] = MODULE_ERROR
95               return True
96
97            qstate.return_rcode = RCODE_NOERROR
98            qstate.ext_state[id] = MODULE_FINISHED
99            return True
100
101         else:
102            qstate.return_rcode = RCODE_SERVFAIL
103            qstate.ext_state[id] = MODULE_FINISHED
104            return True
105
106       else: #Pass on the unknown query to the iterator
107         qstate.ext_state[id] = MODULE_WAIT_MODULE
108         return True
109
110    elif event == MODULE_EVENT_MODDONE: #the iterator has finished
111         #we don't need modify result
112         qstate.ext_state[id] = MODULE_FINISHED
113         return True
114
115    log_err("pythonmod: Unknown event")
116    qstate.ext_state[id] = MODULE_ERROR
117    return True
118
119def inform_super(id, qstate, superqstate, qdata):
120   return True
121
122