MODE READER
[citadel.git] / citadel / modules / nntp / serv_nntp.c
index 74018e45f5bd048fb882948b851ce72ce985ce92..86368596002aeb59d8aef9b99daa6d974b8010e3 100644 (file)
@@ -231,19 +231,14 @@ void nntp_starttls(void)
 }
 
 
-void nntp_noop(void)
-{
-       cprintf("250 NOOP\r\n");
-}
-
-
 void nntp_capabilities(void)
 {
        cprintf("101 Capability list:\r\n");
+       cprintf("IMPLEMENTATION Citadel v%d.%02d\r\n", (REV_LEVEL/100), (REV_LEVEL%100));
        cprintf("VERSION 2\r\n");
        cprintf("READER\r\n");
+       cprintf("MODE-READER\r\n");
        cprintf("LIST ACTIVE NEWSGROUPS\r\n");
-       cprintf("IMPLEMENTATION Citadel v%d.%02d\r\n", (REV_LEVEL/100), (REV_LEVEL%100));
 #ifdef HAVE_OPENSSL
        cprintf("STARTTLS\r\n");
 #endif
@@ -378,13 +373,20 @@ enum {
 /*
  * Output a room name (newsgroup name) in formats required for LIST and NEWGROUPS command
  */
-void output_roomname_in_list_format(struct ctdlroom *qrbuf, int which_format) {
+void output_roomname_in_list_format(struct ctdlroom *qrbuf, int which_format, char *wildmat_pattern) {
        char n_name[1024];
        struct nntp_msglist nm;
        long low_water_mark = 0;
        long high_water_mark = 0;
 
        room_to_newsgroup(n_name, qrbuf->QRname, sizeof n_name);
+
+       if ((wildmat_pattern != NULL) && (!IsEmptyStr(wildmat_pattern))) {
+               if (!wildmat(n_name, wildmat_pattern)) {
+                       return;
+               }
+       }
+
        nm = nntp_fetch_msglist(qrbuf);
        if ((nm.num_msgs > 0) && (nm.msgnums != NULL)) {
                low_water_mark = nm.msgnums[0];
@@ -429,7 +431,7 @@ void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data)
 
        if (ra & UA_KNOWN) {
                if (qrbuf->QRgen >= thetime) {
-                       output_roomname_in_list_format(qrbuf, NNTP_LIST_ACTIVE);
+                       output_roomname_in_list_format(qrbuf, NNTP_LIST_ACTIVE, NULL);
                }
        }
 }
@@ -491,13 +493,9 @@ void nntp_list_backend(struct ctdlroom *qrbuf, void *data)
        int view;
        struct nntp_list_data *nld = (struct nntp_list_data *)data;
 
-       // FIXME do something with nld->wildmat, bitch
-
-       wildmat(NULL,NULL);     // linkage test
-
        CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
        if (ra & UA_KNOWN) {
-               output_roomname_in_list_format(qrbuf, nld->list_format);
+               output_roomname_in_list_format(qrbuf, nld->list_format, nld->wildmat_pattern);
        }
 }
 
@@ -513,17 +511,17 @@ void nntp_list(const char *cmd) {
        if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
 
        char list_format[64];
-       char wildmat[1024];
+       char wildmat_pattern[1024];
        struct nntp_list_data nld;
 
        extract_token(list_format, cmd, 1, ' ', sizeof list_format);
-       extract_token(wildmat, cmd, 2, ' ', sizeof wildmat);
+       extract_token(wildmat_pattern, cmd, 2, ' ', sizeof wildmat_pattern);
 
-       if (strlen(wildmat) > 0) {
-               nld.wildmat = wildmat;
+       if (strlen(wildmat_pattern) > 0) {
+               nld.wildmat_pattern = wildmat_pattern;
        }
        else {
-               nld.wildmat = NULL;
+               nld.wildmat_pattern = NULL;
        }
 
        if ( (strlen(cmd) < 6) || (!strcasecmp(list_format, "ACTIVE")) ) {
@@ -536,8 +534,6 @@ void nntp_list(const char *cmd) {
                cprintf("501 syntax error , unsupported list format\r\n");
                return;
        }
-       // FIXME do all of the other variants, bitch
-
 
        cprintf("231 list of newsgroups follows\r\n");
        CtdlGetUser(&CC->user, CC->curr_user);
@@ -546,6 +542,138 @@ void nntp_list(const char *cmd) {
 }
 
 
+/*
+ * Implement HELP command.
+ */
+void nntp_help(void) {
+       cprintf("100 This is the Citadel NNTP service.\r\n");
+       cprintf("RTFM http://www.ietf.org/rfc/rfc3977.txt\r\n");
+       cprintf(".\r\n");
+}
+
+
+/*
+ * back end for the LISTGROUP command , called for each message number
+ */
+void nntp_listgroup_backend(long msgnum, void *userdata) {
+
+       struct listgroup_range *lr = (struct listgroup_range *)userdata;
+
+       // check range if supplied
+       if (msgnum < lr->lo) return;
+       if ((lr->hi != 0) && (msgnum > lr->hi)) return;
+
+       cprintf("%ld\r\n", msgnum);
+}
+
+
+/*
+ * Implements the GROUP and LISTGROUP commands
+ */
+void nntp_group(const char *cmd) {
+       /*
+        * HACK: this works because the 5XX series error codes from citadel
+        * protocol will also be considered error codes by an NNTP client
+        */
+       if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
+
+       char verb[16];
+       char requested_group[1024];
+       char message_range[256];
+       char range_lo[256];
+       char range_hi[256];
+       char requested_room[ROOMNAMELEN];
+       char augmented_roomname[ROOMNAMELEN];
+       int c = 0;
+       int ok = 0;
+       int ra = 0;
+       struct ctdlroom QRscratch;
+       int msgs, new;
+       long oldest,newest;
+       struct listgroup_range lr;
+
+       extract_token(verb, cmd, 0, ' ', sizeof verb);
+       extract_token(requested_group, cmd, 1, ' ', sizeof requested_group);
+       extract_token(message_range, cmd, 2, ' ', sizeof message_range);
+       extract_token(range_lo, message_range, 0, '-', sizeof range_lo);
+       extract_token(range_hi, message_range, 1, '-', sizeof range_hi);
+       lr.lo = atoi(range_lo);
+       lr.hi = atoi(range_hi);
+
+       /* In LISTGROUP mode we can specify an empty name for 'currently selected' */
+       if ((!strcasecmp(verb, "LISTGROUP")) && (IsEmptyStr(requested_group))) {
+               room_to_newsgroup(requested_group, CC->room.QRname, sizeof requested_group);
+       }
+
+       /* First try a regular match */
+       newsgroup_to_room(requested_room, requested_group, sizeof requested_room);
+       c = CtdlGetRoom(&QRscratch, requested_room);
+
+       /* Then try a mailbox name match */
+       if (c != 0) {
+               CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, requested_room);
+               c = CtdlGetRoom(&QRscratch, augmented_roomname);
+               if (c == 0) {
+                       safestrncpy(requested_room, augmented_roomname, sizeof(requested_room));
+               }
+       }
+
+       /* If the room exists, check security/access */
+       if (c == 0) {
+               /* See if there is an existing user/room relationship */
+               CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
+
+               /* normal clients have to pass through security */
+               if (ra & UA_KNOWN) {
+                       ok = 1;
+               }
+       }
+
+       /* Fail here if no such room */
+       if (!ok) {
+               cprintf("411 no such newsgroup\r\n");
+               return;
+       }
+
+
+       /*
+        * CtdlUserGoto() formally takes us to the desired room, happily returning
+        * the number of messages and number of new messages.
+        */
+       memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
+       CtdlUserGoto(NULL, 0, 0, &msgs, &new, &oldest, &newest);
+       cprintf("211 %d %ld %ld %s\r\n", msgs, oldest, newest, requested_group);
+
+       // If this is a GROUP command, we can stop here.
+       if (!strcasecmp(verb, "GROUP")) {
+               return;
+       }
+
+       // If we get to this point we are running a LISTGROUP command.  Fetch those message numbers.
+       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_listgroup_backend, &lr);
+       cprintf(".\r\n");
+}
+
+
+/*
+ * Implements the MODE command
+ */
+void nntp_mode(const char *cmd) {
+
+       char which_mode[16];
+
+       extract_token(which_mode, cmd, 1, ' ', sizeof which_mode);
+
+       if (!strcasecmp(which_mode, "reader")) {
+               cprintf("201 Reader mode FIXME implement posting and change to 200\r\n");
+       }
+       else {
+               cprintf("501 unknown mode\r\n");
+       }
+}
+
+
+
 /* 
  * Main command loop for NNTP server sessions.
  */
@@ -572,6 +700,10 @@ void nntp_command_loop(void)
                nntp_quit();
        }
 
+       else if (!strcasecmp(cmdname, "help")) {
+               nntp_help();
+       }
+
        else if (!strcasecmp(cmdname, "capabilities")) {
                nntp_capabilities();
        }
@@ -580,10 +712,6 @@ void nntp_command_loop(void)
                nntp_starttls();
        }
 
-       else if (!strcasecmp(cmdname, "noop")) {
-               nntp_noop();
-       }
-
        else if (!strcasecmp(cmdname, "authinfo")) {
                nntp_authinfo(ChrPtr(Cmd));
        }
@@ -596,6 +724,18 @@ void nntp_command_loop(void)
                nntp_list(ChrPtr(Cmd));
        }
 
+       else if (!strcasecmp(cmdname, "group")) {
+               nntp_group(ChrPtr(Cmd));
+       }
+
+       else if (!strcasecmp(cmdname, "listgroup")) {
+               nntp_group(ChrPtr(Cmd));
+       }
+
+       else if (!strcasecmp(cmdname, "mode")) {
+               nntp_mode(ChrPtr(Cmd));
+       }
+
        else {
                cprintf("500 I'm afraid I can't do that.\r\n");
        }
@@ -650,4 +790,3 @@ CTDL_MODULE_INIT(nntp)
        /* return our module name for the log */
        return "nntp";
 }
-