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 arrayWithContentsOfFile
:[[NSBundle mainBundle
] pathForResource
:@
"Printers" ofType
:@
"plist"]];
48 * Sets the status text
50 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
52 [status setStringValue
:msg
];
55 [status setTextColor
:[NSColor redColor
]];
56 [progress stopAnimation
:self];
60 [status setTextColor
:[NSColor blackColor
]];
65 * Reads through a channel (in non-blocking) mode until there is no more left to read
66 * and then it returns. This calls sleep(1) so that the channel can have time to process.
67 * Be sure this is threaded otherwis the interface will stall.
69 - (void)readChannel
:(LIBSSH2_CHANNEL
*)channel
71 libssh2_channel_set_blocking(channel
, 0);
77 memset(&buf
, '\0', sizeof(buf
));
79 numbytes
= libssh2_channel_read(channel
, buf
, sizeof(buf
));
81 NSLog(@
"buf: %s", buf
);
83 } while (numbytes
> 0);
87 * Sends an item to the printer
89 - (IBAction
)print
:(id)sender
91 [NSThread detachNewThreadSelector
:@selector(uploadAndPrint
:) toTarget
:self withObject
:sender
];
95 * Opens an SSH session, creates a SCP channel to upload the file, followed by a shell channel
98 - (void)uploadAndPrint
:(id)sender
100 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
102 [progress startAnimation
:self];
103 [progress setHidden
:NO
];
104 [status setHidden
:NO
];
106 NSString
*printer
= [[printersController selection
] valueForKey
:@
"unixName"];
108 struct stat fileInfo
;
110 [self setStatus
:@
"Connecting to acs.bu.edu" isError
:NO
];
111 struct sockaddr_in sin
;
112 int sock
= socket(AF_INET
, SOCK_STREAM
, 0);
113 sin.sin_port
= htons(22);
114 sin.sin_family
= AF_INET
;
116 struct hostent
*host
= gethostbyname("acs.bu.edu");
117 memcpy(&sin.sin_addr
, host
->h_addr_list
[0], host
->h_length
);
119 if (stat([[dragRegion filePath
] UTF8String
], &fileInfo
))
121 return [self setStatus
:@
"Invalid file selected" isError
:YES
];
124 if (connect(sock
, (struct sockaddr
*)(&sin
), sizeof(struct sockaddr_in
)) != 0)
126 return [self setStatus
:@
"Could not connect to acs.bu.edu" isError
:YES
];
129 LIBSSH2_SESSION
*ssh
= libssh2_session_init();
132 return [self setStatus
:@
"Failed to initialize SSH context" isError
:YES
];
135 if (libssh2_session_startup(ssh
, sock
))
137 return [self setStatus
:@
"Could not tunnel over SSH" isError
:YES
];
140 if (libssh2_userauth_password(ssh
, [[username stringValue
] UTF8String
], [[password stringValue
] UTF8String
]))
142 [self setStatus
:@
"Bad username/password" isError
:YES
];
146 LIBSSH2_CHANNEL
*channel
= libssh2_scp_send(ssh
, "~/__bu_print_drop__.pdf", 0755, (unsigned long)fileInfo.st_size
);
149 [self setStatus
:@
"Unable to open upload SCP session" isError
:YES
];
153 [self setStatus
:@
"Uploading file..." isError
:NO
];
155 localFile
= fopen([[dragRegion filePath
] UTF8String
], "r");
158 int numread
, numwrote
;
161 numread
= fread(buf
, 1, sizeof(buf
), localFile
);
170 numwrote
= libssh2_channel_write(channel
, pbuf
, numread
);
173 } while (numwrote
> 0);
177 [self setStatus
:@
"File uploaded!" isError
:NO
];
179 libssh2_channel_send_eof(channel
);
180 libssh2_channel_wait_eof(channel
);
181 libssh2_channel_wait_closed(channel
);
182 libssh2_channel_free(channel
);
185 channel
= libssh2_channel_open_session(ssh
);
188 [self setStatus
:@
"Could not open SSH channel for printing" isError
:YES
];
192 if (libssh2_channel_request_pty(channel
, "vanilla"))
194 [self setStatus
:@
"Could not open ANSI TTY" isError
:YES
];
198 if (libssh2_channel_shell(channel
))
200 [self setStatus
:@
"Failed to open remote shell" isError
:YES
];
204 [self setStatus
:@
"Opened remote SSH shell" isError
:NO
];
207 [self readChannel
:channel
];
209 // send the job to lpr
210 char *cmd
= (char *)[[NSString stringWithFormat
:@
"lpr -m -P%@ __bu_print_drop__.pdf\r\n\0", printer
] UTF8String
];
211 libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
));
212 [self readChannel
:channel
];
214 // remove our temp file
215 cmd
= "rm -f __bu_print_drop__.pdf\r\n\0";
216 libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
));
217 [self readChannel
:channel
];
219 [self setStatus
:@
"Printed!" isError
:NO
];
221 libssh2_channel_send_eof(channel
);
222 libssh2_channel_eof(channel
);
223 libssh2_channel_close(channel
);
228 //libssh2_channel_free(channel);
231 libssh2_session_disconnect(ssh
, "Normal disconnect.");
232 libssh2_session_free(ssh
);
236 [progress stopAnimation
:self];