/*
 * Copyright (C), 2000-2007 by the monit project group.
 * All Rights Reserved.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include "protocol.h"

/**
 *  Check the server for greeting code 200 and then send a QUIT and
 *  check for code 205. If alive return TRUE, else, return FALSE.
 *
 *  @author Jan-Henrik Haukeland, <hauk@tildeslash.com>
 *
 *  @version \$Id: nntp.c,v 1.23 2007/07/25 12:54:33 hauk Exp $
 *
 *  @file
 */
int check_nntp(Socket_T s) {

  int status= 0;
  char buf[STRLEN];

  ASSERT(s);

  if(!socket_readln(s, buf, sizeof(buf))) {
    LogError("NNTP: error receiving data -- %s\n", STRERROR);
    return FALSE;
  }

  Util_chomp(buf);
  
  sscanf(buf, "%d %*s", &status);
  if(status != 200) {
    LogError("NNTP error: %s\n", buf);
    return FALSE;
  }
  
  if(socket_print(s, "QUIT\r\n") < 0) {
    LogError("NNTP: error sending data -- %s\n", STRERROR);
    return FALSE;
  }

  if(!socket_readln(s, buf, sizeof(buf))) {
    LogError("NNTP: error receiving data -- %s\n", STRERROR);
    return FALSE;
  }

  Util_chomp(buf);

  sscanf(buf, "%d %*s", &status);
  if(status != 205) {
    LogError("NNTP error: %s\n", buf);
    return FALSE;
  }

  return TRUE;
  
}



syntax highlighted by Code2HTML, v. 0.9.1