* new version 2.11.8
[alpine.git] / imap / src / c-client / nntp.c
blob5a805e86820bea3501a832a8659a3d5d8aeeaf56
1 /* ========================================================================
2 * Copyright 1988-2007 University of Washington
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
15 * Program: Network News Transfer Protocol (NNTP) routines
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
25 * Date: 10 February 1992
26 * Last Edited: 6 September 2007
30 #include <ctype.h>
31 #include <stdio.h>
32 #include "c-client.h"
33 #include "newsrc.h"
34 #include "netmsg.h"
35 #include "flstring.h"
37 /* Constants */
39 #define NNTPSSLPORT (long) 563 /* assigned SSL TCP contact port */
40 #define NNTPGREET (long) 200 /* NNTP successful greeting */
41 /* NNTP successful greeting w/o posting priv */
42 #define NNTPGREETNOPOST (long) 201
43 #define NNTPEXTOK (long) 202 /* NNTP extensions OK */
44 #define NNTPGOK (long) 211 /* NNTP group selection OK */
45 #define NNTPGLIST (long) 215 /* NNTP group list being returned */
46 #define NNTPARTICLE (long) 220 /* NNTP article file */
47 #define NNTPHEAD (long) 221 /* NNTP header text */
48 #define NNTPBODY (long) 222 /* NNTP body text */
49 #define NNTPOVER (long) 224 /* NNTP overview text */
50 #define NNTPOK (long) 240 /* NNTP OK code */
51 #define NNTPAUTHED (long) 281 /* NNTP successful authentication */
52 /* NNTP successful authentication with data */
53 #define NNTPAUTHEDDATA (long) 282
54 #define NNTPREADY (long) 340 /* NNTP ready for data */
55 #define NNTPWANTAUTH2 (long) 380/* NNTP authentication needed (old) */
56 #define NNTPWANTPASS (long) 381 /* NNTP password needed */
57 #define NNTPTLSSTART (long) 382 /* NNTP continue with TLS negotiation */
58 #define NNTPCHALLENGE (long) 383/* NNTP challenge, want response */
59 #define NNTPSOFTFATAL (long) 400/* NNTP soft fatal code */
60 #define NNTPWANTAUTH (long) 480 /* NNTP authentication needed */
61 #define NNTPBADCMD (long) 500 /* NNTP unrecognized command */
62 #define IDLETIMEOUT (long) 3 /* defined in NNTPEXT WG base draft */
65 /* NNTP I/O stream local data */
67 typedef struct nntp_local {
68 SENDSTREAM *nntpstream; /* NNTP stream for I/O */
69 unsigned int dirty : 1; /* disk copy of .newsrc needs updating */
70 unsigned int tlsflag : 1; /* TLS session */
71 unsigned int tlssslv23 : 1; /* TLS using SSLv23 client method */
72 unsigned int notlsflag : 1; /* TLS not used in session */
73 unsigned int sslflag : 1; /* SSL session */
74 unsigned int tls1 : 1; /* TLSv1 on SSL port */
75 unsigned int dtls1 : 1; /* DTLSv1 on SSL port */
76 unsigned int tls1_1 : 1; /* TLSv1_1 on SSL port */
77 unsigned int tls1_2 : 1; /* TLSv1_2 on SSL port */
78 unsigned int novalidate : 1; /* certificate not validated */
79 unsigned int xover : 1; /* supports XOVER */
80 unsigned int xhdr : 1; /* supports XHDR */
81 char *name; /* remote newsgroup name */
82 char *user; /* mailbox user */
83 char *newsrc; /* newsrc file */
84 char *over_fmt; /* overview format */
85 unsigned long msgno; /* current text message number */
86 FILE *txt; /* current text */
87 unsigned long txtsize; /* current text size */
88 } NNTPLOCAL;
91 /* Convenient access to local data */
93 #define LOCAL ((NNTPLOCAL *) stream->local)
96 /* Convenient access to protocol-specific data */
98 #define NNTP stream->protocol.nntp
101 /* Convenient access to extensions */
103 #define EXTENSION LOCAL->nntpstream->protocol.nntp.ext
105 /* Function prototypes */
107 DRIVER *nntp_valid (char *name);
108 DRIVER *nntp_isvalid (char *name,char *mbx);
109 void *nntp_parameters (long function,void *value);
110 void nntp_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
111 void nntp_list (MAILSTREAM *stream,char *ref,char *pat);
112 void nntp_lsub (MAILSTREAM *stream,char *ref,char *pat);
113 long nntp_canonicalize (char *ref,char *pat,char *pattern,char *wildmat);
114 long nntp_subscribe (MAILSTREAM *stream,char *mailbox);
115 long nntp_unsubscribe (MAILSTREAM *stream,char *mailbox);
116 long nntp_create (MAILSTREAM *stream,char *mailbox);
117 long nntp_delete (MAILSTREAM *stream,char *mailbox);
118 long nntp_rename (MAILSTREAM *stream,char *old,char *newname);
119 long nntp_status (MAILSTREAM *stream,char *mbx,long flags);
120 long nntp_getmap (MAILSTREAM *stream,char *name,
121 unsigned long first,unsigned long last,
122 unsigned long rnmsgs,unsigned long nmsgs,char *tmp);
123 MAILSTREAM *nntp_mopen (MAILSTREAM *stream);
124 void nntp_mclose (MAILSTREAM *stream,long options);
125 void nntp_fetchfast (MAILSTREAM *stream,char *sequence,long flags);
126 void nntp_flags (MAILSTREAM *stream,char *sequence,long flags);
127 long nntp_overview (MAILSTREAM *stream,overview_t ofn);
128 long nntp_parse_overview (OVERVIEW *ov,char *text,MESSAGECACHE *elt);
129 long nntp_over (MAILSTREAM *stream,char *sequence);
130 char *nntp_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *size,
131 long flags);
132 long nntp_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
133 FILE *nntp_article (MAILSTREAM *stream,char *msgid,unsigned long *size,
134 unsigned long *hsiz);
135 void nntp_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
136 long nntp_search (MAILSTREAM *stream,char *charset,SEARCHPGM *pgm,long flags);
137 long nntp_search_msg (MAILSTREAM *stream,unsigned long msgno,SEARCHPGM *pgm,
138 OVERVIEW *ov);
139 unsigned long *nntp_sort (MAILSTREAM *stream,char *charset,SEARCHPGM *spg,
140 SORTPGM *pgm,long flags);
141 SORTCACHE **nntp_sort_loadcache (MAILSTREAM *stream,SORTPGM *pgm,
142 unsigned long start,unsigned long last,
143 long flags);
144 THREADNODE *nntp_thread (MAILSTREAM *stream,char *type,char *charset,
145 SEARCHPGM *spg,long flags);
146 long nntp_ping (MAILSTREAM *stream);
147 void nntp_check (MAILSTREAM *stream);
148 long nntp_expunge (MAILSTREAM *stream,char *sequence,long options);
149 long nntp_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
150 long nntp_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
152 long nntp_extensions (SENDSTREAM *stream,long flags);
153 long nntp_send (SENDSTREAM *stream,char *command,char *args);
154 long nntp_send_work (SENDSTREAM *stream,char *command,char *args);
155 long nntp_send_auth (SENDSTREAM *stream,long flags);
156 long nntp_send_auth_work (SENDSTREAM *stream,NETMBX *mb,char *pwd,long flags);
157 void *nntp_challenge (void *s,unsigned long *len);
158 long nntp_response (void *s,char *response,unsigned long size);
159 long nntp_reply (SENDSTREAM *stream);
160 long nntp_fake (SENDSTREAM *stream,char *text);
161 long nntp_soutr (void *stream,char *s);
163 /* Driver dispatch used by MAIL */
165 DRIVER nntpdriver = {
166 "nntp", /* driver name */
167 /* driver flags */
168 #ifdef INADEQUATE_MEMORY
169 DR_LOWMEM |
170 #endif
171 DR_NEWS|DR_READONLY|DR_NOFAST|DR_NAMESPACE|DR_CRLF|DR_RECYCLE|DR_XPOINT |
172 DR_NOINTDATE|DR_NONEWMAIL|DR_HALFOPEN,
173 (DRIVER *) NIL, /* next driver */
174 nntp_valid, /* mailbox is valid for us */
175 nntp_parameters, /* manipulate parameters */
176 nntp_scan, /* scan mailboxes */
177 nntp_list, /* find mailboxes */
178 nntp_lsub, /* find subscribed mailboxes */
179 nntp_subscribe, /* subscribe to mailbox */
180 nntp_unsubscribe, /* unsubscribe from mailbox */
181 nntp_create, /* create mailbox */
182 nntp_delete, /* delete mailbox */
183 nntp_rename, /* rename mailbox */
184 nntp_status, /* status of mailbox */
185 nntp_mopen, /* open mailbox */
186 nntp_mclose, /* close mailbox */
187 nntp_fetchfast, /* fetch message "fast" attributes */
188 nntp_flags, /* fetch message flags */
189 nntp_overview, /* fetch overview */
190 NIL, /* fetch message structure */
191 nntp_header, /* fetch message header */
192 nntp_text, /* fetch message text */
193 NIL, /* fetch message */
194 NIL, /* unique identifier */
195 NIL, /* message number from UID */
196 NIL, /* modify flags */
197 nntp_flagmsg, /* per-message modify flags */
198 nntp_search, /* search for message based on criteria */
199 nntp_sort, /* sort messages */
200 nntp_thread, /* thread messages */
201 nntp_ping, /* ping mailbox to see if still alive */
202 nntp_check, /* check for new messages */
203 nntp_expunge, /* expunge deleted messages */
204 nntp_copy, /* copy messages to another mailbox */
205 nntp_append, /* append string message to mailbox */
206 NIL /* garbage collect stream */
209 /* prototype stream */
210 MAILSTREAM nntpproto = {&nntpdriver};
213 /* driver parameters */
214 static unsigned long nntp_maxlogintrials = MAXLOGINTRIALS;
215 static long nntp_port = 0;
216 static long nntp_sslport = 0;
217 static unsigned long nntp_range = 0;
218 static long nntp_hidepath = 0;
220 /* NNTP validate mailbox
221 * Accepts: mailbox name
222 * Returns: our driver if name is valid, NIL otherwise
225 DRIVER *nntp_valid (char *name)
227 char tmp[MAILTMPLEN];
228 return nntp_isvalid (name,tmp);
232 /* NNTP validate mailbox work routine
233 * Accepts: mailbox name
234 * buffer for returned mailbox name
235 * Returns: our driver if name is valid, NIL otherwise
238 DRIVER *nntp_isvalid (char *name,char *mbx)
240 NETMBX mb;
241 if (!mail_valid_net_parse (name,&mb) || strcmp (mb.service,nntpdriver.name)||
242 mb.anoflag) return NIL;
243 if (mb.mailbox[0] != '#') strcpy (mbx,mb.mailbox);
244 /* namespace format name */
245 else if ((mb.mailbox[1] == 'n') && (mb.mailbox[2] == 'e') &&
246 (mb.mailbox[3] == 'w') && (mb.mailbox[4] == 's') &&
247 (mb.mailbox[5] == '.')) strcpy (mbx,mb.mailbox+6);
248 else return NIL; /* bogus name */
249 return &nntpdriver;
252 /* News manipulate driver parameters
253 * Accepts: function code
254 * function-dependent value
255 * Returns: function-dependent return value
258 void *nntp_parameters (long function,void *value)
260 switch ((int) function) {
261 case SET_MAXLOGINTRIALS:
262 nntp_maxlogintrials = (unsigned long) value;
263 break;
264 case GET_MAXLOGINTRIALS:
265 value = (void *) nntp_maxlogintrials;
266 break;
267 case SET_NNTPPORT:
268 nntp_port = (long) value;
269 break;
270 case GET_NNTPPORT:
271 value = (void *) nntp_port;
272 break;
273 case SET_SSLNNTPPORT:
274 nntp_sslport = (long) value;
275 break;
276 case GET_SSLNNTPPORT:
277 value = (void *) nntp_sslport;
278 break;
279 case SET_NNTPRANGE:
280 nntp_range = (unsigned long) value;
281 break;
282 case GET_NNTPRANGE:
283 value = (void *) nntp_range;
284 break;
285 case SET_NNTPHIDEPATH:
286 nntp_hidepath = (long) value;
287 break;
288 case GET_NNTPHIDEPATH:
289 value = (void *) nntp_hidepath;
290 break;
291 case GET_NEWSRC:
292 if (value)
293 value = (void *) ((NNTPLOCAL *) ((MAILSTREAM *) value)->local)->newsrc;
294 break;
295 case GET_IDLETIMEOUT:
296 value = (void *) IDLETIMEOUT;
297 break;
298 case ENABLE_DEBUG:
299 if (value)
300 ((NNTPLOCAL *) ((MAILSTREAM *) value)->local)->nntpstream->debug = T;
301 break;
302 case DISABLE_DEBUG:
303 if (value)
304 ((NNTPLOCAL *) ((MAILSTREAM *) value)->local)->nntpstream->debug = NIL;
305 break;
306 default:
307 value = NIL; /* error case */
308 break;
310 return value;
313 /* NNTP mail scan mailboxes for string
314 * Accepts: mail stream
315 * reference
316 * pattern to search
317 * string to scan
320 void nntp_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
322 char tmp[MAILTMPLEN];
323 if (nntp_canonicalize (ref,pat,tmp,NIL))
324 mm_log ("Scan not valid for NNTP mailboxes",ERROR);
328 /* NNTP list newsgroups
329 * Accepts: mail stream
330 * reference
331 * pattern to search
334 void nntp_list (MAILSTREAM *stream,char *ref,char *pat)
336 MAILSTREAM *st = stream;
337 char *s,*t,*lcl,pattern[MAILTMPLEN],name[MAILTMPLEN],wildmat[MAILTMPLEN];
338 int showuppers = pat[strlen (pat) - 1] == '%';
339 if (!*pat) {
340 if (nntp_canonicalize (ref,"*",pattern,NIL)) {
341 /* tie off name at root */
342 if ((s = strchr (pattern,'}')) && (s = strchr (s+1,'.'))) *++s = '\0';
343 else pattern[0] = '\0';
344 mm_list (stream,'.',pattern,NIL);
347 /* ask server for open newsgroups */
348 else if (nntp_canonicalize (ref,pat,pattern,wildmat) &&
349 ((stream && LOCAL && LOCAL->nntpstream) ||
350 (stream = mail_open (NIL,pattern,OP_HALFOPEN|OP_SILENT))) &&
351 ((nntp_send (LOCAL->nntpstream,"LIST ACTIVE",
352 wildmat[0] ? wildmat : NIL) == NNTPGLIST) ||
353 (nntp_send (LOCAL->nntpstream,"LIST",NIL) == NNTPGLIST))) {
354 /* namespace format name? */
355 if (*(lcl = strchr (strcpy (name,pattern),'}') + 1) == '#') lcl += 6;
356 /* process data until we see final dot */
357 while (s = net_getline (LOCAL->nntpstream->netstream)) {
358 if ((*s == '.') && !s[1]){/* end of text */
359 fs_give ((void **) &s);
360 break;
362 if (t = strchr (s,' ')) { /* tie off after newsgroup name */
363 *t = '\0';
364 strcpy (lcl,s); /* make full form of name */
365 /* report if match */
366 if (pmatch_full (name,pattern,'.')) mm_list (stream,'.',name,NIL);
367 else while (showuppers && (t = strrchr (lcl,'.'))) {
368 *t = '\0'; /* tie off the name */
369 if (pmatch_full (name,pattern,'.'))
370 mm_list (stream,'.',name,LATT_NOSELECT);
373 fs_give ((void **) &s); /* clean up */
375 if (stream != st) mail_close (stream);
379 /* NNTP list subscribed newsgroups
380 * Accepts: mail stream
381 * reference
382 * pattern to search
385 void nntp_lsub (MAILSTREAM *stream,char *ref,char *pat)
387 void *sdb = NIL;
388 char *s,mbx[MAILTMPLEN];
389 /* return data from newsrc */
390 if (nntp_canonicalize (ref,pat,mbx,NIL)) newsrc_lsub (stream,mbx);
391 if (*pat == '{') { /* if remote pattern, must be NNTP */
392 if (!nntp_valid (pat)) return;
393 ref = NIL; /* good NNTP pattern, punt reference */
395 /* if remote reference, must be valid NNTP */
396 if (ref && (*ref == '{') && !nntp_valid (ref)) return;
397 /* kludgy application of reference */
398 if (ref && *ref) sprintf (mbx,"%s%s",ref,pat);
399 else strcpy (mbx,pat);
401 if (s = sm_read (&sdb)) do if (nntp_valid (s) && pmatch (s,mbx))
402 mm_lsub (stream,NIL,s,NIL);
403 while (s = sm_read (&sdb)); /* until no more subscriptions */
406 /* NNTP canonicalize newsgroup name
407 * Accepts: reference
408 * pattern
409 * returned single pattern
410 * returned wildmat pattern
411 * Returns: T on success, NIL on failure
414 long nntp_canonicalize (char *ref,char *pat,char *pattern,char *wildmat)
416 char *s;
417 DRIVER *ret;
418 if (ref && *ref) { /* have a reference */
419 if (!nntp_valid (ref)) return NIL;
420 strcpy (pattern,ref); /* copy reference to pattern */
421 /* # overrides mailbox field in reference */
422 if (*pat == '#') strcpy (strchr (pattern,'}') + 1,pat);
423 /* pattern starts, reference ends, with . */
424 else if ((*pat == '.') && (pattern[strlen (pattern) - 1] == '.'))
425 strcat (pattern,pat + 1); /* append, omitting one of the period */
426 else strcat (pattern,pat); /* anything else is just appended */
428 else strcpy (pattern,pat); /* just have basic name */
429 if ((ret = wildmat ? /* if valid and wildmat */
430 nntp_isvalid (pattern,wildmat) : nntp_valid (pattern)) && wildmat) {
431 /* don't return wildmat if specials present */
432 if (strpbrk (wildmat,",?![\\]")) wildmat[0] = '\0';
433 /* replace all % with * */
434 for (s = wildmat; s = strchr (s,'%'); *s = '*');
436 return ret ? LONGT : NIL;
439 /* NNTP subscribe to mailbox
440 * Accepts: mail stream
441 * mailbox to add to subscription list
442 * Returns: T on success, NIL on failure
445 long nntp_subscribe (MAILSTREAM *stream,char *mailbox)
447 char mbx[MAILTMPLEN];
448 return nntp_isvalid (mailbox,mbx) ? newsrc_update (stream,mbx,':') : NIL;
452 /* NNTP unsubscribe to mailbox
453 * Accepts: mail stream
454 * mailbox to delete from subscription list
455 * Returns: T on success, NIL on failure
458 long nntp_unsubscribe (MAILSTREAM *stream,char *mailbox)
460 char mbx[MAILTMPLEN];
461 return nntp_isvalid (mailbox,mbx) ? newsrc_update (stream,mbx,'!') : NIL;
464 /* NNTP create mailbox
465 * Accepts: mail stream
466 * mailbox name to create
467 * Returns: T on success, NIL on failure
470 long nntp_create (MAILSTREAM *stream,char *mailbox)
472 return NIL; /* never valid for NNTP */
476 /* NNTP delete mailbox
477 * mailbox name to delete
478 * Returns: T on success, NIL on failure
481 long nntp_delete (MAILSTREAM *stream,char *mailbox)
483 return NIL; /* never valid for NNTP */
487 /* NNTP rename mailbox
488 * Accepts: mail stream
489 * old mailbox name
490 * new mailbox name
491 * Returns: T on success, NIL on failure
494 long nntp_rename (MAILSTREAM *stream,char *old,char *newname)
496 return NIL; /* never valid for NNTP */
499 /* NNTP status
500 * Accepts: mail stream
501 * mailbox name
502 * status flags
503 * Returns: T on success, NIL on failure
506 long nntp_status (MAILSTREAM *stream,char *mbx,long flags)
508 MAILSTATUS status;
509 NETMBX mb;
510 unsigned long i,j,k,rnmsgs;
511 long ret = NIL;
512 char *s,*name,*state,tmp[MAILTMPLEN];
513 char *old = (stream && !stream->halfopen) ? LOCAL->name : NIL;
514 MAILSTREAM *tstream = NIL;
515 if (!(mail_valid_net_parse (mbx,&mb) && !strcmp (mb.service,"nntp") &&
516 *mb.mailbox &&
517 ((mb.mailbox[0] != '#') ||
518 ((mb.mailbox[1] == 'n') && (mb.mailbox[2] == 'e') &&
519 (mb.mailbox[3] == 'w') && (mb.mailbox[4] == 's') &&
520 (mb.mailbox[5] == '.'))))) {
521 sprintf (tmp,"Invalid NNTP name %s",mbx);
522 mm_log (tmp,ERROR);
523 return NIL;
525 /* note mailbox name */
526 name = (*mb.mailbox == '#') ? mb.mailbox+6 : mb.mailbox;
527 /* stream to reuse? */
528 if (!(stream && LOCAL->nntpstream &&
529 mail_usable_network_stream (stream,mbx)) &&
530 !(tstream = stream =
531 mail_open (NIL,mbx,OP_HALFOPEN|OP_SILENT|
532 ((flags & SA_MULNEWSRC) ? OP_MULNEWSRC : NIL))))
533 return NIL; /* can't reuse or make a new one */
535 if (nntp_send (LOCAL->nntpstream,"GROUP",name) == NNTPGOK) {
536 status.flags = flags; /* status validity flags */
537 k = strtoul (LOCAL->nntpstream->reply + 4,&s,10);
538 i = strtoul (s,&s,10); /* first assigned UID */
539 /* next UID to be assigned */
540 status.uidnext = (j = strtoul (s,NIL,10)) + 1;
541 /* maximum number of messages */
542 rnmsgs = status.messages = (i | j) ? status.uidnext - i : 0;
543 if (k > status.messages) { /* check for absurdity */
544 sprintf (tmp,"NNTP SERVER BUG (impossible message count): %lu > %lu",
545 k,status.messages);
546 mm_log (tmp,WARN);
548 /* restrict article range if needed */
549 if (nntp_range && (status.messages > nntp_range)) {
550 i = status.uidnext - (status.messages = nntp_range);
551 if (k > nntp_range) k = nntp_range;
553 /* initially zero */
554 status.recent = status.unseen = 0;
555 if (!status.messages); /* empty case */
556 /* use server guesstimate in simple case */
557 else if (!(flags & (SA_RECENT | SA_UNSEEN))) status.messages = k;
559 /* have newsrc state? */
560 else if (state = newsrc_state (stream,name)) {
561 /* yes, get the UID/sequence map */
562 if (nntp_getmap (stream,name,i,status.uidnext - 1,rnmsgs,
563 status.messages,tmp)) {
564 /* calculate true count */
565 for (status.messages = 0;
566 (s = net_getline (LOCAL->nntpstream->netstream)) &&
567 strcmp (s,"."); ) {
568 /* only count if in range */
569 if (((k = atol (s)) >= i) && (k < status.uidnext)) {
570 newsrc_check_uid (state,k,&status.recent,&status.unseen);
571 status.messages++;
573 fs_give ((void **) &s);
575 if (s) fs_give ((void **) &s);
577 /* assume c-client/NNTP map is entire range */
578 else while (i < status.uidnext)
579 newsrc_check_uid (state,i++,&status.recent,&status.unseen);
580 fs_give ((void **) &state);
582 /* no .newsrc state, all messages new */
583 else status.recent = status.unseen = status.messages;
584 /* UID validity is a constant */
585 status.uidvalidity = stream->uid_validity;
586 /* pass status to main program */
587 mm_status (stream,mbx,&status);
588 ret = T; /* succes */
590 /* flush temporary stream */
591 if (tstream) mail_close (tstream);
592 /* else reopen old newsgroup */
593 else if (old && nntp_send (LOCAL->nntpstream,"GROUP",old) != NNTPGOK) {
594 mm_log (LOCAL->nntpstream->reply,ERROR);
595 stream->halfopen = T; /* go halfopen */
597 return ret; /* success */
600 /* NNTP get map
601 * Accepts: stream
602 * newsgroup name
603 * first UID in map range
604 * last UID in map range
605 * reported total number of messages in newsgroup
606 * calculated number of messages in range
607 * temporary buffer
608 * Returns: T on success, NIL on failure
611 long nntp_getmap (MAILSTREAM *stream,char *name,
612 unsigned long first,unsigned long last,
613 unsigned long rnmsgs,unsigned long nmsgs,char *tmp)
615 short trylistgroup = NIL;
616 if (rnmsgs > (nmsgs * 8)) /* small subrange? */
617 trylistgroup = T; /* yes, can try LISTGROUP if [X]HDR fails */
618 else switch ((int) nntp_send (LOCAL->nntpstream,"LISTGROUP",name)) {
619 case NNTPGOK: /* got data */
620 return LONGT;
621 default: /* else give up if server claims LISTGROUP */
622 if (EXTENSION.listgroup) return NIL;
624 /* build range */
625 sprintf (tmp,"%lu-%lu",first,last);
626 if (EXTENSION.hdr) /* have HDR extension? */
627 return (nntp_send (LOCAL->nntpstream,"HDR Date",tmp) == NNTPHEAD) ?
628 LONGT : NIL;
629 if (LOCAL->xhdr) /* try the experimental extension then */
630 switch ((int) nntp_send (LOCAL->nntpstream,"XHDR Date",tmp)) {
631 case NNTPHEAD: /* got an overview? */
632 return LONGT;
633 case NNTPBADCMD: /* unknown command? */
634 LOCAL->xhdr = NIL; /* disable future XHDR attempts */
636 if (trylistgroup && /* no [X]HDR, maybe do LISTGROUP after all */
637 (nntp_send (LOCAL->nntpstream,"LISTGROUP",name) == NNTPGOK))
638 return LONGT;
639 return NIL;
642 /* NNTP open
643 * Accepts: stream to open
644 * Returns: stream on success, NIL on failure
647 MAILSTREAM *nntp_mopen (MAILSTREAM *stream)
649 unsigned long i,j,k,nmsgs,rnmsgs;
650 char *s,*mbx,tmp[MAILTMPLEN];
651 FILE *f;
652 NETMBX mb;
653 char *newsrc = (char *) mail_parameters (NIL,GET_NEWSRC,NIL);
654 newsrcquery_t nq = (newsrcquery_t) mail_parameters (NIL,GET_NEWSRCQUERY,NIL);
655 SENDSTREAM *nstream = NIL;
656 /* return prototype for OP_PROTOTYPE call */
657 if (!stream) return &nntpproto;
658 mail_valid_net_parse (stream->mailbox,&mb);
659 /* note mailbox anme */
660 mbx = (*mb.mailbox == '#') ? mb.mailbox+6 : mb.mailbox;
661 if (LOCAL) { /* recycle stream */
662 nstream = LOCAL->nntpstream;/* remember NNTP protocol stream */
663 sprintf (tmp,"Reusing connection to %s",net_host (nstream->netstream));
664 if (!stream->silent) mm_log (tmp,(long) NIL);
665 if (stream->rdonly) mb.readonlyflag = T;
666 if (LOCAL->tlsflag) mb.tlsflag = T;
667 if (LOCAL->tlssslv23) mb.tlssslv23 = T;
668 if (LOCAL->notlsflag) mb.notlsflag = T;
669 if (LOCAL->sslflag) mb.sslflag = T;
670 if (LOCAL->tls1) mb.tls1 = T;
671 if (LOCAL->dtls1) mb.dtls1 = T;
672 if (LOCAL->tls1_1) mb.tls1_1 = T;
673 if (LOCAL->tls1_2) mb.tls1_2 = T;
674 if (LOCAL->novalidate) mb.novalidate = T;
675 if (LOCAL->nntpstream->loser) mb.loser = T;
676 if (stream->secure) mb.secflag = T;
677 LOCAL->nntpstream = NIL; /* keep nntp_mclose() from punting it */
678 nntp_mclose (stream,NIL); /* do close action */
679 stream->dtb = &nntpdriver; /* reattach this driver */
681 /* copy flags */
682 if (mb.dbgflag) stream->debug = T;
683 if (mb.readonlyflag) stream->rdonly = T;
684 if (mb.secflag) stream->secure = T;
685 mb.trysslflag = stream->tryssl = (mb.trysslflag || stream->tryssl) ? T : NIL;
686 if (!nstream) { /* open NNTP now if not already open */
687 char *hostlist[2];
688 hostlist[0] = strcpy (tmp,mb.host);
689 if (mb.port || nntp_port)
690 sprintf (tmp + strlen (tmp),":%lu",mb.port ? mb.port : nntp_port);
691 if (mb.tlsflag) strcat (tmp,"/tls");
692 if (mb.tlssslv23) strcat (tmp,"/tls-sslv23");
693 if (mb.notlsflag) strcat (tmp,"/notls");
694 if (mb.sslflag) strcat (tmp,"/ssl");
695 if (mb.tls1) strcat (tmp,"/tls1");
696 if (mb.tls1_1) strcat (tmp,"/tls1_1");
697 if (mb.tls1_2) strcat (tmp,"/tls1_2");
698 if (mb.dtls1) strcat (tmp,"/dtls1");
699 if (mb.novalidate) strcat (tmp,"/novalidate-cert");
700 if (mb.loser) strcat (tmp,"/loser");
701 if (mb.secflag) strcat (tmp,"/secure");
702 if (mb.user[0]) sprintf (tmp + strlen (tmp),"/user=\"%s\"",mb.user);
703 hostlist[1] = NIL;
704 if (!(nstream = nntp_open (hostlist,NOP_READONLY |
705 (stream->debug ? NOP_DEBUG : NIL)))) return NIL;
708 /* always zero messages if halfopen */
709 if (stream->halfopen) i = j = k = rnmsgs = nmsgs = 0;
710 /* otherwise open the newsgroup */
711 else if (nntp_send (nstream,"GROUP",mbx) == NNTPGOK) {
712 k = strtoul (nstream->reply + 4,&s,10);
713 i = strtoul (s,&s,10);
714 stream->uid_last = j = strtoul (s,&s,10);
715 rnmsgs = nmsgs = (i | j) ? 1 + j - i : 0;
716 if (k > nmsgs) { /* check for absurdity */
717 sprintf (tmp,"NNTP SERVER BUG (impossible message count): %lu > %lu",
718 k,nmsgs);
719 mm_log (tmp,WARN);
721 /* restrict article range if needed */
722 if (nntp_range && (nmsgs > nntp_range)) i = 1 + j - (nmsgs = nntp_range);
724 else { /* no such newsgroup */
725 mm_log (nstream->reply,ERROR);
726 nntp_close (nstream); /* punt stream */
727 return NIL;
729 /* instantiate local data */
730 stream->local = memset (fs_get (sizeof (NNTPLOCAL)),0,sizeof (NNTPLOCAL));
731 LOCAL->nntpstream = nstream;
732 /* save state for future recycling */
733 if (mb.tlsflag) LOCAL->tlsflag = T;
734 if (mb.tlssslv23) LOCAL->tlssslv23 = T;
735 if (mb.notlsflag) LOCAL->notlsflag = T;
736 if (mb.sslflag) LOCAL->sslflag = T;
737 if (mb.novalidate) LOCAL->novalidate = T;
738 if (mb.loser) LOCAL->nntpstream->loser = T;
739 /* assume present until proven otherwise */
740 LOCAL->xhdr = LOCAL->xover = T;
741 LOCAL->name = cpystr (mbx); /* copy newsgroup name */
742 if (stream->mulnewsrc) { /* want to use multiple .newsrc files? */
743 strcpy (tmp,newsrc);
744 s = tmp + strlen (tmp); /* end of string */
745 *s++ = '-'; /* hyphen delimiter and host */
746 lcase (strcpy (s,(long) mail_parameters (NIL,GET_NEWSRCCANONHOST,NIL) ?
747 net_host (nstream->netstream) : mb.host));
748 LOCAL->newsrc = cpystr (nq ? (*nq) (stream,tmp,newsrc) : tmp);
750 else LOCAL->newsrc = cpystr (newsrc);
751 if (mb.user[0]) LOCAL->user = cpystr (mb.user);
752 stream->sequence++; /* bump sequence number */
753 stream->rdonly = stream->perm_deleted = T;
754 /* UIDs are always valid */
755 stream->uid_validity = 0xbeefface;
756 sprintf (tmp,"{%s:%lu/nntp",(long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
757 net_host (nstream->netstream) : mb.host,
758 net_port (nstream->netstream));
759 if (LOCAL->tlsflag) strcat (tmp,"/tls");
760 if (LOCAL->tlssslv23) strcat (tmp,"/tls-sslv23");
761 if (LOCAL->notlsflag) strcat (tmp,"/notls");
762 if (LOCAL->sslflag) strcat (tmp,"/ssl");
763 if (LOCAL->novalidate) strcat (tmp,"/novalidate-cert");
764 if (LOCAL->nntpstream->loser) strcat (tmp,"/loser");
765 if (stream->secure) strcat (tmp,"/secure");
766 if (stream->rdonly) strcat (tmp,"/readonly");
767 if (LOCAL->user) sprintf (tmp + strlen (tmp),"/user=\"%s\"",LOCAL->user);
768 if (stream->halfopen) strcat (tmp,"}<no_mailbox>");
769 else sprintf (tmp + strlen (tmp),"}#news.%s",mbx);
770 fs_give ((void **) &stream->mailbox);
771 stream->mailbox = cpystr (tmp);
773 if (EXTENSION.over && /* get overview format if have OVER */
774 (nntp_send (LOCAL->nntpstream,"LIST","OVERVIEW.FMT") == NNTPGLIST) &&
775 (f = netmsg_slurp (LOCAL->nntpstream->netstream,&k,NIL))) {
776 fread (LOCAL->over_fmt = (char *) fs_get ((size_t) k + 3),
777 (size_t) 1,(size_t) k,f);
778 LOCAL->over_fmt[k] = '\0';
779 fclose (f); /* flush temp file */
781 if (nmsgs) { /* if any messages exist */
782 short silent = stream->silent;
783 stream->silent = T; /* don't notify main program yet */
784 mail_exists (stream,nmsgs); /* silently set the cache to the guesstimate */
785 /* get UID/sequence map, nuke holes */
786 if (nntp_getmap (stream,mbx,i,j,rnmsgs,nmsgs,tmp)) {
787 for (nmsgs = 0; /* calculate true count */
788 (s = net_getline (nstream->netstream)) && strcmp (s,"."); ) {
789 if ((k = atol (s)) > j){/* discard too high article numbers */
790 sprintf (tmp,"NNTP SERVER BUG (out of range article ID): %lu > %lu",
791 k,j);
792 mm_notify (stream,tmp,NIL);
793 stream->unhealthy = T;
795 else if (k >= i) { /* silently ignore too-low article numbers */
796 /* guard against server returning extra msgs */
797 if (nmsgs == stream->nmsgs) mail_exists (stream,nmsgs+1);
798 /* create elt for this message, set UID */
799 mail_elt (stream,++nmsgs)->private.uid = k;
801 fs_give ((void **) &s);
803 if (s) fs_give ((void **) &s);
805 /* assume c-client/NNTP map is entire range */
806 else for (k = 1; k <= nmsgs; k++) mail_elt (stream,k)->private.uid = i++;
807 stream->unhealthy = NIL; /* set healthy */
808 stream->nmsgs = 0; /* whack it back down */
809 stream->silent = silent; /* restore old silent setting */
810 mail_exists (stream,nmsgs); /* notify upper level that messages exist */
811 /* read .newsrc entries */
812 mail_recent (stream,newsrc_read (mbx,stream));
814 else { /* empty newsgroup or halfopen */
815 if (!(stream->silent || stream->halfopen)) {
816 sprintf (tmp,"Newsgroup %s is empty",mbx);
817 mm_log (tmp,WARN);
819 mail_exists (stream,(long) 0);
820 mail_recent (stream,(long) 0);
822 return stream; /* return stream to caller */
825 /* NNTP close
826 * Accepts: MAIL stream
827 * option flags
830 void nntp_mclose (MAILSTREAM *stream,long options)
832 unsigned long i;
833 MESSAGECACHE *elt;
834 if (LOCAL) { /* only if a file is open */
835 nntp_check (stream); /* dump final checkpoint */
836 if (LOCAL->over_fmt) fs_give ((void **) &LOCAL->over_fmt);
837 if (LOCAL->name) fs_give ((void **) &LOCAL->name);
838 if (LOCAL->user) fs_give ((void **) &LOCAL->user);
839 if (LOCAL->newsrc) fs_give ((void **) &LOCAL->newsrc);
840 if (LOCAL->txt) fclose (LOCAL->txt);
841 /* close NNTP connection */
842 if (LOCAL->nntpstream) nntp_close (LOCAL->nntpstream);
843 for (i = 1; i <= stream->nmsgs; i++)
844 if ((elt = mail_elt (stream,i))->private.spare.ptr)
845 fs_give ((void **) &elt->private.spare.ptr);
846 /* nuke the local data */
847 fs_give ((void **) &stream->local);
848 stream->dtb = NIL; /* log out the DTB */
852 /* NNTP fetch fast information
853 * Accepts: MAIL stream
854 * sequence
855 * option flags
856 * This is ugly and slow
859 void nntp_fetchfast (MAILSTREAM *stream,char *sequence,long flags)
861 unsigned long i;
862 MESSAGECACHE *elt;
863 /* get sequence */
864 if (stream && LOCAL && ((flags & FT_UID) ?
865 mail_uid_sequence (stream,sequence) :
866 mail_sequence (stream,sequence)))
867 for (i = 1; i <= stream->nmsgs; i++) {
868 if ((elt = mail_elt (stream,i))->sequence && (elt->valid = T) &&
869 !(elt->day && elt->rfc822_size)) {
870 ENVELOPE **env = NIL;
871 ENVELOPE *e = NIL;
872 if (!stream->scache) env = &elt->private.msg.env;
873 else if (stream->msgno == i) env = &stream->env;
874 else env = &e;
875 if (!*env || !elt->rfc822_size) {
876 STRING bs;
877 unsigned long hs;
878 char *ht = (*stream->dtb->header) (stream,i,&hs,NIL);
879 /* need to make an envelope? */
880 if (!*env) rfc822_parse_msg (env,NIL,ht,hs,NIL,BADHOST,
881 stream->dtb->flags);
882 /* need message size too, ugh */
883 if (!elt->rfc822_size) {
884 (*stream->dtb->text) (stream,i,&bs,FT_PEEK);
885 elt->rfc822_size = hs + SIZE (&bs) - GETPOS (&bs);
888 /* if need date, have date in envelope? */
889 if (!elt->day && *env && (*env)->date)
890 mail_parse_date (elt,(*env)->date);
891 /* sigh, fill in bogus default */
892 if (!elt->day) elt->day = elt->month = 1;
893 mail_free_envelope (&e);
898 /* NNTP fetch flags
899 * Accepts: MAIL stream
900 * sequence
901 * option flags
904 void nntp_flags (MAILSTREAM *stream,char *sequence,long flags)
906 unsigned long i;
907 if ((flags & FT_UID) ? /* validate all elts */
908 mail_uid_sequence (stream,sequence) : mail_sequence (stream,sequence))
909 for (i = 1; i <= stream->nmsgs; i++) mail_elt (stream,i)->valid = T;
912 /* NNTP fetch overview
913 * Accepts: MAIL stream, sequence bits set
914 * overview return function
915 * Returns: T if successful, NIL otherwise
918 long nntp_overview (MAILSTREAM *stream,overview_t ofn)
920 unsigned long i,j,k,uid;
921 char c,*s,*t,*v,tmp[MAILTMPLEN];
922 MESSAGECACHE *elt;
923 OVERVIEW ov;
924 if (!LOCAL->nntpstream->netstream) return NIL;
925 /* scan sequence to load cache */
926 for (i = 1; i <= stream->nmsgs; i++)
927 /* have cached overview yet? */
928 if ((elt = mail_elt (stream,i))->sequence && !elt->private.spare.ptr) {
929 for (j = i + 1; /* no, find end of cache gap range */
930 (j <= stream->nmsgs) && (elt = mail_elt (stream,j))->sequence &&
931 !elt->private.spare.ptr; j++);
932 /* make NNTP range */
933 sprintf (tmp,(i == (j - 1)) ? "%lu" : "%lu-%lu",mail_uid (stream,i),
934 mail_uid (stream,j - 1));
935 i = j; /* advance beyond gap */
936 /* ask server for overview data to cache */
937 if (nntp_over (stream,tmp)) {
938 while ((s = net_getline (LOCAL->nntpstream->netstream)) &&
939 strcmp (s,".")) {
940 /* death to embedded newlines */
941 for (t = v = s; c = *v++;)
942 if ((c != '\012') && (c != '\015')) *t++ = c;
943 *t++ = '\0'; /* tie off string in case it was shortened */
944 /* cache the overview if found its sequence */
945 if ((uid = atol (s)) && (k = mail_msgno (stream,uid)) &&
946 (t = strchr (s,'\t'))) {
947 if ((elt = mail_elt (stream,k))->private.spare.ptr)
948 fs_give ((void **) &elt->private.spare.ptr);
949 elt->private.spare.ptr = cpystr (t + 1);
951 else { /* shouldn't happen, snarl if it does */
952 sprintf (tmp,"Server returned data for unknown UID %lu",uid);
953 mm_notify (stream,tmp,WARN);
954 stream->unhealthy = T;
956 /* flush the overview */
957 fs_give ((void **) &s);
959 stream->unhealthy = NIL;/* set healthy */
960 /* flush the terminating dot */
961 if (s) fs_give ((void **) &s);
963 else i = stream->nmsgs; /* OVER failed, punt cache load */
966 /* now scan sequence to return overviews */
967 if (ofn) for (i = 1; i <= stream->nmsgs; i++)
968 if ((elt = mail_elt (stream,i))->sequence) {
969 uid = mail_uid (stream,i);/* UID for this message */
970 /* parse cached overview */
971 if (nntp_parse_overview (&ov,s = (char *) elt->private.spare.ptr,elt))
972 (*ofn) (stream,uid,&ov,i);
973 else { /* parse failed */
974 (*ofn) (stream,uid,NIL,i);
975 if (s && *s) { /* unusable cached entry? */
976 sprintf (tmp,"Unable to parse overview for UID %lu: %.500s",uid,s);
977 mm_notify (stream,tmp,WARN);
978 stream->unhealthy = T;
979 /* erase it from the cache */
980 fs_give ((void **) &s);
982 stream->unhealthy = NIL;/* set healthy */
983 /* insert empty cached text as necessary */
984 if (!s) elt->private.spare.ptr = cpystr ("");
986 /* clean up overview data */
987 if (ov.from) mail_free_address (&ov.from);
988 if (ov.subject) fs_give ((void **) &ov.subject);
990 return T;
993 /* Send OVER to NNTP server
994 * Accepts: mail stream
995 * sequence to send
996 * Returns: T if success and overviews will follow, else NIL
999 long nntp_over (MAILSTREAM *stream,char *sequence)
1001 unsigned char *s;
1002 /* test for Netscape Collabra server */
1003 if (EXTENSION.over && LOCAL->xover &&
1004 nntp_send (LOCAL->nntpstream,"OVER","0") == NNTPOVER) {
1005 /* "Netscape-Collabra/3.52 03615 NNTP" responds to the OVER command with
1006 * a bogus "Subject:From:Date:Bytes:Lines" response followed by overviews
1007 * which lack the Message-ID and References:. This violates the draft
1008 * NNTP specification (draft-ietf-nntpext-base-18.txt as of this writing).
1009 * XOVER works fine.
1011 while ((s = net_getline (LOCAL->nntpstream->netstream)) && strcmp (s,".")){
1012 if (!isdigit (*s)) { /* is it that fetid piece of reptile dung? */
1013 EXTENSION.over = NIL; /* sure smells like it */
1014 mm_log ("Working around Netscape Collabra bug",WARN);
1016 fs_give ((void **) &s); /* flush the overview */
1018 if (s) fs_give ((void **) &s);
1019 /* don't do this test again */
1020 if (EXTENSION.over) LOCAL->xover = NIL;
1022 if (EXTENSION.over) /* have OVER extension? */
1023 return (nntp_send (LOCAL->nntpstream,"OVER",sequence) == NNTPOVER) ?
1024 LONGT : NIL;
1025 if (LOCAL->xover) /* try the experiment extension then */
1026 switch ((int) nntp_send (LOCAL->nntpstream,"XOVER",sequence)) {
1027 case NNTPOVER: /* got an overview? */
1028 return LONGT;
1029 case NNTPBADCMD: /* unknown command? */
1030 LOCAL->xover = NIL; /* disable future XOVER attempts */
1032 return NIL;
1035 /* Parse OVERVIEW struct from cached NNTP OVER response
1036 * Accepts: struct to load
1037 * cached OVER response
1038 * internaldate
1039 * Returns: T if success, NIL if fail
1042 long nntp_parse_overview (OVERVIEW *ov,char *text,MESSAGECACHE *elt)
1044 char *t;
1045 /* nothing in overview yet */
1046 memset ((void *) ov,0,sizeof (OVERVIEW));
1047 /* no cached data */
1048 if (!(text && *text)) return NIL;
1049 ov->subject = cpystr (text); /* make hackable copy of overview */
1050 /* find end of Subject */
1051 if (t = strchr (ov->subject,'\t')) {
1052 *t++ = '\0'; /* tie off Subject, point to From */
1053 /* find end of From */
1054 if (ov->date = strchr (t,'\t')) {
1055 *ov->date++ = '\0'; /* tie off From, point to Date */
1056 /* load internaldate too */
1057 if (!elt->day) mail_parse_date (elt,ov->date);
1058 /* parse From */
1059 rfc822_parse_adrlist (&ov->from,t,BADHOST);
1060 /* find end of Date */
1061 if (ov->message_id = strchr (ov->date,'\t')) {
1062 /* tie off Date, point to Message-ID */
1063 *ov->message_id++ = '\0';
1064 /* find end of Message-ID */
1065 if (ov->references = strchr (ov->message_id,'\t')) {
1066 /* tie off Message-ID, point to References */
1067 *ov->references++ = '\0';
1068 /* fine end of References */
1069 if (t = strchr (ov->references,'\t')) {
1070 *t++ = '\0'; /* tie off References, point to octet size */
1071 /* parse size of message in octets */
1072 ov->optional.octets = atol (t);
1073 /* find end of size */
1074 if (t = strchr (t,'\t')) {
1075 /* parse size of message in lines */
1076 ov->optional.lines = atol (++t);
1077 /* find Xref */
1078 if (ov->optional.xref = strchr (t,'\t'))
1079 *ov->optional.xref++ = '\0';
1086 return ov->references ? T : NIL;
1089 /* NNTP fetch header as text
1090 * Accepts: mail stream
1091 * message number
1092 * pointer to return size
1093 * flags
1094 * Returns: header text
1097 char *nntp_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *size,
1098 long flags)
1100 char tmp[MAILTMPLEN];
1101 MESSAGECACHE *elt;
1102 FILE *f;
1103 *size = 0;
1104 if ((flags & FT_UID) && !(msgno = mail_msgno (stream,msgno))) return "";
1105 /* have header text? */
1106 if (!(elt = mail_elt (stream,msgno))->private.msg.header.text.data) {
1107 sprintf (tmp,"%lu",mail_uid (stream,msgno));
1108 /* get header text */
1109 switch (nntp_send (LOCAL->nntpstream,"HEAD",tmp)) {
1110 case NNTPHEAD:
1111 if (f = netmsg_slurp (LOCAL->nntpstream->netstream,size,NIL)) {
1112 fread (elt->private.msg.header.text.data =
1113 (unsigned char *) fs_get ((size_t) *size + 3),
1114 (size_t) 1,(size_t) *size,f);
1115 fclose (f); /* flush temp file */
1116 /* tie off header with extra CRLF and NUL */
1117 elt->private.msg.header.text.data[*size] = '\015';
1118 elt->private.msg.header.text.data[++*size] = '\012';
1119 elt->private.msg.header.text.data[++*size] = '\0';
1120 elt->private.msg.header.text.size = *size;
1121 elt->valid = T; /* make elt valid now */
1122 break;
1124 /* fall into default case */
1125 default: /* failed, mark as deleted and empty */
1126 elt->valid = elt->deleted = T;
1127 case NNTPSOFTFATAL: /* don't mark deleted if stream dead */
1128 *size = elt->private.msg.header.text.size = 0;
1129 break;
1132 /* just return size of text */
1133 else *size = elt->private.msg.header.text.size;
1134 return elt->private.msg.header.text.data ?
1135 (char *) elt->private.msg.header.text.data : "";
1138 /* NNTP fetch body
1139 * Accepts: mail stream
1140 * message number
1141 * pointer to stringstruct to initialize
1142 * flags
1143 * Returns: T if successful, else NIL
1146 long nntp_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
1148 char tmp[MAILTMPLEN];
1149 MESSAGECACHE *elt;
1150 INIT (bs,mail_string,(void *) "",0);
1151 if ((flags & FT_UID) && !(msgno = mail_msgno (stream,msgno))) return NIL;
1152 elt = mail_elt (stream,msgno);
1153 /* different message, flush cache */
1154 if (LOCAL->txt && (LOCAL->msgno != msgno)) {
1155 fclose (LOCAL->txt);
1156 LOCAL->txt = NIL;
1158 LOCAL->msgno = msgno; /* note cached message */
1159 if (!LOCAL->txt) { /* have file for this message? */
1160 sprintf (tmp,"%lu",elt->private.uid);
1161 switch (nntp_send (LOCAL->nntpstream,"BODY",tmp)) {
1162 case NNTPBODY:
1163 if (LOCAL->txt = netmsg_slurp (LOCAL->nntpstream->netstream,
1164 &LOCAL->txtsize,NIL)) break;
1165 /* fall into default case */
1166 default: /* failed, mark as deleted */
1167 elt->deleted = T;
1168 case NNTPSOFTFATAL: /* don't mark deleted if stream dead */
1169 return NIL;
1172 if (!(flags & FT_PEEK)) { /* mark seen if needed */
1173 elt->seen = T;
1174 mm_flags (stream,elt->msgno);
1176 INIT (bs,file_string,(void *) LOCAL->txt,LOCAL->txtsize);
1177 return T;
1180 /* NNTP fetch article from message ID (for news: URL support)
1181 * Accepts: mail stream
1182 * message ID
1183 * pointer to return total message size
1184 * pointer to return file size
1185 * Returns: FILE * to message if successful, else NIL
1188 FILE *nntp_article (MAILSTREAM *stream,char *msgid,unsigned long *size,
1189 unsigned long *hsiz)
1191 return (nntp_send (LOCAL->nntpstream,"ARTICLE",msgid) == NNTPARTICLE) ?
1192 netmsg_slurp (LOCAL->nntpstream->netstream,size,hsiz) : NIL;
1196 /* NNTP per-message modify flag
1197 * Accepts: MAIL stream
1198 * message cache element
1201 void nntp_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
1203 if (!LOCAL->dirty) { /* only bother checking if not dirty yet */
1204 if (elt->valid) { /* if done, see if deleted changed */
1205 if (elt->sequence != elt->deleted) LOCAL->dirty = T;
1206 elt->sequence = T; /* leave the sequence set */
1208 /* note current setting of deleted flag */
1209 else elt->sequence = elt->deleted;
1213 /* NNTP search messages
1214 * Accepts: mail stream
1215 * character set
1216 * search program
1217 * option flags
1218 * Returns: T on success, NIL on failure
1221 long nntp_search (MAILSTREAM *stream,char *charset,SEARCHPGM *pgm,long flags)
1223 unsigned long i;
1224 MESSAGECACHE *elt;
1225 OVERVIEW ov;
1226 char *msg;
1227 /* make sure that charset is good */
1228 if (msg = utf8_badcharset (charset)) {
1229 MM_LOG (msg,ERROR); /* output error */
1230 fs_give ((void **) &msg);
1231 return NIL;
1233 utf8_searchpgm (pgm,charset);
1234 if (flags & SO_OVERVIEW) { /* only if specified to use overview */
1235 /* identify messages that will be searched */
1236 for (i = 1; i <= stream->nmsgs; ++i)
1237 mail_elt (stream,i)->sequence = nntp_search_msg (stream,i,pgm,NIL);
1238 nntp_overview (stream,NIL); /* load the overview cache */
1240 /* init in case no overview at cleanup */
1241 memset ((void *) &ov,0,sizeof (OVERVIEW));
1242 /* otherwise do default search */
1243 for (i = 1; i <= stream->nmsgs; ++i) {
1244 if (((flags & SO_OVERVIEW) && ((elt = mail_elt (stream,i))->sequence) &&
1245 nntp_parse_overview (&ov,(char *) elt->private.spare.ptr,elt)) ?
1246 nntp_search_msg (stream,i,pgm,&ov) :
1247 mail_search_msg (stream,i,NIL,pgm)) {
1248 if (flags & SE_UID) mm_searched (stream,mail_uid (stream,i));
1249 else { /* mark as searched, notify mail program */
1250 mail_elt (stream,i)->searched = T;
1251 if (!stream->silent) mm_searched (stream,i);
1254 /* clean up overview data */
1255 if (ov.from) mail_free_address (&ov.from);
1256 if (ov.subject) fs_give ((void **) &ov.subject);
1258 return LONGT;
1261 /* NNTP search message
1262 * Accepts: MAIL stream
1263 * message number
1264 * search program
1265 * overview to search (NIL means preliminary pass)
1266 * Returns: T if found, NIL otherwise
1269 long nntp_search_msg (MAILSTREAM *stream,unsigned long msgno,SEARCHPGM *pgm,
1270 OVERVIEW *ov)
1272 unsigned short d;
1273 unsigned long now = (unsigned long) time (0);
1274 MESSAGECACHE *elt = mail_elt (stream,msgno);
1275 SEARCHHEADER *hdr;
1276 SEARCHOR *or;
1277 SEARCHPGMLIST *not;
1278 if (pgm->msgno || pgm->uid) { /* message set searches */
1279 SEARCHSET *set;
1280 /* message sequences */
1281 if (set = pgm->msgno) { /* must be inside this sequence */
1282 while (set) { /* run down until find matching range */
1283 if (set->last ? ((msgno < set->first) || (msgno > set->last)) :
1284 msgno != set->first) set = set->next;
1285 else break;
1287 if (!set) return NIL; /* not found within sequence */
1289 if (set = pgm->uid) { /* must be inside this sequence */
1290 unsigned long uid = mail_uid (stream,msgno);
1291 while (set) { /* run down until find matching range */
1292 if (set->last ? ((uid < set->first) || (uid > set->last)) :
1293 uid != set->first) set = set->next;
1294 else break;
1296 if (!set) return NIL; /* not found within sequence */
1300 /* Fast data searches */
1301 /* message flags */
1302 if ((pgm->answered && !elt->answered) ||
1303 (pgm->unanswered && elt->answered) ||
1304 (pgm->deleted && !elt->deleted) ||
1305 (pgm->undeleted && elt->deleted) ||
1306 (pgm->draft && !elt->draft) ||
1307 (pgm->undraft && elt->draft) ||
1308 (pgm->flagged && !elt->flagged) ||
1309 (pgm->unflagged && elt->flagged) ||
1310 (pgm->recent && !elt->recent) ||
1311 (pgm->old && elt->recent) ||
1312 (pgm->seen && !elt->seen) ||
1313 (pgm->unseen && elt->seen)) return NIL;
1314 /* keywords */
1315 if ((pgm->keyword && !mail_search_keyword (stream,elt,pgm->keyword,LONGT)) ||
1316 (pgm->unkeyword && mail_search_keyword (stream,elt,pgm->unkeyword,NIL)))
1317 return NIL;
1318 if (ov) { /* only do this if real searching */
1319 MESSAGECACHE delt;
1320 /* size ranges */
1321 if ((pgm->larger && (ov->optional.octets <= pgm->larger)) ||
1322 (pgm->smaller && (ov->optional.octets >= pgm->smaller))) return NIL;
1323 /* date ranges */
1324 if ((pgm->sentbefore || pgm->senton || pgm->sentsince ||
1325 pgm->before || pgm->on || pgm->since) &&
1326 (!mail_parse_date (&delt,ov->date) ||
1327 !(d = mail_shortdate (delt.year,delt.month,delt.day)) ||
1328 (pgm->sentbefore && (d >= pgm->sentbefore)) ||
1329 (pgm->senton && (d != pgm->senton)) ||
1330 (pgm->sentsince && (d < pgm->sentsince)) ||
1331 (pgm->before && (d >= pgm->before)) ||
1332 (pgm->on && (d != pgm->on)) ||
1333 (pgm->since && (d < pgm->since)))) return NIL;
1334 if (pgm->older || pgm->younger) {
1335 unsigned long msgd = mail_longdate (elt);
1336 if (pgm->older && msgd > (now - pgm->older)) return NIL;
1337 if (pgm->younger && msgd < (now - pgm->younger)) return NIL;
1339 if ((pgm->from && !mail_search_addr (ov->from,pgm->from)) ||
1340 (pgm->subject && !mail_search_header_text (ov->subject,pgm->subject))||
1341 (pgm->message_id &&
1342 !mail_search_header_text (ov->message_id,pgm->message_id)) ||
1343 (pgm->references &&
1344 !mail_search_header_text (ov->references,pgm->references)))
1345 return NIL;
1348 /* envelope searches */
1349 if (pgm->bcc || pgm->cc || pgm->to || pgm->return_path || pgm->sender ||
1350 pgm->reply_to || pgm->in_reply_to || pgm->newsgroups ||
1351 pgm->followup_to) {
1352 ENVELOPE *env = mail_fetchenvelope (stream,msgno);
1353 if (!env) return NIL; /* no envelope obtained */
1354 /* search headers */
1355 if ((pgm->bcc && !mail_search_addr (env->bcc,pgm->bcc)) ||
1356 (pgm->cc && !mail_search_addr (env->cc,pgm->cc)) ||
1357 (pgm->to && !mail_search_addr (env->to,pgm->to)))
1358 return NIL;
1359 /* These criteria are not supported by IMAP and have to be emulated */
1360 if ((pgm->return_path &&
1361 !mail_search_addr (env->return_path,pgm->return_path)) ||
1362 (pgm->sender && !mail_search_addr (env->sender,pgm->sender)) ||
1363 (pgm->reply_to && !mail_search_addr (env->reply_to,pgm->reply_to)) ||
1364 (pgm->in_reply_to &&
1365 !mail_search_header_text (env->in_reply_to,pgm->in_reply_to)) ||
1366 (pgm->newsgroups &&
1367 !mail_search_header_text (env->newsgroups,pgm->newsgroups)) ||
1368 (pgm->followup_to &&
1369 !mail_search_header_text (env->followup_to,pgm->followup_to)))
1370 return NIL;
1373 /* search header lines */
1374 for (hdr = pgm->header; hdr; hdr = hdr->next) {
1375 char *t,*e,*v;
1376 SIZEDTEXT s;
1377 STRINGLIST sth,stc;
1378 sth.next = stc.next = NIL;/* only one at a time */
1379 sth.text.data = hdr->line.data;
1380 sth.text.size = hdr->line.size;
1381 /* get the header text */
1382 if ((t = mail_fetch_header (stream,msgno,NIL,&sth,&s.size,
1383 FT_INTERNAL | FT_PEEK)) && strchr (t,':')) {
1384 if (hdr->text.size) { /* anything matches empty search string */
1385 /* non-empty, copy field data */
1386 s.data = (unsigned char *) fs_get (s.size + 1);
1387 /* for each line */
1388 for (v = (char *) s.data, e = t + s.size; t < e;) switch (*t) {
1389 default: /* non-continuation, skip leading field name */
1390 while ((t < e) && (*t++ != ':'));
1391 if ((t < e) && (*t == ':')) t++;
1392 case '\t': case ' ': /* copy field data */
1393 while ((t < e) && (*t != '\015') && (*t != '\012')) *v++ = *t++;
1394 *v++ = '\n'; /* tie off line */
1395 while (((*t == '\015') || (*t == '\012')) && (t < e)) t++;
1397 /* calculate true size */
1398 s.size = v - (char *) s.data;
1399 *v = '\0'; /* tie off results */
1400 stc.text.data = hdr->text.data;
1401 stc.text.size = hdr->text.size;
1402 /* search header */
1403 if (mail_search_header (&s,&stc)) fs_give ((void **) &s.data);
1404 else { /* search failed */
1405 fs_give ((void **) &s.data);
1406 return NIL;
1410 else return NIL; /* no matching header text */
1412 /* search strings */
1413 if ((pgm->text &&
1414 !mail_search_text (stream,msgno,NIL,pgm->text,LONGT))||
1415 (pgm->body && !mail_search_text (stream,msgno,NIL,pgm->body,NIL)))
1416 return NIL;
1418 /* logical conditions */
1419 for (or = pgm->or; or; or = or->next)
1420 if (!(nntp_search_msg (stream,msgno,or->first,ov) ||
1421 nntp_search_msg (stream,msgno,or->second,ov))) return NIL;
1422 for (not = pgm->not; not; not = not->next)
1423 if (nntp_search_msg (stream,msgno,not->pgm,ov)) return NIL;
1424 return T;
1427 /* NNTP sort messages
1428 * Accepts: mail stream
1429 * character set
1430 * search program
1431 * sort program
1432 * option flags
1433 * Returns: vector of sorted message sequences or NIL if error
1436 unsigned long *nntp_sort (MAILSTREAM *stream,char *charset,SEARCHPGM *spg,
1437 SORTPGM *pgm,long flags)
1439 unsigned long i,start,last;
1440 SORTCACHE **sc;
1441 mailcache_t mailcache = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
1442 unsigned long *ret = NIL;
1443 sortresults_t sr = (sortresults_t) mail_parameters (NIL,GET_SORTRESULTS,NIL);
1444 if (spg) { /* only if a search needs to be done */
1445 int silent = stream->silent;
1446 stream->silent = T; /* don't pass up mm_searched() events */
1447 /* search for messages */
1448 mail_search_full (stream,charset,spg,NIL);
1449 stream->silent = silent; /* restore silence state */
1451 /* initialize progress counters */
1452 pgm->nmsgs = pgm->progress.cached = 0;
1453 /* pass 1: count messages to sort */
1454 for (i = 1,start = last = 0; i <= stream->nmsgs; ++i)
1455 if (mail_elt (stream,i)->searched) {
1456 pgm->nmsgs++;
1457 /* have this in the sortcache already? */
1458 if (!((SORTCACHE *) (*mailcache) (stream,i,CH_SORTCACHE))->date) {
1459 /* no, record as last message */
1460 last = mail_uid (stream,i);
1461 /* and as first too if needed */
1462 if (!start) start = last;
1465 if (pgm->nmsgs) { /* pass 2: load sort cache */
1466 sc = nntp_sort_loadcache (stream,pgm,start,last,flags);
1467 /* pass 3: sort messages */
1468 if (!pgm->abort) ret = mail_sort_cache (stream,pgm,sc,flags);
1469 fs_give ((void **) &sc); /* don't need sort vector any more */
1471 /* empty sort results */
1472 else ret = (unsigned long *) memset (fs_get (sizeof (unsigned long)),0,
1473 sizeof (unsigned long));
1474 /* also return via callback if requested */
1475 if (sr) (*sr) (stream,ret,pgm->nmsgs);
1476 return ret;
1479 /* Mail load sortcache
1480 * Accepts: mail stream, already searched
1481 * sort program
1482 * first UID to OVER
1483 * last UID to OVER
1484 * option flags
1485 * Returns: vector of sortcache pointers matching search
1488 SORTCACHE **nntp_sort_loadcache (MAILSTREAM *stream,SORTPGM *pgm,
1489 unsigned long start,unsigned long last,
1490 long flags)
1492 unsigned long i;
1493 char c,*s,*t,*v,tmp[MAILTMPLEN];
1494 SORTPGM *pg;
1495 SORTCACHE **sc,*r;
1496 MESSAGECACHE telt;
1497 ADDRESS *adr = NIL;
1498 mailcache_t mailcache = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
1499 /* verify that the sortpgm is OK */
1500 for (pg = pgm; pg; pg = pg->next) switch (pg->function) {
1501 case SORTARRIVAL: /* sort by arrival date */
1502 case SORTSIZE: /* sort by message size */
1503 case SORTDATE: /* sort by date */
1504 case SORTFROM: /* sort by first from */
1505 case SORTSUBJECT: /* sort by subject */
1506 break;
1507 case SORTTO: /* sort by first to */
1508 mm_notify (stream,"[NNTPSORT] Can't do To-field sorting in NNTP",WARN);
1509 break;
1510 case SORTCC: /* sort by first cc */
1511 mm_notify (stream,"[NNTPSORT] Can't do cc-field sorting in NNTP",WARN);
1512 break;
1513 default:
1514 fatal ("Unknown sort function");
1517 if (start) { /* messages need to be loaded in sortcache? */
1518 /* yes, build range */
1519 if (start != last) sprintf (tmp,"%lu-%lu",start,last);
1520 else sprintf (tmp,"%lu",start);
1521 /* get it from the NNTP server */
1522 if (!nntp_over (stream,tmp)) return mail_sort_loadcache (stream,pgm);
1523 while ((s = net_getline (LOCAL->nntpstream->netstream)) && strcmp (s,".")){
1524 /* death to embedded newlines */
1525 for (t = v = s; c = *v++;) if ((c != '\012') && (c != '\015')) *t++ = c;
1526 *t++ = '\0'; /* tie off resulting string */
1527 /* parse OVER response */
1528 if ((i = mail_msgno (stream,atol (s))) &&
1529 (t = strchr (s,'\t')) && (v = strchr (++t,'\t'))) {
1530 *v++ = '\0'; /* tie off subject */
1531 /* put stripped subject in sortcache */
1532 r = (SORTCACHE *) (*mailcache) (stream,i,CH_SORTCACHE);
1533 r->refwd = mail_strip_subject (t,&r->subject);
1534 if (t = strchr (v,'\t')) {
1535 *t++ = '\0'; /* tie off from */
1536 if (adr = rfc822_parse_address (&adr,adr,&v,BADHOST,0)) {
1537 r->from = adr->mailbox;
1538 adr->mailbox = NIL;
1539 mail_free_address (&adr);
1541 if (v = strchr (t,'\t')) {
1542 *v++ = '\0'; /* tie off date */
1543 if (mail_parse_date (&telt,t)) r->date = mail_longdate (&telt);
1544 if ((v = strchr (v,'\t')) && (v = strchr (++v,'\t')))
1545 r->size = atol (++v);
1549 fs_give ((void **) &s);
1551 if (s) fs_give ((void **) &s);
1554 /* calculate size of sortcache index */
1555 i = pgm->nmsgs * sizeof (SORTCACHE *);
1556 /* instantiate the index */
1557 sc = (SORTCACHE **) memset (fs_get ((size_t) i),0,(size_t) i);
1558 /* see what needs to be loaded */
1559 for (i = 1; !pgm->abort && (i <= stream->nmsgs); i++)
1560 if ((mail_elt (stream,i))->searched) {
1561 sc[pgm->progress.cached++] =
1562 r = (SORTCACHE *) (*mailcache) (stream,i,CH_SORTCACHE);
1563 r->pgm = pgm; /* note sort program */
1564 r->num = (flags & SE_UID) ? mail_uid (stream,i) : i;
1565 if (!r->date) r->date = r->num;
1566 if (!r->arrival) r->arrival = mail_uid (stream,i);
1567 if (!r->size) r->size = 1;
1568 if (!r->from) r->from = cpystr ("");
1569 if (!r->to) r->to = cpystr ("");
1570 if (!r->cc) r->cc = cpystr ("");
1571 if (!r->subject) r->subject = cpystr ("");
1573 return sc;
1577 /* NNTP thread messages
1578 * Accepts: mail stream
1579 * thread type
1580 * character set
1581 * search program
1582 * option flags
1583 * Returns: thread node tree
1586 THREADNODE *nntp_thread (MAILSTREAM *stream,char *type,char *charset,
1587 SEARCHPGM *spg,long flags)
1589 return mail_thread_msgs (stream,type,charset,spg,flags,nntp_sort);
1592 /* NNTP ping mailbox
1593 * Accepts: MAIL stream
1594 * Returns: T if stream alive, else NIL
1597 long nntp_ping (MAILSTREAM *stream)
1599 return (nntp_send (LOCAL->nntpstream,"STAT",NIL) != NNTPSOFTFATAL);
1603 /* NNTP check mailbox
1604 * Accepts: MAIL stream
1607 void nntp_check (MAILSTREAM *stream)
1609 /* never do if no updates */
1610 if (LOCAL->dirty) newsrc_write (LOCAL->name,stream);
1611 LOCAL->dirty = NIL;
1615 /* NNTP expunge mailbox
1616 * Accepts: MAIL stream
1617 * sequence to expunge if non-NIL
1618 * expunge options
1619 * Returns: T if success, NIL if failure
1622 long nntp_expunge (MAILSTREAM *stream,char *sequence,long options)
1624 if (!stream->silent) mm_log ("Expunge ignored on readonly mailbox",NIL);
1625 return LONGT;
1628 /* NNTP copy message(s)
1629 * Accepts: MAIL stream
1630 * sequence
1631 * destination mailbox
1632 * option flags
1633 * Returns: T if copy successful, else NIL
1636 long nntp_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
1638 mailproxycopy_t pc =
1639 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
1640 if (pc) return (*pc) (stream,sequence,mailbox,options);
1641 mm_log ("Copy not valid for NNTP",ERROR);
1642 return NIL;
1646 /* NNTP append message from stringstruct
1647 * Accepts: MAIL stream
1648 * destination mailbox
1649 * append callback
1650 * data for callback
1651 * Returns: T if append successful, else NIL
1654 long nntp_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
1656 mm_log ("Append not valid for NNTP",ERROR);
1657 return NIL;
1660 /* NNTP open connection
1661 * Accepts: network driver
1662 * service host list
1663 * port number
1664 * service name
1665 * NNTP open options
1666 * Returns: SEND stream on success, NIL on failure
1669 SENDSTREAM *nntp_open_full (NETDRIVER *dv,char **hostlist,char *service,
1670 unsigned long port,long options)
1672 SENDSTREAM *stream = NIL;
1673 NETSTREAM *netstream = NIL;
1674 NETMBX mb;
1675 char tmp[MAILTMPLEN];
1676 long extok = LONGT;
1677 NETDRIVER *ssld = (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL);
1678 sslstart_t stls = (sslstart_t) mail_parameters (NIL,GET_SSLSTART,NIL);
1679 if (!(hostlist && *hostlist)) mm_log ("Missing NNTP service host",ERROR);
1680 else do { /* try to open connection */
1681 sprintf (tmp,"{%.200s/%.20s}",*hostlist,service ? service : "nntp");
1682 if (!mail_valid_net_parse (tmp,&mb) || mb.anoflag) {
1683 sprintf (tmp,"Invalid host specifier: %.80s",*hostlist);
1684 mm_log (tmp,ERROR);
1686 else { /* light tryssl flag if requested */
1687 mb.trysslflag = (options & NOP_TRYSSL) ? T : NIL;
1688 /* default port */
1689 if (mb.port) port = mb.port;
1690 else if (!port) port = nntp_port ? nntp_port : NNTPTCPPORT;
1691 if (netstream = /* try to open ordinary connection */
1692 net_open (&mb,dv,port,
1693 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL),
1694 "*nntps",nntp_sslport ? nntp_sslport : NNTPSSLPORT)) {
1695 stream = (SENDSTREAM *) fs_get (sizeof (SENDSTREAM));
1696 /* initialize stream */
1697 memset ((void *) stream,0,sizeof (SENDSTREAM));
1698 stream->netstream = netstream;
1699 stream->host = cpystr ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
1700 net_host (netstream) : mb.host);
1701 stream->debug = (mb.dbgflag || (options & NOP_DEBUG)) ? T : NIL;
1702 if (mb.loser) stream->loser = T;
1703 /* process greeting */
1704 switch ((int) nntp_reply (stream)) {
1705 case NNTPGREET: /* allow posting */
1706 NNTP.post = T;
1707 mm_notify (NIL,stream->reply + 4,(long) NIL);
1708 break;
1709 case NNTPGREETNOPOST: /* posting not allowed, must be readonly */
1710 NNTP.post = NIL;
1711 break;
1712 default:
1713 mm_log (stream->reply,ERROR);
1714 stream = nntp_close (stream);
1715 break;
1719 } while (!stream && *++hostlist);
1721 /* get extensions */
1722 if (stream && extok)
1723 extok = nntp_extensions (stream,(mb.secflag ? AU_SECURE : NIL) |
1724 (mb.authuser[0] ? AU_AUTHUSER : NIL));
1725 if (stream && !dv && stls && NNTP.ext.starttls &&
1726 !mb.sslflag && !mb.notlsflag &&
1727 (nntp_send_work (stream,"STARTTLS",NNTP.ext.multidomain ? mb.host : NIL)
1728 == NNTPTLSSTART)) {
1729 mb.tlsflag = T; /* TLS OK, get into TLS at this end */
1730 stream->netstream->dtb = ssld;
1731 /* negotiate TLS */
1732 if (stream->netstream->stream =
1733 (*stls) (stream->netstream->stream,mb.host,
1734 SSL_MTHD(mb) | (mb.novalidate ? NET_NOVALIDATECERT:NIL)))
1735 extok = nntp_extensions (stream,(mb.secflag ? AU_SECURE : NIL) |
1736 (mb.authuser[0] ? AU_AUTHUSER : NIL));
1737 else {
1738 sprintf (tmp,"Unable to negotiate TLS with this server: %.80s",mb.host);
1739 mm_log (tmp,ERROR);
1740 /* close without doing QUIT */
1741 if (stream->netstream) net_close (stream->netstream);
1742 stream->netstream = NIL;
1743 stream = nntp_close (stream);
1746 else if (mb.tlsflag) { /* user specified /tls but can't do it */
1747 mm_log ("Unable to negotiate TLS with this server",ERROR);
1748 return NIL;
1750 if (stream) { /* have a session? */
1751 if (mb.user[0]) { /* yes, have user name? */
1752 if ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL)) {
1753 /* remote name for authentication */
1754 strncpy (mb.host,(long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
1755 net_remotehost (netstream) : net_host (netstream),
1756 NETMAXHOST-1);
1757 mb.host[NETMAXHOST-1] = '\0';
1759 if (!nntp_send_auth_work (stream,&mb,tmp,NIL))
1760 stream = nntp_close (stream);
1762 /* authenticate if no-post and not readonly */
1763 else if (!(NNTP.post || (options & NOP_READONLY) ||
1764 nntp_send_auth (stream,NIL))) stream = nntp_close (stream);
1767 /* in case server demands MODE READER */
1768 if (stream) switch ((int) nntp_send_work (stream,"MODE","READER")) {
1769 case NNTPGREET:
1770 NNTP.post = T;
1771 break;
1772 case NNTPGREETNOPOST:
1773 NNTP.post = NIL;
1774 break;
1775 case NNTPWANTAUTH: /* server wants auth first, do so and retry */
1776 case NNTPWANTAUTH2: /* remote name for authentication */
1777 if ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL)) {
1778 strncpy (mb.host,(long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
1779 net_remotehost (netstream) : net_host (netstream),NETMAXHOST-1);
1780 mb.host[NETMAXHOST-1] = '\0';
1782 if (nntp_send_auth_work (stream,&mb,tmp,NIL))
1783 switch ((int) nntp_send (stream,"MODE","READER")) {
1784 case NNTPGREET:
1785 NNTP.post = T;
1786 break;
1787 case NNTPGREETNOPOST:
1788 NNTP.post = NIL;
1789 break;
1791 else stream = nntp_close (stream);
1792 break;
1794 if (stream) { /* looks like we have a stream? */
1795 /* yes, make sure can post if not readonly */
1796 if (!(NNTP.post || (options & NOP_READONLY))) stream = nntp_close (stream);
1797 else if (extok) nntp_extensions (stream,(mb.secflag ? AU_SECURE : NIL) |
1798 (mb.authuser[0] ? AU_AUTHUSER : NIL));
1800 return stream;
1803 /* NNTP extensions
1804 * Accepts: stream
1805 * authenticator flags
1806 * Returns: T on success, NIL on failure
1809 long nntp_extensions (SENDSTREAM *stream,long flags)
1811 unsigned long i;
1812 char *t,*r,*args;
1813 /* zap all old extensions */
1814 memset (&NNTP.ext,0,sizeof (NNTP.ext));
1815 if (stream->loser) return NIL;/* nothing at all for losers */
1816 /* get server extensions */
1817 switch ((int) nntp_send_work (stream,"LIST","EXTENSIONS")) {
1818 case NNTPEXTOK: /* what NNTP base spec says */
1819 case NNTPGLIST: /* some servers do this instead */
1820 break;
1821 default: /* no LIST EXTENSIONS on this server */
1822 return NIL;
1824 NNTP.ext.ok = T; /* server offers extensions */
1825 while ((t = net_getline (stream->netstream)) && (t[1] || (*t != '.'))) {
1826 if (stream->debug) mm_dlog (t);
1827 /* get optional capability arguments */
1828 if (args = strchr (t,' ')) *args++ = '\0';
1829 if (!compare_cstring (t,"LISTGROUP")) NNTP.ext.listgroup = T;
1830 else if (!compare_cstring (t,"OVER")) NNTP.ext.over = T;
1831 else if (!compare_cstring (t,"HDR")) NNTP.ext.hdr = T;
1832 else if (!compare_cstring (t,"PAT")) NNTP.ext.pat = T;
1833 else if (!compare_cstring (t,"STARTTLS")) NNTP.ext.starttls = T;
1834 else if (!compare_cstring (t,"MULTIDOMAIN")) NNTP.ext.multidomain = T;
1836 else if (!compare_cstring (t,"AUTHINFO") && args) {
1837 char *sasl = NIL;
1838 for (args = strtok_r (args," ",&r); args; args = strtok_r (NIL," ",&r)) {
1839 if (!compare_cstring (args,"USER")) NNTP.ext.authuser = T;
1840 else if (((args[0] == 'S') || (args[0] == 's')) &&
1841 ((args[1] == 'A') || (args[1] == 'a')) &&
1842 ((args[2] == 'S') || (args[2] == 's')) &&
1843 ((args[3] == 'L') || (args[3] == 'l')) && (args[4] == ':'))
1844 sasl = args + 5;
1846 if (sasl) { /* if SASL, look up authenticators */
1847 for (sasl = strtok_r (sasl,",",&r); sasl; sasl = strtok_r (NIL,",",&r))
1848 if ((i = mail_lookup_auth_name (sasl,flags)) &&
1849 (--i < MAXAUTHENTICATORS))
1850 NNTP.ext.sasl |= (1 << i);
1851 /* disable LOGIN if PLAIN also advertised */
1852 if ((i = mail_lookup_auth_name ("PLAIN",NIL)) &&
1853 (--i < MAXAUTHENTICATORS) && (NNTP.ext.sasl & (1 << i)) &&
1854 (i = mail_lookup_auth_name ("LOGIN",NIL)) &&
1855 (--i < MAXAUTHENTICATORS)) NNTP.ext.sasl &= ~(1 << i);
1858 fs_give ((void **) &t);
1860 if (t) { /* flush end of text indicator */
1861 if (stream->debug) mm_dlog (t);
1862 fs_give ((void **) &t);
1864 return LONGT;
1867 /* NNTP close connection
1868 * Accepts: SEND stream
1869 * Returns: NIL always
1872 SENDSTREAM *nntp_close (SENDSTREAM *stream)
1874 if (stream) { /* send "QUIT" */
1875 if (stream->netstream) nntp_send (stream,"QUIT",NIL);
1876 /* do close actions */
1877 if (stream->netstream) net_close (stream->netstream);
1878 if (stream->host) fs_give ((void **) &stream->host);
1879 if (stream->reply) fs_give ((void **) &stream->reply);
1880 fs_give ((void **) &stream);/* flush the stream */
1882 return NIL;
1885 /* NNTP deliver news
1886 * Accepts: SEND stream
1887 * message envelope
1888 * message body
1889 * Returns: T on success, NIL on failure
1892 long nntp_mail (SENDSTREAM *stream,ENVELOPE *env,BODY *body)
1894 long ret;
1895 RFC822BUFFER buf;
1896 char *s,path[MAILTMPLEN],tmp[SENDBUFLEN+1];
1897 long error = NIL;
1898 long retry = NIL;
1899 buf.f = nntp_soutr; /* initialize buffer */
1900 buf.s = stream->netstream;
1901 buf.end = (buf.beg = buf.cur = tmp) + SENDBUFLEN;
1902 tmp[SENDBUFLEN] = '\0'; /* must have additional null guard byte */
1903 /* Gabba gabba hey, we need some brain damage to send netnews!!!
1905 * First, we give ourselves a frontal lobotomy, and put in some UUCP
1906 * syntax. It doesn't matter that it's completely bogus UUCP, and
1907 * that UUCP has nothing to do with anything we're doing. It's been
1908 * alleged that "Path: not-for-mail" is also acceptable, but we won't
1909 * make assumptions unless the user says so.
1911 * Second, we bop ourselves on the head with a ball-peen hammer. How
1912 * dare we be so presumptious as to insert a *comment* in a Date:
1913 * header line. Why, we were actually trying to be nice to a human
1914 * by giving a symbolic timezone (such as PST) in addition to a
1915 * numeric timezone (such as -0800). But the gods of news transport
1916 * will have none of this. Unix weenies, tried and true, rule!!!
1918 * Third, Netscape Collabra server doesn't give the NNTPWANTAUTH error
1919 * until after requesting and receiving the entire message. So we can't
1920 * call rely upon nntp_send() to do the auth retry.
1922 /* RFC-1036 requires this cretinism */
1923 sprintf (path,"Path: %s!%s\015\012",net_localhost (stream->netstream),
1924 env->sender ? env->sender->mailbox :
1925 (env->from ? env->from->mailbox : "not-for-mail"));
1926 /* here's another cretinism */
1927 if (s = strstr (env->date," (")) *s = NIL;
1928 do if ((ret = nntp_send_work (stream,"POST",NIL)) == NNTPREADY)
1929 /* output data, return success status */
1930 ret = (net_soutr (stream->netstream,
1931 nntp_hidepath ? "Path: not-for-mail\015\012" : path) &&
1932 rfc822_output_full (&buf,env,body,T)) ?
1933 nntp_send_work (stream,".",NIL) :
1934 nntp_fake (stream,"NNTP connection broken (message text)");
1935 while (((ret == NNTPWANTAUTH) || (ret == NNTPWANTAUTH2)) &&
1936 nntp_send_auth (stream,LONGT));
1937 if (s) *s = ' '; /* put the comment in the date back */
1938 if (ret == NNTPOK) return LONGT;
1939 else if (ret < 400) { /* if not an error reply */
1940 sprintf (tmp,"Unexpected NNTP posting reply code %ld",ret);
1941 mm_log (tmp,WARN); /* so someone looks at this eventually */
1942 if ((ret >= 200) && (ret < 300)) return LONGT;
1944 return NIL;
1947 /* NNTP send command
1948 * Accepts: SEND stream
1949 * text
1950 * Returns: reply code
1953 long nntp_send (SENDSTREAM *stream,char *command,char *args)
1955 long ret;
1956 switch ((int) (ret = nntp_send_work (stream,command,args))) {
1957 case NNTPWANTAUTH: /* authenticate and retry */
1958 case NNTPWANTAUTH2:
1959 if (nntp_send_auth (stream,LONGT))
1960 ret = nntp_send_work (stream,command,args);
1961 else { /* we're probably hosed, nuke the session */
1962 nntp_send (stream,"QUIT",NIL);
1963 /* close net connection */
1964 if (stream->netstream) net_close (stream->netstream);
1965 stream->netstream = NIL;
1967 default: /* all others just return */
1968 break;
1970 return ret;
1974 /* NNTP send command worker routine
1975 * Accepts: SEND stream
1976 * text
1977 * Returns: reply code
1980 long nntp_send_work (SENDSTREAM *stream,char *command,char *args)
1982 long ret;
1983 char *s = (char *) fs_get (strlen (command) + (args ? strlen (args) + 1 : 0)
1984 + 3);
1985 if (!stream->netstream) ret = nntp_fake (stream,"NNTP connection lost");
1986 else { /* build the complete command */
1987 if (args) sprintf (s,"%s %s",command,args);
1988 else strcpy (s,command);
1989 if (stream->debug) mail_dlog (s,stream->sensitive);
1990 strcat (s,"\015\012");
1991 /* send the command */
1992 ret = net_soutr (stream->netstream,s) ? nntp_reply (stream) :
1993 nntp_fake (stream,"NNTP connection broken (command)");
1995 fs_give ((void **) &s);
1996 return ret;
1999 /* NNTP send authentication if needed
2000 * Accepts: SEND stream
2001 * flags (non-NIL to get new extensions)
2002 * Returns: T if need to redo command, NIL otherwise
2005 long nntp_send_auth (SENDSTREAM *stream,long flags)
2007 NETMBX mb;
2008 char tmp[MAILTMPLEN];
2009 /* remote name for authentication */
2010 sprintf (tmp,"{%.200s/nntp",(long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
2011 ((long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
2012 net_remotehost (stream->netstream) : net_host (stream->netstream)):
2013 stream->host);
2014 if (stream->netstream->dtb ==
2015 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL))
2016 strcat (tmp,"/ssl");
2017 strcat (tmp,"}<none>");
2018 mail_valid_net_parse (tmp,&mb);
2019 return nntp_send_auth_work (stream,&mb,tmp,flags);
2022 /* NNTP send authentication worker routine
2023 * Accepts: SEND stream
2024 * NETMBX structure
2025 * scratch buffer of length MAILTMPLEN
2026 * flags (non-NIL to get new extensions)
2027 * Returns: T if authenticated, NIL otherwise
2030 long nntp_send_auth_work (SENDSTREAM *stream,NETMBX *mb,char *pwd,long flags)
2032 unsigned long trial,auths;
2033 char tmp[MAILTMPLEN],usr[MAILTMPLEN];
2034 AUTHENTICATOR *at;
2035 char *lsterr = NIL;
2036 long ret = NIL;
2037 /* try SASL first */
2038 for (auths = NNTP.ext.sasl, stream->saslcancel = NIL;
2039 !ret && stream->netstream && auths &&
2040 (at = mail_lookup_auth (find_rightmost_bit (&auths) + 1)); ) {
2041 if (lsterr) { /* previous authenticator failed? */
2042 sprintf (tmp,"Retrying using %s authentication after %.80s",
2043 at->name,lsterr);
2044 mm_log (tmp,NIL);
2045 fs_give ((void **) &lsterr);
2047 trial = 0; /* initial trial count */
2048 tmp[0] = '\0'; /* empty buffer */
2049 if (stream->netstream) do {
2050 if (lsterr) {
2051 sprintf (tmp,"Retrying %s authentication after %.80s",at->name,lsterr);
2052 mm_log (tmp,WARN);
2053 fs_give ((void **) &lsterr);
2055 stream->saslcancel = NIL;
2056 if (nntp_send (stream,"AUTHINFO SASL",at->name) == NNTPCHALLENGE) {
2057 /* hide client authentication responses */
2058 if (!(at->flags & AU_SECURE)) stream->sensitive = T;
2059 if ((*at->client) (nntp_challenge,nntp_response,"nntp",mb,stream,
2060 &trial,usr)) {
2061 if (stream->replycode == NNTPAUTHED) ret = LONGT;
2062 /* if main program requested cancellation */
2063 else if (!trial) mm_log ("NNTP Authentication cancelled",ERROR);
2065 stream->sensitive = NIL;/* unhide */
2067 /* remember response if error and no cancel */
2068 if (!ret && trial) lsterr = cpystr (stream->reply);
2069 } while (!ret && stream->netstream && trial &&
2070 (trial < nntp_maxlogintrials));
2073 if (lsterr) { /* SAIL failed? */
2074 if (!stream->saslcancel) { /* don't do this if a cancel */
2075 sprintf (tmp,"Can not authenticate to NNTP server: %.80s",lsterr);
2076 mm_log (tmp,ERROR);
2078 fs_give ((void **) &lsterr);
2080 else if (mb->secflag) /* no SASL, can't do /secure */
2081 mm_log ("Can't do secure authentication with this server",ERROR);
2082 else if (mb->authuser[0]) /* or /authuser */
2083 mm_log ("Can't do /authuser with this server",ERROR);
2084 /* Always try AUTHINFO USER, even if NNTP.ext.authuser isn't set. There
2085 * are servers that require it but don't return it as an extension.
2087 else for (trial = 0, pwd[0] = 'x';
2088 !ret && pwd[0] && (trial < nntp_maxlogintrials) &&
2089 stream->netstream; ) {
2090 pwd[0] = NIL; /* get user name and password */
2091 mm_login (mb,usr,pwd,trial++);
2092 /* do the authentication */
2093 if (pwd[0]) switch ((int) nntp_send_work (stream,"AUTHINFO USER",usr)) {
2094 case NNTPBADCMD: /* give up if unrecognized command */
2095 mm_log (NNTP.ext.authuser ? stream->reply :
2096 "Can't do AUTHINFO USER to this server",ERROR);
2097 trial = nntp_maxlogintrials;
2098 break;
2099 case NNTPAUTHED: /* successful authentication */
2100 ret = LONGT; /* guess no password was needed */
2101 break;
2102 case NNTPWANTPASS: /* wants password */
2103 stream->sensitive = T; /* hide this command */
2104 if (nntp_send_work (stream,"AUTHINFO PASS",pwd) == NNTPAUTHED)
2105 ret = LONGT; /* password OK */
2106 stream->sensitive = NIL; /* unhide */
2107 if (ret) break; /* OK if successful */
2108 default: /* authentication failed */
2109 mm_log (stream->reply,WARN);
2110 if (trial == nntp_maxlogintrials)
2111 mm_log ("Too many NNTP authentication failures",ERROR);
2113 /* user refused to give a password */
2114 else mm_log ("Login aborted",ERROR);
2116 memset (pwd,0,MAILTMPLEN); /* erase password */
2117 /* get new extensions if needed */
2118 if (ret && flags) nntp_extensions (stream,(mb->secflag ? AU_SECURE : NIL) |
2119 (mb->authuser[0] ? AU_AUTHUSER : NIL));
2120 return ret;
2123 /* Get challenge to authenticator in binary
2124 * Accepts: stream
2125 * pointer to returned size
2126 * Returns: challenge or NIL if not challenge
2129 void *nntp_challenge (void *s,unsigned long *len)
2131 char tmp[MAILTMPLEN];
2132 void *ret = NIL;
2133 SENDSTREAM *stream = (SENDSTREAM *) s;
2134 if ((stream->replycode == NNTPCHALLENGE) &&
2135 !(ret = rfc822_base64 ((unsigned char *) stream->reply + 4,
2136 strlen (stream->reply + 4),len))) {
2137 sprintf (tmp,"NNTP SERVER BUG (invalid challenge): %.80s",stream->reply+4);
2138 mm_log (tmp,ERROR);
2140 return ret;
2144 /* Send authenticator response in BASE64
2145 * Accepts: MAIL stream
2146 * string to send
2147 * length of string
2148 * Returns: T, always
2151 long nntp_response (void *s,char *response,unsigned long size)
2153 SENDSTREAM *stream = (SENDSTREAM *) s;
2154 unsigned long i,j;
2155 char *t,*u;
2156 if (response) { /* make CRLFless BASE64 string */
2157 if (size) {
2158 for (t = (char *) rfc822_binary ((void *) response,size,&i),u = t,j = 0;
2159 j < i; j++) if (t[j] > ' ') *u++ = t[j];
2160 *u = '\0'; /* tie off string */
2161 i = nntp_send_work (stream,t,NIL);
2162 fs_give ((void **) &t);
2164 else i = nntp_send_work (stream,"",NIL);
2166 else { /* abort requested */
2167 i = nntp_send_work (stream,"*",NIL);
2168 stream->saslcancel = T; /* mark protocol-requested SASL cancel */
2170 return LONGT;
2173 /* NNTP get reply
2174 * Accepts: SEND stream
2175 * Returns: reply code
2178 long nntp_reply (SENDSTREAM *stream)
2180 /* flush old reply */
2181 if (stream->reply) fs_give ((void **) &stream->reply);
2182 /* get reply */
2183 if (!(stream->reply = net_getline (stream->netstream)))
2184 return nntp_fake (stream,"NNTP connection broken (response)");
2185 if (stream->debug) mm_dlog (stream->reply);
2186 /* handle continuation by recursion */
2187 if (stream->reply[3] == '-') return nntp_reply (stream);
2188 /* return response code */
2189 return stream->replycode = atol (stream->reply);
2193 /* NNTP set fake error
2194 * Accepts: SEND stream
2195 * error text
2196 * Returns: error code
2199 long nntp_fake (SENDSTREAM *stream,char *text)
2201 if (stream->netstream) { /* close net connection if still open */
2202 net_close (stream->netstream);
2203 stream->netstream = NIL;
2205 /* flush any old reply */
2206 if (stream->reply) fs_give ((void **) &stream->reply);
2207 /* set up pseudo-reply string */
2208 stream->reply = (char *) fs_get (20+strlen (text));
2209 sprintf (stream->reply,"%ld %s",NNTPSOFTFATAL,text);
2210 return NNTPSOFTFATAL; /* return error code */
2213 /* NNTP filter mail
2214 * Accepts: stream
2215 * string
2216 * Returns: T on success, NIL on failure
2219 long nntp_soutr (void *stream,char *s)
2221 char c,*t;
2222 /* "." on first line */
2223 if (s[0] == '.') net_soutr (stream,".");
2224 /* find lines beginning with a "." */
2225 while (t = strstr (s,"\015\012.")) {
2226 c = *(t += 3); /* remember next character after "." */
2227 *t = '\0'; /* tie off string */
2228 /* output prefix */
2229 if (!net_soutr (stream,s)) return NIL;
2230 *t = c; /* restore delimiter */
2231 s = t - 1; /* push pointer up to the "." */
2233 /* output remainder of text */
2234 return *s ? net_soutr (stream,s) : T;