Changeset 5745
- Timestamp:
- 07/26/10 22:41:55 (3 years ago)
- Location:
- network-scanner/branches/NSEFacilitator/umitNSEFacilitator
- Files:
-
- 2 modified
-
ScriptEditor.py (modified) (13 diffs)
-
luaParser.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
network-scanner/branches/NSEFacilitator/umitNSEFacilitator/ScriptEditor.py
r2248 r5745 36 36 from umitCore.Paths import Path, check_access 37 37 from umitGUI.BugReport import BugReport 38 from luaParser import LuaParser 38 39 39 40 # try to use gtksourceview if available … … 77 78 DEFAULT_SELECTED_TEXT_COLOR = gtk.gdk.color_parse("#ffffffffffff") 78 79 DEFAULT_SELECTION_COLOR = gtk.gdk.color_parse("#000000009c9c") 80 DEFAULT_TEMPLATE_PATH = os.path.expanduser("~/.umit/nsetemplates") 81 82 def get_templates(): 83 """ 84 Return the list of user templates in nsetemplates folder. 85 """ 86 templates = [] 87 for template in os.listdir(DEFAULT_TEMPLATE_PATH): 88 ext = template.split(".")[-1] 89 if ext == 'nse': 90 templates.append(template) 91 return templates 79 92 80 93 class ScriptEditorPageManager(object): … … 482 495 f.close() 483 496 self.text_buffer.set_modified(False) 497 498 def save_as_template(self): 499 """ 500 Save a script as a template. 501 """ 502 file_chooser = SaveNSEFileChooserDialog(_("Save As Template...")) 503 file_chooser.set_current_folder(DEFAULT_TEMPLATE_PATH) 504 if not self.is_untitled(): 505 file_chooser.set_filename(self.scriptname) 506 else: 507 file_chooser.set_current_name(self.untitled_name) 508 response = file_chooser.run() 509 filename = file_chooser.get_filename() 510 file_chooser.destroy() 511 if not response == gtk.RESPONSE_OK: 512 return 513 self.set_name(filename) 514 self.save(True) 484 515 485 516 class ScriptEditorWindow(ScriptWindow): … … 550 581 _('Save _As...'), "<Shift><Control>S", 551 582 _('Save the current script file with the different name'), self._save_as_cb), 583 584 ('Save As Template', gtk.STOCK_SAVE_AS, 585 _('Save As Template'), None, 586 _('Save the current script file as template'), self._save_as_template_cb), 552 587 553 588 ('Close', gtk.STOCK_CLOSE, … … 650 685 <menuitem action='Save'/> 651 686 <menuitem action='Save As'/> 687 <menuitem action='Save As Template'/> 652 688 <separator/> 653 689 <menuitem action='Close'/> … … 811 847 page.save_as() 812 848 self.update_title() 849 850 def _save_as_template_cb(self, p): 851 page = self.get_current_page() 852 if not page: 853 return 854 page.save_as_template() 855 self.update_title() 813 856 814 857 def _quit_cb(self, p): … … 999 1042 return boxes 1000 1043 1001 self.vbox.pack_start(HIGSectionLabel(_("General")), False, False) 1044 scrolled_window = gtk.ScrolledWindow() 1045 scrolled_window.set_border_width(5) 1046 scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS) 1047 self.vbox.pack_start(scrolled_window, True, True, 0) 1048 scrolled_window.show() 1049 1050 self.vbox1 = gtk.VBox() 1051 self.vbox1.pack_start(HIGSectionLabel(_("General")), False, False) 1002 1052 table = HIGTable(5, 2) 1003 1053 self.id_entry = create_entry(table, 0, "<b>ID:</b>") … … 1013 1063 hbox.pack_start(self.host_rule) 1014 1064 table.attach(hbox, 1, 2, 4, 5) 1015 self.vbox.pack_start(HIGSpacer(table), False, False) 1016 1017 self.vbox.pack_start(HIGSectionLabel(_("Categories")), False, False) 1065 self.vbox1.pack_start(HIGSpacer(table), False, False) 1066 self.vbox1.pack_start(HIGSpacer()) 1067 1068 self.vbox1.pack_start(HIGSectionLabel(_("Categories")), False, False) 1018 1069 table = HIGTable(4, 2) 1019 1070 self.categories = create_checkboxes(table, … … 1026 1077 "version", 1027 1078 "vulnerability"]) 1028 self.vbox.pack_start(HIGSpacer(table), False, False) 1029 1030 self.vbox.pack_start(HIGSectionLabel(_("Description")), False, False) 1079 self.vbox1.pack_start(HIGSpacer(table), False, False) 1080 self.vbox1.pack_start(HIGSpacer()) 1081 1082 self.vbox1.pack_start(HIGSectionLabel(_("User Template")), False, False) 1083 table = HIGTable(1, 2) 1084 self.templates = gtk.combo_box_new_text() 1085 self.templates.append_text("") 1086 for template in get_templates(): 1087 self.templates.append_text(template) 1088 self.templates.connect('changed', self._template_changed_cb) 1089 table.attach(self.templates, 0, 1, 0, 1) 1090 lbl = HIGEntryLabel("") 1091 table.attach(lbl, 1, 2, 0, 1) 1092 self.vbox1.pack_start(table, False, False) 1093 self.vbox1.pack_start(HIGSpacer(table), False, False) 1094 1095 self.vbox1.pack_start(HIGSectionLabel(_("Description")), False, False) 1031 1096 self.desc_view = HIGTextView() 1032 self.vbox.pack_start(HIGSpacer(scroll_wrap(self.desc_view))) 1097 self.vbox1.pack_start(HIGSpacer(scroll_wrap(self.desc_view))) 1098 1099 scrolled_window.add_with_viewport(self.vbox1) 1100 self.vbox1.show() 1101 1033 1102 self.connect("response", self._response_cb) 1034 1103 self.show_all() … … 1049 1118 %(rule)s 1050 1119 1120 %(content)s 1121 1122 ''' 1123 1124 default_content = '''\ 1051 1125 action = function(host, port) 1052 1126 \t-- main testing actions here 1053 1127 \treturn 1054 1128 end 1055 1056 1129 ''' 1057 1130 … … 1061 1134 version = self.version_entry.get_text() 1062 1135 license = self.license_entry.get_text() 1063 1064 if self.port_rule.get_active(): 1065 rule = '''portrule = function(host, port) 1066 \tdecision = true 1067 \t-- port choosing here 1068 \treturn decision 1069 end''' 1136 template = self.templates.get_active_text() 1137 1138 if template == "" or template==None: 1139 content = self.default_content 1140 if self.port_rule.get_active(): 1141 rule = '''portrule = function(host, port) 1142 \tdecision = true 1143 \t-- port choosing here 1144 \treturn decision 1145 end''' 1146 else: 1147 rule = '''hostrule = function(host, port) 1148 \tdecision = true 1149 \t-- host choosing here 1150 \treturn decision 1151 end''' 1070 1152 else: 1071 rule = '''hostrule = function(host, port) 1072 \tdecision = true 1073 \t-- host choosing here 1074 \treturn decision 1075 end''' 1153 rule = '' 1154 content = self.content 1155 pass 1156 1076 1157 categories = [] 1077 1158 for name, check in self.categories.items(): … … 1101 1182 os.path.join(Path.docs_dir, "script_editor.html#wizard"), new=2) 1102 1183 self.stop_emission("response") 1184 1185 def _template_changed_cb(self, combobox): 1186 """ 1187 Update fields with template content 1188 """ 1189 if combobox.get_active_text()!='': 1190 template = DEFAULT_TEMPLATE_PATH + '/' + combobox.get_active_text() 1191 parser = LuaParser(template) 1192 d = {} 1193 d.update(parser.attr) 1194 1195 id = d.get('id', '') 1196 author = d.get('author', '') 1197 version = d.get('version', '') 1198 license = d.get('license', '') 1199 description = d.get('description', '') 1200 categories = d.get('categories', '') 1201 self.content = d.get('content', '') 1202 categories = categories.split(',') 1203 1204 for name, check in self.categories.items(): 1205 check.set_active(name in categories) 1206 1207 # update entries 1208 self.id_entry.set_text(id) 1209 self.author_entry.set_text(author) 1210 self.version_entry.set_text(version) 1211 self.license_entry.set_text(license) 1212 self.desc_view.get_buffer().set_text(description) 1213 else: 1214 self._clear_all() 1215 1216 def _clear_all(self): 1217 """ 1218 Clear all text fields. 1219 """ 1220 self.id_entry.set_text('') 1221 self.author_entry.set_text('') 1222 self.version_entry.set_text('') 1223 self.license_entry.set_text('') 1224 self.desc_view.get_buffer().set_text('') 1225 self.content = '' 1226 1227 for name, check in self.categories.items(): 1228 check.set_active(False) 1229 1103 1230 1104 1231 class ScriptEditorPreferencesDialog(HIGDialog): … … 1317 1444 os.path.join(Path.docs_dir, "script_editor.html#preferences"), new=2) 1318 1445 self.stop_emission("response") 1319 1446 1320 1447 if __name__ == "__main__": 1321 1448 editor = ScriptEditorWindow() -
network-scanner/branches/NSEFacilitator/umitNSEFacilitator/luaParser.py
r2248 r5745 62 62 ScriptParser.__init__(self, filename) 63 63 64 self.keywords = ['id', 'description', 'author', 'license', 'version', 'categories'] 65 64 66 self._get_attr('id') 65 67 self._get_attr('description') 66 68 self._get_attr('author') 67 69 self._get_attr('license') 70 self._get_attr('version') 68 71 self._get_attr_list('categories') 72 self.attr['content'] = self._get_content() 69 73 if self._get_function('portrule'): 70 74 self.attr['rule'] = 'port' … … 73 77 else: 74 78 self.attr['rule'] = None 75 self.attr['version'] = self._get_attr('version')79 #self.attr['version'] = self._get_attr('version') 76 80 77 81 def _get_attr_list(self, listname): … … 89 93 r = re.findall(funcname + '\s*=\s*function', self.data) 90 94 return bool(r) 95 96 def _get_content(self): 97 content = "" 98 lines = self.data.split('\n') 99 for line in lines: 100 aux = True 101 for key in self.keywords: 102 if line.startswith(key): 103 aux = False 104 if aux: 105 content = content + line + '\n' 106 first_line = re.search('[^\n]', content).start() 107 return content[first_line:] 91 108 92 109 def _normilize(self, string):
