File: names.c

package info (click to toggle)
gnome-network 1.0.2-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,968 kB
  • ctags: 1,007
  • sloc: ansic: 8,129; sh: 6,837; objc: 961; makefile: 251
file content (215 lines) | stat: -rw-r--r-- 5,316 bytes parent folder | download | duplicates (2)
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
/* GnomeTalk: Get names interface module
 * (C) 1997 the Free Software Foundation
 *
 * Author: Federico Mena
 */

#include <config.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <pwd.h>
#include <string.h>
#include <sys/param.h>
#include <unistd.h>
#include "gnome.h"
#include "global.h"
#include "names.h"
#include "protocol.h"


static GtkWidget *dialog;
static GtkWidget *user_entry;
static GtkWidget *tty_check_button;
static GtkWidget *tty_entry;


static void
toggle_tty(GtkWidget *widget, gpointer *data)
{
	int active;

	active = GTK_TOGGLE_BUTTON(widget)->active;
	
	gtk_widget_set_sensitive(tty_entry, active);

	if (active)
		gtk_widget_grab_focus(tty_entry); /* this is a nicety */
}


static void
ok_callback(GtkWidget *widget, gpointer data)
{
	char *user, *tty;
	
	user = g_strdup(gtk_entry_get_text(GTK_ENTRY(gnome_entry_gtk_entry(GNOME_ENTRY(user_entry)))));

	if (GTK_TOGGLE_BUTTON(tty_check_button)->active)
		tty = g_strdup(gtk_entry_get_text(GTK_ENTRY(tty_entry)));
	else
		tty = NULL;
	
	gtk_widget_destroy(dialog);

	if (!init_names(user, tty)){
		gtk_widget_destroy (gtk_widget_get_toplevel (widget));
		get_names_from_user ();
	}
}


static void
cancel_callback(GtkWidget *widget, gpointer data)
{
	gtk_widget_destroy(dialog);
	gtk_main_quit();
}


void
get_names_from_user(void)
{
	GtkWidget *vbox;
	GtkWidget *aa;
	GtkWidget *table;
	GtkWidget *label;
	GtkWidget *button;

	dialog = gtk_dialog_new();
	gtk_window_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
	gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, TRUE);
	gtk_window_set_title(GTK_WINDOW(dialog), _("Talk to"));
	gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
			   (GtkSignalFunc) cancel_callback,
			   NULL);
	
	vbox = GTK_DIALOG(dialog)->vbox;
	aa = GTK_DIALOG(dialog)->action_area;

	table = gtk_table_new(2, 2, FALSE);
	gtk_container_border_width(GTK_CONTAINER(table), 6);
	gtk_table_set_row_spacings(GTK_TABLE(table), 4);
	gtk_table_set_col_spacings(GTK_TABLE(table), 4);
	gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
	gtk_widget_show(table);

	label = gtk_label_new(_("Talk to"));
	gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
	gtk_widget_show(label);

	user_entry = gnome_entry_new("talkto");
	gtk_table_attach_defaults(GTK_TABLE(table), user_entry, 1, 2, 0, 1);
	gtk_widget_show(user_entry);
	gtk_signal_connect (GTK_OBJECT (gnome_entry_gtk_entry(GNOME_ENTRY(user_entry))), "activate", (GtkSignalFunc) ok_callback, NULL);

	tty_check_button = gtk_check_button_new_with_label(_("Specify TTY"));
	gtk_table_attach_defaults(GTK_TABLE(table), tty_check_button, 0, 1, 1, 2);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tty_check_button), FALSE);
	gtk_signal_connect(GTK_OBJECT(tty_check_button), "toggled",
			   (GtkSignalFunc) toggle_tty,
			   NULL);
	gtk_widget_show(tty_check_button);

	tty_entry = gtk_entry_new();
	gtk_table_attach_defaults(GTK_TABLE(table), tty_entry, 1, 2, 1, 2);
	gtk_widget_set_sensitive(tty_entry, FALSE);
	gtk_widget_show(tty_entry);

	gtk_container_border_width(GTK_CONTAINER(aa), 6);

	button = gnome_stock_button (GNOME_STOCK_BUTTON_OK);
	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
	gtk_box_pack_start(GTK_BOX(aa), button, TRUE, TRUE, 0);
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   (GtkSignalFunc) ok_callback,
			   NULL);
	gtk_widget_grab_default(button);
	gtk_widget_show(button);

	button = gnome_stock_button (GNOME_STOCK_BUTTON_CANCEL);
	GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
	gtk_box_pack_start(GTK_BOX(aa), button, TRUE, TRUE, 0);
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			   (GtkSignalFunc) cancel_callback,
			   NULL);
	gtk_widget_show(button);

	gtk_widget_grab_focus(user_entry);
	gtk_widget_show(dialog);
}


static gboolean
get_addrs(char *my_machine, char *his_machine)
{
	struct hostent *host;

	host = gethostbyname(my_machine);
	if (!host) {
		talk_abort (_("Failed to find the address for this host"));
		return FALSE;
	}
	memcpy(&our_machine_addr.s_addr, host->h_addr, sizeof(our_machine_addr.s_addr));

	if (strcmp(my_machine, his_machine) != 0) {
		/* Lookup remote machine */

		host = gethostbyname(his_machine);
		if (!host) {
			talk_abort (_("Failed to find the address for the remote host"));
			return FALSE;
		}
		memcpy(&his_machine_addr.s_addr, host->h_addr, sizeof(our_machine_addr.s_addr));
	} else
		his_machine_addr = our_machine_addr; /* same machine, so just copy addrs */

	return TRUE;
}


gboolean
init_names(char *user, char *tty)
{
	char *me, *he;
	char  my_hostname[MAXHOSTNAMELEN];
	char *my_machine, *his_machine;
	struct passwd *pw;

	me = getlogin();
	if (!me || !me[0]) {
		pw = getpwuid(getuid());
		if (!pw) {
			fprintf(stderr, "init_names: Could not get username!\n");
			exit(1);
		}

		me = pw->pw_name;
	}

	gethostname(my_hostname, sizeof(my_hostname));
	my_machine = my_hostname;

	he = g_strdup(user);

	his_machine = strchr(he, '@');
	if (his_machine)
		*his_machine++ = 0; /* wipe and skip over @ */
	else
		his_machine = my_machine; /* local user */

	if (tty)
		his_tty = tty;
	else
		his_tty = "";

	our_name = me;
	his_name = user;

	if (!get_addrs(my_machine, his_machine))
		return FALSE;
	
	create_connection(me, he, his_tty);
	return TRUE;
}