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: reply.c,v 1.23 2006-03-10 07:11:31 ghudson Exp $ |
---|
10 | * |
---|
11 | * Code for "reply" request in discuss. |
---|
12 | * |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | #ifndef lint |
---|
17 | static char rcsid_discuss_c[] = |
---|
18 | "$Id: reply.c,v 1.23 2006-03-10 07:11:31 ghudson Exp $"; |
---|
19 | #endif /* lint */ |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <sys/file.h> |
---|
24 | #include <string.h> |
---|
25 | #include <fcntl.h> |
---|
26 | #include <sys/wait.h> |
---|
27 | #include <ss/ss.h> |
---|
28 | #include <discuss/discuss.h> |
---|
29 | #include "config.h" |
---|
30 | #include "globals.h" |
---|
31 | |
---|
32 | /* EXTERNAL ROUTINES */ |
---|
33 | |
---|
34 | tfile unix_tfile(); |
---|
35 | extern void flag_interrupts(),dont_flag_interrupts(); |
---|
36 | |
---|
37 | #define DEFAULT_EDITOR "/bin/ed" |
---|
38 | |
---|
39 | repl(argc, argv) |
---|
40 | int argc; |
---|
41 | char **argv; |
---|
42 | { |
---|
43 | int fd; |
---|
44 | trn_nums txn_no, orig_trn; |
---|
45 | tfile tf; |
---|
46 | selection_list *trn_list; |
---|
47 | trn_info t_info; |
---|
48 | int code; |
---|
49 | int prompt_subject = FALSE; |
---|
50 | char *subject = &buffer[0]; |
---|
51 | char *editor = NULL; |
---|
52 | char *trans = NULL; |
---|
53 | char *mtg = NULL; |
---|
54 | char *myname = NULL; |
---|
55 | |
---|
56 | while (++argv, --argc) { |
---|
57 | if (!strcmp (*argv, "-meeting") || !strcmp (*argv, "-mtg")) { |
---|
58 | if (argc==1) { |
---|
59 | (void) fprintf(stderr, |
---|
60 | "No argument to %s.\n", *argv); |
---|
61 | return; |
---|
62 | } else { |
---|
63 | --argc; |
---|
64 | mtg = *(++argv); |
---|
65 | } |
---|
66 | } else if (!strcmp (*argv, "-editor") || !strcmp(*argv, "-ed")) { |
---|
67 | if (argc==1) { |
---|
68 | (void) fprintf(stderr, |
---|
69 | "No argument to %s.\n", *argv); |
---|
70 | return; |
---|
71 | } else { |
---|
72 | --argc; |
---|
73 | editor = *(++argv); |
---|
74 | } |
---|
75 | } else if (!strcmp(*argv, "-no_editor")) { |
---|
76 | editor = ""; |
---|
77 | } else if (!strcmp(*argv, "-subject") || |
---|
78 | !strcmp(*argv, "-sj")) { |
---|
79 | /* prompt for subject */ |
---|
80 | prompt_subject = TRUE; |
---|
81 | } else { |
---|
82 | if (!trans) trans = *argv; |
---|
83 | else { |
---|
84 | ss_perror(sci_idx, 0, |
---|
85 | "Cannot reply to multiple transactions"); |
---|
86 | return; |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | if (mtg && !trans) { |
---|
91 | fprintf(stderr, |
---|
92 | "Must have transaction specifier if using -mtg.\n"); |
---|
93 | return; |
---|
94 | } |
---|
95 | if (mtg) { |
---|
96 | (void) sprintf(buffer, "goto %s", mtg); |
---|
97 | code = ss_execute_line(sci_idx, buffer); |
---|
98 | if (code != 0) { |
---|
99 | ss_perror(sci_idx, code, buffer); |
---|
100 | return; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | flag_interrupts(); |
---|
105 | if (!dsc_public.attending) { |
---|
106 | ss_perror(sci_idx, 0, "No current meeting.\n"); |
---|
107 | goto abort2; |
---|
108 | } |
---|
109 | |
---|
110 | dsc_get_trn_info(&dsc_public.nb, dsc_public.current, &t_info, &code); |
---|
111 | if (code != 0) |
---|
112 | t_info.current = 0; |
---|
113 | dsc_destroy_trn_info(&t_info); |
---|
114 | |
---|
115 | trn_list = trn_select(&t_info, trans ? trans : "current" , |
---|
116 | (selection_list *)NULL, &code); |
---|
117 | if (code) { |
---|
118 | ss_perror(sci_idx, code, ""); |
---|
119 | sl_free((char *) trn_list); |
---|
120 | goto abort2; |
---|
121 | } |
---|
122 | |
---|
123 | if (trn_list -> low != trn_list -> high) { |
---|
124 | ss_perror(sci_idx, 0, "Cannot reply to range"); |
---|
125 | sl_free((char *)trn_list); |
---|
126 | goto abort2; |
---|
127 | } |
---|
128 | |
---|
129 | orig_trn = trn_list -> low; |
---|
130 | sl_free((char *)trn_list); |
---|
131 | |
---|
132 | dsc_get_trn_info(&dsc_public.nb, orig_trn, &t_info, &code); |
---|
133 | |
---|
134 | if (code != 0) { |
---|
135 | ss_perror(sci_idx, code, ""); |
---|
136 | goto abort; |
---|
137 | } |
---|
138 | |
---|
139 | if(!acl_is_subset("a", dsc_public.m_info.access_modes)) { |
---|
140 | (void) ss_perror(sci_idx, 0, |
---|
141 | "You do not have permission to create replies in this meeting."); |
---|
142 | goto abort; |
---|
143 | } |
---|
144 | |
---|
145 | dsc_whoami(&dsc_public.nb, &myname, &code); |
---|
146 | |
---|
147 | if (code != 0) { |
---|
148 | ss_perror(sci_idx, code, "while checking for anonymity"); |
---|
149 | } else { |
---|
150 | if (strncmp(myname, "???", 3) == 0) { |
---|
151 | printf("Reply will be anonymous.\n"); |
---|
152 | } |
---|
153 | } |
---|
154 | free(myname); |
---|
155 | myname = NULL; |
---|
156 | |
---|
157 | if (prompt_subject) { |
---|
158 | (void) printf("Subject: "); |
---|
159 | if (fgets(subject,BUFSIZ,stdin) == (char *)NULL) { |
---|
160 | clearerr(stdin); |
---|
161 | if (interrupt) goto abort; |
---|
162 | ss_perror(sci_idx, errno, "Error reading subject."); |
---|
163 | goto abort; |
---|
164 | } |
---|
165 | if (interrupt) goto abort; |
---|
166 | subject[strlen(subject)-1] = '\0'; /* Get rid of NL */ |
---|
167 | } else { |
---|
168 | if (strncmp(t_info.subject, "Re: ", 4)) { |
---|
169 | char *new_subject = malloc((unsigned)strlen(t_info.subject)+5); |
---|
170 | (void) strcpy(new_subject, "Re: "); |
---|
171 | (void) strcat(new_subject, t_info.subject); |
---|
172 | (void) free(t_info.subject); |
---|
173 | t_info.subject = new_subject; |
---|
174 | } |
---|
175 | |
---|
176 | printf("Subject: %s\n", t_info.subject); |
---|
177 | } |
---|
178 | (void) unlink(temp_file); |
---|
179 | if (edit(temp_file, editor) != 0) { |
---|
180 | (void) fprintf(stderr, |
---|
181 | "Error during edit; transaction not entered\n"); |
---|
182 | goto abort; |
---|
183 | } |
---|
184 | if (interrupt) goto abort; |
---|
185 | fd = open(temp_file, O_RDONLY, 0); |
---|
186 | if (fd < 0) { |
---|
187 | (void) fprintf(stderr, "No file; not entered.\n"); |
---|
188 | goto abort; |
---|
189 | } |
---|
190 | tf = unix_tfile(fd); |
---|
191 | |
---|
192 | dsc_add_trn(&dsc_public.nb, tf, |
---|
193 | prompt_subject ? subject : t_info.subject, |
---|
194 | orig_trn, &txn_no, &code); |
---|
195 | if (code != 0) { |
---|
196 | ss_perror(sci_idx, code, "while adding transaction\n"); |
---|
197 | close(fd); |
---|
198 | goto abort; |
---|
199 | } |
---|
200 | |
---|
201 | close(fd); |
---|
202 | (void) unlink(temp_file); |
---|
203 | (void) printf("Transaction [%04d] entered in the %s meeting.\n", |
---|
204 | txn_no, dsc_public.mtg_name); |
---|
205 | |
---|
206 | dsc_public.current = orig_trn; |
---|
207 | |
---|
208 | /* and now a pragmatic definition of 'seen': If you are up-to-date |
---|
209 | in a meeting, then you see transactions you enter. */ |
---|
210 | if (dsc_public.highest_seen == txn_no -1) { |
---|
211 | dsc_public.highest_seen = txn_no; |
---|
212 | } |
---|
213 | |
---|
214 | /* Update last */ |
---|
215 | dsc_public.m_info.last = txn_no; |
---|
216 | dsc_public.m_info.highest = txn_no; |
---|
217 | |
---|
218 | abort: |
---|
219 | dsc_destroy_trn_info(&t_info); |
---|
220 | abort2: |
---|
221 | dont_flag_interrupts(); |
---|
222 | } |
---|
223 | |
---|