(no commit message)
[sfa.git] / sfatables / commands / Add.py
1 import os, time
2 import libxml2
3 from sfatables.command import Command
4 from sfatables.globals import *
5
6 class Add(Command):
7     options = [('-A','--add')]
8     help = 'Add a rule to a chain'
9     matches = True
10     targets = True
11
12     def __init__(self):
13         return
14
15     def getnextfilename(self,type,chain):
16         dir = sfatables_config + chain;
17         last_rule_number = 1
18
19         for file in os.walk(dir):
20             if (file.startswith('sfatables-')):
21                 number_str = file.split('-')[1]
22                 number = int(number_str)
23                 if (number>last_rule_number):
24                     last_rule_number = number
25
26         return "sfatables-%d-%s"%(last_rule_number,type)
27
28
29
30     def call(self, command_options, match_options, target_options):
31         import pdb
32         filename = match_dir + "/"+match_options.match_name+".xml"
33         xmldoc = libxml2.parseFile(filename)
34     
35         p = xmldoc.xpathNewContext()
36
37         supplied_arguments = match_options.arguments
38         for option in supplied_arguments:
39             option_name = option['name']
40             option_value = getattr(match_options,option_name)
41
42             if (hasattr(match_options,option_name)):
43                 context = p.xpathEval("//rule/argument[name='%s']"%option_name)
44                 if (not context):
45                     raise Exception('Unknown option %s for match %s'%(option,option['name']))
46                 else:
47                     # Add the value of option
48                     valueNode = libxml2.newNode('value')
49                     valueNode.addContent(option_value)
50                     context[0].addChild(valueNode)
51
52         pdb.set_trace()
53         chain = command_options.args[0]
54         filename = self.getnextfilename('match',chain)
55         xmldoc.saveFile(match_dir + '/' + chain + '/' + filename)
56         p.xpathFreeContext()
57         xmldoc.freeDoc()
58
59
60         return True
61