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: randrp.c,v 1.15 1999-02-08 14:46:53 danw Exp $ |
---|
10 | * |
---|
11 | * Code for "randrp" request in discuss. |
---|
12 | * |
---|
13 | */ |
---|
14 | |
---|
15 | #ifndef lint |
---|
16 | static char rcsid_discuss_c[] = |
---|
17 | "$Id: randrp.c,v 1.15 1999-02-08 14:46:53 danw Exp $"; |
---|
18 | #endif /* lint */ |
---|
19 | |
---|
20 | #include <discuss/discuss.h> |
---|
21 | #include "globals.h" |
---|
22 | #include <stdio.h> |
---|
23 | #include <sys/time.h> |
---|
24 | #if HAVE_SRAND48 |
---|
25 | /* XXX note that we will just lose if we have neither random or srand48. |
---|
26 | * this is correctable, but 's sort of an edge case these days. |
---|
27 | */ |
---|
28 | #define random lrand48 |
---|
29 | #define srandom srand48 |
---|
30 | #endif |
---|
31 | |
---|
32 | |
---|
33 | randrp(argc, argv, sci_idx) |
---|
34 | int argc; |
---|
35 | char **argv; |
---|
36 | int sci_idx; |
---|
37 | { |
---|
38 | char *meeting = NULL; |
---|
39 | char *editor = NULL; |
---|
40 | char *trans = NULL; |
---|
41 | struct timeval tv; |
---|
42 | int randrp_retry = 15; |
---|
43 | int i, code; |
---|
44 | int active_transactions; |
---|
45 | long rnd_num; |
---|
46 | int rnd_trn; |
---|
47 | int pid = getpid(); |
---|
48 | int noeditor = FALSE; |
---|
49 | int prompt_subject = FALSE; |
---|
50 | trn_info t_info; |
---|
51 | trn_nums current_transaction; |
---|
52 | |
---|
53 | while (++argv, --argc) { |
---|
54 | if (!strcmp (*argv, "-meeting") || !strcmp (*argv, "-mtg")) { |
---|
55 | if (argc==1) { |
---|
56 | (void) fprintf(stderr, |
---|
57 | "No argument to %s.\n", *argv); |
---|
58 | return; |
---|
59 | } else { |
---|
60 | --argc; |
---|
61 | meeting = *(++argv); |
---|
62 | } |
---|
63 | } else if (!strcmp (*argv, "-editor") || !strcmp(*argv, "-ed")) { |
---|
64 | if (argc==1) { |
---|
65 | (void) fprintf(stderr, |
---|
66 | "No argument to %s.\n", *argv); |
---|
67 | return; |
---|
68 | } else { |
---|
69 | --argc; |
---|
70 | editor = *(++argv); |
---|
71 | } |
---|
72 | } else if (!strcmp(*argv, "-no_editor")) { |
---|
73 | noeditor = TRUE; |
---|
74 | } else if (!strcmp(*argv, "-subject") || |
---|
75 | !strcmp(*argv, "-sj")) { |
---|
76 | /* prompt for subject */ |
---|
77 | prompt_subject = TRUE; |
---|
78 | } else { |
---|
79 | ss_perror(sci_idx, 0, |
---|
80 | "Cannot specify transaction in random reply"); |
---|
81 | return; } |
---|
82 | } |
---|
83 | |
---|
84 | if (meeting) { |
---|
85 | (void) sprintf(buffer, "goto %s", meeting); |
---|
86 | ss_execute_line(sci_idx, buffer, &code); |
---|
87 | if (code != 0) { |
---|
88 | ss_perror(sci_idx, code, buffer); |
---|
89 | return; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | if (!dsc_public.attending) { |
---|
94 | ss_perror(sci_idx, 0, "No current meeting.\n"); |
---|
95 | return; |
---|
96 | } |
---|
97 | |
---|
98 | /* Need to preserve current transaction across the call to reply */ |
---|
99 | |
---|
100 | current_transaction = dsc_public.current; |
---|
101 | |
---|
102 | gettimeofday(&tv, (struct timezone *) NULL); |
---|
103 | srandom(tv.tv_sec ^ tv.tv_usec ^ pid); |
---|
104 | |
---|
105 | /* improved randomization routine... |
---|
106 | The idea here is to look for the first transaction in a chain. |
---|
107 | Try randrp_retry times. If we find one before then, fine. |
---|
108 | If not, use the last txn found. This increases the distribution |
---|
109 | of randrp subject lines. |
---|
110 | |
---|
111 | BAL 9/24/88 */ |
---|
112 | |
---|
113 | for (i=1;i<=randrp_retry;i++) { |
---|
114 | do { |
---|
115 | rnd_num = random(); |
---|
116 | active_transactions = |
---|
117 | (dsc_public.m_info.last - dsc_public.m_info.first); |
---|
118 | if (active_transactions != 0) { |
---|
119 | rnd_trn = (dsc_public.m_info.first + |
---|
120 | (rnd_num % active_transactions)); |
---|
121 | } else { |
---|
122 | rnd_trn = dsc_public.m_info.first; |
---|
123 | } |
---|
124 | dsc_get_trn_info(&dsc_public.nb, rnd_trn, &t_info, &code); |
---|
125 | dsc_destroy_trn_info(&t_info); |
---|
126 | } while (code != 0); |
---|
127 | if (!t_info.pref) break; |
---|
128 | } |
---|
129 | |
---|
130 | if ((editor != NULL) && !noeditor) { |
---|
131 | (void) sprintf(buffer, "reply %s-editor %s %d", |
---|
132 | prompt_subject ? "-subject " : "", |
---|
133 | editor, rnd_trn); |
---|
134 | ss_execute_line(sci_idx, buffer, &code); |
---|
135 | } |
---|
136 | else if (noeditor) { |
---|
137 | (void) sprintf(buffer, "reply %s-no_editor %d", |
---|
138 | prompt_subject ? "-subject " : "",rnd_trn); |
---|
139 | ss_execute_line(sci_idx, buffer, &code); |
---|
140 | } |
---|
141 | else { |
---|
142 | (void) sprintf(buffer, "reply %s%d", |
---|
143 | prompt_subject ? "-subject " : "",rnd_trn); |
---|
144 | ss_execute_line(sci_idx, buffer, &code); |
---|
145 | } |
---|
146 | |
---|
147 | dsc_public.current = current_transaction; |
---|
148 | if (code != 0) { |
---|
149 | ss_perror(sci_idx, code, buffer); |
---|
150 | return; |
---|
151 | } |
---|
152 | |
---|
153 | return; |
---|
154 | } |
---|
155 | |
---|