11 #include "citadelapi.h"
14 struct CtdlInternalList {
15 struct CtdlInternalList *next;
20 struct CtdlServerHandle CtdlAppHandle;
21 struct CtdlServInfo CtdlAppServInfo;
24 void logoff(int exitcode) {
31 * CtdlInternalNumParms() - discover number of parameters...
33 int CtdlInternalNumParms(char *source)
38 for (a=0; a<strlen(source); ++a)
39 if (source[a]=='|') ++count;
44 * CtdlInternalExtract() - extract a parameter from a series of "|" separated
46 void CtdlInternalExtract(char *dest, char *source, int parmnum)
52 if (strlen(source)==0) {
57 n = CtdlInternalNumParms(source);
64 if ( (parmnum == 0) && (n == 1) ) {
66 for (n=0; n<strlen(dest); ++n)
67 if (dest[n]=='|') dest[n] = 0;
71 while (count++ < parmnum) do {
73 } while( (strlen(buf)>0) && (buf[0]!='|') );
74 if (buf[0]=='|') strcpy(buf,&buf[1]);
75 for (count = 0; count<strlen(buf); ++count)
76 if (buf[count] == '|') buf[count] = 0;
81 * CtdlInternalExtractInt() - CtdlInternalExtract an int parm w/o supplying a buffer
83 int CtdlInternalExtractInt(char *source, int parmnum)
87 CtdlInternalExtract(buf,source,parmnum);
92 * CtdlInternalExtractLong() - CtdlInternalExtract an long parm w/o supplying a buffer
94 long CtdlInternalExtractLong(char *source, long int parmnum)
98 CtdlInternalExtract(buf,source,parmnum);
114 * Programs linked against the Citadel server extension library need to
115 * be called with the following arguments:
116 * 0 - program name (as always)
117 * 1 - server address (usually 127.0.0.1)
118 * 2 - server port number
119 * 3 - internal program secret
123 * 7 - associated client session
128 main(int argc, char *argv[])
133 /* We're really not interested in stdio */
138 /* Bail out if someone tries to run this thing manually */
139 if (argc < 3) exit(1);
141 /* Zeroing out the server handle neatly sets the values of
142 * CtdlAppHandle to sane default values
144 bzero(&CtdlAppHandle, sizeof(struct CtdlServerHandle));
146 /* Now parse the command-line arguments fed to us by the server */
147 for (a=0; a<argc; ++a) switch(a) {
148 case 1: strcpy(CtdlAppHandle.ServerAddress, argv[a]);
150 case 2: CtdlAppHandle.ServerPort = atoi(argv[a]);
152 case 3: strcpy(CtdlAppHandle.ipgmSecret, argv[a]);
154 case 4: strcpy(CtdlAppHandle.UserName, argv[a]);
156 case 5: strcpy(CtdlAppHandle.Password, argv[a]);
158 case 6: strcpy(CtdlAppHandle.InitialRoom, argv[a]);
160 case 7: CtdlAppHandle.AssocClientSession = atoi(argv[a]);
164 /* Connect to the server */
166 attach_to_server(argc, argv);
168 if (buf[0] != '2') exit(1);
170 /* Set up the server environment to our liking */
172 CtdlInternalGetServInfo(&CtdlAppServInfo);
174 sprintf(buf, "IDEN 0|5|006|CitadelAPI Client");
178 if (strlen(CtdlAppHandle.ipgmSecret) > 0) {
179 sprintf(buf, "IPGM %s", CtdlAppHandle.ipgmSecret);
184 if (strlen(CtdlAppHandle.UserName) > 0) {
185 sprintf(buf, "USER %s", CtdlAppHandle.UserName);
188 sprintf(buf, "PASS %s", CtdlAppHandle.Password);
193 sprintf(buf, "GOTO %s", CtdlAppHandle.InitialRoom);
197 serv_puts("GOTO _BASEROOM_");
201 /* Now do the loop. */
204 /* Clean up gracefully and exit. */
210 int CtdlGetLastError(void) {
215 int CtdlSendExpressMessage(char *ToUser, char *MsgText) {
218 if (strlen(ToUser) + strlen(MsgText) > 248) {
219 CtdlErrno = ERROR + TOO_BIG;
223 sprintf(buf, "SEXP %s|%s", ToUser, MsgText);
227 CtdlErrno = atoi(buf);
228 if (CtdlErrno == OK) CtdlErrno = 0;
234 int CtdlInternalGetUserParam(char *ParamBuf, int ParamNum, char *WhichUser) {
237 sprintf(buf, "AGUP %s", WhichUser);
240 if (buf[0] != '2') return(atoi(buf));
241 CtdlInternalExtract(ParamBuf, &buf[4], ParamNum);
246 int CtdlInternalSetUserParam(char *ParamBuf, int ParamNum, char *WhichUser) {
251 sprintf(buf, "AGUP %s", WhichUser);
254 if (buf[0] != '2') return(atoi(buf));
255 for (a=0; a<8; ++a) {
256 CtdlInternalExtract(¶ms[a][0], &buf[4], a);
258 strcpy(¶ms[ParamNum][0], ParamBuf);
259 strcpy(buf, "ASUP ");
260 for (a=0; a<8; ++a) {
261 strcat(buf, ¶ms[a][0]);
266 if (buf[0] != '2') return(atoi(buf));
273 2 - Flags (see citadel.h)
278 7 - Timestamp of last call
281 int CtdlGetUserPassword(char *buf, char *WhichUser) {
282 CtdlErrno = CtdlInternalGetUserParam(buf, 1, WhichUser);
286 int CtdlSetUserPassword(char *buf, char *WhichUser) {
287 CtdlErrno = CtdlInternalSetUserParam(buf, 1, WhichUser);
291 unsigned int CtdlGetUserFlags(char *WhichUser) {
293 CtdlErrno = CtdlInternalGetUserParam(buf, 2, WhichUser);
294 return((CtdlErrno == 0) ? atoi(buf) : (-1));
297 int CtdlSetUserFlags(unsigned int NewFlags, char *WhichUser) {
299 sprintf(buf, "%u", NewFlags);
300 CtdlErrno = CtdlInternalGetUserParam(buf, 2, WhichUser);
304 int CtdlGetUserTimesCalled(char *WhichUser) {
306 CtdlErrno = CtdlInternalGetUserParam(buf, 3, WhichUser);
307 return((CtdlErrno == 0) ? atoi(buf) : (-1));
310 int CtdlSetUserTimesCalled(int NewValue, char *WhichUser) {
312 sprintf(buf, "%d", NewValue);
313 CtdlErrno = CtdlInternalGetUserParam(buf, 3, WhichUser);
317 int CtdlGetUserMessagesPosted(char *WhichUser) {
319 CtdlErrno = CtdlInternalGetUserParam(buf, 4, WhichUser);
320 return((CtdlErrno == 0) ? atoi(buf) : (-1));
323 int CtdlSetUserMessagesPosted(int NewValue, char *WhichUser) {
325 sprintf(buf, "%d", NewValue);
326 CtdlErrno = CtdlInternalGetUserParam(buf, 4, WhichUser);
330 int CtdlGetUserAccessLevel(char *WhichUser) {
332 CtdlErrno = CtdlInternalGetUserParam(buf, 5, WhichUser);
333 return((CtdlErrno == 0) ? atoi(buf) : (-1));
336 int CtdlSetUserAccessLevel(int NewValue, char *WhichUser) {
339 if ( (NewValue < 1) || (NewValue > 6) ) {
340 return(ERROR + ILLEGAL_VALUE);
343 sprintf(buf, "%d", NewValue);
344 CtdlErrno = CtdlInternalGetUserParam(buf, 5, WhichUser);
348 long CtdlGetUserNumber(char *WhichUser) {
350 CtdlErrno = CtdlInternalGetUserParam(buf, 6, WhichUser);
351 return((CtdlErrno == 0) ? atol(buf) : (-1L));
354 int CtdlSetUserNumber(long NewValue, char *WhichUser) {
356 sprintf(buf, "%ld", NewValue);
357 CtdlErrno = CtdlInternalGetUserParam(buf, 6, WhichUser);
361 time_t CtdlGetUserLastCall(char *WhichUser) {
363 CtdlErrno = CtdlInternalGetUserParam(buf, 7, WhichUser);
364 return((CtdlErrno == 0) ? atol(buf) : (time_t)(-1L));
367 int CtdlSetUserLastCall(time_t NewValue, char *WhichUser) {
369 sprintf(buf, "%ld", NewValue);
370 CtdlErrno = CtdlInternalGetUserParam(buf, 7, WhichUser);
374 int CtdlForEachUser(int (*CallBack)(char *EachUser)) {
375 struct CtdlInternalList *TheList = NULL;
376 struct CtdlInternalList *ptr;
381 if (buf[0] != '1') return(-1);
383 while (serv_gets(buf), strcmp(buf, "000")) {
384 ptr = (struct CtdlInternalList *)
385 malloc(sizeof (struct CtdlInternalList));
387 CtdlInternalExtract(ptr->data, buf, 0);
393 while (TheList != NULL) {
394 (*CallBack)(TheList->data);