1 | /* |
---|
2 | * |
---|
3 | * Copyright (C) 1989 by the Massachusetts Institute of Technology |
---|
4 | * Developed by the MIT Student Information Processing Board (SIPB). |
---|
5 | * For copying information, see the file mit-copyright.h in this release. |
---|
6 | * |
---|
7 | */ |
---|
8 | /* |
---|
9 | * $Id: goto.c,v 1.22 2006-03-10 07:11:31 ghudson Exp $ |
---|
10 | * |
---|
11 | * Code for "goto" request in discuss. |
---|
12 | * |
---|
13 | */ |
---|
14 | |
---|
15 | #ifndef lint |
---|
16 | static char rcsid_discuss_c[] = |
---|
17 | "$Id: goto.c,v 1.22 2006-03-10 07:11:31 ghudson Exp $"; |
---|
18 | #endif /* lint */ |
---|
19 | |
---|
20 | #include <stdio.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <sys/file.h> |
---|
23 | #include <signal.h> |
---|
24 | #include <string.h> |
---|
25 | #include <sys/wait.h> |
---|
26 | #include <ctype.h> |
---|
27 | #include <ss/ss.h> |
---|
28 | #include <discuss/discuss.h> |
---|
29 | #include <time.h> |
---|
30 | #include "globals.h" |
---|
31 | |
---|
32 | #ifdef lint |
---|
33 | #define DONT_USE(var) var=var; |
---|
34 | #else /* lint */ |
---|
35 | #define DONT_USE(var) ; |
---|
36 | #endif /* lint */ |
---|
37 | |
---|
38 | #define FREE(ptr) { if (ptr) free(ptr); } |
---|
39 | #ifndef max |
---|
40 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
---|
41 | #endif |
---|
42 | |
---|
43 | extern ss_request_table discuss_cmds; |
---|
44 | |
---|
45 | /* EXTERNAL ROUTINES */ |
---|
46 | |
---|
47 | tfile unix_tfile(); |
---|
48 | void leave_mtg (); |
---|
49 | |
---|
50 | goto_mtg(argc, argv) |
---|
51 | int argc; |
---|
52 | char **argv; |
---|
53 | { |
---|
54 | if (argc != 2) { |
---|
55 | (void) fprintf(stderr, "Usage: %s mtg_name\n", argv[0]); |
---|
56 | return; |
---|
57 | } |
---|
58 | |
---|
59 | switch_to_mtg(argv[1]); |
---|
60 | } |
---|
61 | |
---|
62 | switch_to_mtg(name) |
---|
63 | char *name; |
---|
64 | { |
---|
65 | int code; |
---|
66 | name_blk nb; |
---|
67 | |
---|
68 | leave_mtg(); |
---|
69 | |
---|
70 | dsc_get_mtg (user_id, name, &nb, &code); |
---|
71 | if (code != 0) { |
---|
72 | ss_perror(sci_idx, DISC_MTG_NOT_FOUND, name); |
---|
73 | return; |
---|
74 | } |
---|
75 | |
---|
76 | switch_to_mtg_nb (&nb); |
---|
77 | dsc_destroy_name_blk (&nb); |
---|
78 | } |
---|
79 | |
---|
80 | switch_to_mtg_nb(nbp) |
---|
81 | name_blk *nbp; |
---|
82 | { |
---|
83 | int code, have_a, have_w, dummy; |
---|
84 | char msgbuf[80],*old_hostname,*old_pathname; |
---|
85 | trn_info t_info; |
---|
86 | |
---|
87 | /* Check to see if we're switching to same meeting. */ |
---|
88 | if (dsc_public.attending |
---|
89 | && !strcmp(dsc_public.path, nbp->pathname) |
---|
90 | && !strcmp(dsc_public.host, nbp ->hostname)) { |
---|
91 | nbp -> date_attended = time((time_t *) 0); |
---|
92 | nbp -> last = dsc_public.highest_seen; |
---|
93 | } |
---|
94 | |
---|
95 | leave_mtg(); |
---|
96 | |
---|
97 | dsc_copy_name_blk(nbp, &dsc_public.nb); |
---|
98 | dsc_public.host = dsc_public.nb.hostname; /* warning - sharing */ |
---|
99 | dsc_public.path = dsc_public.nb.pathname; |
---|
100 | dsc_get_mtg_info(&dsc_public.nb, |
---|
101 | &dsc_public.m_info, &code); |
---|
102 | if (code != 0) { |
---|
103 | while (code == MTG_MOVED) { |
---|
104 | /* Meeting has moved. In this case, dsc_public.m_info.long_name |
---|
105 | should contain the hostname/pathname for the meeting. |
---|
106 | We should update our information to reflect this change. */ |
---|
107 | old_hostname = dsc_public.nb.hostname; |
---|
108 | dsc_public.nb.hostname = malloc(strlen(dsc_public.m_info.long_name)+1); |
---|
109 | strcpy(dsc_public.nb.hostname, dsc_public.m_info.long_name); |
---|
110 | old_pathname = dsc_public.nb.pathname; |
---|
111 | dsc_public.nb.pathname = malloc(strlen(dsc_public.m_info.location)+1); |
---|
112 | strcpy(dsc_public.nb.pathname, dsc_public.m_info.location); |
---|
113 | dsc_public.host = dsc_public.nb.hostname; /* warning - sharing */ |
---|
114 | dsc_public.path = dsc_public.nb.pathname; |
---|
115 | dsc_get_mtg_info(&dsc_public.nb, |
---|
116 | &dsc_public.m_info, &code); |
---|
117 | if (code != 0 && code != MTG_MOVED) { |
---|
118 | fprintf(stderr, "Error checking moved meeting %s. %s\n", |
---|
119 | dsc_public.nb.aliases[0], error_message(code)); |
---|
120 | dsc_public.host = NULL; |
---|
121 | } else { |
---|
122 | fprintf(stdout, "Warning: %s moved to %s:%s\n", |
---|
123 | dsc_public.nb.aliases[0], dsc_public.host, dsc_public.path); |
---|
124 | /* Delete old meeting */ |
---|
125 | dsc_public.nb.hostname = old_hostname; |
---|
126 | dsc_public.nb.pathname = old_pathname; |
---|
127 | dsc_public.nb.status |= DSC_ST_DELETED; |
---|
128 | dsc_update_mtg_set(user_id, &dsc_public.nb, 1, &dummy); |
---|
129 | dsc_public.nb.status &= ~(DSC_ST_DELETED); |
---|
130 | free(dsc_public.nb.hostname); |
---|
131 | dsc_public.nb.hostname = dsc_public.host; |
---|
132 | free(dsc_public.nb.pathname); |
---|
133 | dsc_public.nb.pathname = dsc_public.path; |
---|
134 | } |
---|
135 | } |
---|
136 | if (code != 0) { |
---|
137 | if (code == NO_ACCESS) |
---|
138 | code = CANT_ATTEND; |
---|
139 | (void) fprintf(stderr, |
---|
140 | "Error going to %s: %s\n", |
---|
141 | dsc_public.nb.aliases[0], error_message(code)); |
---|
142 | dsc_public.host = (char *)NULL; |
---|
143 | return; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | dsc_public.mtg_name = (char *)malloc((unsigned)strlen(dsc_public.m_info.long_name)+1); |
---|
148 | (void) strcpy(dsc_public.mtg_name, dsc_public.m_info.long_name); |
---|
149 | |
---|
150 | dsc_public.attending = TRUE; |
---|
151 | dsc_public.highest_seen = dsc_public.current = dsc_public.nb.last; |
---|
152 | |
---|
153 | /* Check to see if meeting has been accidentally truncated */ |
---|
154 | if (dsc_public.highest_seen > dsc_public.m_info.highest) { |
---|
155 | printf ("Warning: Transactions appear to have been lost.\n"); |
---|
156 | dsc_public.highest_seen = dsc_public.current = 0; |
---|
157 | } |
---|
158 | |
---|
159 | printf ("%s meeting: %d new, %d last.", |
---|
160 | dsc_public.m_info.long_name, |
---|
161 | max (dsc_public.m_info.last - dsc_public.highest_seen, 0), |
---|
162 | dsc_public.m_info.last); |
---|
163 | if (acl_is_subset("c", dsc_public.m_info.access_modes)) |
---|
164 | strcpy(msgbuf, "you are a chairman"); |
---|
165 | else |
---|
166 | msgbuf[0] = '\0'; |
---|
167 | have_w = acl_is_subset("w", dsc_public.m_info.access_modes); |
---|
168 | have_a = acl_is_subset("a", dsc_public.m_info.access_modes); |
---|
169 | if (have_w && have_a) |
---|
170 | goto done; |
---|
171 | if (msgbuf[0]) |
---|
172 | strcat(msgbuf, "; "); |
---|
173 | if (!have_w) |
---|
174 | strcat(msgbuf, |
---|
175 | have_a ? "reply access only" : "read access only"); |
---|
176 | else |
---|
177 | strcat(msgbuf, "no replies"); |
---|
178 | done: |
---|
179 | if (msgbuf[0]) { |
---|
180 | /* the first character is currently lower-case */ |
---|
181 | msgbuf[0] = toupper (msgbuf[0]); |
---|
182 | printf(" (%s.)", msgbuf); |
---|
183 | } |
---|
184 | printf("\n"); |
---|
185 | |
---|
186 | /* See if our current transaction is deleted. If so, |
---|
187 | * rewind ourselves to the previous non-deleted transaction. |
---|
188 | * Sigh. This will be slow if there is a lot of deleted |
---|
189 | * transactions. Can you say "Forced to be like forum?" */ |
---|
190 | if (dsc_public.current == 0) |
---|
191 | return; |
---|
192 | if (dsc_public.current < dsc_public.m_info.first) { |
---|
193 | /* don't fall off the ends */ |
---|
194 | dsc_public.current = dsc_public.m_info.first; |
---|
195 | } |
---|
196 | else if (dsc_public.current > dsc_public.m_info.last) |
---|
197 | dsc_public.current = dsc_public.m_info.last; |
---|
198 | else { |
---|
199 | code = DELETED_TRN; |
---|
200 | while (code == DELETED_TRN) { |
---|
201 | dsc_get_trn_info (&dsc_public.nb, dsc_public.current, |
---|
202 | &t_info, &code); |
---|
203 | dsc_destroy_trn_info(&t_info); |
---|
204 | if (code == DELETED_TRN) |
---|
205 | dsc_public.current--; |
---|
206 | else if (code != 0) { |
---|
207 | ss_perror(sci_idx, code, |
---|
208 | "Looking for non-deleted transaction"); |
---|
209 | break; |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | /* |
---|
216 | * |
---|
217 | * leave_mtg () -- Internal routine to leave the current meeting, updating |
---|
218 | * all the stuff we need. Not a light-weight operation. |
---|
219 | * |
---|
220 | */ |
---|
221 | |
---|
222 | void leave_mtg() |
---|
223 | { |
---|
224 | int code; |
---|
225 | mtg_info minfo; |
---|
226 | |
---|
227 | if (!dsc_public.attending) |
---|
228 | return; /* bye, jack */ |
---|
229 | if (dsc_public.host == (char *)NULL) { |
---|
230 | ss_perror (sci_idx, 0, "program error: meeting state inconsistent!"); |
---|
231 | #ifdef DEBUG |
---|
232 | fprintf (stderr, "leave_mtg: attending meeting, no host\n"); |
---|
233 | /* we should have some sort of debug mode... */ |
---|
234 | abort (); |
---|
235 | #endif |
---|
236 | return; |
---|
237 | } |
---|
238 | |
---|
239 | dsc_public.nb.date_attended = time((time_t *)0); |
---|
240 | dsc_public.nb.last = dsc_public.highest_seen; |
---|
241 | dsc_get_mtg_info (&dsc_public.nb, &minfo, &code); |
---|
242 | if (!code) { |
---|
243 | if (dsc_public.highest_seen == dsc_public.m_info.last) |
---|
244 | dsc_public.nb.status &= ~ DSC_ST_CHANGED; |
---|
245 | } |
---|
246 | dsc_destroy_mtg_info (&minfo); |
---|
247 | dsc_update_mtg_set (user_id, &dsc_public.nb, 1, &code); |
---|
248 | |
---|
249 | dsc_destroy_name_blk (&dsc_public.nb); |
---|
250 | |
---|
251 | /* done with everything. start nuking stuff */ |
---|
252 | dsc_public.current = 0; |
---|
253 | dsc_public.highest_seen = 0; |
---|
254 | dsc_public.attending = FALSE; |
---|
255 | dsc_public.host = (char *)NULL; |
---|
256 | dsc_public.path = (char *)NULL; |
---|
257 | |
---|
258 | /* Don't forget the women and children... */ |
---|
259 | FREE (dsc_public.mtg_name); |
---|
260 | dsc_public.mtg_name = (char *)NULL; |
---|
261 | |
---|
262 | dsc_destroy_mtg_info (&dsc_public.m_info); |
---|
263 | } |
---|