~ubuntu-branches/ubuntu/precise/gozerbot/precise

« back to all changes in this revision

Viewing changes to gozerplugs/plugs/dig.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2008-06-02 19:26:39 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080602192639-3rn65nx4q1sgd6sy
Tags: 0.8.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# plugs/dig.py
2
 
#
3
 
#
4
 
#
5
 
 
6
 
""" do the dig """
7
 
 
8
 
__copyright__ = 'this file is in the public domain'
9
 
 
10
 
from gozerbot.generic import gozerpopen
11
 
from gozerbot.commands import cmnds
12
 
from gozerbot.examples import examples
13
 
from gozerbot.plughelp import plughelp
14
 
 
15
 
plughelp.add('dig', 'run a dig command')
16
 
 
17
 
def handle_dig(bot, ievent):
18
 
    """ dig <domain> <querytype> .. show dig response """
19
 
    if len(ievent.args) < 2:
20
 
        ievent.missing('<domain> <querytype>')
21
 
        return
22
 
    args = []
23
 
    userargs = []
24
 
    args.append('dig')
25
 
    userargs.append(ievent.args[0])
26
 
    userargs.append(ievent.args[1])
27
 
    try:
28
 
        proces = gozerpopen(args, userargs)
29
 
    except Exception, ex:
30
 
        ievent.reply('error running popen: %s' % str(ex))
31
 
        return
32
 
    data = proces.fromchild.readlines()
33
 
    returncode = proces.close()
34
 
    if returncode != 0:
35
 
        ievent.reply('error running dig')
36
 
        return
37
 
    result = []
38
 
    doit = 0
39
 
    for i in data:
40
 
        # search output for answer section
41
 
        if i.find('ANSWER SECTION') != -1:
42
 
            doit = 1
43
 
            continue
44
 
        i = i.strip()
45
 
        if i and doit:
46
 
            i = i.replace('\t',' ')
47
 
            result.append(i)
48
 
            if i.find(';;') != -1:
49
 
                doit = 0
50
 
    if result:
51
 
        ievent.reply(' ==> '.join(result))
52
 
    else:
53
 
        ievent.reply('no result')
54
 
 
55
 
cmnds.add('dig', handle_dig, 'USER')
56
 
examples.add('dig', 'dig <hostname> <querytype> .. show dig output', \
57
 
'dig gozerbot.org A')