R/server-response-log.R

Defines functions find_no_reply last_server_error make_debug_function

# Keeps the server lines of the most recent IMAP exchange, so that a command
# the server rejects with "NO" or "BAD" can be reported with the server's own
# reason instead of libcurl's generic "Quote command returned error".

.mR_last_response <- new.env(parent = emptyenv())
.mR_last_response$lines <- character(0)

#' Build the libcurl debug callback of a connection handle (INTERNAL HELPER)
#'
#' The callback records the server response lines (CURLINFO_HEADER_IN) of the
#' current exchange in \code{dbg$lines}, resetting them whenever a new command
#' is sent (CURLINFO_HEADER_OUT, whose tag is kept in \code{dbg$tag}), and mirrors libcurl's usual verbose output
#' to stderr when \code{dbg$verbose} is \code{TRUE}. The handle is therefore
#' always created with \code{verbose = TRUE}, and the user-facing
#' \code{verbose} setting only controls the printing.
#' @param dbg An environment with the fields \code{verbose} and \code{lines}.
#' @noRd
make_debug_function <- function(dbg) {
  function(type, msg) {
    # 0 = text, 1 = header in, 2 = header out; 3/4 are body data and 5/6 are
    # raw TLS records, which are binary and of no interest here
    if (!(type %in% c(0L, 1L, 2L))) {
      return(invisible(NULL))
    }
    txt <- rawToChar(msg[msg != as.raw(0)])
    if (type == 0L && grepl("^(Connected to|Established connection to) ", txt)) {
      # "Connected to host" up to libcurl 8.13, "Established connection to
      # host" from 8.14 on
      # a (re)connection: per-connection server state (ENABLE, selection) is lost
      dbg$epoch <- (if (is.null(dbg$epoch)) 0L else dbg$epoch) + 1L
    }
    if (type == 2L) {          # header out: a new command is being sent
      dbg$lines <- character(0)
      # the tag alone (the line may carry credentials, e.g. AUTHENTICATE)
      dbg$tag <- sub("^([A-Za-z0-9]+)\\s.*$", "\\1", sub("\r?\n.*$", "", txt))
    } else if (type == 1L) {   # header in: server response line(s)
      new <- strsplit(txt, "\r?\n")[[1]]
      dbg$lines <- c(dbg$lines, new[nzchar(new)])
      .mR_last_response$lines <- dbg$lines
      dbg$last_in <- Sys.time()
    }
    if (isTRUE(dbg$verbose)) {
      prefix <- switch(as.character(type), "0" = "* ", "1" = "< ", "2" = "> ",
                       NULL)
      if (!is.null(prefix)) {
        cat(prefix, txt, sep = "", file = stderr())
      }
    }
    invisible(NULL)
  }
}

#' The tagged NO/BAD line of the most recent exchange, if any (INTERNAL HELPER)
#' @return A string such as \code{"NO [CANNOT] ..."} (tag stripped), or
#'   \code{NA_character_}.
#' @noRd
last_server_error <- function(lines = NULL) {
  # prefer the calling connection's own log: the package-level fallback is
  # shared by every connection in the session and can cross-contaminate
  l <- if (!is.null(lines)) lines else .mR_last_response$lines
  m <- grep("^[A-Za-z]+[0-9]+ (NO|BAD)( |$)", l, value = TRUE)
  if (length(m) == 0) {
    return(NA_character_)
  }
  sub("^[A-Za-z]+[0-9]+ ", "", m[length(m)])
}

#' The reason of a NO reply anywhere in a response blob, if any (INTERNAL)
#'
#' Scans a raw response text (headers and/or content) for an untagged
#' \code{* NO ...} or a tagged \code{A12 NO ...} line and returns the
#' server's reason, or \code{NA_character_} when the response carries no NO
#' reply. Anchored per line, so the greeting or any earlier line cannot hide
#' a NO that appears later in the blob.
#' @noRd
find_no_reply <- function(text) {
  lines <- strsplit(text, "\r?\n")[[1]]
  m <- grep("^(\\*|[A-Za-z]+[0-9]+) NO( |$)", lines, value = TRUE)
  if (length(m) == 0) {
    return(NA_character_)
  }
  sub("^(\\*|[A-Za-z]+[0-9]+) NO ?", "", m[length(m)])
}

Try the mRpostman package in your browser

Any scripts or data that you put into this service are public.

mRpostman documentation built on Nov. 6, 2026, 9:06 a.m.