11 #include "citadelapi.h"
15 struct CtdlInternalList {
16 struct CtdlInternalList *next;
21 struct CtdlServerHandle CtdlAppHandle;
22 struct CtdlServInfo CtdlAppServInfo;
23 struct CtdlRoomInfo CtdlCurrentRoom;
26 void logoff(int exitcode) {
33 * CtdlInternalNumParms() - discover number of parameters...
35 int CtdlInternalNumParms(char *source)
40 for (a=0; a<strlen(source); ++a)
41 if (source[a]=='|') ++count;
46 * CtdlInternalExtract() - extract a parameter from a series of "|" separated
48 void CtdlInternalExtract(char *dest, char *source, int parmnum)
54 if (strlen(source)==0) {
59 n = CtdlInternalNumParms(source);
66 if ( (parmnum == 0) && (n == 1) ) {
68 for (n=0; n<strlen(dest); ++n)
69 if (dest[n]=='|') dest[n] = 0;
73 while (count++ < parmnum) do {
75 } while( (strlen(buf)>0) && (buf[0]!='|') );
76 if (buf[0]=='|') strcpy(buf,&buf[1]);
77 for (count = 0; count<strlen(buf); ++count)
78 if (buf[count] == '|') buf[count] = 0;
83 * CtdlInternalExtractInt() - Extract an int parm w/o supplying a buffer
85 int CtdlInternalExtractInt(char *source, int parmnum)
89 CtdlInternalExtract(buf,source,parmnum);
94 * CtdlInternalExtractLong() - Extract an long parm w/o supplying a buffer
96 long CtdlInternalExtractLong(char *source, long int parmnum)
100 CtdlInternalExtract(buf,source,parmnum);
116 * Programs linked against the Citadel server extension library need to
117 * be called with the following arguments:
118 * 0 - program name (as always)
119 * 1 - server address (usually 127.0.0.1)
120 * 2 - server port number
121 * 3 - internal program secret
125 * 7 - associated client session
130 main(int argc, char *argv[])
135 /* We're really not interested in stdio */
140 /* Bail out if someone tries to run this thing manually */
141 if (argc < 3) exit(1);
143 /* Zeroing out the server handle neatly sets the values of
144 * CtdlAppHandle to sane default values. This also holds true
145 * for the CtdlCurrentRoom.
147 bzero(&CtdlAppHandle, sizeof(struct CtdlServerHandle));
148 bzero(&CtdlCurrentRoom, sizeof(struct CtdlRoomInfo));
150 /* Now parse the command-line arguments fed to us by the server */
151 for (a=0; a<argc; ++a) switch(a) {
152 case 1: strcpy(CtdlAppHandle.ServerAddress, argv[a]);
154 case 2: CtdlAppHandle.ServerPort = atoi(argv[a]);
156 case 3: strcpy(CtdlAppHandle.ipgmSecret, argv[a]);
158 case 4: strcpy(CtdlAppHandle.UserName, argv[a]);
160 case 5: strcpy(CtdlAppHandle.Password, argv[a]);
162 case 6: strcpy(CtdlAppHandle.InitialRoom, argv[a]);
164 case 7: CtdlAppHandle.AssocClientSession = atoi(argv[a]);
168 /* Connect to the server */
170 attach_to_server(argc, argv);
172 if (buf[0] != '2') exit(1);
174 /* Set up the server environment to our liking */
176 CtdlInternalGetServInfo(&CtdlAppServInfo);
178 sprintf(buf, "IDEN 0|5|006|CitadelAPI Client");
182 if (strlen(CtdlAppHandle.ipgmSecret) > 0) {
183 sprintf(buf, "IPGM %s", CtdlAppHandle.ipgmSecret);
188 if (strlen(CtdlAppHandle.UserName) > 0) {
189 sprintf(buf, "USER %s", CtdlAppHandle.UserName);
192 sprintf(buf, "PASS %s", CtdlAppHandle.Password);
197 if (CtdlGotoRoom(CtdlAppHandle.InitialRoom) != 0) {
198 CtdlGotoRoom("_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 < 0) || (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);
406 * Goto a different room
408 int CtdlGotoRoom(char *RoomName) {
411 sprintf(buf, "GOTO %s", RoomName);
415 CtdlErrno = atoi(buf);
418 extract(CtdlCurrentRoom.RoomName, &buf[4], 0);