]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/nntp/serv_nntp.c
NEWGROUPS command now parses the supplied date and time correctly
[citadel.git] / citadel / modules / nntp / serv_nntp.c
index 5958a3607e6dfab20a076748e3a9288065f322e3..07bb5f443695b3bc8d6e33b18835acc031f0007e 100644 (file)
 #include "citadel_dirs.h"
 #include "ctdl_module.h"
 
+
+extern long timezone;
+
+/****************** BEGIN UTILITY FUNCTIONS THAT COULD BE MOVED ELSEWHERE LATER **************/
+
+
+/*
+ * Tests whether the supplied string is a valid newsgroup name
+ * Returns true (nonzero) or false (0)
+ */
+int is_valid_newsgroup_name(char *name) {
+       char *ptr = name;
+       int has_a_letter = 0;
+
+       if (!ptr) return(0);
+       if (!strncasecmp(name, "ctdl.", 5)) return(0);
+
+       while (*ptr != 0) {
+
+               if (isalpha(ptr[0])) {
+                       has_a_letter = 1;
+               }
+
+               if (    (isalnum(ptr[0]))
+                       || (ptr[0] == '.')
+                       || (ptr[0] == '+')
+                       || (ptr[0] == '-')
+               ) {
+                       ++ptr;
+               }
+               else {
+                       return(0);
+               }
+       }
+       return(has_a_letter);
+}
+
+
+
+/*
+ * Convert a Citadel room name to a valid newsgroup name
+ */
+void room_to_newsgroup(char *target, char *source, size_t target_size) {
+
+       if (!target) return;
+       if (!source) return;
+
+       if (is_valid_newsgroup_name(source)) {
+               strncpy(target, source, target_size);
+               return;
+       }
+
+       strcpy(target, "ctdl.");
+       int len = 5;
+       char *ptr = source;
+       char ch;
+
+       while (ch=*ptr++, ch!=0) {
+               if (len >= target_size) return;
+               if (    (isalnum(ch))
+                       || (ch == '.')
+                       || (ch == '-')
+               ) {
+                       target[len++] = ch;
+                       target[len] = 0;
+               }
+               else {
+                       target[len++] = '+' ;
+                       sprintf(&target[len], "%02x", ch);
+                       len += 2;
+                       target[len] = 0;
+               }
+       }
+}
+
+
+/*
+ * Convert a newsgroup name to a Citadel room name.
+ * This function recognizes names converted with room_to_newsgroup() and restores them with full fidelity.
+ */
+void newsgroup_to_room(char *target, char *source, size_t target_size) {
+
+       if (!target) return;
+       if (!source) return;
+
+       if (strncasecmp(source, "ctdl.", 5)) {                  // not a converted room name; pass through as-is
+               strncpy(target, source, target_size);
+               return;
+       }
+
+       target[0] = 0;
+       int len = 0;
+       char *ptr = &source[5];
+       char ch;
+
+       while (ch=*ptr++, ch!=0) {
+               if (len >= target_size) return;
+               if (ch == '+') {
+                       char hex[3];
+                       long digit;
+                       hex[0] = *ptr++;
+                       hex[1] = *ptr++;
+                       hex[2] = 0;
+                       digit = strtol(hex, NULL, 16);
+                       ch = (char)digit;
+               }
+               target[len++] = ch;
+               target[len] = 0;
+       }
+}
+
+
+/******************  END  UTILITY FUNCTIONS THAT COULD BE MOVED ELSEWHERE LATER **************/
+
+
+
 /*
  * Here's where our NNTP session begins its happy day.
  */
@@ -220,6 +336,87 @@ void nntp_authinfo(const char *cmd) {
 
 
 
+/* FIXME not finished need to add water marks
+ */
+void output_roomname_in_list_active_format(struct ctdlroom *qrbuf) {
+       char n_name[1024];
+       room_to_newsgroup(n_name, qrbuf->QRname, sizeof n_name);
+       cprintf("%s\r\n", n_name);
+}
+
+
+
+/* 
+ */
+void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data)
+{
+       int ra;
+       int view;
+       time_t thetime = *(time_t *)data;
+
+       CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
+
+       /*
+        * The "created after <date/time>" heuristics depend on the happy coincidence
+        * that for a very long time we have used a unix timestamp as the room record's
+        * generation number (QRgen).  When this module is merged into the master
+        * source tree we should rename QRgen to QR_create_time or something like that.
+        */
+
+       if (ra & UA_KNOWN) {
+               if (qrbuf->QRgen >= thetime) {
+                       output_roomname_in_list_active_format(qrbuf);
+               }
+       }
+}
+
+/*
+ * Implements the NEWGROUPS command
+ */
+void nntp_newgroups(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 stringy_date[16];
+       char stringy_time[16];
+       char stringy_gmt[16];
+       struct tm tm;
+       time_t thetime;
+
+       extract_token(stringy_date, cmd, 1, ' ', sizeof stringy_date);
+       extract_token(stringy_time, cmd, 2, ' ', sizeof stringy_time);
+       extract_token(stringy_gmt, cmd, 3, ' ', sizeof stringy_gmt);
+
+       memset(&tm, 0, sizeof tm);
+       if (strlen(stringy_date) == 6) {
+               sscanf(stringy_date, "%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
+               tm.tm_year += 100;
+       }
+       else {
+               sscanf(stringy_date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
+               tm.tm_year -= 1900;
+       }
+       tm.tm_mon -= 1;         // tm_mon is zero based (0=January)
+       tm.tm_isdst = (-1);     // let the C library figure out whether DST is in effect
+       sscanf(stringy_time, "%2d%2d%2d", &tm.tm_hour, &tm.tm_min ,&tm.tm_sec);
+       thetime = mktime(&tm);
+       if (!strcasecmp(stringy_gmt, "GMT")) {
+               tzset();
+               thetime += timezone;
+       }
+
+
+       cprintf("231 list of new newsgroups follows\r\n");
+       CtdlGetUser(&CC->user, CC->curr_user);
+       CtdlForEachRoom(nntp_newgroups_backend, &thetime);
+       cprintf(".\r\n");
+}
+
+
 /* 
  * Main command loop for NNTP server sessions.
  */
@@ -262,6 +459,10 @@ void nntp_command_loop(void)
                nntp_authinfo(ChrPtr(Cmd));
        }
 
+       else if (!strcasecmp(cmdname, "newgroups")) {
+               nntp_newgroups(ChrPtr(Cmd));
+       }
+
        else {
                cprintf("500 I'm afraid I can't do that.\r\n");
        }
@@ -271,7 +472,7 @@ void nntp_command_loop(void)
 
 
 /*****************************************************************************/
-/*                      MODULE INITIALIZATION STUFF                          */
+/*                   MODULE INITIALIZATION STUFF                         */
 /*****************************************************************************/
 
 
@@ -316,3 +517,4 @@ CTDL_MODULE_INIT(nntp)
        /* return our module name for the log */
        return "nntp";
 }
+