00001 #include <sys/types.h>
00002 #include <time.h>
00003 #include "main.h"
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "mit-copyright.h"
00014
00015
00016
00017
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
00030
00031
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
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
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
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 string
00097 get_field(data, length, num)
00098 char *data;
00099 int length;
00100 int num;
00101 {
00102
00103
00104
00105 while (length && num > 1) {
00106 if (!*data)
00107 num--;
00108 length--;
00109 data++;
00110 }
00111
00112
00113
00114
00115
00116 if (length)
00117 return (get_next_field(&data, &length));
00118 else
00119 return (string_Copy(""));
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
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
00150
00151
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 }