]> Dogcows Code - chaz/thecheat/blob - AppController.m
The Cheat 1.2.2
[chaz/thecheat] / AppController.m
1
2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 1, or (at your option)
9 // any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 //
20
21 #import "AppController.h"
22
23 #import "CheatDocument.h"
24 #import "AboutBoxController.h"
25 #import "HelpController.h"
26 #import "PreferenceController.h"
27
28 // Privilage elevation libs
29 #include <security/authorization.h>
30 #include <security/authorizationdb.h>
31 #include <security/authorizationtags.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34
35 @implementation AppController
36
37
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 #pragma mark Initialization
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41
42
43 + (void)initialize
44 {
45 NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
46
47 TCFirstLaunchPref = [[NSString stringWithFormat:@"TC%@%@Pref", ChazAppName(), ChazAppVersion()] retain];
48 NSString *broadcastName = [NSString stringWithFormat:@"%@'s Computer", NSFullUserName()];
49
50 // register user defaults
51 [defaults setObject:[NSNumber numberWithBool:NO] forKey:TCFirstLaunchPref];
52 [defaults setObject:[NSNumber numberWithBool:NO] forKey:TCWindowsOnTopPref];
53 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCUpdateCheckPref];
54 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCDisplayValuesPref];
55 [defaults setObject:[NSNumber numberWithFloat:1.0] forKey:TCValueUpdatePref];
56 [defaults setObject:[NSNumber numberWithInt:1000] forKey:TCHitsDisplayedPref];
57 [defaults setObject:[NSNumber numberWithBool:NO] forKey:TCRunServerPref];
58 [defaults setObject:broadcastName forKey:TCBroadcastNamePref];
59 [defaults setObject:[NSNumber numberWithInt:TCDefaultListenPort] forKey:TCListenPortPref];
60 [defaults setObject:[NSNumber numberWithFloat:gFadeAnimationDuration] forKey:TCFadeAnimationPref];
61 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCAskForSavePref];
62 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCSwitchVariablesPref];
63 [defaults setObject:[NSNumber numberWithBool:YES] forKey:TCAutoStartEditingVarsPref];
64
65 // register it
66 [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
67
68 // set globals
69 gFadeAnimationDuration = [[NSUserDefaults standardUserDefaults] floatForKey:TCFadeAnimationPref];
70 }
71
72 - (id)init
73 {
74 if ( self = [super init] )
75 {
76 if( geteuid() != 0 )
77 {
78 [self launchAuthPrgm];
79 [self setDelegate:self];
80 }
81 }
82
83 if( geteuid() != 0 )
84 {
85 NSRunAlertPanel(@"The Cheat must be run as root,",
86 @"Due to a limitation of Leopard, the application needs elevated privileges to run.",
87 @"Exit", nil, nil );
88 [self terminate: 0];
89 }
90
91 return self;
92 }
93
94 - (int) preAuthorize
95 {
96 int err;
97 AuthorizationFlags authFlags;
98
99
100 NSLog (@"MyWindowController: preAuthorize");
101
102 if (_authRef)
103 return errAuthorizationSuccess;
104
105 NSLog (@"MyWindowController: preAuthorize: ** calling AuthorizationCreate...**\n");
106
107 authFlags = kAuthorizationFlagDefaults;
108 err = AuthorizationCreate (NULL, kAuthorizationEmptyEnvironment, authFlags, &_authRef);
109 if (err != errAuthorizationSuccess)
110 return err;
111
112 NSLog (@"MyWindowController: preAuthorize: ** calling AuthorizationCopyRights...**\n");
113
114 _authItem.name = kAuthorizationRightExecute;
115 _authItem.valueLength = 0;
116 _authItem.value = NULL;
117 _authItem.flags = 0;
118 _authRights.count = 1;
119 _authRights.items = (AuthorizationItem*) malloc (sizeof (_authItem));
120 memcpy (&_authRights.items[0], &_authItem, sizeof (_authItem));
121 authFlags = kAuthorizationFlagDefaults
122 | kAuthorizationFlagExtendRights
123 | kAuthorizationFlagInteractionAllowed
124 | kAuthorizationFlagPreAuthorize;
125 err = AuthorizationCopyRights (_authRef, &_authRights, kAuthorizationEmptyEnvironment, authFlags, NULL);
126
127 return err;
128 }
129
130 - (int) launchAuthPrgm
131 {
132 AuthorizationFlags authFlags;
133 int err;
134
135 // path
136 NSString * path = [[NSBundle mainBundle] executablePath];
137 if (![[NSFileManager defaultManager] isExecutableFileAtPath: path])
138 return -1;
139
140 // auth
141
142 if (!_authRef)
143 {
144 err = [self preAuthorize];
145 if (err != errAuthorizationSuccess)
146 return err;
147 }
148
149 // launch
150
151 NSLog (@"MyWindowController: launchWithPath: ** calling AuthorizationExecuteWithPrivileges...**\n");
152 authFlags = kAuthorizationFlagDefaults;
153 err = AuthorizationExecuteWithPrivileges (_authRef, [path cString], authFlags, NULL, NULL);
154 if(err==0) [NSApp terminate:self];
155
156 return err;
157 }
158
159 - (void)dealloc
160 {
161 ChazLog( @"AppController deallocated!!" );
162 [self stopCheatServer];
163 [super dealloc];
164 }
165
166
167 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
168 #pragma mark NSApplication Delegate
169 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
170
171
172 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
173 {
174 // check if this is the first launch
175 if ( ![[NSUserDefaults standardUserDefaults] boolForKey:TCFirstLaunchPref] ) {
176 // FIRST LAUNCH
177 [self showAboutBoxWindow:self];
178 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:TCFirstLaunchPref];
179 }
180
181 // if should check for updates on launch
182 if ( [[NSUserDefaults standardUserDefaults] boolForKey:TCUpdateCheckPref] ) {
183 ChazCheckForUpdate( TCUpdateCheckURL, NO );
184 }
185
186 // automaticall start the cheat server if the pref is set
187 if ( [[NSUserDefaults standardUserDefaults] boolForKey:TCRunServerPref] ) {
188 if ( ![self startCheatServer] ) {
189 // inform the user that the server won't start
190 NSRunAlertPanel( @"The Cheat could not start the server.",
191 @"The cheat server failed to start. Check the server settings and start it manually.",
192 @"OK", nil, nil );
193 // open server prefs
194 [self showPreferenceWindow:self];
195 [_preferenceController chooseServer:self];
196 }
197 }
198 }
199
200
201 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
202 #pragma mark Interface Actions
203 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
204
205
206 - (IBAction)newSearchWindow:(id)sender
207 {
208 NSDocumentController *controller = [NSDocumentController sharedDocumentController];
209 CheatDocument *doc = [controller makeUntitledDocumentOfType:@"Cheat Document"];
210 if ( !doc ) {
211 ChazLog( @"nil document" );
212 }
213 [doc setMode:TCSearchMode];
214 [controller addDocument:doc];
215 [doc makeWindowControllers];
216 [doc showWindows];
217 }
218
219 - (IBAction)newBlankCheatWindow:(id)sender
220 {
221 NSDocumentController *controller = [NSDocumentController sharedDocumentController];
222 CheatDocument *doc = [controller makeUntitledDocumentOfType:@"Cheat Document"];
223 if ( !doc ) {
224 ChazLog( @"nil document" );
225 }
226 [doc setMode:TCCheatMode];
227 [controller addDocument:doc];
228 [doc makeWindowControllers];
229 [doc showWindows];
230 }
231
232 - (IBAction)showAboutBoxWindow:(id)sender
233 {
234 if ( !_aboutBoxController ) {
235 _aboutBoxController = [[AboutBoxController alloc] init];
236 }
237 [_aboutBoxController showWindow:self];
238 }
239
240 - (IBAction)showPreferenceWindow:(id)sender
241 {
242 if ( !_preferenceController ) {
243 _preferenceController = [[PreferenceController alloc] init];
244 }
245 [_preferenceController showWindow:self];
246 }
247
248
249 - (IBAction)launchHelpFile:(id)sender
250 {
251 if ( !_helpController ) {
252 _helpController = [[HelpController alloc] init];
253 }
254 [_helpController showWindow:self];
255 }
256
257 - (IBAction)launchEmailMenu:(id)sender
258 {
259 LaunchEmail();
260 }
261
262 - (IBAction)launchWebsiteMenu:(id)sender
263 {
264 LaunchWebsite();
265 }
266
267
268 - (IBAction)checkForUpdate:(id)sender
269 {
270 ChazCheckForUpdate( TCUpdateCheckURL, YES );
271 }
272
273
274 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
275 #pragma mark CheatServer Stuff
276 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
277
278 - (CheatServer *)cheatServer
279 {
280 if ( !_server ) {
281 _server = [[CheatServer alloc] initWithDelegate:self];
282 }
283 return _server;
284 }
285
286 - (BOOL)startCheatServer
287 {
288 ChazLog( @"cheat server starting..." );
289
290 // start the server with saved settings
291 int port = [[NSUserDefaults standardUserDefaults] integerForKey:TCListenPortPref];
292 NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:TCBroadcastNamePref];
293 if ( [name isEqualToString:@""] ) {
294 name = nil;
295 }
296
297 // stop the cheat server if it's running
298 [self stopCheatServer];
299
300 // start the server
301 if ( [[self cheatServer] listenOnPort:port broadcast:name] ) {
302 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerStartedNote object:[self cheatServer]];
303 return YES;
304 }
305 return NO;
306 }
307
308 - (void)stopCheatServer
309 {
310 if ( _server ) {
311 [_server stop];
312 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerStoppedNote object:[self cheatServer]];
313 }
314 }
315
316
317 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
318 #pragma mark CheatServerDelegate
319 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
320
321 - (void)serverDisconnectedUnexpectedly:(CheatServer *)theServer
322 {
323 ChazLog( @"server disconnected unexpectedly." );
324 [self stopCheatServer];
325 }
326
327 - (void)server:(CheatServer *)theServer failedToBroadcastName:(NSString *)theName
328 {
329 NSBeginInformationalAlertSheet( @"The cheat server can not broadcast.", @"OK", nil, nil, [_preferenceController window], nil, NULL, NULL, NULL,
330 @"The Cheat can't broadcast as \"%@\" because that name is in use by another server. The server will continue running with broadcasting disabled.", theName );
331 }
332
333 - (void)serverChildrenChanged:(CheatServer *)theServer
334 {
335 [[NSNotificationCenter defaultCenter] postNotificationName:TCServerConnectionsChangedNote object:theServer];
336 }
337
338
339 @end
This page took 0.04775 seconds and 4 git commands to generate.