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 #include <WebKit/WebKit.h>
25 @interface AppController (Private
)
26 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
;
28 - (void)readChannel
:(LIBSSH2_CHANNEL
*)channel
;
29 - (void)uploadAndPrint
:(id)sender
;
31 - (NSString
*)getUploadSafeName
:(NSString
*)name
;
35 @implementation AppController
38 * Set up the printer list
42 if (self = [super init
])
44 printers
= [[NSArray alloc
] initWithContentsOfFile
:[[NSBundle mainBundle
] pathForResource
:@
"Printers" ofType
:@
"plist"]];
59 * Sets the status text
61 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
63 [status setStringValue
:msg
];
66 [status setTextColor
:[NSColor redColor
]];
67 [progress stopAnimation
:self];
71 [status setTextColor
:[NSColor blackColor
]];
81 NSString
*helpPath
= [[NSBundle mainBundle
] pathForResource
:@
"Help" ofType
:@
"html"];
82 [[helpWebView mainFrame
] loadRequest
:[NSURLRequest requestWithURL
:[NSURL fileURLWithPath
:helpPath
]]];
84 [window makeKeyAndOrderFront
:self];
86 NSUserDefaults
*defaults
= [NSUserDefaults standardUserDefaults
];
87 NSString
*hash
= [[[NSBundle mainBundle
] infoDictionary
] valueForKey
:@
"CFBundleVersion"];
88 if (![[defaults valueForKey
:@
"LastVersionHelpWindow"] isEqualToString
:hash
])
90 [helpWindow makeKeyAndOrderFront
:self];
91 [defaults setObject
:hash forKey
:@
"LastVersionHelpWindow"];
96 * Reads through a channel (in non-blocking) mode until there is no more left to read
97 * and then it returns. This calls sleep(1) so that the channel can have time to process.
98 * Be sure this is threaded otherwis the interface will stall.
100 - (void)readChannel
:(LIBSSH2_CHANNEL
*)channel
102 libssh2_channel_set_blocking(channel
, 0);
108 memset(&buf
, '\0', sizeof(buf
));
109 numbytes
= libssh2_channel_read(channel
, buf
, sizeof(buf
));
111 NSLog(@
"SSH buffer: %s", buf
);
112 } while (libssh2_poll_channel_read(channel
, 0));
116 * Sends an item to the printer
118 - (IBAction
)print
:(id)sender
120 [NSThread detachNewThreadSelector
:@selector(uploadAndPrint
:) toTarget
:self withObject
:sender
];
124 * Opens an SSH session, creates a SCP channel to upload the file, followed by a shell channel
127 - (void)uploadAndPrint
:(id)sender
129 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
131 [progress startAnimation
:self];
132 [progress setHidden
:NO
];
133 [status setHidden
:NO
];
136 struct stat fileInfo
;
137 const char *fileName
= [[self getUploadSafeName
:[dragRegion filePath
]] UTF8String
];
139 [self setStatus
:@
"Connecting to acs.bu.edu" isError
:NO
];
140 struct sockaddr_in sin
;
141 int sock
= socket(AF_INET
, SOCK_STREAM
, 0);
142 sin.sin_port
= htons(22);
143 sin.sin_family
= AF_INET
;
145 struct hostent
*host
= gethostbyname("acs.bu.edu");
146 memcpy(&sin.sin_addr
, host
->h_addr_list
[0], host
->h_length
);
148 if (stat([[dragRegion filePath
] UTF8String
], &fileInfo
))
150 return [self setStatus
:@
"Invalid file selected" isError
:YES
];
153 if (connect(sock
, (struct sockaddr
*)(&sin
), sizeof(struct sockaddr_in
)) != 0)
155 return [self setStatus
:@
"Could not connect to acs.bu.edu" isError
:YES
];
158 LIBSSH2_SESSION
*ssh
= libssh2_session_init();
161 return [self setStatus
:@
"Failed to initialize SSH context" isError
:YES
];
164 if (libssh2_session_startup(ssh
, sock
))
166 return [self setStatus
:@
"Could not tunnel over SSH" isError
:YES
];
169 if (libssh2_userauth_password(ssh
, [[username stringValue
] UTF8String
], [[password stringValue
] UTF8String
]))
171 [self setStatus
:@
"Bad username/password" isError
:YES
];
175 LIBSSH2_CHANNEL
*channel
= libssh2_scp_send(ssh
, fileName
, 0755, (unsigned long)fileInfo.st_size
);
178 [self setStatus
:@
"Unable to open upload SCP session" isError
:YES
];
182 [self setStatus
:@
"Uploading file..." isError
:NO
];
184 localFile
= fopen([[dragRegion filePath
] UTF8String
], "r");
187 int numread
, numwrote
;
190 numread
= fread(buf
, 1, sizeof(buf
), localFile
);
199 numwrote
= libssh2_channel_write(channel
, pbuf
, numread
);
202 } while (numwrote
> 0);
206 [self setStatus
:@
"File uploaded!" isError
:NO
];
208 libssh2_channel_send_eof(channel
);
209 libssh2_channel_wait_eof(channel
);
210 libssh2_channel_wait_closed(channel
);
211 libssh2_channel_free(channel
);
214 channel
= libssh2_channel_open_session(ssh
);
217 [self setStatus
:@
"Could not open SSH channel for printing" isError
:YES
];
221 if (libssh2_channel_request_pty(channel
, "vanilla"))
223 [self setStatus
:@
"Could not open ANSI TTY" isError
:YES
];
227 if (libssh2_channel_shell(channel
))
229 [self setStatus
:@
"Failed to open remote shell" isError
:YES
];
233 [self setStatus
:@
"Opened remote SSH shell" isError
:NO
];
236 [self readChannel
:channel
];
238 // ACS outage messages --> skip some more
239 char *delay
= "q\r\n\0";
240 libssh2_channel_write(channel
, delay
, sizeof(char) * strlen(delay
));
241 [self readChannel
:channel
];
243 // send the job to lpr
246 NSString
*printer
= [[printersController selection
] valueForKey
:@
"unixName"];
247 cmd
= (char *)[[NSString stringWithFormat
:@
"lpr -m -P%@ %s\r\n\0", printer
, fileName
] UTF8String
];
249 cmd
= "touch __PRINT__\r\n\0";
251 libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
));
252 [self readChannel
:channel
];
254 // remove our temp file
255 cmd
= (char *)[[NSString stringWithFormat
:@
"rm -f %s\r\n\0", fileName
] UTF8String
];
256 libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
));
257 [self readChannel
:channel
];
259 [self setStatus
:@
"Printed!" isError
:NO
];
261 libssh2_channel_send_eof(channel
);
262 libssh2_channel_eof(channel
);
263 libssh2_channel_close(channel
);
268 //libssh2_channel_free(channel);
271 libssh2_session_disconnect(ssh
, "Normal disconnect.");
272 libssh2_session_free(ssh
);
276 [progress stopAnimation
:self];
282 * Returns an NSString that is safe for uploding onto the server
284 - (NSString
*)getUploadSafeName
:(NSString
*)name
286 NSMutableString
*n
= [NSMutableString stringWithString
:name
];
287 [n replaceOccurrencesOfString
:@
" " withString
:@
"" options
:NSBackwardsSearch range
:NSMakeRange(0, [name length
])];
288 name
= (NSString
*)n
;
289 name
= [name lastPathComponent
];
290 name
= [name stringByDeletingPathExtension
];
291 if ([name length
] > 20)
292 name
= [name substringToIndex
:20];
295 return [NSString stringWithFormat
:@
"~/%@.%d.pdf", name
, random()];