2 // NNTP server module (RFC 3977)
4 // Copyright (c) 2014-2020 by the citadel.org team
6 // This program is open source software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License version 3.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
24 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <libcitadel.h>
37 #include "citserver.h"
45 #include "internet_addressing.h"
48 #include "clientsocket.h"
49 #include "locate_host.h"
50 #include "citadel_dirs.h"
51 #include "ctdl_module.h"
52 #include "serv_nntp.h"
57 // Tests whether the supplied string is a valid newsgroup name
58 // Returns true (nonzero) or false (0)
60 int is_valid_newsgroup_name(char *name) {
66 if (!strncasecmp(name, "ctdl.", 5)) return(0);
70 if (isalpha(ptr[0])) {
78 if ( (isalnum(ptr[0]))
89 return( (has_a_letter) && (num_dots >= 1) ) ;
94 // Convert a Citadel room name to a valid newsgroup name
96 void room_to_newsgroup(char *target, char *source, size_t target_size) {
101 if (is_valid_newsgroup_name(source)) {
102 strncpy(target, source, target_size);
106 strcpy(target, "ctdl.");
111 while (ch=*ptr++, ch!=0) {
112 if (len >= target_size) return;
117 target[len++] = tolower(ch);
121 target[len++] = '+' ;
122 sprintf(&target[len], "%02x", ch);
131 // Convert a newsgroup name to a Citadel room name.
132 // This function recognizes names converted with room_to_newsgroup() and restores them with full fidelity.
134 void newsgroup_to_room(char *target, char *source, size_t target_size) {
139 if (strncasecmp(source, "ctdl.", 5)) { // not a converted room name; pass through as-is
140 strncpy(target, source, target_size);
146 char *ptr = &source[5];
149 while (ch=*ptr++, ch!=0) {
150 if (len >= target_size) return;
157 digit = strtol(hex, NULL, 16);
167 // Here's where our NNTP session begins its happy day.
169 void nntp_greeting(void)
171 strcpy(CC->cs_clientname, "NNTP session");
172 CC->cs_flags |= CS_STEALTH;
174 CC->session_specific_data = malloc(sizeof(citnntp));
175 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
176 memset(nntpstate, 0, sizeof(citnntp));
178 if (CC->nologin==1) {
179 cprintf("451 Too many connections are already open; please try again later.\r\n");
180 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
184 // Display the standard greeting
185 cprintf("200 %s NNTP Citadel server is not finished yet\r\n", CtdlGetConfigStr("c_fqdn"));
190 // NNTPS is just like NNTP, except it goes crypto right away.
192 void nntps_greeting(void) {
193 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
195 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */
202 // implements the STARTTLS command
204 void nntp_starttls(void)
206 char ok_response[SIZ];
207 char nosup_response[SIZ];
208 char error_response[SIZ];
210 sprintf(ok_response, "382 Begin TLS negotiation now\r\n");
211 sprintf(nosup_response, "502 Can not initiate TLS negotiation\r\n");
212 sprintf(error_response, "580 Internal error\r\n");
213 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
218 // Implements the CAPABILITY command
220 void nntp_capabilities(void)
222 cprintf("101 Capability list:\r\n");
223 cprintf("IMPLEMENTATION Citadel %d\r\n", REV_LEVEL);
224 cprintf("VERSION 2\r\n");
225 cprintf("READER\r\n");
226 cprintf("MODE-READER\r\n");
227 cprintf("LIST ACTIVE NEWSGROUPS\r\n");
230 cprintf("STARTTLS\r\n");
232 if (!CC->logged_in) {
233 cprintf("AUTHINFO USER\r\n");
240 // Implements the QUIT command
244 cprintf("221 Goodbye...\r\n");
245 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
250 // Implements the AUTHINFO USER command (RFC 4643)
252 void nntp_authinfo_user(const char *username)
254 int a = CtdlLoginExistingUser(username);
256 case login_already_logged_in:
257 cprintf("482 Already logged in\r\n");
259 case login_too_many_users:
260 cprintf("481 Too many users are already online (maximum is %d)\r\n", CtdlGetConfigInt("c_maxsessions"));
263 cprintf("381 Password required for %s\r\n", CC->curr_user);
265 case login_not_found:
266 cprintf("481 %s not found\r\n", username);
269 cprintf("502 Internal error\r\n");
275 // Implements the AUTHINFO PASS command (RFC 4643)
277 void nntp_authinfo_pass(const char *buf)
281 a = CtdlTryPassword(buf, strlen(buf));
284 case pass_already_logged_in:
285 cprintf("482 Already logged in\r\n");
288 cprintf("482 Authentication commands issued out of sequence\r\n");
290 case pass_wrong_password:
291 cprintf("481 Authentication failed\r\n");
294 cprintf("281 Authentication accepted\r\n");
301 // Implements the AUTHINFO extension (RFC 4643) in USER/PASS mode
303 void nntp_authinfo(const char *cmd) {
305 if (!strncasecmp(cmd, "authinfo user ", 14)) {
306 nntp_authinfo_user(&cmd[14]);
309 else if (!strncasecmp(cmd, "authinfo pass ", 14)) {
310 nntp_authinfo_pass(&cmd[14]);
314 cprintf("502 command unavailable\r\n");
320 // Utility function to fetch the current list of message numbers in a room
322 struct nntp_msglist nntp_fetch_msglist(struct ctdlroom *qrbuf) {
323 struct nntp_msglist nm;
324 struct cdbdata *cdbfr;
326 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
328 nm.msgnums = (long*)cdbfr->ptr;
330 nm.num_msgs = cdbfr->len / sizeof(long);
342 // Output a room name (newsgroup name) in formats required for LIST and NEWGROUPS command
344 void output_roomname_in_list_format(struct ctdlroom *qrbuf, int which_format, char *wildmat_pattern) {
346 struct nntp_msglist nm;
347 long low_water_mark = 0;
348 long high_water_mark = 0;
350 room_to_newsgroup(n_name, qrbuf->QRname, sizeof n_name);
352 if ((wildmat_pattern != NULL) && (!IsEmptyStr(wildmat_pattern))) {
353 if (!wildmat(n_name, wildmat_pattern)) {
358 nm = nntp_fetch_msglist(qrbuf);
359 if ((nm.num_msgs > 0) && (nm.msgnums != NULL)) {
360 low_water_mark = nm.msgnums[0];
361 high_water_mark = nm.msgnums[nm.num_msgs - 1];
364 // Only the mandatory formats are supported
365 switch(which_format) {
366 case NNTP_LIST_ACTIVE:
367 // FIXME we have hardcoded "n" for "no posting allowed" -- fix when we add posting
368 cprintf("%s %ld %ld n\r\n", n_name, high_water_mark, low_water_mark);
370 case NNTP_LIST_NEWSGROUPS:
371 cprintf("%s %s\r\n", n_name, qrbuf->QRname);
375 if (nm.msgnums != NULL) {
382 // Called once per room by nntp_newgroups() to qualify and possibly output a single room
384 void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data)
388 time_t thetime = *(time_t *)data;
390 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
393 * The "created after <date/time>" heuristics depend on the happy coincidence
394 * that for a very long time we have used a unix timestamp as the room record's
395 * generation number (QRgen). When this module is merged into the master
396 * source tree we should rename QRgen to QR_create_time or something like that.
400 if (qrbuf->QRgen >= thetime) {
401 output_roomname_in_list_format(qrbuf, NNTP_LIST_ACTIVE, NULL);
408 // Implements the NEWGROUPS command
410 void nntp_newgroups(const char *cmd) {
411 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
413 char stringy_date[16];
414 char stringy_time[16];
415 char stringy_gmt[16];
419 extract_token(stringy_date, cmd, 1, ' ', sizeof stringy_date);
420 extract_token(stringy_time, cmd, 2, ' ', sizeof stringy_time);
421 extract_token(stringy_gmt, cmd, 3, ' ', sizeof stringy_gmt);
423 memset(&tm, 0, sizeof tm);
424 if (strlen(stringy_date) == 6) {
425 sscanf(stringy_date, "%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
429 sscanf(stringy_date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
432 tm.tm_mon -= 1; // tm_mon is zero based (0=January)
433 tm.tm_isdst = (-1); // let the C library figure out whether DST is in effect
434 sscanf(stringy_time, "%2d%2d%2d", &tm.tm_hour, &tm.tm_min ,&tm.tm_sec);
435 thetime = mktime(&tm);
436 if (!strcasecmp(stringy_gmt, "GMT")) {
442 cprintf("231 list of new newsgroups follows\r\n");
443 CtdlGetUser(&CC->user, CC->curr_user);
444 CtdlForEachRoom(nntp_newgroups_backend, &thetime);
450 // Called once per room by nntp_list() to qualify and possibly output a single room
452 void nntp_list_backend(struct ctdlroom *qrbuf, void *data)
456 struct nntp_list_data *nld = (struct nntp_list_data *)data;
458 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
460 output_roomname_in_list_format(qrbuf, nld->list_format, nld->wildmat_pattern);
466 // Implements the LIST commands
468 void nntp_list(const char *cmd) {
469 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
471 char list_format[64];
472 char wildmat_pattern[1024];
473 struct nntp_list_data nld;
475 extract_token(list_format, cmd, 1, ' ', sizeof list_format);
476 extract_token(wildmat_pattern, cmd, 2, ' ', sizeof wildmat_pattern);
478 if (strlen(wildmat_pattern) > 0) {
479 nld.wildmat_pattern = wildmat_pattern;
482 nld.wildmat_pattern = NULL;
485 if ( (strlen(cmd) < 6) || (!strcasecmp(list_format, "ACTIVE")) ) {
486 nld.list_format = NNTP_LIST_ACTIVE;
488 else if (!strcasecmp(list_format, "NEWSGROUPS")) {
489 nld.list_format = NNTP_LIST_NEWSGROUPS;
491 else if (!strcasecmp(list_format, "OVERVIEW.FMT")) {
492 nld.list_format = NNTP_LIST_OVERVIEW_FMT;
495 cprintf("501 syntax error , unsupported list format\r\n");
499 // OVERVIEW.FMT delivers a completely different type of data than all of the
500 // other LIST commands. It's a stupid place to put it. But that's how it's
501 // written into RFC3977, so we have to handle it here.
502 if (nld.list_format == NNTP_LIST_OVERVIEW_FMT) {
503 cprintf("215 Order of fields in overview database.\r\n");
504 cprintf("Subject:\r\n");
505 cprintf("From:\r\n");
506 cprintf("Date:\r\n");
507 cprintf("Message-ID:\r\n");
508 cprintf("References:\r\n");
509 cprintf("Bytes:\r\n");
510 cprintf("Lines:\r\n");
515 cprintf("215 list of newsgroups follows\r\n");
516 CtdlGetUser(&CC->user, CC->curr_user);
517 CtdlForEachRoom(nntp_list_backend, &nld);
523 // Implement HELP command.
525 void nntp_help(void) {
526 cprintf("100 This is the Citadel NNTP service.\r\n");
527 cprintf("RTFM http://www.ietf.org/rfc/rfc3977.txt\r\n");
533 // Implement DATE command.
535 void nntp_date(void) {
542 localtime_r(&now, &nowLocal);
543 gmtime_r(&now, &nowUtc);
545 strftime(tsFromUtc, sizeof(tsFromUtc), "%Y%m%d%H%M%S", &nowUtc);
547 cprintf("111 %s\r\n", tsFromUtc);
552 // back end for the LISTGROUP command , called for each message number
554 void nntp_listgroup_backend(long msgnum, void *userdata) {
556 struct listgroup_range *lr = (struct listgroup_range *)userdata;
558 // check range if supplied
559 if (msgnum < lr->lo) return;
560 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
562 cprintf("%ld\r\n", msgnum);
567 // Implements the GROUP and LISTGROUP commands
569 void nntp_group(const char *cmd) {
570 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
572 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
574 char requested_group[1024];
575 char message_range[256];
578 char requested_room[ROOMNAMELEN];
579 char augmented_roomname[ROOMNAMELEN];
583 struct ctdlroom QRscratch;
586 struct listgroup_range lr;
588 extract_token(verb, cmd, 0, ' ', sizeof verb);
589 extract_token(requested_group, cmd, 1, ' ', sizeof requested_group);
590 extract_token(message_range, cmd, 2, ' ', sizeof message_range);
591 extract_token(range_lo, message_range, 0, '-', sizeof range_lo);
592 extract_token(range_hi, message_range, 1, '-', sizeof range_hi);
593 lr.lo = atoi(range_lo);
594 lr.hi = atoi(range_hi);
596 /* In LISTGROUP mode we can specify an empty name for 'currently selected' */
597 if ((!strcasecmp(verb, "LISTGROUP")) && (IsEmptyStr(requested_group))) {
598 room_to_newsgroup(requested_group, CC->room.QRname, sizeof requested_group);
601 /* First try a regular match */
602 newsgroup_to_room(requested_room, requested_group, sizeof requested_room);
603 c = CtdlGetRoom(&QRscratch, requested_room);
605 /* Then try a mailbox name match */
607 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, requested_room);
608 c = CtdlGetRoom(&QRscratch, augmented_roomname);
610 safestrncpy(requested_room, augmented_roomname, sizeof(requested_room));
614 /* If the room exists, check security/access */
616 /* See if there is an existing user/room relationship */
617 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
619 /* normal clients have to pass through security */
625 /* Fail here if no such room */
627 cprintf("411 no such newsgroup\r\n");
633 * CtdlUserGoto() formally takes us to the desired room, happily returning
634 * the number of messages and number of new messages.
636 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
637 CtdlUserGoto(NULL, 0, 0, &msgs, &new, &oldest, &newest);
638 cprintf("211 %d %ld %ld %s\r\n", msgs, oldest, newest, requested_group);
640 // If this is a GROUP command, set the "current article number" to zero, and then stop here.
641 if (!strcasecmp(verb, "GROUP")) {
642 nntpstate->current_article_number = oldest;
646 // If we get to this point we are running a LISTGROUP command. Fetch those message numbers.
647 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_listgroup_backend, &lr);
653 // Implements the MODE command
655 void nntp_mode(const char *cmd) {
659 extract_token(which_mode, cmd, 1, ' ', sizeof which_mode);
661 if (!strcasecmp(which_mode, "reader")) {
662 // FIXME implement posting and change to 200
663 cprintf("201 Reader mode activated\r\n");
666 cprintf("501 unknown mode\r\n");
672 // Implements the ARTICLE, HEAD, BODY, and STAT commands.
673 // (These commands all accept the same parameters; they differ only in how they output the retrieved message.)
675 void nntp_article(const char *cmd) {
676 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
678 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
679 char which_command[16];
681 char requested_article[256];
682 long requested_msgnum = 0;
683 char *lb, *rb = NULL;
684 int must_change_currently_selected_article = 0;
686 // We're going to store one of these values in the variable 'acmd' so that
687 // we can quickly check later which version of the output we want.
695 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
697 if (!strcasecmp(which_command, "article")) {
700 else if (!strcasecmp(which_command, "head")) {
703 else if (!strcasecmp(which_command, "body")) {
706 else if (!strcasecmp(which_command, "stat")) {
710 cprintf("500 I'm afraid I can't do that.\r\n");
714 // Which NNTP command was issued, determines whether we will fetch headers, body, or both.
715 int headers_only = HEADERS_ALL;
716 if (acmd == HEAD) headers_only = HEADERS_FAST;
717 else if (acmd == BODY) headers_only = HEADERS_NONE;
718 else if (acmd == STAT) headers_only = HEADERS_FAST;
720 // now figure out what the client is asking for.
721 extract_token(requested_article, cmd, 1, ' ', sizeof requested_article);
722 lb = strchr(requested_article, '<');
723 rb = strchr(requested_article, '>');
724 requested_msgnum = atol(requested_article);
726 // If no article number or message-id is specified, the client wants the "currently selected article"
727 if (IsEmptyStr(requested_article)) {
728 if (nntpstate->current_article_number < 1) {
729 cprintf("420 No current article selected\r\n");
732 requested_msgnum = nntpstate->current_article_number;
733 must_change_currently_selected_article = 1;
734 // got it -- now fall through and keep going
737 // If the requested article is numeric, it maps directly to a message number. Good.
738 else if (requested_msgnum > 0) {
739 must_change_currently_selected_article = 1;
740 // good -- fall through and keep going
743 // If the requested article has angle brackets, the client wants a specific message-id.
744 // We don't know how to do that yet.
745 else if ( (lb != NULL) && (rb != NULL) && (lb < rb) ) {
746 must_change_currently_selected_article = 0;
747 cprintf("500 I don't know how to fetch by message-id yet.\r\n"); // FIXME
751 // Anything else is noncompliant gobbledygook and should die in a car fire.
753 must_change_currently_selected_article = 0;
754 cprintf("500 syntax error\r\n");
758 // At this point we know the message number of the "article" being requested.
759 // We have an awesome API call that does all the heavy lifting for us.
760 char *fetched_message_id = NULL;
761 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
762 int fetch = CtdlOutputMsg(requested_msgnum,
763 MT_RFC822, // output in RFC822 format ... sort of
764 headers_only, // headers, body, or both?
765 0, // don't do Citadel protocol responses
767 NULL, // teh whole thing, not just a section
768 0, // no flags yet ... maybe new ones for Path: etc ?
771 &fetched_message_id // extract the message ID from the message as we go...
773 StrBuf *msgtext = CC->redirect_buffer;
774 CC->redirect_buffer = NULL;
776 if (fetch != om_ok) {
777 cprintf("423 no article with that number\r\n");
778 FreeStrBuf(&msgtext);
782 // RFC3977 6.2.1.2 specifes conditions under which the "currently selected article"
783 // MUST or MUST NOT be set to the message we just referenced.
784 if (must_change_currently_selected_article) {
785 nntpstate->current_article_number = requested_msgnum;
788 // Now give the client what it asked for.
789 if (acmd == ARTICLE) {
790 cprintf("220 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
793 cprintf("221 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
796 cprintf("222 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
799 cprintf("223 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
800 FreeStrBuf(&msgtext);
804 client_write(SKEY(msgtext));
805 cprintf(".\r\n"); // this protocol uses a dot terminator
806 FreeStrBuf(&msgtext);
807 if (fetched_message_id) free(fetched_message_id);
812 // Utility function for nntp_last_next() that turns a msgnum into a message ID.
813 // The memory for the returned string is pwnz0red by the caller.
815 char *message_id_from_msgnum(long msgnum) {
817 char *fetched_message_id = NULL;
818 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
819 CtdlOutputMsg(msgnum,
820 MT_RFC822, // output in RFC822 format ... sort of
821 HEADERS_FAST, // headers, body, or both?
822 0, // don't do Citadel protocol responses
824 NULL, // teh whole thing, not just a section
825 0, // no flags yet ... maybe new ones for Path: etc ?
828 &fetched_message_id // extract the message ID from the message as we go...
830 StrBuf *msgtext = CC->redirect_buffer;
831 CC->redirect_buffer = NULL;
833 FreeStrBuf(&msgtext);
834 return(fetched_message_id);
839 // The LAST and NEXT commands are so similar that they are handled by a single function.
841 void nntp_last_next(const char *cmd) {
842 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
844 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
845 char which_command[16];
848 // We're going to store one of these values in the variable 'acmd' so that
849 // we can quickly check later which version of the output we want.
855 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
857 if (!strcasecmp(which_command, "last")) {
860 else if (!strcasecmp(which_command, "next")) {
864 cprintf("500 I'm afraid I can't do that.\r\n");
868 // ok, here we go ... fetch the msglist so we can figure out our place in the universe
869 struct nntp_msglist nm;
871 long selected_msgnum = 0;
872 char *message_id = NULL;
874 nm = nntp_fetch_msglist(&CC->room);
875 if ((nm.num_msgs < 0) || (nm.msgnums == NULL)) {
876 cprintf("500 something bad happened\r\n");
880 if ( (acmd == NNTP_LAST) && (nm.num_msgs == 0) ) {
881 cprintf("422 no previous article in this group\r\n"); // nothing here
884 else if ( (acmd == NNTP_LAST) && (nntpstate->current_article_number <= nm.msgnums[0]) ) {
885 cprintf("422 no previous article in this group\r\n"); // already at the beginning
888 else if (acmd == NNTP_LAST) {
889 for (i=0; ((i<nm.num_msgs)&&(selected_msgnum<=0)); ++i) {
890 if ( (nm.msgnums[i] >= nntpstate->current_article_number) && (i > 0) ) {
891 selected_msgnum = nm.msgnums[i-1];
894 if (selected_msgnum > 0) {
895 nntpstate->current_article_number = selected_msgnum;
896 message_id = message_id_from_msgnum(nntpstate->current_article_number);
897 cprintf("223 %ld <%s>\r\n", nntpstate->current_article_number, message_id);
898 if (message_id) free(message_id);
901 cprintf("422 no previous article in this group\r\n");
905 else if ( (acmd == NNTP_NEXT) && (nm.num_msgs == 0) ) {
906 cprintf("421 no next article in this group\r\n"); // nothing here
909 else if ( (acmd == NNTP_NEXT) && (nntpstate->current_article_number >= nm.msgnums[nm.num_msgs-1]) ) {
910 cprintf("421 no next article in this group\r\n"); // already at the end
913 else if (acmd == NNTP_NEXT) {
914 for (i=0; ((i<nm.num_msgs)&&(selected_msgnum<=0)); ++i) {
915 if (nm.msgnums[i] > nntpstate->current_article_number) {
916 selected_msgnum = nm.msgnums[i];
919 if (selected_msgnum > 0) {
920 nntpstate->current_article_number = selected_msgnum;
921 message_id = message_id_from_msgnum(nntpstate->current_article_number);
922 cprintf("223 %ld <%s>\r\n", nntpstate->current_article_number, message_id);
923 if (message_id) free(message_id);
926 cprintf("421 no next article in this group\r\n");
930 // should never get here
932 cprintf("500 internal error\r\n");
935 if (nm.msgnums != NULL) {
943 // back end for the XOVER command , called for each message number
945 void nntp_xover_backend(long msgnum, void *userdata) {
947 struct listgroup_range *lr = (struct listgroup_range *)userdata;
949 // check range if supplied
950 if (msgnum < lr->lo) return;
951 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
953 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 0);
958 // Teh RFC says we need:
959 // -------------------------
960 // Subject header content
961 // From header content
962 // Date header content
963 // Message-ID header content
964 // References header content
965 // :bytes metadata item
966 // :lines metadata item
968 time_t msgtime = atol(msg->cm_fields[eTimestamp]);
970 ctime_r(&msgtime, strtimebuf);
972 // here we go -- print the line o'data
973 cprintf("%ld\t%s\t%s <%s>\t%s\t%s\t%s\t100\t10\r\n",
975 msg->cm_fields[eMsgSubject],
976 msg->cm_fields[eAuthor],
977 msg->cm_fields[erFc822Addr],
979 msg->cm_fields[emessageId],
980 msg->cm_fields[eWeferences]
989 // XOVER is used by some clients, even if we don't offer it
991 void nntp_xover(const char *cmd) {
992 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
994 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
996 struct listgroup_range lr;
998 extract_token(range, cmd, 1, ' ', sizeof range);
1001 lr.lo = nntpstate->current_article_number;
1002 lr.hi = nntpstate->current_article_number;
1005 char *dash = strchr(range, '-');
1012 if (lr.hi < lr.lo) {
1021 cprintf("224 Overview information follows\r\n");
1022 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_xover_backend, &lr);
1028 // Main command loop for NNTP server sessions.
1030 void nntp_command_loop(void)
1032 StrBuf *Cmd = NewStrBuf();
1036 if (CtdlClientGetLine(Cmd) < 1) {
1037 syslog(LOG_CRIT, "NNTP: client disconnected: ending session.\n");
1038 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
1042 syslog(LOG_DEBUG, "NNTP: %s\n", ((!strncasecmp(ChrPtr(Cmd), "AUTHINFO", 8)) ? "AUTHINFO ..." : ChrPtr(Cmd)));
1043 extract_token(cmdname, ChrPtr(Cmd), 0, ' ', sizeof cmdname);
1045 // Rumpelstiltskin lookups are *awesome*
1047 if (!strcasecmp(cmdname, "quit")) {
1051 else if (!strcasecmp(cmdname, "help")) {
1055 else if (!strcasecmp(cmdname, "date")) {
1059 else if (!strcasecmp(cmdname, "capabilities")) {
1060 nntp_capabilities();
1063 else if (!strcasecmp(cmdname, "starttls")) {
1067 else if (!strcasecmp(cmdname, "authinfo")) {
1068 nntp_authinfo(ChrPtr(Cmd));
1071 else if (!strcasecmp(cmdname, "newgroups")) {
1072 nntp_newgroups(ChrPtr(Cmd));
1075 else if (!strcasecmp(cmdname, "list")) {
1076 nntp_list(ChrPtr(Cmd));
1079 else if (!strcasecmp(cmdname, "group")) {
1080 nntp_group(ChrPtr(Cmd));
1083 else if (!strcasecmp(cmdname, "listgroup")) {
1084 nntp_group(ChrPtr(Cmd));
1087 else if (!strcasecmp(cmdname, "mode")) {
1088 nntp_mode(ChrPtr(Cmd));
1092 (!strcasecmp(cmdname, "article"))
1093 || (!strcasecmp(cmdname, "head"))
1094 || (!strcasecmp(cmdname, "body"))
1095 || (!strcasecmp(cmdname, "stat"))
1098 nntp_article(ChrPtr(Cmd));
1102 (!strcasecmp(cmdname, "last"))
1103 || (!strcasecmp(cmdname, "next"))
1106 nntp_last_next(ChrPtr(Cmd));
1110 (!strcasecmp(cmdname, "xover"))
1111 || (!strcasecmp(cmdname, "over"))
1114 nntp_xover(ChrPtr(Cmd));
1118 cprintf("500 I'm afraid I can't do that.\r\n");
1125 // ****************************************************************************
1126 // MODULE INITIALIZATION STUFF
1127 // ****************************************************************************
1131 // This cleanup function blows away the temporary memory used by
1134 void nntp_cleanup_function(void)
1136 /* Don't do this stuff if this is not an NNTP session! */
1137 if (CC->h_command_function != nntp_command_loop) return;
1139 syslog(LOG_DEBUG, "Performing NNTP cleanup hook\n");
1140 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
1141 if (nntpstate != NULL) {
1147 const char *CitadelServiceNNTP="NNTP";
1148 const char *CitadelServiceNNTPS="NNTPS";
1150 CTDL_MODULE_INIT(nntp)
1154 CtdlRegisterServiceHook(CtdlGetConfigInt("c_nntp_port"),
1159 CitadelServiceNNTP);
1162 CtdlRegisterServiceHook(CtdlGetConfigInt("c_nntps_port"),
1167 CitadelServiceNNTPS);
1170 CtdlRegisterSessionHook(nntp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
1173 /* return our module name for the log */