<s> import os <EOL> import subprocess <EOL> from ajenti . api import * <EOL> from ajenti . plugins . main . api import SectionPlugin <EOL> from ajenti . ui import on <EOL> from ajenti . ui . binder import Binder <EOL> from reconfigure . configs import CSFConfig <EOL> from reconfigure . items . hosts import AliasData , HostData <EOL> @ plugin <EOL> class CSFSection ( SectionPlugin ) : <EOL> def init ( self ) : <EOL> self . title = _ ( '<STR_LIT>' ) <EOL> self . icon = '<STR_LIT>' <EOL> self . category = _ ( '<STR_LIT>' ) <EOL> self . backend = CSFBackend . get ( ) <EOL> self . append ( self . ui . inflate ( '<STR_LIT>' ) ) <EOL> self . config = CSFConfig ( path = '<STR_LIT>' ) <EOL> self . list_allow = [ ] <EOL> self . list_deny = [ ] <EOL> self . list_tempallow = [ ] <EOL> self . list_tempban = [ ] <EOL> def delete_rule ( csf_option , i ) : <EOL> self . save ( ) <EOL> subprocess . call ( [ '<STR_LIT>' , csf_option , i . value . split ( '<STR_LIT:#>' ) [ <NUM_LIT:0> ] ] ) <EOL> self . refresh ( ) <EOL> self . find ( '<STR_LIT>' ) . on_delete = lambda i , c : delete ( '<STR_LIT>' , i ) <EOL> self . find ( '<STR_LIT>' ) . on_delete = lambda i , c : delete ( '<STR_LIT>' , i ) <EOL> self . find ( '<STR_LIT>' ) . on_delete = lambda i , c : delete ( '<STR_LIT>' , i ) <EOL> self . find ( '<STR_LIT>' ) . on_delete = lambda i , c : delete ( '<STR_LIT>' , i ) <EOL> def add_rule ( csf_option , address ) : <EOL> self . save ( ) <EOL> p = subprocess . Popen ( [ '<STR_LIT>' , csf_option , address ] , stdout = subprocess . PIPE ) <EOL> o , e = p . communicate ( ) <EOL> self . context . notify ( '<STR_LIT:info>' , o ) <EOL> self . refresh ( ) <EOL> self . find ( '<STR_LIT>' ) . on ( '<STR_LIT>' , lambda : add_rule ( '<STR_LIT>' , self . find ( '<STR_LIT>' ) . value ) ) <EOL> self . find ( '<STR_LIT>' ) . on ( '<STR_LIT>' , lambda : add_rule ( '<STR_LIT>' , self . find ( '<STR_LIT>' ) . value ) ) <EOL> self . find ( '<STR_LIT>' ) . on ( '<STR_LIT>' , lambda : add_rule ( '<STR_LIT>' , self . find ( '<STR_LIT>' ) . value ) ) <EOL> self . find ( '<STR_LIT>' ) . on ( '<STR_LIT>' , lambda : add_rule ( '<STR_LIT>' , self . find ( '<STR_LIT>' ) . value ) ) <EOL> self . binder = Binder ( None , self ) <EOL> self . binder_lists = Binder ( self , self . find ( '<STR_LIT>' ) ) <EOL> def on_page_load ( self ) : <EOL> self . refresh ( ) <EOL> def refresh ( self ) : <EOL> self . config . load ( ) <EOL> self . list_allow = self . backend . read_list ( '<STR_LIT>' ) <EOL> self . list_deny = self . backend . read_list ( '<STR_LIT>' ) <EOL> self . list_tempallow = self . backend . read_list ( '<STR_LIT>' ) <EOL> self . list_tempban = self . backend . read_list ( '<STR_LIT>' ) <EOL> self . binder . setup ( self . config . tree ) . populate ( ) <EOL> self . binder_lists . populate ( ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_apply ( self ) : <EOL> self . backend . apply ( ) <EOL> self . context . notify ( '<STR_LIT:info>' , _ ( '<STR_LIT>' ) ) <EOL> @ on ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def save ( self ) : <EOL> self . binder . update ( ) <EOL> self . config . save ( ) <EOL> self . backend . write_list ( '<STR_LIT>' , self . list_allow ) <EOL> self . backend . write_list ( '<STR_LIT>' , self . list_deny ) <EOL> self . backend . write_list ( '<STR_LIT>' , self . list_tempallow ) <EOL> self . backend . write_list ( '<STR_LIT>' , self . list_tempban ) <EOL> self . binder . setup ( self . config . tree ) . populate ( ) <EOL> self . context . notify ( '<STR_LIT:info>' , _ ( '<STR_LIT>' ) ) <EOL> try : <EOL> self . backend . test_config ( ) <EOL> self . context . notify ( '<STR_LIT:info>' , _ ( '<STR_LIT>' ) ) <EOL> except Exception as e : <EOL> self . context . notify ( '<STR_LIT:error>' , str ( e ) ) <EOL> class CSFListItem ( object ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> @ plugin <EOL> @ persistent <EOL> @ rootcontext <EOL> class CSFBackend ( object ) : <EOL> csf_files = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def apply ( self ) : <EOL> subprocess . call ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_config ( self ) : <EOL> p = subprocess . Popen ( [ '<STR_LIT>' ] , stdout = subprocess . PIPE ) <EOL> o , e = p . communicate ( ) <EOL> if p . returncode != <NUM_LIT:0> : <EOL> raise Exception ( e ) <EOL> def read_list ( self , name ) : <EOL> if not os . path . exists ( self . csf_files [ name ] ) : <EOL> return [ ] <EOL> return [ <EOL> CSFListItem ( l . strip ( ) ) <EOL> for l in open ( self . csf_files [ name ] ) <EOL> if l . strip ( ) and not l . startswith ( '<STR_LIT:#>' ) <EOL> ] <EOL> def write_list ( self , name , lst ) : <EOL> open ( self . csf_files [ name ] , '<STR_LIT:w>' ) . write ( <EOL> '<STR_LIT:\n>' . join ( <EOL> x . value for x in lst <EOL> ) + '<STR_LIT:\n>' <EOL> ) </s>
