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"
59 // Tests whether the supplied string is a valid newsgroup name
60 // Returns true (nonzero) or false (0)
62 int is_valid_newsgroup_name(char *name) {
68 if (!strncasecmp(name, "ctdl.", 5)) return(0);
72 if (isalpha(ptr[0])) {
80 if ( (isalnum(ptr[0]))
91 return( (has_a_letter) && (num_dots >= 1) ) ;
96 // Convert a Citadel room name to a valid newsgroup name
98 void room_to_newsgroup(char *target, char *source, size_t target_size) {
103 if (is_valid_newsgroup_name(source)) {
104 strncpy(target, source, target_size);
108 strcpy(target, "ctdl.");
113 while (ch=*ptr++, ch!=0) {
114 if (len >= target_size) return;
119 target[len++] = tolower(ch);
123 target[len++] = '+' ;
124 sprintf(&target[len], "%02x", ch);
133 // Convert a newsgroup name to a Citadel room name.
134 // This function recognizes names converted with room_to_newsgroup() and restores them with full fidelity.
136 void newsgroup_to_room(char *target, char *source, size_t target_size) {
141 if (strncasecmp(source, "ctdl.", 5)) { // not a converted room name; pass through as-is
142 strncpy(target, source, target_size);
148 char *ptr = &source[5];
151 while (ch=*ptr++, ch!=0) {
152 if (len >= target_size) return;
159 digit = strtol(hex, NULL, 16);
169 // Here's where our NNTP session begins its happy day.
171 void nntp_greeting(void) {
172 strcpy(CC->cs_clientname, "NNTP session");
173 CC->cs_flags |= CS_STEALTH;
175 CC->session_specific_data = malloc(sizeof(citnntp));
176 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
177 memset(nntpstate, 0, sizeof(citnntp));
179 if (CC->nologin==1) {
180 cprintf("451 Too many connections are already open; please try again later.\r\n");
181 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
185 // Display the standard greeting
186 cprintf("200 %s NNTP Citadel server is not finished yet\r\n", CtdlGetConfigStr("c_fqdn"));
191 // NNTPS is just like NNTP, except it goes crypto right away.
193 void nntps_greeting(void) {
194 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
196 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */
203 // implements the STARTTLS command
205 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) {
221 cprintf("101 Capability list:\r\n");
222 cprintf("IMPLEMENTATION Citadel %d\r\n", REV_LEVEL);
223 cprintf("VERSION 2\r\n");
224 cprintf("READER\r\n");
225 cprintf("MODE-READER\r\n");
226 cprintf("LIST ACTIVE NEWSGROUPS\r\n");
229 cprintf("STARTTLS\r\n");
231 if (!CC->logged_in) {
232 cprintf("AUTHINFO USER\r\n");
239 // Implements the QUIT command
241 void nntp_quit(void) {
242 cprintf("221 Goodbye...\r\n");
243 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
248 // Implements the AUTHINFO USER command (RFC 4643)
250 void nntp_authinfo_user(const char *username) {
251 int a = CtdlLoginExistingUser(username);
253 case login_already_logged_in:
254 cprintf("482 Already logged in\r\n");
256 case login_too_many_users:
257 cprintf("481 Too many users are already online (maximum is %d)\r\n", CtdlGetConfigInt("c_maxsessions"));
260 cprintf("381 Password required for %s\r\n", CC->curr_user);
262 case login_not_found:
263 cprintf("481 %s not found\r\n", username);
266 cprintf("502 Internal error\r\n");
272 // Implements the AUTHINFO PASS command (RFC 4643)
274 void nntp_authinfo_pass(const char *buf) {
277 a = CtdlTryPassword(buf, strlen(buf));
280 case pass_already_logged_in:
281 cprintf("482 Already logged in\r\n");
284 cprintf("482 Authentication commands issued out of sequence\r\n");
286 case pass_wrong_password:
287 cprintf("481 Authentication failed\r\n");
290 cprintf("281 Authentication accepted\r\n");
297 // Implements the AUTHINFO extension (RFC 4643) in USER/PASS mode
299 void nntp_authinfo(const char *cmd) {
301 if (!strncasecmp(cmd, "authinfo user ", 14)) {
302 nntp_authinfo_user(&cmd[14]);
305 else if (!strncasecmp(cmd, "authinfo pass ", 14)) {
306 nntp_authinfo_pass(&cmd[14]);
310 cprintf("502 command unavailable\r\n");
316 // Utility function to fetch the current list of message numbers in a room
318 struct nntp_msglist nntp_fetch_msglist(struct ctdlroom *qrbuf) {
319 struct nntp_msglist nm;
320 struct cdbdata *cdbfr;
322 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
324 nm.msgnums = (long*)cdbfr->ptr;
326 nm.num_msgs = cdbfr->len / sizeof(long);
338 // Output a room name (newsgroup name) in formats required for LIST and NEWGROUPS command
340 void output_roomname_in_list_format(struct ctdlroom *qrbuf, int which_format, char *wildmat_pattern) {
342 struct nntp_msglist nm;
343 long low_water_mark = 0;
344 long high_water_mark = 0;
346 room_to_newsgroup(n_name, qrbuf->QRname, sizeof n_name);
348 if ((wildmat_pattern != NULL) && (!IsEmptyStr(wildmat_pattern))) {
349 if (!wildmat(n_name, wildmat_pattern)) {
354 nm = nntp_fetch_msglist(qrbuf);
355 if ((nm.num_msgs > 0) && (nm.msgnums != NULL)) {
356 low_water_mark = nm.msgnums[0];
357 high_water_mark = nm.msgnums[nm.num_msgs - 1];
360 // Only the mandatory formats are supported
361 switch(which_format) {
362 case NNTP_LIST_ACTIVE:
363 // FIXME we have hardcoded "n" for "no posting allowed" -- fix when we add posting
364 cprintf("%s %ld %ld n\r\n", n_name, high_water_mark, low_water_mark);
366 case NNTP_LIST_NEWSGROUPS:
367 cprintf("%s %s\r\n", n_name, qrbuf->QRname);
371 if (nm.msgnums != NULL) {
378 // Called once per room by nntp_newgroups() to qualify and possibly output a single room
380 void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data) {
383 time_t thetime = *(time_t *)data;
385 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
388 * The "created after <date/time>" heuristics depend on the happy coincidence
389 * that for a very long time we have used a unix timestamp as the room record's
390 * generation number (QRgen). When this module is merged into the master
391 * source tree we should rename QRgen to QR_create_time or something like that.
395 if (qrbuf->QRgen >= thetime) {
396 output_roomname_in_list_format(qrbuf, NNTP_LIST_ACTIVE, NULL);
403 // Implements the NEWGROUPS command
405 void nntp_newgroups(const char *cmd) {
406 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
408 char stringy_date[16];
409 char stringy_time[16];
410 char stringy_gmt[16];
414 extract_token(stringy_date, cmd, 1, ' ', sizeof stringy_date);
415 extract_token(stringy_time, cmd, 2, ' ', sizeof stringy_time);
416 extract_token(stringy_gmt, cmd, 3, ' ', sizeof stringy_gmt);
418 memset(&tm, 0, sizeof tm);
419 if (strlen(stringy_date) == 6) {
420 sscanf(stringy_date, "%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
424 sscanf(stringy_date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
427 tm.tm_mon -= 1; // tm_mon is zero based (0=January)
428 tm.tm_isdst = (-1); // let the C library figure out whether DST is in effect
429 sscanf(stringy_time, "%2d%2d%2d", &tm.tm_hour, &tm.tm_min ,&tm.tm_sec);
430 thetime = mktime(&tm);
431 if (!strcasecmp(stringy_gmt, "GMT")) {
434 thetime += &tm.tm_gmtoff;
441 cprintf("231 list of new newsgroups follows\r\n");
442 CtdlGetUser(&CC->user, CC->curr_user);
443 CtdlForEachRoom(nntp_newgroups_backend, &thetime);
449 // Called once per room by nntp_list() to qualify and possibly output a single room
451 void nntp_list_backend(struct ctdlroom *qrbuf, void *data) {
454 struct nntp_list_data *nld = (struct nntp_list_data *)data;
456 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
458 output_roomname_in_list_format(qrbuf, nld->list_format, nld->wildmat_pattern);
464 // Implements the LIST commands
466 void nntp_list(const char *cmd) {
467 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
469 char list_format[64];
470 char wildmat_pattern[1024];
471 struct nntp_list_data nld;
473 extract_token(list_format, cmd, 1, ' ', sizeof list_format);
474 extract_token(wildmat_pattern, cmd, 2, ' ', sizeof wildmat_pattern);
476 if (strlen(wildmat_pattern) > 0) {
477 nld.wildmat_pattern = wildmat_pattern;
480 nld.wildmat_pattern = NULL;
483 if ( (strlen(cmd) < 6) || (!strcasecmp(list_format, "ACTIVE")) ) {
484 nld.list_format = NNTP_LIST_ACTIVE;
486 else if (!strcasecmp(list_format, "NEWSGROUPS")) {
487 nld.list_format = NNTP_LIST_NEWSGROUPS;
489 else if (!strcasecmp(list_format, "OVERVIEW.FMT")) {
490 nld.list_format = NNTP_LIST_OVERVIEW_FMT;
493 cprintf("501 syntax error , unsupported list format\r\n");
497 // OVERVIEW.FMT delivers a completely different type of data than all of the
498 // other LIST commands. It's a stupid place to put it. But that's how it's
499 // written into RFC3977, so we have to handle it here.
500 if (nld.list_format == NNTP_LIST_OVERVIEW_FMT) {
501 cprintf("215 Order of fields in overview database.\r\n");
502 cprintf("Subject:\r\n");
503 cprintf("From:\r\n");
504 cprintf("Date:\r\n");
505 cprintf("Message-ID:\r\n");
506 cprintf("References:\r\n");
507 cprintf("Bytes:\r\n");
508 cprintf("Lines:\r\n");
513 cprintf("215 list of newsgroups follows\r\n");
514 CtdlGetUser(&CC->user, CC->curr_user);
515 CtdlForEachRoom(nntp_list_backend, &nld);
521 // Implement HELP command.
523 void nntp_help(void) {
524 cprintf("100 This is the Citadel NNTP service.\r\n");
525 cprintf("RTFM http://www.ietf.org/rfc/rfc3977.txt\r\n");
531 // Implement DATE command.
533 void nntp_date(void) {
540 localtime_r(&now, &nowLocal);
541 gmtime_r(&now, &nowUtc);
543 strftime(tsFromUtc, sizeof(tsFromUtc), "%Y%m%d%H%M%S", &nowUtc);
545 cprintf("111 %s\r\n", tsFromUtc);
550 // back end for the LISTGROUP command , called for each message number
552 void nntp_listgroup_backend(long msgnum, void *userdata) {
554 struct listgroup_range *lr = (struct listgroup_range *)userdata;
556 // check range if supplied
557 if (msgnum < lr->lo) return;
558 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
560 cprintf("%ld\r\n", msgnum);
565 // Implements the GROUP and LISTGROUP commands
567 void nntp_group(const char *cmd) {
568 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
570 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
572 char requested_group[1024];
573 char message_range[256];
576 char requested_room[ROOMNAMELEN];
577 char augmented_roomname[ROOMNAMELEN];
581 struct ctdlroom QRscratch;
584 struct listgroup_range lr;
586 extract_token(verb, cmd, 0, ' ', sizeof verb);
587 extract_token(requested_group, cmd, 1, ' ', sizeof requested_group);
588 extract_token(message_range, cmd, 2, ' ', sizeof message_range);
589 extract_token(range_lo, message_range, 0, '-', sizeof range_lo);
590 extract_token(range_hi, message_range, 1, '-', sizeof range_hi);
591 lr.lo = atoi(range_lo);
592 lr.hi = atoi(range_hi);
594 /* In LISTGROUP mode we can specify an empty name for 'currently selected' */
595 if ((!strcasecmp(verb, "LISTGROUP")) && (IsEmptyStr(requested_group))) {
596 room_to_newsgroup(requested_group, CC->room.QRname, sizeof requested_group);
599 /* First try a regular match */
600 newsgroup_to_room(requested_room, requested_group, sizeof requested_room);
601 c = CtdlGetRoom(&QRscratch, requested_room);
603 /* Then try a mailbox name match */
605 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, requested_room);
606 c = CtdlGetRoom(&QRscratch, augmented_roomname);
608 safestrncpy(requested_room, augmented_roomname, sizeof(requested_room));
612 /* If the room exists, check security/access */
614 /* See if there is an existing user/room relationship */
615 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
617 /* normal clients have to pass through security */
623 /* Fail here if no such room */
625 cprintf("411 no such newsgroup\r\n");
631 * CtdlUserGoto() formally takes us to the desired room, happily returning
632 * the number of messages and number of new messages.
634 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
635 CtdlUserGoto(NULL, 0, 0, &msgs, &new, &oldest, &newest);
636 cprintf("211 %d %ld %ld %s\r\n", msgs, oldest, newest, requested_group);
638 // If this is a GROUP command, set the "current article number" to zero, and then stop here.
639 if (!strcasecmp(verb, "GROUP")) {
640 nntpstate->current_article_number = oldest;
644 // If we get to this point we are running a LISTGROUP command. Fetch those message numbers.
645 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_listgroup_backend, &lr);
651 // Implements the MODE command
653 void nntp_mode(const char *cmd) {
657 extract_token(which_mode, cmd, 1, ' ', sizeof which_mode);
659 if (!strcasecmp(which_mode, "reader")) {
660 // FIXME implement posting and change to 200
661 cprintf("201 Reader mode activated\r\n");
664 cprintf("501 unknown mode\r\n");
670 // Implements the ARTICLE, HEAD, BODY, and STAT commands.
671 // (These commands all accept the same parameters; they differ only in how they output the retrieved message.)
673 void nntp_article(const char *cmd) {
674 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
676 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
677 char which_command[16];
679 char requested_article[256];
680 long requested_msgnum = 0;
681 char *lb, *rb = NULL;
682 int must_change_currently_selected_article = 0;
684 // We're going to store one of these values in the variable 'acmd' so that
685 // we can quickly check later which version of the output we want.
693 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
695 if (!strcasecmp(which_command, "article")) {
698 else if (!strcasecmp(which_command, "head")) {
701 else if (!strcasecmp(which_command, "body")) {
704 else if (!strcasecmp(which_command, "stat")) {
708 cprintf("500 I'm afraid I can't do that.\r\n");
712 // Which NNTP command was issued, determines whether we will fetch headers, body, or both.
713 int headers_only = HEADERS_ALL;
714 if (acmd == HEAD) headers_only = HEADERS_FAST;
715 else if (acmd == BODY) headers_only = HEADERS_NONE;
716 else if (acmd == STAT) headers_only = HEADERS_FAST;
718 // now figure out what the client is asking for.
719 extract_token(requested_article, cmd, 1, ' ', sizeof requested_article);
720 lb = strchr(requested_article, '<');
721 rb = strchr(requested_article, '>');
722 requested_msgnum = atol(requested_article);
724 // If no article number or message-id is specified, the client wants the "currently selected article"
725 if (IsEmptyStr(requested_article)) {
726 if (nntpstate->current_article_number < 1) {
727 cprintf("420 No current article selected\r\n");
730 requested_msgnum = nntpstate->current_article_number;
731 must_change_currently_selected_article = 1;
732 // got it -- now fall through and keep going
735 // If the requested article is numeric, it maps directly to a message number. Good.
736 else if (requested_msgnum > 0) {
737 must_change_currently_selected_article = 1;
738 // good -- fall through and keep going
741 // If the requested article has angle brackets, the client wants a specific message-id.
742 // We don't know how to do that yet.
743 else if ( (lb != NULL) && (rb != NULL) && (lb < rb) ) {
744 must_change_currently_selected_article = 0;
745 cprintf("500 I don't know how to fetch by message-id yet.\r\n"); // FIXME
749 // Anything else is noncompliant gobbledygook and should die in a car fire.
751 must_change_currently_selected_article = 0;
752 cprintf("500 syntax error\r\n");
756 // At this point we know the message number of the "article" being requested.
757 // We have an awesome API call that does all the heavy lifting for us.
758 char *fetched_message_id = NULL;
759 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
760 int fetch = CtdlOutputMsg(requested_msgnum,
761 MT_RFC822, // output in RFC822 format ... sort of
762 headers_only, // headers, body, or both?
763 0, // don't do Citadel protocol responses
765 NULL, // teh whole thing, not just a section
766 0, // no flags yet ... maybe new ones for Path: etc ?
769 &fetched_message_id // extract the message ID from the message as we go...
771 StrBuf *msgtext = CC->redirect_buffer;
772 CC->redirect_buffer = NULL;
774 if (fetch != om_ok) {
775 cprintf("423 no article with that number\r\n");
776 FreeStrBuf(&msgtext);
780 // RFC3977 6.2.1.2 specifes conditions under which the "currently selected article"
781 // MUST or MUST NOT be set to the message we just referenced.
782 if (must_change_currently_selected_article) {
783 nntpstate->current_article_number = requested_msgnum;
786 // Now give the client what it asked for.
787 if (acmd == ARTICLE) {
788 cprintf("220 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
791 cprintf("221 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
794 cprintf("222 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
797 cprintf("223 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
798 FreeStrBuf(&msgtext);
802 client_write(SKEY(msgtext));
803 cprintf(".\r\n"); // this protocol uses a dot terminator
804 FreeStrBuf(&msgtext);
805 if (fetched_message_id) free(fetched_message_id);
810 // Utility function for nntp_last_next() that turns a msgnum into a message ID.
811 // The memory for the returned string is pwnz0red by the caller.
813 char *message_id_from_msgnum(long msgnum) {
814 char *fetched_message_id = NULL;
815 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
816 CtdlOutputMsg(msgnum,
817 MT_RFC822, // output in RFC822 format ... sort of
818 HEADERS_FAST, // headers, body, or both?
819 0, // don't do Citadel protocol responses
821 NULL, // teh whole thing, not just a section
822 0, // no flags yet ... maybe new ones for Path: etc ?
825 &fetched_message_id // extract the message ID from the message as we go...
827 StrBuf *msgtext = CC->redirect_buffer;
828 CC->redirect_buffer = NULL;
830 FreeStrBuf(&msgtext);
831 return(fetched_message_id);
836 // The LAST and NEXT commands are so similar that they are handled by a single function.
838 void nntp_last_next(const char *cmd) {
839 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
841 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
842 char which_command[16];
845 // We're going to store one of these values in the variable 'acmd' so that
846 // we can quickly check later which version of the output we want.
852 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
854 if (!strcasecmp(which_command, "last")) {
857 else if (!strcasecmp(which_command, "next")) {
861 cprintf("500 I'm afraid I can't do that.\r\n");
865 // ok, here we go ... fetch the msglist so we can figure out our place in the universe
866 struct nntp_msglist nm;
868 long selected_msgnum = 0;
869 char *message_id = NULL;
871 nm = nntp_fetch_msglist(&CC->room);
872 if ((nm.num_msgs < 0) || (nm.msgnums == NULL)) {
873 cprintf("500 something bad happened\r\n");
877 if ( (acmd == NNTP_LAST) && (nm.num_msgs == 0) ) {
878 cprintf("422 no previous article in this group\r\n"); // nothing here
881 else if ( (acmd == NNTP_LAST) && (nntpstate->current_article_number <= nm.msgnums[0]) ) {
882 cprintf("422 no previous article in this group\r\n"); // already at the beginning
885 else if (acmd == NNTP_LAST) {
886 for (i=0; ((i<nm.num_msgs)&&(selected_msgnum<=0)); ++i) {
887 if ( (nm.msgnums[i] >= nntpstate->current_article_number) && (i > 0) ) {
888 selected_msgnum = nm.msgnums[i-1];
891 if (selected_msgnum > 0) {
892 nntpstate->current_article_number = selected_msgnum;
893 message_id = message_id_from_msgnum(nntpstate->current_article_number);
894 cprintf("223 %ld <%s>\r\n", nntpstate->current_article_number, message_id);
895 if (message_id) free(message_id);
898 cprintf("422 no previous article in this group\r\n");
902 else if ( (acmd == NNTP_NEXT) && (nm.num_msgs == 0) ) {
903 cprintf("421 no next article in this group\r\n"); // nothing here
906 else if ( (acmd == NNTP_NEXT) && (nntpstate->current_article_number >= nm.msgnums[nm.num_msgs-1]) ) {
907 cprintf("421 no next article in this group\r\n"); // already at the end
910 else if (acmd == NNTP_NEXT) {
911 for (i=0; ((i<nm.num_msgs)&&(selected_msgnum<=0)); ++i) {
912 if (nm.msgnums[i] > nntpstate->current_article_number) {
913 selected_msgnum = nm.msgnums[i];
916 if (selected_msgnum > 0) {
917 nntpstate->current_article_number = selected_msgnum;
918 message_id = message_id_from_msgnum(nntpstate->current_article_number);
919 cprintf("223 %ld <%s>\r\n", nntpstate->current_article_number, message_id);
920 if (message_id) free(message_id);
923 cprintf("421 no next article in this group\r\n");
927 // should never get here
929 cprintf("500 internal error\r\n");
932 if (nm.msgnums != NULL) {
940 // back end for the XOVER command , called for each message number
942 void nntp_xover_backend(long msgnum, void *userdata) {
944 struct listgroup_range *lr = (struct listgroup_range *)userdata;
946 // check range if supplied
947 if (msgnum < lr->lo) return;
948 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
950 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 0);
955 // Teh RFC says we need:
956 // -------------------------
957 // Subject header content
958 // From header content
959 // Date header content
960 // Message-ID header content
961 // References header content
962 // :bytes metadata item
963 // :lines metadata item
965 time_t msgtime = atol(msg->cm_fields[eTimestamp]);
967 ctime_r(&msgtime, strtimebuf);
969 // here we go -- print the line o'data
970 cprintf("%ld\t%s\t%s <%s>\t%s\t%s\t%s\t100\t10\r\n",
972 msg->cm_fields[eMsgSubject],
973 msg->cm_fields[eAuthor],
974 msg->cm_fields[erFc822Addr],
976 msg->cm_fields[emessageId],
977 msg->cm_fields[eWeferences]
986 // XOVER is used by some clients, even if we don't offer it
988 void nntp_xover(const char *cmd) {
989 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
991 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
993 struct listgroup_range lr;
995 extract_token(range, cmd, 1, ' ', sizeof range);
998 lr.lo = nntpstate->current_article_number;
999 lr.hi = nntpstate->current_article_number;
1002 char *dash = strchr(range, '-');
1009 if (lr.hi < lr.lo) {
1018 cprintf("224 Overview information follows\r\n");
1019 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_xover_backend, &lr);
1025 // Main command loop for NNTP server sessions.
1027 void nntp_command_loop(void) {
1028 StrBuf *Cmd = NewStrBuf();
1032 if (CtdlClientGetLine(Cmd) < 1) {
1033 syslog(LOG_CRIT, "NNTP: client disconnected: ending session.\n");
1034 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
1038 syslog(LOG_DEBUG, "NNTP: %s\n", ((!strncasecmp(ChrPtr(Cmd), "AUTHINFO", 8)) ? "AUTHINFO ..." : ChrPtr(Cmd)));
1039 extract_token(cmdname, ChrPtr(Cmd), 0, ' ', sizeof cmdname);
1041 // Rumpelstiltskin lookups are *awesome*
1043 if (!strcasecmp(cmdname, "quit")) {
1047 else if (!strcasecmp(cmdname, "help")) {
1051 else if (!strcasecmp(cmdname, "date")) {
1055 else if (!strcasecmp(cmdname, "capabilities")) {
1056 nntp_capabilities();
1059 else if (!strcasecmp(cmdname, "starttls")) {
1063 else if (!strcasecmp(cmdname, "authinfo")) {
1064 nntp_authinfo(ChrPtr(Cmd));
1067 else if (!strcasecmp(cmdname, "newgroups")) {
1068 nntp_newgroups(ChrPtr(Cmd));
1071 else if (!strcasecmp(cmdname, "list")) {
1072 nntp_list(ChrPtr(Cmd));
1075 else if (!strcasecmp(cmdname, "group")) {
1076 nntp_group(ChrPtr(Cmd));
1079 else if (!strcasecmp(cmdname, "listgroup")) {
1080 nntp_group(ChrPtr(Cmd));
1083 else if (!strcasecmp(cmdname, "mode")) {
1084 nntp_mode(ChrPtr(Cmd));
1088 (!strcasecmp(cmdname, "article"))
1089 || (!strcasecmp(cmdname, "head"))
1090 || (!strcasecmp(cmdname, "body"))
1091 || (!strcasecmp(cmdname, "stat"))
1094 nntp_article(ChrPtr(Cmd));
1098 (!strcasecmp(cmdname, "last"))
1099 || (!strcasecmp(cmdname, "next"))
1102 nntp_last_next(ChrPtr(Cmd));
1106 (!strcasecmp(cmdname, "xover"))
1107 || (!strcasecmp(cmdname, "over"))
1110 nntp_xover(ChrPtr(Cmd));
1114 cprintf("500 I'm afraid I can't do that.\r\n");
1121 // ****************************************************************************
1122 // MODULE INITIALIZATION STUFF
1123 // ****************************************************************************
1127 // This cleanup function blows away the temporary memory used by
1130 void nntp_cleanup_function(void) {
1131 /* Don't do this stuff if this is not an NNTP session! */
1132 if (CC->h_command_function != nntp_command_loop) return;
1134 syslog(LOG_DEBUG, "Performing NNTP cleanup hook\n");
1135 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
1136 if (nntpstate != NULL) {
1142 const char *CitadelServiceNNTP="NNTP";
1143 const char *CitadelServiceNNTPS="NNTPS";
1145 CTDL_MODULE_INIT(nntp)
1149 CtdlRegisterServiceHook(CtdlGetConfigInt("c_nntp_port"),
1154 CitadelServiceNNTP);
1157 CtdlRegisterServiceHook(CtdlGetConfigInt("c_nntps_port"),
1162 CitadelServiceNNTPS);
1165 CtdlRegisterSessionHook(nntp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
1168 /* return our module name for the log */