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
)
26 - (void)setStatus
:(NSString
*)msg isError
:(BOOL)error
;
31 @implementation AppController
34 * Set up the printer list
38 if (self = [super init
])
40 printers
= [NSMutableArray array
];
42 NSArray
*values
= [NSArray arrayWithObjects
:@
"Double-Sided, Stapled $0.05", @
"publp", nil];
43 NSArray
*keys
= [NSArray arrayWithObjects
:@
"displayName", @
"unixName", nil];
44 [printers addObject
:[NSDictionary dictionaryWithObjects
:values forKeys
:keys
]];
46 values
= [NSArray arrayWithObjects
:@
"Single-Sided, Stapled $0.10", @
"pubps", nil];
47 [printers addObject
:[NSDictionary dictionaryWithObjects
:values forKeys
:keys
]];
49 values
= [NSArray arrayWithObjects
:@
"Double-Sided, Not Stapled $0.05", @
"publpns", nil];
50 [printers addObject
:[NSDictionary dictionaryWithObjects
:values forKeys
:keys
]];
52 values
= [NSArray arrayWithObjects
:@
"Single-Sided, Not Stapled $0.10", @
"pubpsns", nil];
53 [printers addObject
:[NSDictionary dictionaryWithObjects
:values forKeys
:keys
]];
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
]];
76 * Sends an item to the printer
78 - (IBAction
)print
:(id)sender
80 [progress startAnimation
:self];
81 [progress setHidden
:NO
];
82 [status setHidden
:NO
];
84 NSString
*printer
= [[printersController selection
] valueForKey
:@
"unixName"];
88 [self setStatus
:@
"Connecting to acs.bu.edu" isError
:NO
];
89 struct sockaddr_in sin
;
90 int sock
= socket(AF_INET
, SOCK_STREAM
, 0);
91 sin.sin_port
= htons(22);
92 sin.sin_family
= AF_INET
;
94 struct hostent
*host
= gethostbyname("acs.bu.edu");
95 memcpy(&sin.sin_addr
, host
->h_addr_list
[0], host
->h_length
);
97 if (connect(sock
, (struct sockaddr
*)(&sin
), sizeof(struct sockaddr_in
)) != 0)
99 return [self setStatus
:@
"Could not connect to acs.bu.edu" isError
:YES
];
102 LIBSSH2_SESSION
*ssh
= libssh2_session_init();
105 return [self setStatus
:@
"Failed to initialize SSH context" isError
:YES
];
108 if (libssh2_session_startup(ssh
, sock
))
110 return [self setStatus
:@
"Could not tunnel over SSH" isError
:YES
];
113 if (libssh2_userauth_password(ssh
, [[username stringValue
] UTF8String
], [[password stringValue
] UTF8String
]))
115 [self setStatus
:@
"Bad username/password" isError
:YES
];
119 stat([[dragRegion filePath] UTF8String], &fileInfo);
121 LIBSSH2_CHANNEL *channel = libssh2_scp_send(ssh, "~/tempupload.pdf", 0755, (unsigned long)fileInfo.st_size);
124 [self setStatus:@"Unable to open upload SCP session" isError:YES];
128 [self setStatus:@"Uploading file..." isError:NO];
130 localFile = fopen([[dragRegion filePath] UTF8String], "r");
133 int numread, numwrote;
136 numread = fread(buf, 1, sizeof(buf), localFile);
145 numwrote = libssh2_channel_write(channel, pbuf, numread);
148 } while (numwrote > 0);
152 [self setStatus:@"File uploaded!" isError:NO];
154 libssh2_channel_send_eof(channel);
155 libssh2_channel_wait_eof(channel);
156 libssh2_channel_wait_closed(channel);
157 libssh2_channel_free(channel);
161 LIBSSH2_CHANNEL
*channel
= libssh2_channel_open_session(ssh
);
164 [self setStatus
:@
"Could not open SSH channel for printing" isError
:YES
];
168 if (libssh2_channel_request_pty(channel
, "vanilla"))
170 [self setStatus
:@
"Could not open ANSI TTY" isError
:YES
];
174 if (libssh2_channel_shell(channel
))
176 [self setStatus
:@
"Failed to open remote shell" isError
:YES
];
180 [self setStatus
:@
"Opened remote SSH shell" isError
:NO
];
182 libssh2_channel_flush_ex(channel
, LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL
);
185 char *e = malloc(1024 * sizeof(char));
186 libssh2_session_last_error(ssh, &e, NULL, 0);
190 libssh2_channel_set_blocking(channel
, 0);
196 memset(&b
, '\0', sizeof(b
));
198 r
= libssh2_channel_read(channel
, b
, sizeof(b
));
199 NSLog(@
"b(%i) = %s", r
, b
);
202 char *cmd
= "touch /u17/ugrad/rsesek/temp.foo\r\n\0";
203 NSLog(@
"numbytes = %i", libssh2_channel_write(channel
, cmd
, sizeof(char) * strlen(cmd
)));
207 memset(&b
, '\0', sizeof(b
));
209 r
= libssh2_channel_read(channel
, b
, sizeof(b
));
210 NSLog(@
"b(%i) = %s", r
, b
);
213 [self setStatus
:@
"Printed!" isError
:NO
];
215 libssh2_channel_send_eof(channel
);
216 libssh2_channel_eof(channel
);
217 libssh2_channel_close(channel
);
222 libssh2_channel_free(channel
);
225 libssh2_session_disconnect(ssh
, "Normal disconnect.");
226 libssh2_session_free(ssh
);
230 [progress stopAnimation
:self];