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>
26 @interface AppController (Private
)
27 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
;
29 - (void)readChannel
:(LIBSSH2_CHANNEL
*)channel
;
30 - (void)uploadAndPrint
:(id)sender
;
32 - (NSString
*)getUploadSafeName
:(NSString
*)name
;
36 @implementation AppController
39 * Set up the printer list
43 if (self = [super init
])
45 printers
= [[NSArray alloc
] initWithContentsOfFile
:[[NSBundle mainBundle
] pathForResource
:@
"Printers" ofType
:@
"plist"]];
60 * Sets the status text
62 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
64 [status setStringValue
:msg
];
67 [status setTextColor
:[NSColor redColor
]];
68 [progress stopAnimation
:self];
72 [status setTextColor
:[NSColor blackColor
]];
82 NSString
*helpPath
= [[NSBundle mainBundle
] pathForResource
:@
"Help" ofType
:@
"html"];
83 [[helpWebView mainFrame
] loadRequest
:[NSURLRequest requestWithURL
:[NSURL fileURLWithPath
:helpPath
]]];
85 [window makeKeyAndOrderFront
:self];
87 NSUserDefaults
*defaults
= [NSUserDefaults standardUserDefaults
];
88 NSString
*hash
= [[[NSBundle mainBundle
] infoDictionary
] valueForKey
:@
"CFBundleVersion"];
89 if (![[defaults valueForKey
:@
"LastVersionHelpWindow"] isEqualToString
:hash
])
91 [helpWindow makeKeyAndOrderFront
:self];
92 [defaults setObject
:hash forKey
:@
"LastVersionHelpWindow"];
97 * Reads through a channel (in non-blocking) mode until there is no more left to read
98 * and then it returns. This calls sleep(1) so that the channel can have time to process.
99 * Be sure this is threaded otherwis the interface will stall.
101 - (void)readChannel
:(LIBSSH2_CHANNEL
*)channel
103 libssh2_channel_set_blocking(channel
, 0);
109 memset(&buf
, '\0', sizeof(buf
));
110 numbytes
= libssh2_channel_read(channel
, buf
, sizeof(buf
));
112 NSLog(@
"SSH buffer: %s", buf
);
113 } while (libssh2_poll_channel_read(channel
, 0));
117 * Sends an item to the printer
119 - (IBAction
)print
:(id)sender
121 [NSThread detachNewThreadSelector
:@selector(uploadAndPrint
:) toTarget
:self withObject
:sender
];
125 * Opens an SSH session, creates a SCP channel to upload the file, followed by a shell channel
128 - (void)uploadAndPrint
:(id)sender
130 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
132 [progress startAnimation
:self];
133 [progress setHidden
:NO
];
134 [status setHidden
:NO
];
135 [goButton setEnabled
:NO
];
138 struct stat fileInfo
;
139 NSString
*uploadName
= [self getUploadSafeName
:[dragRegion filePath
]];
140 const char *fileName
= [uploadName UTF8String
];
142 [self setStatus
:@
"Connecting to acs.bu.edu" isError
:NO
];
143 struct sockaddr_in sin
;
144 int sock
= socket(AF_INET
, SOCK_STREAM
, 0);
145 sin.sin_port
= htons(22);
146 sin.sin_family
= AF_INET
;
148 struct hostent
*host
= gethostbyname("acs.bu.edu");
149 memcpy(&sin.sin_addr
, host
->h_addr_list
[0], host
->h_length
);
151 if (stat([[dragRegion filePath
] UTF8String
], &fileInfo
))
153 return [self setStatus
:@
"Invalid file selected" isError
:YES
];
156 if (connect(sock
, (struct sockaddr
*)(&sin
), sizeof(struct sockaddr_in
)) != 0)
158 return [self setStatus
:@
"Could not connect to acs.bu.edu" isError
:YES
];
161 LIBSSH2_SESSION
*ssh
= libssh2_session_init();
164 return [self setStatus
:@
"Failed to initialize SSH context" isError
:YES
];
167 if (libssh2_session_startup(ssh
, sock
))
169 return [self setStatus
:@
"Could not tunnel over SSH" isError
:YES
];
172 if (libssh2_userauth_password(ssh
, [[username stringValue
] UTF8String
], [[password stringValue
] UTF8String
]))
174 [self setStatus
:@
"Bad username/password" isError
:YES
];
178 LIBSSH2_CHANNEL
*channel
= libssh2_scp_send(ssh
, fileName
, 0755, (unsigned long)fileInfo.st_size
);
181 [self setStatus
:@
"Unable to open upload SCP session" isError
:YES
];
185 [self setStatus
:@
"Uploading file..." isError
:NO
];
187 localFile
= fopen([[dragRegion filePath
] UTF8String
], "r");
190 int numread
, numwrote
;
193 numread
= fread(buf
, 1, sizeof(buf
), localFile
);
202 numwrote
= libssh2_channel_write(channel
, pbuf
, numread
);
205 } while (numwrote
> 0);
209 [self setStatus
:@
"File uploaded!" isError
:NO
];
211 libssh2_channel_send_eof(channel
);
212 libssh2_channel_wait_eof(channel
);
213 libssh2_channel_wait_closed(channel
);
214 libssh2_channel_free(channel
);
217 channel
= libssh2_channel_open_session(ssh
);
220 [self setStatus
:@
"Could not open SSH channel for printing" isError
:YES
];
224 if (libssh2_channel_request_pty(channel
, "vanilla"))
226 [self setStatus
:@
"Could not open ANSI TTY" isError
:YES
];
230 if (libssh2_channel_shell(channel
))
232 [self setStatus
:@
"Failed to open remote shell" isError
:YES
];
236 [self setStatus
:@
"Opened remote SSH shell" isError
:NO
];
239 [self readChannel
:channel
];
241 // ACS outage messages --> skip some more
242 char *delay
= "q\r\n\0";
243 libssh2_channel_write(channel
, delay
, sizeof(char) * strlen(delay
));
244 [self readChannel
:channel
];
246 // send the job to lpr
249 NSString
*printer
= [[printersController selection
] valueForKey
:@
"unixName"];
250 cmd
= (char *)[[NSString stringWithFormat
:@
"lpr -m -#%d -P%@ %s\r\n\0", [[copiesButtons selectedCell
] tag
], printer
, fileName
] UTF8String
];
252 if ([[copiesButtons selectedCell
] tag
] == 1)
253 cmd
= "touch __PRINT_1__\r\n\0";
255 cmd
= "touch __PRINT_2__\r\n\0";
257 libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
));
258 [self readChannel
:channel
];
260 // remove our temp file
261 cmd
= (char *)[[NSString stringWithFormat
:@
"rm -f %s\r\n\0", fileName
] UTF8String
];
262 libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
));
263 [self readChannel
:channel
];
265 [self setStatus
:[NSString stringWithFormat
:@
"Printed %@", uploadName
] isError
:NO
];
267 [dragRegion clearFile
];
269 libssh2_channel_send_eof(channel
);
270 libssh2_channel_eof(channel
);
271 libssh2_channel_close(channel
);
276 //libssh2_channel_free(channel);
279 libssh2_session_disconnect(ssh
, "Normal disconnect.");
280 libssh2_session_free(ssh
);
284 [progress stopAnimation
:self];
290 * Returns an NSString that is safe for uploding onto the server
292 - (NSString
*)getUploadSafeName
:(NSString
*)name
294 NSMutableString
*safeName
= [NSMutableString string
];
296 name
= [name lastPathComponent
];
297 name
= [name stringByDeletingPathExtension
];
299 for (int i
= 0, j
= 0; i
< [name length
] && j
< 20; i
++)
301 char c
= [name characterAtIndex
:i
];
302 if ((c
>= 48 && c
<= 57) ||
(c
>= 65 && c
<= 90) ||
(c
>= 97 && 122) || c
== 95)
304 [safeName appendFormat
:@
"%c", c
];
309 if (j
> 0 && [safeName characterAtIndex
:j
- 1] != '-')
311 [safeName appendString
:@
"_"];
315 else if (c
>= 38 && c
<= 46)
317 if (j
> 0 && [safeName characterAtIndex
:j
- 1] != '_')
319 [safeName appendString
:@
"-"];
326 return [NSString stringWithFormat
:@
"~/%@.%d.pdf", safeName
, random()];
330 * Opens the print quota check page
332 - (IBAction
)helpCheckPQuota
:(id)sender
334 NSURL
*url
= [NSURL URLWithString
:@
"http://www.bu.edu/phpbin/myacs/"];
335 [[NSWorkspace sharedWorkspace
] openURL
:url
];
339 * Opens the link to the ACS help desk homepage
341 - (IBAction
)helpACS
:(id)sender
343 NSURL
*url
= [NSURL URLWithString
:@
"http://www.bu.edu/cc/"];
344 [[NSWorkspace sharedWorkspace
] openURL
:url
];