add ability to add and remove to the rpc access list

This commit is contained in:
Mitchell Livingston
2008-06-04 04:48:17 +00:00
parent 9aa4c2b4ae
commit 63c06c99b1
4 changed files with 208 additions and 64 deletions

View File

@@ -34,6 +34,9 @@
#define RPC_ACCESS_ALLOW 0
#define RPC_ACCESS_BLOCK 1
#define RPC_IP_ADD_TAG 0
#define RPC_IP_REMOVE_TAG 1
#define UPDATE_SECONDS 86400
#define TOOLBAR_GENERAL @"TOOLBAR_GENERAL"
@@ -144,6 +147,13 @@
[self setPrefView: nil];
if (![NSApp isOnLeopardOrBetter])
{
[fRPCAddRemoveControl sizeToFit];
[fRPCAddRemoveControl setLabel: @"+" forSegment: RPC_IP_ADD_TAG];
[fRPCAddRemoveControl setLabel: @"-" forSegment: RPC_IP_REMOVE_TAG];
}
//set download folder
[fFolderPopUp selectItemAtIndex: [fDefaults boolForKey: @"DownloadLocationConstant"] ? DOWNLOAD_FOLDER : DOWNLOAD_TORRENT];
@@ -651,24 +661,47 @@
- (void) updateRPCAccessList
{
NSMutableString * string = [NSMutableString stringWithCapacity: 17 * [fRPCAccessArray count]];
NSMutableArray * components = [NSMutableArray arrayWithCapacity: [fRPCAccessArray count]];
NSEnumerator * enumerator = [fRPCAccessArray objectEnumerator];
NSDictionary * dict;
while ((dict = [enumerator nextObject]))
{
[string appendFormat: @"%c%@,", [[dict objectForKey: @"Allow"] boolValue] ? '+' : '-', [dict objectForKey: @"IP"]];
}
[components addObject: [NSString stringWithFormat: @"%c%@", [[dict objectForKey: @"Allow"] boolValue] ? '+' : '-',
[dict objectForKey: @"IP"]]];
//remove last comma
NSUInteger length = [string length];
if (length > 0)
[string deleteCharactersInRange: NSMakeRange(length-1, 1)];
NSString * string = [components componentsJoinedByString: @","];NSLog(string);
#warning check for an error!
tr_sessionSetRPCACL(fHandle, [string UTF8String], NULL);
}
- (void) addRemoveRPCIP: (id) sender
{
//don't allow add/remove when currently adding - it leads to weird results
if ([fRPCAccessTable editedRow] != -1)
return;
if ([[sender cell] tagForSegment: [sender selectedSegment]] == RPC_IP_REMOVE_TAG)
{
[fRPCAccessArray removeObjectsAtIndexes: [fRPCAccessTable selectedRowIndexes]];
[fRPCAccessTable deselectAll: self];
[fRPCAccessTable reloadData];
[fDefaults setObject: fRPCAccessArray forKey: @"RPCAccessList"];
[self updateRPCAccessList];
}
else
{
[fRPCAccessArray addObject: [NSDictionary dictionaryWithObjectsAndKeys: @"", @"IP",
[NSNumber numberWithBool: YES], @"Allow", nil]];
[fRPCAccessTable reloadData];
int row = [fRPCAccessArray count] - 1;
[fRPCAccessTable selectRow: row byExtendingSelection: NO];
[fRPCAccessTable editColumn: 0 row: row withEvent: nil select: YES];
}
}
- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
{
return [fRPCAccessArray count];
@@ -703,35 +736,49 @@
{
//verify ip
NSArray * components = [object componentsSeparatedByString: @"."];
if ([components count] != 4)
{
NSBeep();
return;
}
BOOL valid = [components count] == 4;
NSMutableArray * newComponents = [NSMutableArray arrayWithCapacity: 4];
NSEnumerator * enumerator = [components objectEnumerator];
NSString * component;
while ((component = [enumerator nextObject]))
NSMutableArray * newComponents;
if (valid)
{
if ([component isEqualToString: @"*"])
[newComponents addObject: component];
else
newComponents = [NSMutableArray arrayWithCapacity: 4];
NSEnumerator * enumerator = [components objectEnumerator];
NSString * component;
while ((component = [enumerator nextObject]))
{
int value = [component intValue];
if (value >= 0 && value < 256)
[newComponents addObject: [[NSNumber numberWithInt: value] stringValue]];
if ([component isEqualToString: @"*"])
[newComponents addObject: component];
else
{
NSBeep();
return;
int value = [component intValue];
if (value >= 0 && value < 256)
[newComponents addObject: [[NSNumber numberWithInt: value] stringValue]];
else
{
valid = NO;
break;
}
}
}
}
if (!valid)
{
NSBeep();
if ([[oldDict objectForKey: @"IP"] isEqualToString: @""])
{
[fRPCAccessArray removeObjectAtIndex: row];
[fRPCAccessTable deselectAll: self];
[fRPCAccessTable reloadData];
}
return;
}
newDict = [NSDictionary dictionaryWithObjectsAndKeys: [newComponents componentsJoinedByString: @"."], @"IP",
[oldDict objectForKey: @"Allow"], @"Allow", nil];NSLog([newDict description]);
[oldDict objectForKey: @"Allow"], @"Allow", nil];
}
[fRPCAccessArray replaceObjectAtIndex: row withObject: newDict];
@@ -740,6 +787,11 @@
[self updateRPCAccessList];
}
- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
[fRPCAddRemoveControl setEnabled: [fRPCAccessTable numberOfSelectedRows] > 0 forSegment: RPC_IP_REMOVE_TAG];
}
- (void) helpForPeers: (id) sender
{
[[NSHelpManager sharedHelpManager] openHelpAnchor: @"PeersPrefs"