11 #include "citadelapi.h"
14 struct CtdlInternalList {
15 struct CtdlInternalList *next;
20 struct CtdlServerHandle CtdlAppHandle;
21 struct CtdlServInfo CtdlAppServInfo;
22 struct CtdlRoomInfo CtdlCurrentRoom;
25 void logoff(int exitcode) {
32 * CtdlInternalNumParms() - discover number of parameters...
34 int CtdlInternalNumParms(char *source)
39 for (a=0; a<strlen(source); ++a)
40 if (source[a]=='|') ++count;
45 * CtdlInternalExtract() - extract a parameter from a series of "|" separated
47 void CtdlInternalExtract(char *dest, char *source, int parmnum)
53 if (strlen(source)==0) {
58 n = CtdlInternalNumParms(source);
65 if ( (parmnum == 0) && (n == 1) ) {
67 for (n=0; n<strlen(dest); ++n)
68 if (dest[n]=='|') dest[n] = 0;
72 while (count++ < parmnum) do {
74 } while( (strlen(buf)>0) && (buf[0]!='|') );
75 if (buf[0]=='|') strcpy(buf,&buf[1]);
76 for (count = 0; count<strlen(buf); ++count)
77 if (buf[count] == '|') buf[count] = 0;
82 * CtdlInternalExtractInt() - Extract an int parm w/o supplying a buffer
84 int CtdlInternalExtractInt(char *source, int parmnum)
88 CtdlInternalExtract(buf,source,parmnum);
93 * CtdlInternalExtractLong() - Extract an long parm w/o supplying a buffer
95 long CtdlInternalExtractLong(char *source, long int parmnum)
99 CtdlInternalExtract(buf,source,parmnum);
115 * Programs linked against the Citadel server extension library need to
116 * be called with the following arguments:
117 * 0 - program name (as always)
118 * 1 - server address (usually 127.0.0.1)
119 * 2 - server port number
120 * 3 - internal program secret
124 * 7 - associated client session
129 main(int argc, char *argv[])
134 /* We're really not interested in stdio */
139 /* Bail out if someone tries to run this thing manually */
140 if (argc < 3) exit(1);
142 /* Zeroing out the server handle neatly sets the values of
143 * CtdlAppHandle to sane default values. This also holds true
144 * for the CtdlCurrentRoom.
146 bzero(&CtdlAppHandle, sizeof(struct CtdlServerHandle));
147 bzero(&CtdlCurrentRoom, sizeof(struct CtdlRoomInfo));
149 /* Now parse the command-line arguments fed to us by the server */
150 for (a=0; a<argc; ++a) switch(a) {
151 case 1: strcpy(CtdlAppHandle.ServerAddress, argv[a]);
153 case 2: CtdlAppHandle.ServerPort = atoi(argv[a]);
155 case 3: strcpy(CtdlAppHandle.ipgmSecret, argv[a]);
157 case 4: strcpy(CtdlAppHandle.UserName, argv[a]);
159 case 5: strcpy(CtdlAppHandle.Password, argv[a]);
161 case 6: strcpy(CtdlAppHandle.InitialRoom, argv[a]);
163 case 7: CtdlAppHandle.AssocClientSession = atoi(argv[a]);
167 /* Connect to the server */
169 attach_to_server(argc, argv);
171 if (buf[0] != '2') exit(1);
173 /* Set up the server environment to our liking */
175 CtdlInternalGetServInfo(&CtdlAppServInfo);
177 sprintf(buf, "IDEN 0|5|006|CitadelAPI Client");
181 if (strlen(CtdlAppHandle.ipgmSecret) > 0) {
182 sprintf(buf, "IPGM %s", CtdlAppHandle.ipgmSecret);
187 if (strlen(CtdlAppHandle.UserName) > 0) {
188 sprintf(buf, "USER %s", CtdlAppHandle.UserName);
191 sprintf(buf, "PASS %s", CtdlAppHandle.Password);
196 if (CtdlGotoRoom(CtdlAppHandle.InitialRoom) != 0) {
197 CtdlGotoRoom("_BASEROOM_");
200 /* Now do the loop. */
203 /* Clean up gracefully and exit. */
209 int CtdlGetLastError(void) {
214 int CtdlSendExpressMessage(char *ToUser, char *MsgText) {
217 if (strlen(ToUser) + strlen(MsgText) > 248) {
218 CtdlErrno = ERROR + TOO_BIG;
222 sprintf(buf, "SEXP %s|%s", ToUser, MsgText);
226 CtdlErrno = atoi(buf);
227 if (CtdlErrno == OK) CtdlErrno = 0;
233 int CtdlInternalGetUserParam(char *ParamBuf, int ParamNum, char *WhichUser) {
236 sprintf(buf, "AGUP %s", WhichUser);
239 if (buf[0] != '2') return(atoi(buf));
240 CtdlInternalExtract(ParamBuf, &buf[4], ParamNum);
245 int CtdlInternalSetUserParam(char *ParamBuf, int ParamNum, char *WhichUser) {
250 sprintf(buf, "AGUP %s", WhichUser);
253 if (buf[0] != '2') return(atoi(buf));
254 for (a=0; a<8; ++a) {
255 CtdlInternalExtract(¶ms[a][0], &buf[4], a);
257 strcpy(¶ms[ParamNum][0], ParamBuf);
258 strcpy(buf, "ASUP ");
259 for (a=0; a<8; ++a) {
260 strcat(buf, ¶ms[a][0]);
265 if (buf[0] != '2') return(atoi(buf));
272 2 - Flags (see citadel.h)
277 7 - Timestamp of last call
280 int CtdlGetUserPassword(char *buf, char *WhichUser) {
281 CtdlErrno = CtdlInternalGetUserParam(buf, 1, WhichUser);
285 int CtdlSetUserPassword(char *buf, char *WhichUser) {
286 CtdlErrno = CtdlInternalSetUserParam(buf, 1, WhichUser);
290 unsigned int CtdlGetUserFlags(char *WhichUser) {
292 CtdlErrno = CtdlInternalGetUserParam(buf, 2, WhichUser);
293 return((CtdlErrno == 0) ? atoi(buf) : (-1));
296 int CtdlSetUserFlags(unsigned int NewFlags, char *WhichUser) {
298 sprintf(buf, "%u", NewFlags);
299 CtdlErrno = CtdlInternalGetUserParam(buf, 2, WhichUser);
303 int CtdlGetUserTimesCalled(char *WhichUser) {
305 CtdlErrno = CtdlInternalGetUserParam(buf, 3, WhichUser);
306 return((CtdlErrno == 0) ? atoi(buf) : (-1));
309 int CtdlSetUserTimesCalled(int NewValue, char *WhichUser) {
311 sprintf(buf, "%d", NewValue);
312 CtdlErrno = CtdlInternalGetUserParam(buf, 3, WhichUser);
316 int CtdlGetUserMessagesPosted(char *WhichUser) {
318 CtdlErrno = CtdlInternalGetUserParam(buf, 4, WhichUser);
319 return((CtdlErrno == 0) ? atoi(buf) : (-1));
322 int CtdlSetUserMessagesPosted(int NewValue, char *WhichUser) {
324 sprintf(buf, "%d", NewValue);
325 CtdlErrno = CtdlInternalGetUserParam(buf, 4, WhichUser);
329 int CtdlGetUserAccessLevel(char *WhichUser) {
331 CtdlErrno = CtdlInternalGetUserParam(buf, 5, WhichUser);
332 return((CtdlErrno == 0) ? atoi(buf) : (-1));
335 int CtdlSetUserAccessLevel(int NewValue, char *WhichUser) {
338 if ( (NewValue < 0) || (NewValue > 6) ) {
339 return(ERROR + ILLEGAL_VALUE);
342 sprintf(buf, "%d", NewValue);
343 CtdlErrno = CtdlInternalGetUserParam(buf, 5, WhichUser);
347 long CtdlGetUserNumber(char *WhichUser) {
349 CtdlErrno = CtdlInternalGetUserParam(buf, 6, WhichUser);
350 return((CtdlErrno == 0) ? atol(buf) : (-1L));
353 int CtdlSetUserNumber(long NewValue, char *WhichUser) {
355 sprintf(buf, "%ld", NewValue);
356 CtdlErrno = CtdlInternalGetUserParam(buf, 6, WhichUser);
360 time_t CtdlGetUserLastCall(char *WhichUser) {
362 CtdlErrno = CtdlInternalGetUserParam(buf, 7, WhichUser);
363 return((CtdlErrno == 0) ? atol(buf) : (time_t)(-1L));
366 int CtdlSetUserLastCall(time_t NewValue, char *WhichUser) {
368 sprintf(buf, "%ld", NewValue);
369 CtdlErrno = CtdlInternalGetUserParam(buf, 7, WhichUser);
373 int CtdlForEachUser(int (*CallBack)(char *EachUser)) {
374 struct CtdlInternalList *TheList = NULL;
375 struct CtdlInternalList *ptr;
380 if (buf[0] != '1') return(-1);
382 while (serv_gets(buf), strcmp(buf, "000")) {
383 ptr = (struct CtdlInternalList *)
384 malloc(sizeof (struct CtdlInternalList));
386 CtdlInternalExtract(ptr->data, buf, 0);
392 while (TheList != NULL) {
393 (*CallBack)(TheList->data);
405 * Goto a different room
407 int CtdlGotoRoom(char *RoomName) {
410 sprintf(buf, "GOTO %s", RoomName);
414 CtdlErrno = atoi(buf);
417 extract(&CtdlCurrentRoom.RoomName, &buf[4], 0);