#include "nntp.h" /* *-------------------------------------------------------------------------------- */ int nntp_line_get (int chan,char *bufp,int bufl) { int sz; int pos; pos = 0; do { if ( 0 > (sz = recv(chan,bufp+pos,1, 0)) ) { NNTP_LOG(LOGE,"[Ch:%d]nntp_line_get:%s",chan,strerror(errno)); return -1; } pos += sz; if ( (pos >= 2) && *(bufp+pos-2) == '\r' && *(bufp+pos-1) == '\n' ) break; } while (pos < bufl); pos -= 2; *(bufp+pos) = '\0'; NNTP_LOG(LOGD,"[Ch:%d]nntp_line_get:%d bytes",chan,pos); return pos; } /* *-------------------------------------------------------------------------------- */ int nntp_cmd_get (int chan,char *bufp,int bufl) { int sz; int pos; char *cp; pos = 0; do { if ( 0 > (sz = recv(chan,bufp+pos,bufl-pos, 0)) ) { NNTP_LOG(LOGE,"[Ch:%d]nntp_cmd_get:%s",chan,strerror(errno)); return -1; } pos += sz; *(bufp+pos) = '\0'; if ( pos >= 2 ) if ( cp = strstr(bufp,"\r\n") ) break; } while (pos < bufl); *(cp) = '\0'; NNTP_LOG(LOGD,"[Ch:%d]nntp_cmd_get:%d bytes",chan,cp-bufp); return cp-bufp; } /* *-------------------------------------------------------------------------------- */ int nntp_txt_get (int chan,char *bufp,int bufl) { int sz,pos,flag; char *cp; pos = flag = 0; /*@RRL ? What need do with big messages ? */ while (1) { do { if ( 0 > (sz = recv(chan,bufp+pos,bufl-pos, 0)) ) { NNTP_LOG(LOGE,"[Ch:%d]nntp_txt_get:%s",chan,strerror(errno)); return -1; } pos += sz; *(bufp+pos) = '\0'; if (pos >= 5) { if ( !strcmp((bufp+pos-5),"\r\n.\r\n") ) return ( (flag == 0)?pos:-1 ); } } while (pos < bufl); /* For getting rest of big messages. */ memmove(bufp,bufp+(pos/2),1+(pos/2)); pos /= 2; flag++; } return 0; } /* *-------------------------------------------------------------------------------- */ void nntp_sockshut (int sock) { shutdown(sock,0); close(sock); }