File: nntp.c

package info (click to toggle)
postit 0.5-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 160 kB
  • ctags: 218
  • sloc: ansic: 753; sh: 241; makefile: 61
file content (165 lines) | stat: -rw-r--r-- 3,266 bytes parent folder | download | duplicates (3)
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
/* NNTP part of spoolnews; uses tcp.c to connect to the nntpserver */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>

#include "nntp.h"
#include "tcp.h"
#include "postit.h"

#define TIMEOUT 60
#define MAX_BLOCK_SIZE 2048

void message(char *text)
{
  printf("%s\n",text);
}

void error(char *text)
{
  printf("%s\n",text);
}


void output(char *prefix, char *what)
{
  if (verbosity>=2) printf("%s%s",prefix,what);
}


int nntp_open_connection(char *servername, int port)
{
  char line[LONGSTRING];
  int x;
  int socket;

  socket=tcp_open_connection(servername,port);
  if (socket<0) printf("%d\n",socket);
  if (socket==-6) {
    return -1;
  }
  if (socket<0) {
    error("network failure");
    return -1;
  }
  x=tcp_receive_line_to(socket,line,LONGSTRING,TIMEOUT);
  if (x) {
    sscanf(line,"%d",&x);
    output("<",line);
  }
  if (!x||(x!=200&&x!=201)) {
    tcp_close_connection(socket);
    if (x) error("NNTPserver does not welcome us");
    else error("NNTPserver closed connection");
    return -1;
  }
  return socket;
}

/* Send an NNTP command (must be \0 terminated), wait for response,
   suppressing multi-line responses on-the-fly, returning server reply */
int nntp_send_command(int socknr, char *command)
{
  char reply[512];

  nntp_send_line(socknr,command);
  while (1) {
    if (tcp_receive_line_to(socknr,reply,512,TIMEOUT)<=0) return -1;
    output("<",reply);
    if (!(isdigit(reply[0])&&isdigit(reply[1])&&isdigit(reply[2]))) continue;
    if (reply[3]!='-') break; /* multi-line response */
  }
  return atoi(reply);
}

int nntp_close_connection(int socket)
{
  int x;
	
  x=nntp_send_command(socket,"quit");
  if (x!=205) {
    printf("server does not understand QUIT??? oh well...");
  }
  tcp_close_connection(socket);
  return 0;
}

void nntp_send_line(int socket,char *line)
{
  char buf[VERYLONGSTRING];
  char *s,*t;

  s=line; t=buf;
  while (*s&&*s!='\r'&&*s!='\n') *t++=*s++;
  *t++='\r';
  *t++='\n';
  *t++='\0';
  output(">",buf);
  tcp_send_line(socket,buf);
}

int nntp_end_of_article(int socket)
{
  return nntp_send_command(socket,".");
}

/* this sends a null-terminated block of characters, could be an article
   for example :-)  */
int nntp_send_block(int socknr, char *block)
{
  int l,l2;
  int errcount=0;
  char *s;
  char buf[80];

#ifdef DEBUG
  printf("*** enter nntp_send_block\n");
  fflush(stdout);
#endif
  while (*block) {
    for (s=block; *s&&s-block<MAX_BLOCK_SIZE; s++);
    l=s-block;
    l2=write(socknr,block,l);
    if (l<0) {
      printf(buf,"tcp error: couldn't write %d chars, errno=%d",l,errno);
      if (++errcount>20) return -1;
      sleep(1);
    }
    else if (!l) sleep(1);
    else block+=l;
  }
#ifdef DEBUG
  printf("*** leave nntp_send_block\n");
  fflush(stdout);
#endif
  return 0;
}


/* This sends an article from a stream */
int nntp_send_stream(int socknr, FILE *f)
{
  int l;
  char buf[MAX_BLOCK_SIZE+1];

#ifdef DEBUG
  printf("*** enter nntp_send_stream\n");
  fflush(stdout);
#endif
  while (1) {
    l=fread(buf,1,MAX_BLOCK_SIZE,f);
    if (!l) break;
    buf[l]=0;
    nntp_send_block(socknr,buf);
    if (l<MAX_BLOCK_SIZE) break;
  }
#ifdef DEBUG
  printf("*** leave nntp_send_stream\n");
  fflush(stdout);
#endif
  return 0;
}