home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / icqsocket / testlib.c < prev   
C/C++ Source or Header  |  1999-10-25  |  8KB  |  277 lines

  1. /*
  2.  
  3. Test app for icqsocket.library 0.03
  4.  
  5. Don't use anything that isn't used in here, although the things
  6. you find in icqsocket.h work, they may not work in the same way
  7. in the future.
  8.  
  9. If there's anything I've forgotten or that seems strange, please
  10. tell me about it now. It probably will be much harder to fix it
  11. later.
  12.  
  13. The goal of this library is to squeeze everything but the GUI
  14. into it, so if it lacks some important feature, I'll put it in
  15. there. (just let me know)
  16.  
  17. Before you get started I suggest you get familiar with the
  18. original ICQ client from Mirabilis (www.mirabilis.com).
  19.  
  20. Also check out the (unofficial) protocol specs:
  21.  
  22. http://www.algonet.se/~henisak/icq/icqv5.html
  23.  
  24.  
  25. BTW, read the comments carefully, there might be some important
  26. information there.
  27.  
  28. Good luck.
  29.  
  30. digitaliz <hki@hem1.passagen.se>
  31.  
  32. UIN: 11214923
  33.  
  34. */
  35.  
  36. #include "icqsocket.h"
  37. #include "icqsocket_pragmas.h"
  38.  
  39. #include <dos.h>
  40.  
  41. #include <clib/exec_protos.h>
  42.  
  43. #include <stdio.h>
  44.  
  45. struct ICQSocketBase *ICQSocketBase;
  46.  
  47. ICQHandle *ih=NULL;
  48.  
  49. struct Hook icqerror;
  50. struct Hook icqloggedin;
  51. struct Hook icqstatus;
  52. struct Hook icqmsg;
  53. struct Hook icqtimer;
  54. struct Hook messhook;
  55.  
  56. void __asm __saveds icqstatusfunc(register __a2 ICQStatusUpdate *s)
  57. {
  58.     /* This function is to be called when a user changes his/her */
  59.     /* online status */
  60.  
  61.     printf("%ld changed status to %08lx\n", s->UIN, s->Status);
  62.     if(s->Type==SUT_ONLINE) {
  63.         printf(" IP:     %08lx\n", s->IP);
  64.         printf(" RealIP: %08lx\n", s->RealIP);
  65.         printf(" Port:   %ld\n", s->Port);
  66.         printf(" TCPVer: %08lx\n", s->TCP);
  67.         printf(" DC:     %02x\n", s->Direct);
  68.         /* Note, this info is also stored in the user database */
  69.         /* for later retrieval. */
  70.     }
  71. }
  72.  
  73. void __asm __saveds icqmsgfunc(register __a2 ICQMessage *m)
  74. {
  75.     /* This function is called when a new message has been */
  76.     /* added to the message queue, or while connecting to */
  77.     /* the server, if old, unread, messages are left in the */
  78.     /* queue. If so, the last added message is passed to this */
  79.     /* function. */
  80.  
  81.     /* To fetch queued messages, mark them as 'read', and */
  82.     /* move them to the message log, use the MsgQueue functions. */
  83.     /* see below, in ShowMsgQueue */
  84.  
  85.     printf("\n\n*** Recieved a message ***\n");
  86.     printf("  UIN: %ld\n", m->UIN);
  87.     printf(" Type: %ld\n", m->Type);
  88.     if(m->Instant) {
  89.         printf(" Time: Instantly delivered\n");
  90.     } else {
  91.         printf(" Time: %ld/%ld %ld kl. %ld:%ld\n", m->Time.Day, m->Time.Month, m->Time.Year, m->Time.Hour, m->Time.Minute);
  92.     }
  93.     printf(" Text: %s\n", m->Msg);
  94. }
  95.  
  96. void __asm __saveds icqmessfunc(register __a2 ULONG userdata)
  97. {
  98.     printf("A message to %ld has been delivered.");
  99. }
  100.  
  101. void __asm __saveds icqloggedinfunc(register __a2 ICQLogin *l)
  102. {
  103.     /* This function is called when the login procedure has completed */
  104.  
  105.     printf("Logged in!\n");
  106.  
  107.     /* Send a test message */
  108.     //is_Send(ih, &messhook, ISC_TextMessage, 1214923, "Test!");
  109.     /* messhook gets called when the message has been acknowledged */
  110.     /* by the server. You could, for example, keep the send message */
  111.     /* window open until it is called. (the way the Mirabilis client */
  112.     /* does it) By passing the window pointer in the h_Data field */
  113.     /* of the hook struct, you can manage several send-message- */
  114.     /* windows at once. MUI makes this simple via the addmember */
  115.     /* method. */
  116.  
  117.     /* Change our online status */
  118.     is_Send(ih, NULL, ISC_NewStatus, 0);
  119.     /* You can pass a hook here too, which is called when your new */
  120.     /* status has been acknowledged by the server. */
  121.  
  122.     /*is_Send(ih, NULL, ISC_RandSearch, GRP_GENERAL);*/
  123. }
  124.  
  125. void __asm __saveds icqerrorfunc(register __a2 ICQError *e)
  126. {
  127.     /* This is called when an error occurs */
  128.  
  129.     if(e->SokErr) {
  130.         printf("Socket error: %ld\n", e->SokErr);
  131.     }
  132.     if(e->ICQErr) {
  133.         printf("ICQSocket error: %ld\n", e->ICQErr);
  134.     }
  135.     if(e->Hdr) {
  136.         printf(" Ver: %04x\n", e->Hdr->Version);
  137.         printf(" Cmd: %04x\n", e->Hdr->Command);
  138.         printf(" UIN: %ld\n", e->Hdr->UIN);
  139.         printf(" Seq: %08x\n", e->Hdr->Seq);
  140.         printf(" SID: %08x\n", e->Hdr->SID);
  141.     }
  142. }
  143.  
  144. void __asm __saveds icqtimerfunc(void)
  145. {
  146.     /* This is called every half second */
  147.  
  148.     /*printf("timer\n");*/
  149. }
  150.  
  151. void ListLog(ICQHandle *ih)
  152. {
  153.     ICQMessage *im;
  154.  
  155.     printf("--- MESSAGE LOG ---\n");
  156.     if(is_OpenMsgLog(ih)) {
  157.         do {
  158.             im=is_GetLoggedMsg(ih, 0);
  159.             if(im) {
  160.                 printf("---\nFrom: %ld\nType: %ld\nText: %s\n", im->UIN, im->Type, im->Msg);
  161.             }
  162.         } while(im);
  163.         is_CloseMsgLog(ih);
  164.     }
  165.     printf("--- END OF MESSAGE LOG ---\n");
  166. }
  167.  
  168. void ShowMsgQueue(ICQHandle *ih)
  169. {
  170.     ICQMessage *im;
  171.  
  172.     /* Note! When you get a message from the queue, it's no */
  173.     /* longer there. (it has been removed from the list and is */
  174.     /* only stored in the log) */
  175.  
  176.     printf("--- QUEUED MESSAGES ---\n");
  177.     if(is_OpenMsgQueue(ih)) {
  178.         do {
  179.             im=is_GetQueuedMsg(ih, 0);
  180.                 /* Setting the 0 to a positive integer will  (an UIN) */
  181.                 /* filter out all messages coming from other users. */
  182.                 /* (and only get messages from the right user) */
  183.             if(im) {
  184.                 printf("---\nFrom: %ld\nType: %ld\nText: %s\n", im->UIN, im->Type, im->Msg);
  185.             }
  186.         } while(im);
  187.         is_CloseMsgQueue(ih);
  188.     }
  189.     printf("--- END OF QUEUED MESSAGES ---\n");
  190. }
  191.  
  192. void ListContacts(ICQHandle *ih)
  193. {
  194.     ICQUser *iu;
  195.  
  196.     /* List all users in the DB_CONTACTS database. */
  197.     /* Other databases are: */
  198.     /*    DB_UNKNOWN  - users not on the contact list that have */
  199.     /*              sent messages to you */
  200.     /*    DB_RANDOM   - "random search" users */
  201.     /*    DB_CONTACTS - your contact list */
  202.  
  203.     /* NOTE 1: Theese things will be automatically saved and loaded */
  204.     /*         from disk in the near future. */
  205.     /* NOTE 2: Also planned is a system to add user specific records */
  206.     /*         to the database entries. (the users) */
  207.  
  208.     printf("--- CONTACT LIST ---\n");
  209.     if(is_OpenDatabase(ih, DB_CONTACTS)) {
  210.         do {
  211.             iu=is_GetEntry(ih, DB_CONTACTS);
  212.             if(iu) {
  213.                 printf("UIN: %ld, Nick: %s\n", iu->UIN, iu->Nick);
  214.             }
  215.         } while(iu);
  216.         is_CloseDatabase(ih, DB_CONTACTS);
  217.     }
  218.     printf("--- END OF CONTACT LIST ---\n");
  219. }
  220.  
  221. void main(int ac, char *av[])
  222. {
  223.  
  224.     /* Set up the hook function pointers */
  225.     icqstatus.h_Entry=(HOOKFUNC)icqstatusfunc;
  226.     icqloggedin.h_Entry=(HOOKFUNC)icqloggedinfunc;
  227.     icqerror.h_Entry=(HOOKFUNC)icqerrorfunc;
  228.     icqmsg.h_Entry=(HOOKFUNC)icqmsgfunc;
  229.     icqtimer.h_Entry=(HOOKFUNC)icqtimerfunc;
  230.     messhook.h_Entry=(HOOKFUNC)icqmessfunc;
  231.  
  232.     ICQSocketBase=(struct ICQSocketBase *)OpenLibrary("icqsocket.library", 1);
  233.     if(ICQSocketBase) {
  234.         is_Debug(Output(), 0);    /* Set up filehandle for debugging */
  235.                     /* Pass NULL to disable debugging */
  236.  
  237.         if((ih=is_InitA(13951811, NULL))) {
  238.             /* Initialise icqsocket for my test-UIN. */
  239.             /* (this loads config and log from S:) */
  240.             /* The NULL is for optional tags. (not implemented) */
  241.  
  242.             is_InstallHook(ih, IID_ERROR, &icqerror);
  243.             is_InstallHook(ih, IID_LOGIN, &icqloggedin);
  244.             is_InstallHook(ih, IID_STATUS, &icqstatus);
  245.             is_InstallHook(ih, IID_MESSAGE, &icqmsg);
  246.             is_InstallHook(ih, IID_TIMER_EVENT, &icqtimer);
  247.  
  248.             ListLog(ih);
  249.             ShowMsgQueue(ih);
  250.  
  251.             is_AddUINQ(ih, 11214923);    /* Adds an UIN quickly to the contact list */
  252.                             /* (ie. without looking up the users nickname etc.) */
  253.                             /* currently this the only supported method */
  254.  
  255.             ListContacts(ih);
  256.             
  257.  
  258.             /* NOTE: The library will maintain list of servers in */
  259.             /* future versions, but ISS_Host will override this. */
  260.             /* For now, however, it is required, or the lib will */
  261.             /* try to connect to localhost. */
  262.  
  263.             if(is_Connect(ih, ISS_Host, "icq.mirabilis.com",  ISS_Password, "pass", TAG_DONE)==OK) {
  264.                 printf("*** Press CTRL-F to quit ***\n");
  265.                 while(!(is_NetWait(ih, SIGBREAKF_CTRL_F)&SIGBREAKF_CTRL_F));
  266.                 is_Disconnect(ih);
  267.             }
  268.  
  269.             is_Free(ih);
  270.         }
  271.  
  272.         is_Debug(NULL, 0);    /* Disable debugging output */
  273.  
  274.         CloseLibrary((struct Library *)ICQSocketBase);
  275.     }
  276. }
  277.