Esempio n. 1
0
Str
loadNewsgroup0(ParsedURL *pu)
#endif
{
    volatile Str page;
    Str tmp;
    URLFile f;
    Buffer *buf;
    char *qgroup, *p, *q, *s, *t, *n;
    char *volatile scheme, *volatile group, *volatile list;
    int status, i, first, last;
    volatile int flag = 0, start = 0, end = 0;
    MySignalHandler(*volatile prevtrap) (SIGNAL_ARG) = NULL;
#ifdef USE_M17N
    wc_ces doc_charset = DocumentCharset, mime_charset;

    *charset = WC_CES_US_ASCII;
#endif
    if (current_news.host == NULL || !pu->file || *pu->file == '\0')
	return NULL;
    group = allocStr(pu->file, -1);
    if (pu->scheme == SCM_NNTP_GROUP)
	scheme = "/";
    else
	scheme = "news:";
    if ((list = strchr(group, '/'))) {
	/* <newsgroup>/<start-number>-<end-number> */
	*list++ = '\0';
    }
    if (fmInitialized) {
	message(Sprintf("Reading newsgroup %s...", group)->ptr, 0, 0);
	refresh();
    }
    qgroup = html_quote(group);
    group = file_unquote(group);
    page = Strnew_m_charp("<html>\n<head>\n<base href=\"",
			  parsedURL2Str(pu)->ptr, "\">\n<title>Newsgroup: ",
			  qgroup, "</title>\n</head>\n<body>\n<h1>Newsgroup: ",
			  qgroup, "</h1>\n<hr>\n", NULL);

    if (SETJMP(AbortLoading) != 0) {
	news_close(&current_news);
	Strcat_charp(page, "</table>\n<p>Transfer Interrupted!\n");
	goto news_end;
    }
    TRAP_ON;

    tmp = news_command(&current_news, "GROUP", group, &status);
    if (status != 211)
	goto news_list;
    if (sscanf(tmp->ptr, "%d %d %d %d", &status, &i, &first, &last) != 4)
	goto news_list;
    if (list && *list) {
	if ((p = strchr(list, '-'))) {
	    *p++ = '\0';
	    end = atoi(p);
	}
	start = atoi(list);
	if (start > 0) {
	    if (start < first)
		start = first;
	    if (end <= 0)
		end = start + MaxNewsMessage - 1;
	}
    }
    if (start <= 0) {
	start = first;
	end = last;
	if (end - start > MaxNewsMessage - 1)
	    start = end - MaxNewsMessage + 1;
    }
    page = Sprintf("<html>\n<head>\n<base href=\"%s\">\n\
<title>Newsgroup: %s %d-%d</title>\n\
</head>\n<body>\n<h1>Newsgroup: %s %d-%d</h1>\n<hr>\n", parsedURL2Str(pu)->ptr, qgroup, start, end, qgroup, start, end);
    if (start > first) {
	i = start - MaxNewsMessage;
	if (i < first)
	    i = first;
	Strcat(page, Sprintf("<a href=\"%s%s/%d-%d\">[%d-%d]</a>\n", scheme,
			     qgroup, i, start - 1, i, start - 1));
    }

    Strcat_charp(page, "<table>\n");
    news_command(&current_news, "XOVER", Sprintf("%d-%d", start, end)->ptr,
		 &status);
    if (status == 224) {
	f.scheme = SCM_NEWS;
	while (true) {
	    tmp = StrISgets(current_news.rf);
	    if (NEWS_ENDLINE(tmp->ptr))
		break;
	    if (sscanf(tmp->ptr, "%d", &i) != 1)
		continue;
	    if (!(s = strchr(tmp->ptr, '\t')))
		continue;
	    s++;
	    if (!(n = strchr(s, '\t')))
		continue;
	    *n++ = '\0';
	    if (!(t = strchr(n, '\t')))
		continue;
	    *t++ = '\0';
	    if (!(p = strchr(t, '\t')))
		continue;
	    *p++ = '\0';
	    if (*p == '<')
		p++;
	    if (!(q = strchr(p, '>')) && !(q = strchr(p, '\t')))
		continue;
	    *q = '\0';
	    tmp = decodeMIME(Strnew_charp(s), &mime_charset);
	    s = convertLine(&f, tmp, HEADER_MODE,
			    mime_charset ? &mime_charset : charset,
			    mime_charset ? mime_charset : doc_charset)->ptr;
	    tmp = decodeMIME(Strnew_charp(n), &mime_charset);
	    n = convertLine(&f, tmp, HEADER_MODE,
			    mime_charset ? &mime_charset : charset,
			    mime_charset ? mime_charset : doc_charset)->ptr;
	    add_news_message(page, i, t, n, s, p, scheme,
			     pu->scheme == SCM_NNTP_GROUP ? qgroup : NULL);
	}
    }
    else {
	init_stream(&f, SCM_NEWS, current_news.rf);
	buf = newBuffer(INIT_BUFFER_WIDTH);
	for (i = start; i <= end && i <= last; i++) {
	    news_command(&current_news, "HEAD", Sprintf("%d", i)->ptr,
			 &status);
	    if (status != 221)
		continue;
	    readHeader(&f, buf, FALSE, NULL);
	    if (!(p = checkHeader(buf, "Message-ID:")))
		continue;
	    if (*p == '<')
		p++;
	    if (!(q = strchr(p, '>')) && !(q = strchr(p, '\t')))
		*q = '\0';
	    if (!(s = checkHeader(buf, "Subject:")))
		continue;
	    if (!(n = checkHeader(buf, "From:")))
		continue;
	    if (!(t = checkHeader(buf, "Date:")))
		continue;
	    add_news_message(page, i, t, n, s, p, scheme,
			     pu->scheme == SCM_NNTP_GROUP ? qgroup : NULL);
	}
    }
    Strcat_charp(page, "</table>\n");

    if (end < last) {
	i = end + MaxNewsMessage;
	if (i > last)
	    i = last;
	Strcat(page, Sprintf("<a href=\"%s%s/%d-%d\">[%d-%d]</a>\n", scheme,
			     qgroup, end + 1, i, end + 1, i));
    }
    flag = 1;

  news_list:
    tmp = Sprintf("ACTIVE %s", group);
    if (!strchr(group, '*'))
	Strcat_charp(tmp, ".*");
    news_command(&current_news, "LIST", tmp->ptr, &status);
    if (status != 215)
	goto news_end;
    while (true) {
	tmp = StrISgets(current_news.rf);
	if (NEWS_ENDLINE(tmp->ptr))
	    break;
	if (flag < 2) {
	    if (flag == 1)
		Strcat_charp(page, "<hr>\n");
	    Strcat_charp(page, "<table>\n");
	    flag = 2;
	}
	p = tmp->ptr;
	for (q = p; *q && !IS_SPACE(*q); q++) ;
	*(q++) = '\0';
	if (sscanf(q, "%d %d", &last, &first) == 2 && last >= first)
	    i = last - first + 1;
	else
	    i = 0;
	Strcat(page,
	       Sprintf
	       ("<tr><td align=right>%d<td><a href=\"%s%s\">%s</a>\n", i,
		scheme, html_quote(file_quote(p)), html_quote(p)));
    }
    if (flag == 2)
	Strcat_charp(page, "</table>\n");

  news_end:
    Strcat_charp(page, "</body>\n</html>\n");
    TRAP_OFF;
    return page;
}
Esempio n. 2
0
File: ftp.c Progetto: kumakichi/w3m
Str
loadFTPDir0(ParsedURL *pu)
#endif
{
    Str FTPDIRtmp;
    Str tmp;
    int status;
    volatile int sv_type;
    char *realpathname, *fn, *q;
    char **flist;
    int i, nfile, nfile_max;
    MySignalHandler(*volatile prevtrap) (SIGNAL_ARG) = NULL;
#ifdef USE_M17N
    wc_ces doc_charset = DocumentCharset;

    *charset = WC_CES_US_ASCII;
#endif
    if (current_ftp.data == NULL)
        return NULL;
    tmp = ftp_command(&current_ftp, "SYST", NULL, &status);
    if (strstr(tmp->ptr, "UNIX") != NULL || !strncmp(tmp->ptr + 4, "Windows_NT", 10))	/* :-) */
        sv_type = UNIXLIKE_SERVER;
    else
        sv_type = SERVER_NONE;
    if (pu->file == NULL || *pu->file == '\0') {
        if (sv_type == UNIXLIKE_SERVER)
            ftp_command(&current_ftp, "LIST", NULL, &status);
        else
            ftp_command(&current_ftp, "NLST", NULL, &status);
        pu->file = "/";
    }
    else {
        realpathname = file_unquote(pu->file);
        if (*realpathname == '/' && *(realpathname + 1) == '~')
            realpathname++;
        if (sv_type == UNIXLIKE_SERVER) {
            ftp_command(&current_ftp, "CWD", realpathname, &status);
            if (status == 250)
                ftp_command(&current_ftp, "LIST", NULL, &status);
        }
        else
            ftp_command(&current_ftp, "NLST", realpathname, &status);
    }
    if (status != 125 && status != 150) {
        fclose(current_ftp.data);
        current_ftp.data = NULL;
        return NULL;
    }
    tmp = parsedURL2Str(pu);
    if (Strlastchar(tmp) != '/')
        Strcat_char(tmp, '/');
    fn = html_quote(tmp->ptr);
    tmp =
        convertLine(NULL, Strnew_charp(file_unquote(tmp->ptr)), RAW_MODE,
                    charset, doc_charset);
    q = html_quote(tmp->ptr);
    FTPDIRtmp = Strnew_m_charp("<html>\n<head>\n<base href=\"", fn,
                               "\">\n<title>", q,
                               "</title>\n</head>\n<body>\n<h1>Index of ", q,
                               "</h1>\n", NULL);

    if (SETJMP(AbortLoading) != 0) {
        if (sv_type == UNIXLIKE_SERVER)
            Strcat_charp(FTPDIRtmp, "</a></pre>\n");
        else
            Strcat_charp(FTPDIRtmp, "</a></ul>\n");
        Strcat_charp(FTPDIRtmp, "<p>Transfer Interrupted!\n");
        goto ftp_end;
    }
    TRAP_ON;

    if (sv_type == UNIXLIKE_SERVER)
        Strcat_charp(FTPDIRtmp, "<pre>\n");
    else
        Strcat_charp(FTPDIRtmp, "<ul>\n<li>");
    Strcat_charp(FTPDIRtmp, "<a href=\"..\">[Upper Directory]</a>\n");

    nfile_max = 100;
    flist = New_N(char *, nfile_max);
    nfile = 0;
    if (sv_type == UNIXLIKE_SERVER) {
        char *name, *link, *date, *size, *type_str;
        int ftype, max_len, len, j;

        max_len = 20;
        while (tmp = Strfgets(current_ftp.data), tmp->length > 0) {
            Strchop(tmp);
            if ((ftype =
                        ex_ftpdir_name_size_date(tmp->ptr, &name, &link, &date,
                                                 &size)) == FTPDIR_NONE)
                continue;
            if (!strcmp(".", name) || !strcmp("..", name))
                continue;
            len = strlen(name);
            if (!len)
                continue;
            if (ftype == FTPDIR_DIR) {
                len++;
                type_str = "/";
            }
            else if (ftype == FTPDIR_LINK) {
                len++;
                type_str = "@";
            }
            else {
                type_str = " ";
            }
            if (max_len < len)
                max_len = len;
            flist[nfile++] = Sprintf("%s%s\n%s  %5s%s", name, type_str, date,
                                     size, link)->ptr;
            if (nfile == nfile_max) {
                nfile_max *= 2;
                flist = New_Reuse(char *, flist, nfile_max);
            }
        }
Esempio n. 3
0
InputStream
openNewsStream(ParsedURL *pu)
{
    char *host, *mode, *group, *p;
    Str tmp;
    int port, status;

    if (pu->file == NULL || *pu->file == '\0')
	return NULL;
    if (pu->scheme == SCM_NNTP || pu->scheme == SCM_NNTP_GROUP)
	host = pu->host;
    else
	host = NNTP_server;
    if (!host || *host == '\0') {
	if (current_news.host)
	    news_quit(&current_news);
	return NULL;
    }
    if (pu->scheme != SCM_NNTP && pu->scheme != SCM_NNTP_GROUP &&
	(p = strchr(host, ':'))) {
	host = allocStr(host, p - host);
	port = atoi(p + 1);
    }
    else
	port = pu->port;
    if (NNTP_mode && *NNTP_mode)
	mode = NNTP_mode;
    else
	mode = NULL;
    if (current_news.host) {
	if (!strcmp(current_news.host, host) && current_news.port == port) {
	    tmp = news_command(&current_news, "MODE", mode ? mode : "READER",
			       &status);
	    if (status != 200 && status != 201)
		news_close(&current_news);
	}
	else
	    news_quit(&current_news);
    }
    if (!current_news.host) {
	current_news.host = allocStr(host, -1);
	current_news.port = port;
	current_news.mode = mode ? allocStr(mode, -1) : NULL;
	if (!news_open(&current_news))
	    return NULL;
    }
    if (pu->scheme == SCM_NNTP || pu->scheme == SCM_NEWS) {
	/* News article */
	group = file_unquote(allocStr(pu->file, -1));
	p = strchr(group, '/');
	if (p == NULL) {	/* <message-id> */
	    if (!strchr(group, '@'))
		return NULL;
	    p = group;
	}
	else {			/* <newsgroup>/<message-id or article-number> */
	    *p++ = '\0';
	    news_command(&current_news, "GROUP", group, &status);
	    if (status != 211)
		return NULL;
	}
	if (strchr(p, '@'))	/* <message-id> */
	    news_command(&current_news, "ARTICLE", Sprintf("<%s>", p)->ptr,
			 &status);
	else			/* <article-number> */
	    news_command(&current_news, "ARTICLE", p, &status);
	if (status != 220)
	    return NULL;
	return current_news.rf;
    }
    return NULL;
}
Esempio n. 4
0
File: ftp.c Progetto: kumakichi/w3m
InputStream
openFTPStream(ParsedURL *pu, URLFile *uf)
{
    Str tmp;
    int status;
    char *user = NULL;
    char *pass = NULL;
    Str uname = NULL;
    Str pwd = NULL;
    int add_auth_cookie_flag = FALSE;
    char *realpathname = NULL;

    if (!pu->host)
        return NULL;

    if (pu->user == NULL && pu->pass == NULL) {
        if (find_auth_user_passwd(pu, NULL, &uname, &pwd, 0)) {
            if (uname)
                user = uname->ptr;
            if (pwd)
                pass = pwd->ptr;
        }
    }
    if (user)
        /* do nothing */ ;
    else if (pu->user)
        user = pu->user;
    else
        user = "******";

    if (current_ftp.host) {
        if (!strcmp(current_ftp.host, pu->host) &&
                current_ftp.port == pu->port && !strcmp(current_ftp.user, user)) {
            ftp_command(&current_ftp, "NOOP", NULL, &status);
            if (status != 200)
                ftp_close(&current_ftp);
            else
                goto ftp_read;
        }
        else
            ftp_quit(&current_ftp);
    }

    if (pass)
        /* do nothing */ ;
    else if (pu->pass)
        pass = pu->pass;
    else if (pu->user) {
        pwd = NULL;
        find_auth_user_passwd(pu, NULL, &uname, &pwd, 0);
        if (pwd == NULL) {
            if (fmInitialized) {
                term_raw();
                pwd = Strnew_charp(inputLine("Password: "******"Password: "******"Password: "******"anonymous");
#else
        tmp = Strnew_charp("anonymous");
#endif /* __MINGW32_VERSION */
        Strcat_char(tmp, '@');
        pass = tmp->ptr;
    }

    if (!current_ftp.host) {
        current_ftp.host = allocStr(pu->host, -1);
        current_ftp.port = pu->port;
        current_ftp.user = allocStr(user, -1);
        current_ftp.pass = allocStr(pass, -1);
        if (!ftp_login(&current_ftp))
            return NULL;
    }
    if (add_auth_cookie_flag)
        add_auth_user_passwd(pu, NULL, uname, pwd, 0);

ftp_read:
    ftp_command(&current_ftp, "TYPE", "I", &status);
    if (ftp_pasv(&current_ftp) < 0) {
        ftp_quit(&current_ftp);
        return NULL;
    }
    if (pu->file == NULL || *pu->file == '\0' ||
            pu->file[strlen(pu->file) - 1] == '/')
        goto ftp_dir;

    realpathname = file_unquote(pu->file);
    if (*realpathname == '/' && *(realpathname + 1) == '~')
        realpathname++;
    /* Get file */
    uf->modtime = ftp_modtime(&current_ftp, realpathname);
    ftp_command(&current_ftp, "RETR", realpathname, &status);
    if (status == 125 || status == 150)
        return newFileStream(current_ftp.data, (void (*)())closeFTPdata);

ftp_dir:
    pu->scheme = SCM_FTPDIR;
    return NULL;
}