File: editscore.c

package info (click to toggle)
slrn 1.0.3+dfsg-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,412 kB
  • sloc: ansic: 39,852; sh: 3,777; perl: 467; makefile: 229; sed: 24
file content (329 lines) | stat: -rw-r--r-- 8,401 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 This file is part of SLRN.

 Copyright (c) 1994, 1999, 2007-2016 John E. Davis <jed@jedsoft.org>
 Copyright (c) 2001-2006 Thomas Schultz <tststs@gmx.de>

 This program is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the Free
 Software Foundation; either version 2 of the License, or (at your option)
 any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "slrnfeat.h"

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#include <slang.h>
#include "jdmacros.h"

#include "group.h"
#include "art.h"
#include "startup.h"
#include "misc.h"
#include "score.h"
#include "util.h"
#include "slrn.h"
#include "common.h"
#include "strutil.h"

/* In pathological situations, "Subject" or "From" header fields may include
 * (MIME-encoded) linebreaks that would lead to invalid scorefile entries, so
 * simply replace them with blanks. */
static void remove_linebreaks (char *line)
{
   char c;
   if (line == NULL) return;
   while ((c = *line) != 0)
     {
	if ((c == '\r') || (c == '\n'))
	  *line = ' ';
	line++;
     }
}

/* Returns 0 if score file not modified, -1 if error, or 1 if scores modified. */
int slrn_edit_score (Slrn_Header_Type *h, char *newsgroup)
{
   char ch = 'e';
   int ich;
   char file[256];
   char qregexp[2*SLRL_DISPLAY_BUFFER_SIZE];
   unsigned int mm = 0, dd = 0, yy = 0;
   int days = 0;
   int use_expired = 0, force_score = 0;
   unsigned int linenum = 0;
   char *q, *ng = newsgroup;
   int score;
   FILE *fp;
   time_t myclock;
   int file_modified = 0, re_error = 0;
#if SLANG_VERSION < 20000
   SLRegexp_Type re;
   char buf[2*SLRL_DISPLAY_BUFFER_SIZE];
#else
   SLRegexp_Type *re;
#endif
   /* Note to translators: The translated string needs to have 10 characters.
    * Each pair becomes a valid response for "Subject", "From", "References",
    * "Edit" and "Cancel" (in that order); you cannot use any of the default
    * characters for any other field than they originally stood for!
    */
   char *typeresp = _("SsFfRrEeCc");
   /* Note to translators: The translated string needs to have 4 characters.
    * The two pairs become valid responses for "This group" and "All groups",
    * respectively. You cannot use any of the default characters for any other
    * field than they originally stood for!
    */
   char *scoperesp = _("TtAa");

   if (Slrn_Score_File == NULL)
     {
	slrn_error (_("A Score file has not been specified."));
	return -1;
     }

   if (Slrn_Prefix_Arg_Ptr == NULL)
     {
	char rsp;

	if (strlen(typeresp) != 10) /* Translator messed it up */
	  typeresp = "";
	ch = slrn_get_response ("SsFfRrEeCc\007", typeresp, _("Pick Score type: \001Subject, \001From, \001References, \001Edit, \001Cancel"));
	if (ch == 7) return -1;
	ch = slrn_map_translated_char ("SsFfRrEeCc", typeresp, ch) | 0x20;
	if (ch == 'c') return -1;

	while (1)
	  {
	     *qregexp = 0;
	     if (-1 == slrn_read_input ("Score: ", "=-9999", qregexp, 1, 0))
	       return -1;
	     force_score = (*qregexp == '=');
	     if (1 == sscanf (qregexp + force_score, "%d", &score))
	       break;
	  }

	if (strlen (scoperesp) != 4) /* Translator messed it up */
	  scoperesp = "";
	rsp = slrn_get_response ("TtaA\007", scoperesp, _("Which newsgroups: \001This group, \001All groups"));
	if (rsp == 7) return -1;
	if ((slrn_map_translated_char ("TtAa", scoperesp, rsp) | 0x20) == 'a')
	  ng = "*";

	while (1)
	  {
	     *qregexp = 0;
	     if (-1 == slrn_read_input (_("Expires (MM/DD/YYYY, DD-MM-YYYY, +NN (days) or leave blank): "), NULL, qregexp, 1, 0))
	       return -1;
	     if (*qregexp)
	       {
		  if (1 == sscanf(qregexp, "+%d",&days))
		    {
		       if (days < 1)
			 continue;
		       else
			 {
			    time_t now;
			    struct tm *newtime;
			    time(&now);
			    now += days * 24 * 3600;
			    newtime = localtime(&now);
			    dd = newtime->tm_mday;
			    mm = newtime->tm_mon + 1;
			    yy = newtime->tm_year + 1900;
			 }
		    }
		  else if (((3 != sscanf (qregexp, "%u/%u/%u", &mm, &dd, &yy))
			    && (3 != sscanf (qregexp, "%u-%u-%u", &dd, &mm, &yy)))
			    || (dd > 31)
			    || (mm > 12)
			    || (yy < 1900))
		    continue;
		  use_expired = 1;
		  break;
	       }
	     else
	       {
		  use_expired = 0;
		  break;
	       }
	  }
     }

   if ((NULL == (fp = slrn_open_home_file (Slrn_Score_File, "r+", file,
					   sizeof (file), 1)))
       && (NULL == (fp = slrn_open_home_file (Slrn_Score_File, "w+", file,
					      sizeof (file), 1))))
     {
	slrn_error (_("Unable to open %s"), file);
	return -1;
     }
#if SLANG_VERSION < 20000
   re.pat = (unsigned char*) qregexp;
   re.buf = (unsigned char*) buf;
   re.buf_len = sizeof (buf);
   re.case_sensitive = 0;
#else
   re = NULL;
#endif

   if (Slrn_Prefix_Arg_Ptr == NULL)
     {
	char *line;
	int comment;
	linenum = 1;
	while (EOF != (ich = getc (fp)))
	  {
	     if (ich == '\n')	linenum++;
	  }

	myclock = time((time_t *) 0);
	fprintf(fp, "\n%%BOS\n%%Score created by slrn on %s\n[%s]\nScore: %s%d\n",
		(char *) ctime(&myclock), ng, force_score ? "=" : "", score);

	if (use_expired)
	  fprintf (fp, "Expires: %u/%u/%u\n", mm, dd, yy);
	else fprintf (fp, "%%Expires: \n");

	line = slrn_safe_strmalloc (h->subject);
	slrn_subject_strip_was (line);
	remove_linebreaks (line);

	if ((NULL == (q = SLregexp_quote_string (line, qregexp, sizeof (qregexp)))) ||
	    (ch != 's') ||
#if SLANG_VERSION < 20000
	    (0 != SLang_regexp_compile (&re))
#else
	    (NULL == (re = SLregexp_compile (qregexp, SLREGEXP_CASELESS)))
#endif
	    )
	  {
	     re_error |= (ch == 's');
	     comment = 1;
	  }
	else
	  comment = 0;
	fprintf (fp, "%c\tSubject: %s\n",
		 (comment ? '%' : ' '), (q ? q : line));
	slrn_free (line);
#if SLANG_VERSION >= 20000
	if (re != NULL)
	  {
	     SLregexp_free (re);
	     re = NULL;
	  }
#endif

	line = slrn_safe_strmalloc (h->from);
	remove_linebreaks (line);
	if ((NULL == (q = SLregexp_quote_string (line, qregexp, sizeof (qregexp)))) ||
	    (ch != 'f') ||
#if SLANG_VERSION < 20000
	    (0 != SLang_regexp_compile (&re))
#else
	    (NULL == (re = SLregexp_compile (qregexp, SLREGEXP_CASELESS)))
#endif
	    )
	  {
	     re_error |= (ch == 'f');
	     comment = 1;
	  }
	else
	  comment = 0;
	fprintf (fp, "%c\tFrom: %s\n",
		 (comment ? '%' : ' '), (q ? q : line));
	slrn_free (line);
#if SLANG_VERSION >= 20000
	if (re != NULL)
	  {
	     SLregexp_free (re);
	     re = NULL;
	  }
#endif

	if ((NULL == (q = SLregexp_quote_string (h->msgid, qregexp, sizeof (qregexp)))) ||
	    (ch != 'r') ||
#if SLANG_VERSION < 20000
	    (0 != SLang_regexp_compile (&re))
#else
	    (NULL == (re = SLregexp_compile (qregexp, SLREGEXP_CASELESS)))
#endif
	    )
	  {
	     re_error |= (ch == 'r');
	     comment = 1;
	  }
	else
	  comment = 0;
	fprintf (fp, "%c\tReferences: %s\n",
		 (comment ? '%' : ' '), (q ? q : h->msgid));
#if SLANG_VERSION >= 20000
	if (re != NULL)
	  {
	     SLregexp_free (re);
	     re = NULL;
	  }
#endif

	if (NULL != (q = SLregexp_quote_string (h->xref, qregexp, sizeof (qregexp))))
	  {
	     fprintf (fp, "%%\tXref: %s\n", q);
	  }

	if (NULL != (q = SLregexp_quote_string (newsgroup, qregexp, sizeof (qregexp))))
	  {
	     fprintf (fp, "%%\tNewsgroup: %s\n", q);
	  }

	fprintf (fp, "%%EOS\n");
	file_modified = 1;
     }

   Slrn_Prefix_Arg_Ptr = NULL;

   if (-1 == slrn_fclose (fp))
     return -1;

   if (re_error)
     {
	slrn_error ("Error in pattern - commented new scorefile entry out.");
	return -1;
     }

   if (ch == 'e')
     {
	int status;

	status = slrn_edit_file (Slrn_Editor_Score, file, linenum + 1, 1);

	if ((status == -2)	       /* unmodified */
	    && (file_modified == 0))
	  return 0;

	if (status == -1)
	  return -1;

	/* drop */
     }
   if (Slrn_Scorefile_Open != NULL)
     slrn_free (Slrn_Scorefile_Open);
   Slrn_Scorefile_Open = slrn_safe_strmalloc (file);
   return 1;
}