xref: /original-bsd/old/talk/talk/invite.c (revision e58c8952)
1 /*-
2  * Copyright (c) 1983, 1985
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)invite.c	5.1 (Berkeley) 6/6/85";
10 #endif not lint
11 
12 #include "talk_ctl.h"
13 #include <sys/time.h>
14 #include <signal.h>
15 #include <setjmp.h>
16 
17 /*
18  * There wasn't an invitation waiting, so send a request containing
19  * our sockt address to the remote talk daemon so it can invite
20  * him
21  */
22 
23 /*
24  * The msg.id's for the invitations
25  * on the local and remote machines.
26  * These are used to delete the
27  * invitations.
28  */
29 int	local_id, remote_id;
30 void	re_invite();
31 jmp_buf invitebuf;
32 
33 invite_remote()
34 {
35 	int nfd, read_mask, template, new_sockt;
36 	struct itimerval itimer;
37 	CTL_RESPONSE response;
38 
39 	itimer.it_value.tv_sec = RING_WAIT;
40 	itimer.it_value.tv_usec = 0;
41 	itimer.it_interval = itimer.it_value;
42 	if (listen(sockt, 5) != 0)
43 		p_error("Error on attempt to listen for caller");
44 	msg.addr = my_addr;
45 	msg.id_num = -1;		/* an impossible id_num */
46 	invitation_waiting = 1;
47 	announce_invite();
48 	/*
49 	 * Shut off the automatic messages for a while,
50 	 * so we can use the interupt timer to resend the invitation
51 	 */
52 	end_msgs();
53 	setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
54 	message("Waiting for your party to respond");
55 	signal(SIGALRM, re_invite);
56 	(void) setjmp(invitebuf);
57 	while ((new_sockt = accept(sockt, 0, 0)) < 0) {
58 		if (errno == EINTR)
59 			continue;
60 		p_error("Unable to connect with your party");
61 	}
62 	close(sockt);
63 	sockt = new_sockt;
64 
65 	/*
66 	 * Have the daemons delete the invitations now that we
67 	 * have connected.
68 	 */
69 	current_state = "Waiting for your party to respond";
70 	start_msgs();
71 
72 	msg.id_num = local_id;
73 	ctl_transact(my_machine_addr, msg, DELETE, &response);
74 	msg.id_num = remote_id;
75 	ctl_transact(his_machine_addr, msg, DELETE, &response);
76 	invitation_waiting = 0;
77 }
78 
79 /*
80  * Routine called on interupt to re-invite the callee
81  */
82 void
83 re_invite()
84 {
85 
86 	message("Ringing your party again");
87 	current_line++;
88 	/* force a re-announce */
89 	msg.id_num = remote_id + 1;
90 	announce_invite();
91 	longjmp(invitebuf, 1);
92 }
93 
94 /*
95  * Transmit the invitation and process the response
96  */
97 announce_invite()
98 {
99 	CTL_RESPONSE response;
100 
101 	current_state = "Trying to connect to your party's talk daemon";
102 	ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
103 	remote_id = response.id_num;
104 	if (response.answer != SUCCESS) {
105 		switch (response.answer) {
106 
107 		case NOT_HERE :
108 			message("Your party is not logged on");
109 			break;
110 
111 		case MACHINE_UNKNOWN :
112 			message("Target machine does not recognize us");
113 			break;
114 
115 		case UNKNOWN_REQUEST :
116 			message("Target machine can not handle remote talk");
117 			break;
118 
119 		case FAILED :
120 			message("Target machine is too confused to talk to us");
121 			break;
122 
123 		case PERMISSION_DENIED :
124 			message("Your party is refusing messages");
125 			break;
126 		}
127 		quit();
128 	}
129 	/* leave the actual invitation on my talk daemon */
130 	ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
131 	local_id = response.id_num;
132 }
133 
134 /*
135  * Tell the daemon to remove your invitation
136  */
137 send_delete()
138 {
139 
140 	msg.type = DELETE;
141 	/*
142 	 * This is just a extra clean up, so just send it
143 	 * and don't wait for an answer
144 	 */
145 	msg.id_num = remote_id;
146 	daemon_addr.sin_addr = his_machine_addr;
147 	if (sendto(ctl_sockt, &msg, sizeof(CTL_MSG), 0,
148 	    (struct sockaddr *)&daemon_addr,
149 	    sizeof(daemon_addr)) != sizeof(CTL_MSG))
150 		perror("send_delete remote");
151 	msg.id_num = local_id;
152 	daemon_addr.sin_addr = my_machine_addr;
153 	if (sendto(ctl_sockt, &msg, sizeof(CTL_MSG), 0,
154 	    (struct sockaddr *)&daemon_addr,
155 	    sizeof(daemon_addr)) != sizeof(CTL_MSG))
156 		perror("send_delete local");
157 }
158