Jabber WindowGram Client (JWGC)

Introduction Screenshots Installation Downloads
Documentation Browse Source Resources Project Site

Stable Version
-none-

Latest Version
beta5



Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

jlocate.c

Go to the documentation of this file.
00001 #include <netdb.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <unistd.h>
00005 #include <string.h>
00006 #include <sys/socket.h>
00007 #include <libjwgc.h>
00008 
00009 int showall, groupmode, hideextra, swapjid, exactmatch;
00010 char *target, *whoami;
00011 
00012 void usage() {
00013         fprintf(stderr, "Usage: %s %s[-h] [-e] [-a] [-g] [-n] [-o] [-v] <account name>\n%s\
00014   -a   Show full contact list (normally unavailable is filtered)\n\
00015   -g   Show contacts by group\n\
00016   -e   Exact match on account name (no effect if none specified)\n\
00017   -h   Display help\n\
00018   -n   Show nickname on status line instead of jid\n\
00019   -o   Omit extra information (nickname or jid, depending on if -n is set)\n\
00020   -v   Verbose output\n\
00021 \n\
00022   If no <account name> is provided, it returns your full contact list\n\
00023 ",
00024         whoami,
00025 #ifdef NODEBUG
00026         "",
00027         ""
00028 #else
00029         "[-d <flags>] ",
00030         "  -d   Enable/Disable debugging (leave <flags> blank for usage)\n"
00031 #endif /* NODEBUG */
00032         );
00033         exit(1);
00034 }
00035 
00036 void jlocate_standard_on_event_handler(jwgconn conn, jwgpacket packet) {
00037         xode x, r, rr;
00038         char *jid, *status, *nick, *resid;
00039         int cnt = 0;
00040 
00041         x = packet->x;
00042         x = xode_get_tag(x, "contact");
00043         for (;;) {
00044                 if (!x) break;
00045                 jid = xode_get_attrib(x, "jid");
00046                 nick = xode_get_attrib(x, "nick");
00047 
00048                 r = xode_get_tag(x, "resources");
00049                 rr = xode_get_firstchild(r);
00050                 if (!rr) {
00051                         printf("%-49.49s %-15.15s %.16s\n",
00052                                 swapjid && nick ? nick : jid,
00053                                 "",
00054                                 "unavailable");
00055                 }
00056                 while (rr) {
00057                         resid = xode_get_attrib(rr, "name");
00058                         status = xode_get_attrib(rr, "status");
00059                         printf("%-49.49s %-15.15s %.16s\n",
00060                                 swapjid && nick ? nick : jid,
00061                                 resid && strcmp(resid, "NULL") ? resid : "",
00062                                 status ? status : "unavailable");
00063                         rr = xode_get_nextsibling(rr);
00064                 }
00065 
00066                 if (!hideextra) {
00067                         if (swapjid && nick) {
00068                                 printf("    JID: %.69s\n", jid);
00069                         }
00070                         else if (!swapjid && nick) {
00071                                 printf("    Nickname: %.64s\n", nick);
00072                         }
00073                 }
00074                 cnt++;
00075                 x = xode_get_nextsibling(x);
00076         }
00077 
00078         printf("%d contacts found.\n", cnt);
00079 }
00080 
00081 void jlocate_grouped_on_event_handler(jwgconn conn, jwgpacket packet) {
00082         xode x, y, r, rr;
00083         char *jid, *status, *group, *nick, *resid;
00084         int cnt = 0;
00085         char *spacer;
00086 
00087         x = packet->x;
00088         x = xode_get_tag(x, "group");
00089         while (x) {
00090                 group = xode_get_attrib(x, "name");
00091                 if (group) {
00092                         printf("%s:\n", strcmp(group,"") ? group : "Unknown");
00093                         spacer = "    ";
00094                 }
00095                 else {
00096                         spacer = "";
00097                 }
00098                 y = x;
00099                 y = xode_get_tag(y, "contact");
00100                 while (y) {
00101                         jid = xode_get_attrib(y, "jid");
00102                         nick = xode_get_attrib(y, "nick");
00103 
00104                         r = xode_get_tag(y, "resources");
00105                         rr = xode_get_firstchild(r);
00106                         if (!rr) {
00107                                 printf("%s%-45.45s %-15.15s %.16s\n",
00108                                         spacer,
00109                                         swapjid && nick ? nick : jid,
00110                                         "",
00111                                         "unavailable");
00112                         }
00113                         while (rr) {
00114                                 resid = xode_get_attrib(rr, "name");
00115                                 status = xode_get_attrib(rr, "status");
00116                                 printf("%s%-45.45s %-15.15s %.16s\n",
00117                                         spacer,
00118                                         swapjid && nick ? nick : jid,
00119                                         resid && strcmp(resid, "NULL") ? resid : "",
00120                                         status ? status : "unavailable");
00121                                 rr = xode_get_nextsibling(rr);
00122                         }
00123 
00124                         if (!hideextra) {
00125                                 if (swapjid && nick) {
00126                                         printf("%s    JID: %.65s\n",
00127                                                 spacer,
00128                                                 jid);
00129                                 }
00130                                 else if (!swapjid && nick) {
00131                                         printf("%s    Nickname: %.60s\n",
00132                                                 spacer,
00133                                                 nick);
00134                                 }
00135                         }
00136                         cnt++;
00137                         y = xode_get_nextsibling(y);
00138                 }
00139                 x = xode_get_nextsibling(x);
00140         }
00141 
00142         printf("%d contacts found.\n", cnt);
00143 }
00144 
00145 int main(argc, argv)
00146         int argc;
00147         char *argv[];
00148 {
00149         int arg;
00150         xode x;
00151         jwgconn jwg;
00152 
00153         showall = hideextra = swapjid = groupmode = exactmatch = 0;
00154         target = NULL;
00155 
00156         whoami = argv[0];
00157         dinit();
00158 
00159         arg = 1;
00160 
00161         for (; arg < argc; arg++) {
00162                 if (*argv[arg] != '-') {
00163                         if (target) {
00164                                 usage();
00165                         }
00166                         target = argv[arg];
00167                         showall = 1;
00168                         continue;
00169                 } 
00170 
00171                 switch (argv[arg][1]) {
00172 #ifndef NODEBUG
00173                         case 'd':
00174                                 arg++;
00175                                 if (arg >= argc) {
00176                                         dprinttypes();
00177                                 }
00178                                 dparseflags(argv[arg]);
00179                                 break;
00180 #endif /* NODEBUG */
00181                         case 'a':
00182                                 showall = 1;
00183                                 break;
00184                         case 'g':
00185                                 groupmode = 1;
00186                                 break;
00187                         case 'h':
00188                                 usage();
00189                                 break;
00190                         case 'e':
00191                                 exactmatch = 1;
00192                                 break;
00193                         case 'n':
00194                                 swapjid = 1;
00195                                 break;
00196                         case 'o':
00197                                 hideextra = 1;
00198                                 break;
00199                         default:
00200                                 fprintf(stderr, "Unknown option: %s\n",
00201                                                 argv[arg]);
00202                                 usage();
00203                 }
00204         }
00205 
00206         jwg = jwg_new();
00207         if (!jwg) {
00208                 fprintf(stderr, "jlocate: failed to initialize jwgc connection\n");
00209                 exit(1);
00210         }
00211         if (groupmode) {
00212                 jwg_event_handler(jwg, jlocate_grouped_on_event_handler);
00213         }
00214         else {
00215                 jwg_event_handler(jwg, jlocate_standard_on_event_handler);
00216         }
00217         jwg_start(jwg);
00218         if (jwg_getfd(jwg) < 0) {
00219                 fprintf(stderr, "jlocate: failed to create jwgc connection\n");
00220                 exit(1);
00221         }
00222         x = xode_new("locate");
00223         if (target) {
00224                 xode_put_attrib(x, "target", target);
00225                 if (exactmatch) {
00226                         xode_put_attrib(x, "match", "strict");
00227                 }
00228                 else {
00229                         xode_put_attrib(x, "match", "loose");
00230                 }
00231         }
00232         if (groupmode) {
00233                 xode_put_attrib(x, "organization", "group");
00234         }
00235         if (showall) {
00236                 xode_put_attrib(x, "showall", "yes");
00237         }
00238         jwg_send(jwg, x);
00239         xode_free(x);
00240 
00241         jwg_poll(jwg, -1);
00242         jwg_stop(jwg);
00243 
00244         exit(0);
00245 }


Last updated at Tue Dec 18 21:07:42 PST 2007. This site and project hosted by...SourceForge.net Logo
Source Perspective by Fisheye