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

notice.c

Go to the documentation of this file.
00001 #include <sys/types.h>
00002 #include <time.h>
00003 #include "main.h"
00004 
00005 /*
00006  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
00007  *      For copying and distribution information, see the file
00008  *      "mit-copyright.h".
00009  *
00010  *      Modified for jwgc by Daniel Henninger.
00011  */
00012 
00013 #include "mit-copyright.h"
00014 
00015 /****************************************************************************/
00016 /* */
00017 /* Module containing code to extract a notice's fields:             */
00018 /* */
00019 /****************************************************************************/
00020 
00021 #include <netdb.h>
00022 #include <sys/socket.h>
00023 #include <arpa/inet.h>
00024 #include "error.h"
00025 #include "variables.h"
00026 #include "notice.h"
00027 
00028 /*
00029  *    int count_nulls(char *data, int length)
00030  *        Requires: length>=0
00031  *        Effects: Returns the # of nulls in data[0]..data[length-1]
00032  */
00033 
00034 int 
00035 count_nulls(data, length)
00036         char *data;
00037         int length;
00038 {
00039         int count = 0;
00040 
00041         for (; length; data++, length--)
00042                 if (!*data)
00043                         count++;
00044 
00045         return (count);
00046 }
00047 
00048 /*
00049  *    string get_next_field(char **data_p, int *length_p)
00050  *        Requires: *length_p >= 0
00051  *        Modifies: *data_p, *length_p
00052  *        Effects: Treats (*data_p)[0], (*data_p)[1], ... (*data_p)[length-1]
00053  *                 as a series of null-seperated fields.  This function
00054  *                 returns a copy of the first field on the heap.  This
00055  *                 string must eventually be freed.  Also, *data_p is
00056  *                 advanced and *length_p decreased so that another
00057  *                 call to this procedure with the same arguments will
00058  *                 return the second field.  The next call will return
00059  *                 the third field, etc.  "" is returned if 0 fields
00060  *                 remain.  (this is the case when *length_p == 0)
00061  */
00062 
00063 string 
00064 get_next_field(data_p, length_p)
00065         char **data_p;
00066         int *length_p;
00067 {
00068         char *data = *data_p;
00069         int length = *length_p;
00070         char *ptr;
00071 
00072         for (ptr = data; length; ptr++, length--)
00073                 if (!*ptr) {
00074                         *data_p = ptr + 1;
00075                         *length_p = length - 1;
00076                         return (string_Copy(data));
00077                 }
00078 
00079         length = *length_p;
00080         *data_p = ptr;
00081         *length_p = 0;
00082         return (string_CreateFromData(data, length));
00083 }
00084 
00085 /*
00086  *    string get_field(char *data, int length, int num)
00087  *        Requires: length>=0, num>0
00088  *        Effects: Treats data[0]..data[length-1] as a series of
00089  *                 null-seperated fields.  This function returns a copy of
00090  *                 the num'th field (numbered from 1 in this case) on the
00091  *                 heap.  This string must eventually be freed.  If there
00092  *                 is no num'th field (because num<1 or num># of fields),
00093  *                 "" is returned.
00094  */
00095 
00096 string 
00097 get_field(data, length, num)
00098         char *data;
00099         int length;
00100         int num;
00101 {
00102         /*
00103          * While num>1 and there are fields left, skip a field & decrement num:
00104          */
00105         while (length && num > 1) {
00106                 if (!*data)
00107                         num--;
00108                 length--;
00109                 data++;
00110         }
00111 
00112         /*
00113          * If any more fields left, the first field is the one we want.
00114          * Otherwise, there is no such field as num -- return "".
00115          */
00116         if (length)
00117                 return (get_next_field(&data, &length));
00118         else
00119                 return (string_Copy(""));
00120 }
00121 
00122 /*
00123  *    string convert_nulls_to_newlines(data, length)
00124  *       Requires: length>=0, malloc never returns NULL
00125  *       Effects: Takes data[0]..data[length-1], converts all nulls to
00126  *                newlines ('\n') and returns the result as a null-terminated
00127  *                string on the heap.  The returned string must eventually
00128  *                be freed.
00129  */
00130 
00131 string 
00132 convert_nulls_to_newlines(data, length)
00133         char *data;
00134         int length;
00135 {
00136         char *result, *ptr;
00137         char c;
00138 
00139         result = (char *) malloc(length + 1);
00140         result[length] = '\0';
00141 
00142         for (ptr = result; length; data++, ptr++, length--)
00143                 *ptr = (c = *data) ? c : '\n';
00144 
00145         return (result);
00146 }
00147 
00148 /*
00149  *    char *decode_notice(JNotice_t *notice)
00150  *        Modifies: various description language variables
00151  *        Effects:
00152  */
00153 
00154 char *
00155 decode_notice(notice)
00156         struct jabpacket_struct *notice;
00157 {
00158         char *temp, *c, *d, *from, *ns, *jid;
00159         xode x, y;
00160         char timestr[26];
00161         time_t curtime;
00162 
00163         var_set_variable_then_free_value("type",
00164                 jab_type_to_ascii(notice->type));
00165         var_set_variable_then_free_value("subtype",
00166                 jab_subtype_to_ascii(notice->subtype));
00167 
00168         dprintf(dXML, "Decoding Notice [%d,%d]:\n%s\n",
00169                 notice->type,
00170                 notice->subtype,
00171                 xode_to_prettystr(notice->x));
00172 
00173         temp = xode_get_attrib(notice->x, "type");
00174         if (temp) {
00175                 var_set_variable("msgtype", temp);
00176         }
00177         else {
00178                 var_set_variable("msgtype", "");
00179         }
00180 
00181         var_set_variable("from", "");
00182         var_set_variable("sender", "");
00183         var_set_variable("server", "");
00184         var_set_variable("resource", "");
00185         var_set_variable("nickname", "");
00186         jid = notice->from->full;
00187         if (jid) {
00188                 from = strdup(jid);
00189         }
00190         else {
00191                 from = strdup(xode_get_attrib(notice->x, "from"));
00192         }
00193         if (from) {
00194                 var_set_variable("from", from);
00195                 temp = (char *) find_nickname_from_jid(from);
00196                 if (temp) {
00197                         var_set_variable_then_free_value("nickname", temp);
00198                 }
00199 
00200                 c = from;
00201                 d = strchr(c, '@');
00202                 if (d) {
00203                         *d = '\0';
00204                         temp = strdup(c);
00205                         var_set_variable_then_free_value("sender", temp);
00206                         d++;
00207                         c = d;
00208                         d = strchr(c, '/');
00209                         if (d) {
00210                                 *d = '\0';
00211                                 temp = strdup(c);
00212                                 var_set_variable_then_free_value("server",
00213                                                 temp);
00214                                 d++;
00215                                 c = d;
00216                                 temp = strdup(c);
00217                                 var_set_variable_then_free_value("resource",
00218                                                 temp);
00219                         }
00220                         else {
00221                                 var_set_variable("server", c);
00222                                 var_set_variable("resource", "");
00223                         }
00224                 }
00225                 else {
00226                         var_set_variable("sender", from);
00227                 }
00228         }
00229         else {
00230                 var_set_variable("from", "[unknown]");
00231         }
00232         free(from);
00233 
00234         x = xode_get_tag(notice->x, "subject");
00235         if (x) {
00236                 temp = xode_get_data(x);
00237                 var_set_variable("subject", temp);
00238         }
00239         else {
00240                 var_set_variable("subject", "");
00241         }
00242 
00243         var_set_variable("event", "");
00244         x = xode_get_tag(notice->x, "x");
00245         if (x) {
00246                 ns = xode_get_attrib(x, "xmlns");
00247                 if (ns && !strcmp(ns, NS_EVENT)) {
00248                         y = xode_get_tag(x, "composing");
00249                         if (y) {
00250                                 var_set_variable("event", "composing");
00251                         }
00252                 }
00253         }
00254 
00255         x = xode_get_tag(notice->x, "show");
00256         if (x) {
00257                 char *convtemp;
00258                 temp = xode_get_data(x);
00259                 if (!unicode_to_str(temp, &convtemp)) {
00260                         convtemp = strdup("");
00261                 }
00262                 var_set_variable_then_free_value("show", convtemp);
00263         }
00264         else {
00265                 var_set_variable("show", "");
00266         }
00267 
00268         x = xode_get_tag(notice->x, "status");
00269         if (x) {
00270                 char *convtemp;
00271                 temp = xode_get_data(x);
00272                 if (!unicode_to_str(temp, &convtemp)) {
00273                         convtemp = strdup("");
00274                 }
00275                 if (temp) {
00276                         var_set_variable_then_free_value("status", convtemp);
00277                 }
00278                 else {
00279                         var_set_variable("status", "");
00280                 }
00281         }
00282         else {
00283                 var_set_variable("status", "");
00284         }
00285 
00286         x = xode_get_tag(notice->x, "error");
00287         if (x) {
00288                 temp = xode_get_data(x);
00289                 var_set_variable("error", temp);
00290         }
00291         else {
00292                 var_set_variable("error", "");
00293         }
00294 
00295         curtime = time(NULL);
00296         strftime(timestr, 25, "%T", localtime(&curtime));
00297         var_set_variable("time", timestr);
00298 
00299         strftime(timestr, 25, "%r", localtime(&curtime));
00300         var_set_variable("time12", timestr);
00301 
00302         strftime(timestr, 25, "%x", localtime(&curtime));
00303         var_set_variable("date", timestr);
00304 
00305         strftime(timestr, 25, "%a %b %e %Y", localtime(&curtime));
00306         var_set_variable("longdate", timestr);
00307 
00308         return (0);
00309 }


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