3 * Copyright (c) 2008, Blue Static <http://www.bluestatic.org>
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 #import "AppController.h"
19 #import <sys/socket.h>
23 @interface AppController (Private
)
25 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
;
27 - (void)readChannel
:(LIBSSH2_CHANNEL
*)channel
;
28 - (void)uploadAndPrint
:(id)sender
;
33 @implementation AppController
36 * Set up the printer list
40 if (self = [super init
])
42 printers
= [[NSArray alloc
] initWithContentsOfFile
:[[NSBundle mainBundle
] pathForResource
:@
"Printers" ofType
:@
"plist"]];
43 [NSThread detachNewThreadSelector
:@selector(versionCheck
:) toTarget
:self withObject
:self];
58 * Checks and sees if the current version is the most up-to-date one
60 - (void)versionCheck
:(id)sender
62 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
64 NSMutableString
*version
= [NSMutableString stringWithString
:[[[NSBundle mainBundle
] infoDictionary
] valueForKey
:@
"CFBundleShortVersionString"]];
65 [version replaceOccurrencesOfString
:@
" " withString
:@
"-" options
:NSLiteralSearch range
:NSMakeRange(0, [version length
])];
67 NSURL
*url
= [NSURL URLWithString
:[NSString stringWithFormat
:@
"http://www.bluestatic.org/versioncheck.php?prod=printdrop&ver=%@", version
]];
68 NSURLRequest
*request
= [NSURLRequest requestWithURL
:url cachePolicy
:NSURLRequestReloadIgnoringCacheData timeoutInterval
:10];
69 NSURLResponse
*response
;
70 NSData
*result
= [NSURLConnection sendSynchronousRequest
:request returningResponse
:&response error
:nil];
77 NSXMLDocument
*xml
= [[NSXMLDocument alloc
] initWithData
:result options
:0 error
:nil];
78 NSXMLNode
*comp
= [[xml rootElement
] childAtIndex
:0];
79 if ([[comp name
] isEqualToString
:@
"update"])
81 [updateString setStringValue
:[NSString stringWithFormat
:[updateString stringValue
], [comp stringValue
]]];
82 [updateWindow makeKeyAndOrderFront
:self];
91 * Opens the URL to the download page
93 - (IBAction
)openUpdateInformation
:(id)sender
95 [[NSWorkspace sharedWorkspace
] openURL
:[NSURL URLWithString
:@
"http://www.bluestatic.org/software/printdrop/"]];
99 * Sets the status text
101 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
103 [status setStringValue
:msg
];
106 [status setTextColor
:[NSColor redColor
]];
107 [progress stopAnimation
:self];
111 [status setTextColor
:[NSColor blackColor
]];
116 * Reads through a channel (in non-blocking) mode until there is no more left to read
117 * and then it returns. This calls sleep(1) so that the channel can have time to process.
118 * Be sure this is threaded otherwis the interface will stall.
120 - (void)readChannel
:(LIBSSH2_CHANNEL
*)channel
122 libssh2_channel_set_blocking(channel
, 0);
128 memset(&buf
, '\0', sizeof(buf
));
129 numbytes
= libssh2_channel_read(channel
, buf
, sizeof(buf
));
131 NSLog(@
"SSH buffer: %s", buf
);
132 } while (libssh2_poll_channel_read(channel
, 0));
136 * Sends an item to the printer
138 - (IBAction
)print
:(id)sender
140 [NSThread detachNewThreadSelector
:@selector(uploadAndPrint
:) toTarget
:self withObject
:sender
];
144 * Opens an SSH session, creates a SCP channel to upload the file, followed by a shell channel
147 - (void)uploadAndPrint
:(id)sender
149 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
151 [progress startAnimation
:self];
152 [progress setHidden
:NO
];
153 [status setHidden
:NO
];
156 struct stat fileInfo
;
158 [self setStatus
:@
"Connecting to acs.bu.edu" isError
:NO
];
159 struct sockaddr_in sin
;
160 int sock
= socket(AF_INET
, SOCK_STREAM
, 0);
161 sin.sin_port
= htons(22);
162 sin.sin_family
= AF_INET
;
164 struct hostent
*host
= gethostbyname("acs.bu.edu");
165 memcpy(&sin.sin_addr
, host
->h_addr_list
[0], host
->h_length
);
167 if (stat([[dragRegion filePath
] UTF8String
], &fileInfo
))
169 return [self setStatus
:@
"Invalid file selected" isError
:YES
];
172 if (connect(sock
, (struct sockaddr
*)(&sin
), sizeof(struct sockaddr_in
)) != 0)
174 return [self setStatus
:@
"Could not connect to acs.bu.edu" isError
:YES
];
177 LIBSSH2_SESSION
*ssh
= libssh2_session_init();
180 return [self setStatus
:@
"Failed to initialize SSH context" isError
:YES
];
183 if (libssh2_session_startup(ssh
, sock
))
185 return [self setStatus
:@
"Could not tunnel over SSH" isError
:YES
];
188 if (libssh2_userauth_password(ssh
, [[username stringValue
] UTF8String
], [[password stringValue
] UTF8String
]))
190 [self setStatus
:@
"Bad username/password" isError
:YES
];
194 LIBSSH2_CHANNEL
*channel
= libssh2_scp_send(ssh
, "~/__bu_print_drop__.pdf", 0755, (unsigned long)fileInfo.st_size
);
197 [self setStatus
:@
"Unable to open upload SCP session" isError
:YES
];
201 [self setStatus
:@
"Uploading file..." isError
:NO
];
203 localFile
= fopen([[dragRegion filePath
] UTF8String
], "r");
206 int numread
, numwrote
;
209 numread
= fread(buf
, 1, sizeof(buf
), localFile
);
218 numwrote
= libssh2_channel_write(channel
, pbuf
, numread
);
221 } while (numwrote
> 0);
225 [self setStatus
:@
"File uploaded!" isError
:NO
];
227 libssh2_channel_send_eof(channel
);
228 libssh2_channel_wait_eof(channel
);
229 libssh2_channel_wait_closed(channel
);
230 libssh2_channel_free(channel
);
233 channel
= libssh2_channel_open_session(ssh
);
236 [self setStatus
:@
"Could not open SSH channel for printing" isError
:YES
];
240 if (libssh2_channel_request_pty(channel
, "vanilla"))
242 [self setStatus
:@
"Could not open ANSI TTY" isError
:YES
];
246 if (libssh2_channel_shell(channel
))
248 [self setStatus
:@
"Failed to open remote shell" isError
:YES
];
252 [self setStatus
:@
"Opened remote SSH shell" isError
:NO
];
255 [self readChannel
:channel
];
257 // f!cking ACS messages --> skip some more
258 char *delay
= "q\r\n\0";
259 libssh2_channel_write(channel
, delay
, sizeof(char) * strlen(delay
));
260 [self readChannel
:channel
];
262 // send the job to lpr
265 NSString
*printer
= [[printersController selection
] valueForKey
:@
"unixName"];
266 cmd
= (char *)[[NSString stringWithFormat
:@
"lpr -m -P%@ __bu_print_drop__.pdf\r\n\0", printer
] UTF8String
];
268 cmd
= "touch abc.def\r\n\0";
270 libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
));
271 [self readChannel
:channel
];
273 // remove our temp file
274 cmd
= "rm -f __bu_print_drop__.pdf\r\n\0";
275 libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
));
276 [self readChannel
:channel
];
278 [self setStatus
:@
"Printed!" isError
:NO
];
280 libssh2_channel_send_eof(channel
);
281 libssh2_channel_eof(channel
);
282 libssh2_channel_close(channel
);
287 //libssh2_channel_free(channel);
290 libssh2_session_disconnect(ssh
, "Normal disconnect.");
291 libssh2_session_free(ssh
);
295 [progress stopAnimation
:self];