]> code.citadel.org Git - citadel.git/blob - citadel/citadelapi.c
ffdc6ca6d39a637c30f0c0f72acb9488d04f223d
[citadel.git] / citadel / citadelapi.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <time.h>
5 #include <ctype.h>
6 #include <string.h>
7 #include <errno.h>
8 #include "citadel.h"
9
10 struct CtdlServerHandle CtdlAppHandle;
11 struct CtdlServInfo CtdlAppServInfo;
12 void CtdlMain();
13
14 void logoff(exitcode) {
15         exit(exitcode);
16         }
17
18 /*
19  * Programs linked against the Citadel server extension library need to
20  * be called with the following arguments:
21  * 0 - program name (as always)
22  * 1 - server address (usually 127.0.0.1)
23  * 2 - server port number
24  * 3 - internal program secret
25  * 4 - user name
26  * 5 - user password
27  * 6 - initial room
28  * 7 - associated client session
29  * 
30  */
31
32 main(argc, argv)
33 int argc;
34 char *argv[]; {
35         int a;
36         char buf[256];
37
38         /* We're really not interested in stdio */
39         close(0);
40         close(1);
41         close(2);
42
43         /* Bail out if someone tries to run this thing manually */
44         if (argc < 3) exit(1);
45
46         /* Zeroing out the server handle neatly sets the values of
47          * CtdlAppHandle to sane default values
48          */
49         bzero(&CtdlAppHandle, sizeof(struct CtdlServerHandle));
50
51         /* Now parse the command-line arguments fed to us by the server */
52         for (a=0; a<argc; ++a) switch(a) {
53                 case 1: strcpy(CtdlAppHandle.ServerAddress, argv[a]);
54                         break;
55                 case 2: CtdlAppHandle.ServerPort = atoi(argv[a]);
56                         break;
57                 case 3: strcpy(CtdlAppHandle.ipgmSecret, argv[a]);
58                         break;
59                 case 4: strcpy(CtdlAppHandle.UserName, argv[a]);
60                         break;
61                 case 5: strcpy(CtdlAppHandle.Password, argv[a]);
62                         break;
63                 case 6: strcpy(CtdlAppHandle.InitialRoom, argv[a]);
64                         break;
65                 case 7: CtdlAppHandle.AssocClientSession = atoi(argv[a]);
66                         break;
67                 }
68
69         /* Connect to the server */
70         argc = 3;
71         attach_to_server(argc, argv);
72         serv_gets(buf);
73         if (buf[0] != '2') exit(1);
74
75         /* Set up the server environment to our liking */
76
77         CtdlInternalGetServInfo(&CtdlAppServInfo, 0);
78
79         sprintf(buf, "IDEN 0|5|006|CitadelAPI Client");
80         serv_puts(buf);
81         serv_gets(buf);
82
83         if (strlen(CtdlAppHandle.ipgmSecret) > 0) {
84                 sprintf(buf, "IPGM %s", CtdlAppHandle.ipgmSecret);
85                 serv_puts(buf);
86                 serv_gets(buf);
87                 }
88
89         if (strlen(CtdlAppHandle.UserName) > 0) {
90                 sprintf(buf, "USER %s", CtdlAppHandle.UserName);
91                 serv_puts(buf);
92                 serv_gets(buf);
93                 sprintf(buf, "PASS %s", CtdlAppHandle.Password);
94                 serv_puts(buf);
95                 serv_gets(buf);
96                 }
97
98         sprintf(buf, "GOTO %s", CtdlAppHandle.InitialRoom);
99         serv_puts(buf);
100         serv_gets(buf);
101         if (buf[0] != '2') {
102                 serv_puts("GOTO _BASEROOM_");
103                 serv_gets(buf);
104                 }
105
106         /* Now do the loop. */
107         CtdlMain();
108
109         /* Clean up gracefully and exit. */
110         serv_puts("QUIT");
111         exit(0);
112         }