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

jwrite.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 <ctype.h>
00007 #include <sys/socket.h>
00008 #include <libjwgc.h>
00009 #include <libxode.h>
00010 
00011 #define MAXRECIPS 100
00012 
00013 int nrecips, msgarg, verbose, quiet, nocheck, noping;
00014 char *whoami, *recips[MAXRECIPS];
00015 
00016 void 
00017 usage()
00018 {
00019         fprintf(stderr, "Usage: %s %s[-h] [-n] [-q] [-v] [-t type] [-m message] recips...\n%s\
00020   -h                 Display help\n\
00021   -q                 Quiet output\n\
00022   -v                 Verbose output\n\
00023   -e                 Encrypt message\n\
00024   -f                 Send message without checking recipient validity\n\
00025   -n                 Send message without pinging recipient\n\
00026   -t <type>          Sets the type of the outgoing message\n\
00027   -m <message>       Send <message> instead of prompting for one\n\
00028 ",
00029         whoami,
00030 #ifdef NODEBUG
00031         "",
00032         ""
00033 #else
00034         "[-d <flags>] ",
00035         "  -d                 Enable/Disable debugging (leave <flags> blank for usage)\n"
00036 #endif /* NODEBUG */
00037         );
00038         exit(1);
00039 }
00040 
00041 void 
00042 jwrite_on_event_handler(jwgconn conn, jwgpacket packet)
00043 {
00044         xode x;
00045         char *cdata;
00046 
00047         x = packet->x;
00048         cdata = xode_get_data(x);
00049 
00050         if (!strcmp(xode_get_name(x), "success")) {
00051                 printf("%s\n", cdata);
00052         }
00053         else if (!strcmp(xode_get_name(x), "error")) {
00054                 printf("ERROR: %s\n", cdata);
00055         }
00056         else {
00057                 printf("Unknown response from server.\n");
00058         }
00059 }
00060 
00061 void 
00062 jwrite_ping_on_event_handler(jwgconn conn, jwgpacket packet)
00063 {
00064         xode x;
00065         char *cdata;
00066 
00067         x = packet->x;
00068         cdata = xode_get_data(x);
00069 
00070         if (!strcmp(xode_get_name(x), "success")) {
00071                 if (verbose) {
00072                         printf("%s\n", cdata);
00073                 }
00074         }
00075         else if (!strcmp(xode_get_name(x), "error")) {
00076                 printf("ERROR: %s\n", cdata);
00077         }
00078         else {
00079                 printf("Unknown response from server.\n");
00080         }
00081 }
00082 
00083 int 
00084 main(argc, argv)
00085         int argc;
00086         char *argv[];
00087 {
00088         int arg, nchars, msgsize, i, encrypt;
00089         char *message, *type, *convmessage;
00090         static char bfr[1000];
00091         xode x;
00092         jwgconn jwg;
00093 
00094         whoami = argv[0];
00095         dinit();
00096 
00097         if (argc < 2)
00098                 usage();
00099 
00100         verbose = quiet = msgarg = nrecips = nocheck = encrypt = noping = 0;
00101         type = NULL;
00102 
00103         arg = 1;
00104 
00105         for (; arg < argc && !msgarg; arg++) {
00106                 if (*argv[arg] != '-') {
00107                         recips[nrecips++] = argv[arg];
00108                         continue;
00109                 }
00110                 if (strlen(argv[arg]) > 2)
00111                         usage();
00112                 switch (argv[arg][1]) {
00113 #ifndef NODEBUG
00114                         case 'd':
00115                                 arg++;
00116                                 if (arg >= argc) {
00117                                         dprinttypes();
00118                                 }
00119                                 dparseflags(argv[arg]);
00120                                 break;
00121 #endif /* NODEBUG */
00122                         case 'h':
00123                                 usage();
00124                         case 'q':
00125                                 quiet = 1;
00126                                 break;
00127                         case 'e':
00128                                 encrypt = 1;
00129                                 break;
00130                         case 'n':
00131                                 noping = 1;
00132                                 break;
00133                         case 'f':
00134                                 nocheck = 1;
00135                                 break;
00136                         case 'v':
00137                                 verbose = 1;
00138                                 break;
00139                         case 'm':
00140                                 if (arg == argc - 1)
00141                                         usage();
00142                                 noping = 1;
00143                                 msgarg = arg + 1;
00144                                 break;
00145                         case 't':
00146                                 if ((arg + 2) > argc) {
00147                                         usage();
00148                                 }
00149                                 type = argv[++arg];
00150                                 break;
00151                         default:
00152                                 fprintf(stderr, "Illegal option: %s\n",
00153                                         argv[arg]);
00154                                 usage();
00155                 }
00156         }
00157 
00158         if (!nrecips) {
00159                 fprintf(stderr, "No recipients specified.\n");
00160                 usage();
00161         }
00162 
00163         if (!noping) {
00164                 for (i = 0; i < nrecips || !nrecips; i++) {
00165                         if (verbose)
00166                                 printf("Pinging %s\n", recips[i]);
00167 
00168                         jwg = jwg_new();
00169                         if (!jwg) {
00170                                 fprintf(stderr, "jwrite: failed to initialize jwgc connection\n");
00171                                 exit(1);
00172                         }
00173                         jwg_event_handler(jwg, jwrite_ping_on_event_handler);
00174                         jwg_start(jwg);
00175                         if (jwg_getfd(jwg) < 0) {
00176                                 fprintf(stderr, "jwrite: failed to create jwgc connection\n");
00177                                 exit(1);
00178                         }
00179 
00180                         x = xode_new("ping");
00181                         xode_put_attrib(x, "to", recips[i]);
00182                         if (nocheck) {
00183                                 xode_put_attrib(x, "dontmatch", "yes");
00184                         }
00185                         if (type) {
00186                                 xode_put_attrib(x, "type", type);
00187                         }
00188                         jwg_send(jwg, x);
00189                         xode_free(x);
00190 
00191                         jwg_poll(jwg, -1);
00192 
00193                         jwg_stop(jwg);
00194                 }
00195         }
00196 
00197         if (!msgarg && isatty(0))
00198                 printf("Type your message now.  End with control-D or a dot on a line by itself.\n");
00199 
00200         message = (char *)malloc(sizeof(char));
00201         message[0] = '\0';
00202         msgsize = 0;
00203 
00204         if (msgarg) {
00205                 int size = msgsize;
00206                 for (arg = msgarg; arg < argc; arg++)
00207                         size += (strlen(argv[arg]) + 1);
00208                 size++;         /* for the newline */
00209                 message = (char *) realloc(message, (unsigned) size + 2);
00210                 for (arg = msgarg; arg < argc; arg++) {
00211                         dprintf(dExecution, "Adding %s...\n", argv[arg]);
00212                         (void) strcpy(message + msgsize, argv[arg]);
00213                         msgsize += strlen(argv[arg]);
00214                         if (arg != argc - 1) {
00215                                 message[msgsize] = ' ';
00216                                 msgsize++;
00217                         }
00218                 }
00219                 message[msgsize] = '\n';
00220                 msgsize += 1;
00221                 message[msgsize] = '\0';
00222         }
00223         else {
00224                 if (isatty(0)) {
00225                         for (;;) {
00226                                 unsigned int l;
00227                                 if (!fgets(bfr, sizeof bfr, stdin))
00228                                         break;
00229                                 if (bfr[0] == '.' &&
00230                                     (bfr[1] == '\n' || bfr[1] == '\0'))
00231                                         break;
00232                                 l = strlen(bfr);
00233                                 message = (char *) realloc(message, msgsize + l + 1);
00234                                 (void) strcpy(message + msgsize, bfr);
00235                                 msgsize += l;
00236                         }
00237                         message = (char *) realloc(message,
00238                                                    (unsigned) (msgsize + 1));
00239                         message[msgsize+1] = '\0';
00240                 }
00241                 else {          /* Use read so you can send binary
00242                                  * messages... */
00243                         while ((nchars = read(fileno(stdin), bfr, sizeof bfr))) {
00244                                 if (nchars == -1) {
00245                                         fprintf(stderr, "Read error from stdin!  Can't continue!\n");
00246                                         exit(1);
00247                                 }
00248                                 message = (char *) realloc(message,
00249                                              (unsigned) (msgsize + nchars));
00250                                 (void) memcpy(message + msgsize, bfr, nchars);
00251                                 msgsize += nchars;
00252                         }
00253                         /* end of msg */
00254                         message = (char *) realloc(message,
00255                                                    (unsigned) (msgsize + 1));
00256                         message[msgsize+1] = '\0';
00257                 }
00258         }
00259 
00260         dprintf(dExecution, "Message before translation:\n%s\n", message);
00261         str_to_unicode(message, &convmessage);
00262         dprintf(dExecution, "Message after translation:\n%s\n", convmessage);
00263 
00264         for (i = 0; i < nrecips || !nrecips; i++) {
00265                 if (verbose)
00266                         printf("Sending message to %s\n", recips[i]);
00267 
00268                 jwg = jwg_new();
00269                 if (!jwg) {
00270                         fprintf(stderr, "jwrite: failed to initialize jwgc connection\n");
00271                         exit(1);
00272                 }
00273                 jwg_event_handler(jwg, jwrite_on_event_handler);
00274                 jwg_start(jwg);
00275                 if (jwg_getfd(jwg) < 0) {
00276                         fprintf(stderr, "jwrite: failed to create jwgc connection\n");
00277                         exit(1);
00278                 }
00279 
00280                 x = xode_new("message");
00281                 xode_put_attrib(x, "to", recips[i]);
00282                 if (nocheck) {
00283                         xode_put_attrib(x, "dontmatch", "yes");
00284                 }
00285                 if (encrypt) {
00286                         xode_put_attrib(x, "encrypt", "yes");
00287                 }
00288                 if (type) {
00289                         xode_put_attrib(x, "type", type);
00290                 }
00291                 xode_insert_cdata(x, message, strlen(message));
00292                 jwg_send(jwg, x);
00293                 xode_free(x);
00294 
00295                 jwg_poll(jwg, -1);
00296                 jwg_stop(jwg);
00297         }
00298 
00299         exit(0);
00300 }


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