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>
24 @interface AppController (Private
)
25 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
;
27 - (void)readChannel
:(LIBSSH2_CHANNEL
*)channel
;
28 - (void)uploadAndPrint
:(id)sender
;
30 - (NSString
*)getUploadSafeName
:(NSString
*)name
;
34 @implementation AppController
37 * Set up the printer list
41 if (self = [super init
])
43 printers
= [[NSArray alloc
] initWithContentsOfFile
:[[NSBundle mainBundle
] pathForResource
:@
"Printers" ofType
:@
"plist"]];
58 * Sets the status text
60 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
62 [status setStringValue
:msg
];
65 [status setTextColor
:[NSColor redColor
]];
66 [progress stopAnimation
:self];
70 [status setTextColor
:[NSColor blackColor
]];
75 * Reads through a channel (in non-blocking) mode until there is no more left to read
76 * and then it returns. This calls sleep(1) so that the channel can have time to process.
77 * Be sure this is threaded otherwis the interface will stall.
79 - (void)readChannel
:(LIBSSH2_CHANNEL
*)channel
81 libssh2_channel_set_blocking(channel
, 0);
87 memset(&buf
, '\0', sizeof(buf
));
88 numbytes
= libssh2_channel_read(channel
, buf
, sizeof(buf
));
90 NSLog(@
"SSH buffer: %s", buf
);
91 } while (libssh2_poll_channel_read(channel
, 0));
95 * Sends an item to the printer
97 - (IBAction
)print
:(id)sender
99 [NSThread detachNewThreadSelector
:@selector(uploadAndPrint
:) toTarget
:self withObject
:sender
];
103 * Opens an SSH session, creates a SCP channel to upload the file, followed by a shell channel
106 - (void)uploadAndPrint
:(id)sender
108 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
110 [progress startAnimation
:self];
111 [progress setHidden
:NO
];
112 [status setHidden
:NO
];
115 struct stat fileInfo
;
116 const char *fileName
= [[self getUploadSafeName
:[dragRegion filePath
]] UTF8String
];
118 [self setStatus
:@
"Connecting to acs.bu.edu" isError
:NO
];
119 struct sockaddr_in sin
;
120 int sock
= socket(AF_INET
, SOCK_STREAM
, 0);
121 sin.sin_port
= htons(22);
122 sin.sin_family
= AF_INET
;
124 struct hostent
*host
= gethostbyname("acs.bu.edu");
125 memcpy(&sin.sin_addr
, host
->h_addr_list
[0], host
->h_length
);
127 if (stat([[dragRegion filePath
] UTF8String
], &fileInfo
))
129 return [self setStatus
:@
"Invalid file selected" isError
:YES
];
132 if (connect(sock
, (struct sockaddr
*)(&sin
), sizeof(struct sockaddr_in
)) != 0)
134 return [self setStatus
:@
"Could not connect to acs.bu.edu" isError
:YES
];
137 LIBSSH2_SESSION
*ssh
= libssh2_session_init();
140 return [self setStatus
:@
"Failed to initialize SSH context" isError
:YES
];
143 if (libssh2_session_startup(ssh
, sock
))
145 return [self setStatus
:@
"Could not tunnel over SSH" isError
:YES
];
148 if (libssh2_userauth_password(ssh
, [[username stringValue
] UTF8String
], [[password stringValue
] UTF8String
]))
150 [self setStatus
:@
"Bad username/password" isError
:YES
];
154 LIBSSH2_CHANNEL
*channel
= libssh2_scp_send(ssh
, fileName
, 0755, (unsigned long)fileInfo.st_size
);
157 [self setStatus
:@
"Unable to open upload SCP session" isError
:YES
];
161 [self setStatus
:@
"Uploading file..." isError
:NO
];
163 localFile
= fopen([[dragRegion filePath
] UTF8String
], "r");
166 int numread
, numwrote
;
169 numread
= fread(buf
, 1, sizeof(buf
), localFile
);
178 numwrote
= libssh2_channel_write(channel
, pbuf
, numread
);
181 } while (numwrote
> 0);
185 [self setStatus
:@
"File uploaded!" isError
:NO
];
187 libssh2_channel_send_eof(channel
);
188 libssh2_channel_wait_eof(channel
);
189 libssh2_channel_wait_closed(channel
);
190 libssh2_channel_free(channel
);
193 channel
= libssh2_channel_open_session(ssh
);
196 [self setStatus
:@
"Could not open SSH channel for printing" isError
:YES
];
200 if (libssh2_channel_request_pty(channel
, "vanilla"))
202 [self setStatus
:@
"Could not open ANSI TTY" isError
:YES
];
206 if (libssh2_channel_shell(channel
))
208 [self setStatus
:@
"Failed to open remote shell" isError
:YES
];
212 [self setStatus
:@
"Opened remote SSH shell" isError
:NO
];
215 [self readChannel
:channel
];
217 // ACS outage messages --> skip some more
218 char *delay
= "q\r\n\0";
219 libssh2_channel_write(channel
, delay
, sizeof(char) * strlen(delay
));
220 [self readChannel
:channel
];
222 // send the job to lpr
225 NSString
*printer
= [[printersController selection
] valueForKey
:@
"unixName"];
226 cmd
= (char *)[[NSString stringWithFormat
:@
"lpr -m -P%@ %s\r\n\0", printer
, fileName
] UTF8String
];
228 cmd
= "touch __PRINT__\r\n\0";
230 libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
));
231 [self readChannel
:channel
];
233 // remove our temp file
234 cmd
= (char *)[[NSString stringWithFormat
:@
"rm -f %s\r\n\0", fileName
] UTF8String
];
235 libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
));
236 [self readChannel
:channel
];
238 [self setStatus
:@
"Printed!" isError
:NO
];
240 libssh2_channel_send_eof(channel
);
241 libssh2_channel_eof(channel
);
242 libssh2_channel_close(channel
);
247 //libssh2_channel_free(channel);
250 libssh2_session_disconnect(ssh
, "Normal disconnect.");
251 libssh2_session_free(ssh
);
255 [progress stopAnimation
:self];
261 * Returns an NSString that is safe for uploding onto the server
263 - (NSString
*)getUploadSafeName
:(NSString
*)name
265 NSMutableString
*n
= [NSMutableString stringWithString
:name
];
266 [n replaceOccurrencesOfString
:@
" " withString
:@
"" options
:NSBackwardsSearch range
:NSMakeRange(0, [name length
])];
267 name
= (NSString
*)n
;
268 name
= [name lastPathComponent
];
269 name
= [name stringByDeletingPathExtension
];
270 if ([name length
] > 20)
271 name
= [name substringToIndex
:20];
274 return [NSString stringWithFormat
:@
"~/%@.%d.pdf", name
, random()];