Implemented wildmat match in LIST commands
[citadel.git] / citadel / modules / nntp / serv_nntp.c
index c6bca6c4d311dc065b37825e602cb21f8abe7607..82c8f2034eb1e1bcbcc4f2cd3679d9c134a415a5 100644 (file)
@@ -60,7 +60,7 @@
 #include "locate_host.h"
 #include "citadel_dirs.h"
 #include "ctdl_module.h"
-
+#include "serv_nntp.h"
 
 extern long timezone;
 
@@ -240,6 +240,10 @@ void nntp_noop(void)
 void nntp_capabilities(void)
 {
        cprintf("101 Capability list:\r\n");
+       cprintf("VERSION 2\r\n");
+       cprintf("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
@@ -335,12 +339,6 @@ void nntp_authinfo(const char *cmd) {
 }
 
 
-struct nntp_msglist {
-       int num_msgs;
-       long *msgnums;
-};
-
-
 /*
  * Utility function to fetch the current list of message numbers in a room
  */
@@ -364,26 +362,53 @@ struct nntp_msglist nntp_fetch_msglist(struct ctdlroom *qrbuf) {
 
 
 
+/*
+ * Various output formats for the LIST commands
+ */
+enum {
+       NNTP_LIST_ACTIVE,
+       NNTP_LIST_ACTIVE_TIMES,
+       NNTP_LIST_DISTRIB_PATS,
+       NNTP_LIST_HEADERS,
+       NNTP_LIST_NEWSGROUPS,
+       NNTP_LIST_OVERVIEW_FMT
+};
 
 
-
-/* FIXME not finished need to add water marks
+/*
+ * Output a room name (newsgroup name) in formats required for LIST and NEWGROUPS command
  */
-void output_roomname_in_list_active_format(struct ctdlroom *qrbuf) {
+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];
                high_water_mark = nm.msgnums[nm.num_msgs - 1];
        }
 
-       // FIXME we have hardcoded "n" for "no posting allowed" -- fix when we add posting
-       cprintf("%s %ld %ld n\r\n", n_name, low_water_mark, high_water_mark);
+       // Only the mandatory formats are supported
+       switch(which_format) {
+       case NNTP_LIST_ACTIVE:
+               // FIXME we have hardcoded "n" for "no posting allowed" -- fix when we add posting
+               cprintf("%s %ld %ld n\r\n", n_name, high_water_mark, low_water_mark);
+               break;
+       case NNTP_LIST_NEWSGROUPS:
+               cprintf("%s %s\r\n", n_name, qrbuf->QRname);
+               break;
+       }
+
        if (nm.msgnums != NULL) {
                free(nm.msgnums);
        }
@@ -391,7 +416,8 @@ void output_roomname_in_list_active_format(struct ctdlroom *qrbuf) {
 
 
 
-/* 
+/*
+ * Called once per room by nntp_newgroups() to qualify and possibly output a single room
  */
 void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data)
 {
@@ -410,11 +436,12 @@ void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data)
 
        if (ra & UA_KNOWN) {
                if (qrbuf->QRgen >= thetime) {
-                       output_roomname_in_list_active_format(qrbuf);
+                       output_roomname_in_list_format(qrbuf, NNTP_LIST_ACTIVE, NULL);
                }
        }
 }
 
+
 /*
  * Implements the NEWGROUPS command
  */
@@ -462,6 +489,64 @@ void nntp_newgroups(const char *cmd) {
 }
 
 
+/*
+ * Called once per room by nntp_list() to qualify and possibly output a single room
+ */
+void nntp_list_backend(struct ctdlroom *qrbuf, void *data)
+{
+       int ra;
+       int view;
+       struct nntp_list_data *nld = (struct nntp_list_data *)data;
+
+       CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
+       if (ra & UA_KNOWN) {
+               output_roomname_in_list_format(qrbuf, nld->list_format, nld->wildmat_pattern);
+       }
+}
+
+
+/*
+ * Implements the LIST commands
+ */
+void nntp_list(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 list_format[64];
+       char wildmat_pattern[1024];
+       struct nntp_list_data nld;
+
+       extract_token(list_format, cmd, 1, ' ', sizeof list_format);
+       extract_token(wildmat_pattern, cmd, 2, ' ', sizeof wildmat_pattern);
+
+       if (strlen(wildmat_pattern) > 0) {
+               nld.wildmat_pattern = wildmat_pattern;
+       }
+       else {
+               nld.wildmat_pattern = NULL;
+       }
+
+       if ( (strlen(cmd) < 6) || (!strcasecmp(list_format, "ACTIVE")) ) {
+               nld.list_format = NNTP_LIST_ACTIVE;
+       }
+       else if (!strcasecmp(list_format, "NEWSGROUPS")) {
+               nld.list_format = NNTP_LIST_NEWSGROUPS;
+       }
+       else {
+               cprintf("501 syntax error , unsupported list format\r\n");
+               return;
+       }
+
+       cprintf("231 list of newsgroups follows\r\n");
+       CtdlGetUser(&CC->user, CC->curr_user);
+       CtdlForEachRoom(nntp_list_backend, &nld);
+       cprintf(".\r\n");
+}
+
+
 /* 
  * Main command loop for NNTP server sessions.
  */
@@ -508,6 +593,10 @@ void nntp_command_loop(void)
                nntp_newgroups(ChrPtr(Cmd));
        }
 
+       else if (!strcasecmp(cmdname, "list")) {
+               nntp_list(ChrPtr(Cmd));
+       }
+
        else {
                cprintf("500 I'm afraid I can't do that.\r\n");
        }