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