//
|
|
// main.m
|
|
// pEpMacOSAdapter
|
|
//
|
|
// Created by Volker Birk on 20.04.20.
|
|
// Copyleft © 2020 p≡p foundation.
|
|
// This file is under GNU General Public License 3.0
|
|
//
|
|
|
|
#include <signal.h>
|
|
#import <Foundation/Foundation.h>
|
|
#import <AppKit/NSWorkspace.h>
|
|
#import "pEpMacOSAdapter.h"
|
|
#import "pEpUpdater.h"
|
|
|
|
pEpUpdater* updater = nil;
|
|
|
|
@interface ServiceDelegate : NSObject <NSXPCListenerDelegate>
|
|
@end
|
|
|
|
@implementation ServiceDelegate
|
|
|
|
- (id)init{
|
|
self = [super init];
|
|
return self;
|
|
}
|
|
|
|
- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
|
|
// This method is where the NSXPCListener configures, accepts, and resumes a new incoming NSXPCConnection.
|
|
NSLog(@"incoming connection");
|
|
|
|
// Configure the connection.
|
|
// First, set the interface that the exported object implements.
|
|
newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(pEpMacOSAdapterProtocol)];
|
|
|
|
// Next, set the object that the connection exports. All messages sent on
|
|
// the connection to this service will be sent to the exported object to
|
|
// handle. The connection retains the exported object.
|
|
pEpMacOSAdapter *exportedObject = [pEpMacOSAdapter new];
|
|
newConnection.exportedObject = exportedObject;
|
|
|
|
if (!newConnection.exportedInterface || !newConnection.exportedObject) {
|
|
NSLog(@"failed to allocate object and interface");
|
|
return NO;
|
|
}
|
|
|
|
// Resuming the connection allows the system to deliver more incoming messages.
|
|
[newConnection resume];
|
|
|
|
// Returning YES from this method tells the system that you have accepted this connection. If you want to reject the connection for some reason, call -invalidate on the connection and return NO.
|
|
NSLog(@"connection accepted");
|
|
return YES;
|
|
}
|
|
|
|
@end
|
|
|
|
BOOL start_helper(void)
|
|
{
|
|
NSURL *url = [NSURL
|
|
fileURLWithPath:@"/Library/Application Support/pEp/pEp.app/Contents/Library/LoginItems/p≡p updates.app"];
|
|
|
|
return [[NSWorkspace sharedWorkspace] openURL:url];
|
|
}
|
|
|
|
void signal_TERM(int signal)
|
|
{
|
|
NSLog(@"stopping agent");
|
|
exit(0);
|
|
}
|
|
|
|
int main(int argc, const char *argv[])
|
|
{
|
|
signal(SIGTERM, signal_TERM);
|
|
updater = [pEpUpdater new];
|
|
|
|
// Create the delegate for the service.
|
|
ServiceDelegate *delegate = [ServiceDelegate new];
|
|
|
|
// Set up the one NSXPCListener for this service. It will handle all incoming connections.
|
|
NSXPCListener *listener = [[NSXPCListener alloc] initWithMachServiceName:@"foundation.pEp.adapter.macOS"];
|
|
NSLog(@"starting agent");
|
|
listener.delegate = delegate;
|
|
[listener resume];
|
|
|
|
// start the GUI helper app
|
|
BOOL opened = start_helper();
|
|
assert(opened);
|
|
|
|
NSLog(@"going into main runLoop");
|
|
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
|
|
[runLoop run];
|
|
|
|
return EXIT_FAILURE;
|
|
}
|