1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
/*
* $Id$
*
* Note that protocols except ssh aren't supported if USE_LIBSSH2 is defined.
*/
#include <kiklib/kik_def.h> /* USE_WIN32API */
#if defined(USE_WIN32API) || defined(USE_LIBSSH2)
#include "../x_connect_dialog.h"
#include <stdio.h> /* sprintf */
#include <kiklib/kik_mem.h> /* malloc */
#include <kiklib/kik_str.h> /* strdup */
#include <kiklib/kik_debug.h>
#include <kiklib/kik_path.h> /* kik_parse_uri */
/* --- static variables --- */
static int selected_proto = -1 ;
static char ** server_list ;
static char * default_server ;
/* These variables are set in IDOK. If empty string is input, nothing is set (==NULL). */
static char * selected_server ;
static char * selected_port ;
static char * selected_user ;
static char * selected_pass ;
static char * selected_encoding ;
static char * selected_exec_cmd ;
/* --- static functions --- */
/*
* Parsing "<proto>://<user>@<host>:<port>:<encoding>".
*/
static int
parse(
int * protoid , /* If seq doesn't have proto, -1 is set. */
char ** user , /* If seq doesn't have user, NULL is set. */
char ** host ,
char ** port ,
char ** encoding , /* If seq doesn't have encoding, NULL is set. */
char * seq /* broken in this function. */
)
{
char * proto ;
if( ! kik_parse_uri( &proto , user , host , port , NULL , encoding , seq))
{
return 0 ;
}
if( proto)
{
if( strcmp( proto , "ssh") == 0)
{
*protoid = IDD_SSH ;
}
else if( strcmp( proto , "telnet") == 0)
{
*protoid = IDD_TELNET ;
}
else if( strcmp( proto , "rlogin") == 0)
{
*protoid = IDD_RLOGIN ;
}
else
{
*protoid = -1 ;
}
}
else
{
*protoid = -1 ;
}
return 1 ;
}
static char *
get_window_text(
HWND win
)
{
char * p ;
int len ;
if( ( len = GetWindowTextLength( win)) > 0 && ( p = malloc( len + 1)) )
{
if( GetWindowText( win , p , len + 1) > 0)
{
return p ;
}
free( p) ;
}
return NULL ;
}
LRESULT CALLBACK dialog_proc(
HWND dlgwin ,
UINT msg ,
WPARAM wparam ,
LPARAM lparam
)
{
switch(msg)
{
case WM_INITDIALOG:
{
HWND win ;
HWND focus_win ;
char * user_env ;
focus_win = None ;
win = GetDlgItem( dlgwin , IDD_LIST) ;
if( server_list)
{
int count ;
for( count = 0 ; server_list[count] ; count++)
{
SendMessage( win , CB_ADDSTRING , 0 ,
(LPARAM)server_list[count]) ;
}
if( count > 1)
{
focus_win = win ;
}
}
else
{
EnableWindow( win , FALSE) ;
}
selected_proto = IDD_SSH ;
user_env = getenv( "USERNAME") ;
if( default_server)
{
LRESULT res ;
char * user ;
int proto ;
char * server ;
char * port ;
char * encoding ;
res = SendMessage( win , CB_FINDSTRINGEXACT , 0 ,
(LPARAM)default_server) ;
if( res != CB_ERR)
{
SendMessage( win , CB_SETCURSEL , res , 0) ;
}
if( parse( &proto , &user , &server , &port , &encoding ,
kik_str_alloca_dup( default_server)) )
{
SetWindowText( GetDlgItem( dlgwin , IDD_SERVER) , server) ;
if( port)
{
SetWindowText( GetDlgItem( dlgwin , IDD_PORT) ,
port) ;
}
if( user || ( user = user_env))
{
SetWindowText( GetDlgItem( dlgwin , IDD_USER) ,
user) ;
}
#ifndef USE_LIBSSH2
if( proto != -1)
{
selected_proto = proto ;
}
#endif
if( encoding)
{
SetWindowText( GetDlgItem( dlgwin , IDD_ENCODING) ,
encoding) ;
}
}
}
else if( user_env)
{
SetWindowText( GetDlgItem( dlgwin , IDD_USER) , user_env) ;
}
#ifdef USE_LIBSSH2
EnableWindow( GetDlgItem( dlgwin , IDD_TELNET) , FALSE) ;
EnableWindow( GetDlgItem( dlgwin , IDD_RLOGIN) , FALSE) ;
CheckRadioButton( dlgwin , IDD_SSH , IDD_RLOGIN , IDD_SSH) ;
#else
CheckRadioButton( dlgwin , IDD_SSH , IDD_RLOGIN , selected_proto) ;
#endif
if( focus_win)
{
SetFocus( focus_win) ;
}
else
{
SetFocus( GetDlgItem( dlgwin , selected_proto)) ;
}
return FALSE ;
}
case WM_COMMAND:
switch( LOWORD(wparam))
{
case IDOK:
{
selected_server = get_window_text(
GetDlgItem( dlgwin , IDD_SERVER)) ;
selected_port = get_window_text(
GetDlgItem( dlgwin , IDD_PORT)) ;
selected_user = get_window_text(
GetDlgItem( dlgwin , IDD_USER)) ;
selected_pass = get_window_text(
GetDlgItem( dlgwin , IDD_PASS)) ;
selected_encoding = get_window_text(
GetDlgItem( dlgwin , IDD_ENCODING)) ;
selected_exec_cmd = get_window_text(
GetDlgItem( dlgwin , IDD_EXEC_CMD)) ;
EndDialog( dlgwin , IDOK) ;
break ;
}
case IDCANCEL:
selected_proto = -1 ;
EndDialog( dlgwin , IDCANCEL) ;
break ;
case IDD_LIST:
if( HIWORD(wparam) == CBN_SELCHANGE)
{
Window win ;
LRESULT idx ;
LRESULT len ;
char * seq ;
char * user ;
int proto ;
char * server ;
char * port ;
char * encoding ;
win = GetDlgItem( dlgwin , IDD_LIST) ;
if( ( idx = SendMessage( win , CB_GETCURSEL , 0 , 0)) == CB_ERR ||
( len = SendMessage( win , CB_GETLBTEXTLEN , idx , 0)) == CB_ERR ||
( seq = alloca( len + 1)) == NULL ||
( SendMessage( win , CB_GETLBTEXT , idx , seq)) == CB_ERR )
{
seq = NULL ;
}
if( seq &&
parse( &proto , &user , &server , &port , &encoding , seq))
{
SetWindowText( GetDlgItem( dlgwin , IDD_SERVER) , server) ;
if( port)
{
SetWindowText( GetDlgItem( dlgwin , IDD_PORT) ,
port) ;
}
if( user || ( user = getenv( "USERNAME")) || ( user = ""))
{
SetWindowText( GetDlgItem( dlgwin , IDD_USER) ,
user) ;
}
if( proto == -1)
{
selected_proto = IDD_SSH ;
}
else
{
selected_proto = proto ;
}
if( encoding)
{
SetWindowText( GetDlgItem( dlgwin , IDD_ENCODING) ,
encoding) ;
}
CheckRadioButton( dlgwin , IDD_SSH , IDD_RLOGIN ,
selected_proto) ;
}
}
break ;
case IDD_SSH:
case IDD_TELNET:
case IDD_RLOGIN:
selected_proto = LOWORD(wparam) ;
CheckRadioButton( dlgwin, IDD_SSH, IDD_RLOGIN, selected_proto) ;
return TRUE ;
default:
return FALSE ;
}
default:
return FALSE;
}
return TRUE ;
}
/* --- global functions --- */
int
x_connect_dialog(
char ** uri , /* Should be free'ed by those who call this. */
char ** pass , /* Same as uri. If pass is not input, "" is set. */
char ** exec_cmd , /* Same as uri. If exec_cmd is not input, NULL is set. */
char * display_name ,
Window parent_window ,
char ** sv_list ,
char * def_server /* (<user>@)(<proto>:)<server address>(:<encoding>). */
)
{
int ret ;
char * proto ;
server_list = sv_list ;
default_server = def_server ;
#ifdef DEBUG
{
char ** p ;
kik_debug_printf( "DEFAULT server %s\n" , default_server) ;
if( server_list)
{
kik_debug_printf( "SERVER LIST ") ;
p = server_list ;
while( *p)
{
kik_msg_printf( "%s " , *p) ;
p ++ ;
}
kik_msg_printf( "\n") ;
}
}
#endif
DialogBox( GetModuleHandle(NULL) , "ConnectDialog" , parent_window ,
(DLGPROC)dialog_proc) ;
ret = 0 ;
if( selected_server == NULL)
{
goto end ;
}
else if( selected_proto == IDD_SSH)
{
proto = "ssh://" ;
}
else if( selected_proto == IDD_TELNET)
{
proto = "telnet://" ;
}
else if( selected_proto == IDD_RLOGIN)
{
proto = "rlogin://" ;
}
else
{
goto end ;
}
if( ! ( *uri = malloc( strlen(proto) +
(selected_user ? strlen(selected_user) + 1 : 0) +
strlen(selected_server) + 1 +
(selected_port ? strlen(selected_port) + 1 : 0) +
(selected_encoding ? strlen(selected_encoding) + 1 : 0))) )
{
goto end ;
}
(*uri)[0] = '\0' ;
strcat( *uri , proto) ;
if( selected_user)
{
strcat( *uri , selected_user) ;
strcat( *uri , "@") ;
}
strcat( *uri , selected_server) ;
if( selected_port)
{
strcat( *uri , ":") ;
strcat( *uri , selected_port) ;
}
if( selected_encoding)
{
strcat( *uri , ":") ;
strcat( *uri , selected_encoding) ;
}
*pass = selected_pass ? selected_pass : strdup( "") ;
*exec_cmd = selected_exec_cmd ;
/* Successfully */
ret = 1 ;
end:
selected_proto = -1 ;
server_list = NULL ;
default_server = NULL ;
free( selected_server) ;
selected_server = NULL ;
free( selected_port) ;
selected_port = NULL ;
free( selected_user) ;
selected_user = NULL ;
free( selected_encoding) ;
selected_encoding = NULL ;
free( selected_exec_cmd) ;
selected_exec_cmd = NULL ;
if( ret == 0)
{
free( selected_pass) ;
selected_pass = NULL ;
}
return ret ;
}
#endif
|