]> code.citadel.org Git - citadel.git/blobdiff - citadel/migratenet.c
removed all references to sprintf from several files (not all files yet)
[citadel.git] / citadel / migratenet.c
index 50b440726d192e377aa74de306a47087d3643f9e..1ecf2c1e705f0d0b9ffa19b3aa16b823322e692e 100644 (file)
@@ -9,11 +9,21 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
 #include "citadel.h"
 #include "ipc.h"
 #include "tools.h"
+#include "config.h"
 
-extern struct config config;
+struct mn_roomlist {
+       struct mn_roomlist *next;
+       char roomname[SIZ];
+};
 
 void logoff(int code)
 {
@@ -25,6 +35,19 @@ int main(int argc, char **argv)
        char buf[SIZ];
        char hostbuf[SIZ];
        char portbuf[SIZ];
+       DIR *dp;
+       struct dirent *d;
+       long highest = 0L;
+       char node[SIZ];
+       char room[SIZ];
+       long thighest;
+       FILE *fp;
+       FILE *roomfp;
+       char roomfilename[SIZ];
+       FILE *nodefp;
+       char nodefilename[SIZ];
+       struct mn_roomlist *mn = NULL;
+       struct mn_roomlist *mnptr = NULL;
 
        printf("\n\n\n\n\n"
 "This utility migrates your network settings (room sharing with other\n"
@@ -41,12 +64,12 @@ int main(int argc, char **argv)
        attach_to_server(argc, argv, hostbuf, portbuf);
        serv_gets(buf);
        printf("%s\n", &buf[4]);
-       if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
-               fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
+       if ( (buf[0]!='2') && (strncmp(buf,"551",3)) ) {
+               fprintf(stderr, "%s: %s\n", argv[0], &buf[4]);
                logoff(atoi(buf));
        }
 
-       printf("%s\n", buf);
+       snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
        serv_puts(buf);
        serv_gets(buf);
        fprintf(stderr, "%s\n", &buf[4]);
@@ -54,6 +77,175 @@ int main(int argc, char **argv)
                exit(2);
        }
 
+       if (chdir("network/systems") != 0) {
+               perror("cannot chdir network/systems");
+               exit(errno);
+       }
+
+       strcpy(roomfilename, tmpnam(NULL));
+       roomfp = fopen(roomfilename, "w");
+       if (roomfp == NULL) {
+               perror("cannot open temp file");
+               exit(errno);
+       }
+
+       strcpy(nodefilename, tmpnam(NULL));
+       nodefp = fopen(nodefilename, "w");
+       if (roomfp == NULL) {
+               perror("cannot open temp file");
+               exit(errno);
+       }
+
+       dp = opendir(".");
+       if (dp == NULL) {
+               perror("cannot open directory");
+               exit(errno);
+       }
+
+       while (d = readdir(dp), d != NULL) {
+               fp = NULL;
+               if ( (d->d_name[0] != '.') && strcasecmp(d->d_name, "CVS")) {
+                       fp = fopen(d->d_name, "r");
+               }
+               if (fp != NULL) {
+                       printf("\n*** Processing '%s'\n", d->d_name);
+
+                       fprintf(nodefp, "%s|", d->d_name);
+                       printf("Enter shared secret: ");
+                       gets(buf);
+                       if (buf[0] == 0) strcpy(buf, config.c_net_password);
+                       fprintf(nodefp, "%s|", buf);
+                       printf("Enter host name/IP : ");
+                       gets(buf);
+                       if (buf[0] == 0) snprintf(buf, sizeof buf, "%s.citadel.org",
+                               d->d_name);
+                       fprintf(nodefp, "%s|", buf);
+                       printf("Enter port number  : ");
+                       gets(buf);
+                       if (buf[0] == 0) strcpy(buf, "504");
+                       fprintf(nodefp, "%s\n", buf);
+
+                       fgets(buf, sizeof buf, fp);
+                       printf("skipping: %s", buf);
+                       while (fgets(room, sizeof room, fp),
+                             (fgets(buf, sizeof buf, fp) != NULL) ) {
+                               thighest = atol(buf),
+                               room[strlen(room) - 1] = 0;
+                               fprintf(roomfp, "%s|%s\n",
+                                       d->d_name, room);
+                               if (thighest > highest) {
+                                       highest = thighest;
+                               }
+                       }
+                       fclose(fp);
+               }
+       }
+
+       closedir(dp);
+       fclose(roomfp);
+       fclose(nodefp);
+
+       /* Point of no return */
+
+       /* Set up the node table */
+       printf("Creating neighbor node table\n");
+       snprintf(buf, sizeof buf, "CONF putsys|%s", IGNETCFG);
+       serv_puts(buf);
+       serv_gets(buf);
+       if (buf[0] == '4') {
+               nodefp = fopen(nodefilename, "r");
+               if (nodefp != NULL) {
+                       while (fgets(buf, sizeof buf, nodefp) != NULL) {
+                               buf[strlen(buf)-1] = 0;
+                               serv_puts(buf);
+                       }
+                       fclose(nodefp);
+               }
+               serv_puts("000");
+       }
+       else {
+               printf("%s\n", &buf[4]);
+       }
+
+
+       /* Now go through the table looking for node names to enter */
+
+       snprintf(buf, sizeof buf, "cat %s |awk -F \"|\" '{ print $2 }' |sort -f |uniq -i",
+               roomfilename);
+       roomfp = popen(buf, "r");
+       if (roomfp == NULL) {
+               perror("cannot open pipe");
+               unlink(roomfilename);
+       }
+
+       while (fgets(room, sizeof room, roomfp) != NULL) {
+               room[strlen(room)-1] = 0;
+               if (strlen(room) > 0) {
+                       mnptr = (struct mn_roomlist *)
+                               malloc(sizeof (struct mn_roomlist));
+                       strcpy(mnptr->roomname, room);
+                       mnptr->next = mn;
+                       mn = mnptr;
+               }
+       }
+
+       pclose(roomfp);
+
+       /* Enter configurations for each room... */
+       while (mn != NULL) {
+               printf("Room <%s>\n", mn->roomname);
+
+               snprintf(buf, sizeof buf, "GOTO %s", mn->roomname);
+               serv_puts(buf);
+               serv_gets(buf);
+               printf("%s\n", &buf[4]);
+               if (buf[0] != '2') goto roomerror;
+
+               serv_puts("SNET");
+               serv_gets(buf);
+               if (buf[0] != '4') goto roomerror;
+
+               snprintf(buf, sizeof buf, "lastsent|%ld", highest);
+               serv_puts(buf);
+
+               roomfp = fopen(roomfilename, "r");
+               if (roomfp != NULL) {
+                       while (fgets(buf, sizeof buf, roomfp) != NULL) {
+                               buf[strlen(buf)-1] = 0;
+                               extract(node, buf, 0);
+                               extract(room, buf, 1);
+                               if (!strcasecmp(room, mn->roomname)) {
+                                       snprintf(buf, sizeof buf,
+                                               "ignet_push_share|%s", node);
+                                       serv_puts(buf);
+                               }
+                       }
+                       fclose(roomfp);
+               }
+
+               serv_puts("000");
+
+roomerror:     /* free this record */
+               mnptr = mn->next;
+               free(mn);
+               mn = mnptr;
+       }
+
+       unlink(roomfilename);
+       unlink(nodefilename);
+
+       printf("\n\n"
+               "If this conversion was successful, you do not need your\n"
+               "old network configuration files.  Delete them now? "
+       );
+
+       gets(buf);
+       if (tolower(buf[0]) != 'y') exit(0);
+
+       get_config();
+       system("rm -fr ./network/systems");
+       system("rm -f ./network/mail.sysinfo");
+       system("rm -f ./network/internetmail.config");
 
-       printf("FIXME not finished yet\n");
+       exit(0);
 }