mcabber/mcabber/xmpp.c
changeset 2151 8819bab88053
parent 2141 e702073612de
child 2152 2ce6dd6861ea
equal deleted inserted replaced
2150:392cf37d5966 2151:8819bab88053
  1080   update_roster = TRUE;
  1080   update_roster = TRUE;
  1081 #endif
  1081 #endif
  1082 }
  1082 }
  1083 
  1083 
  1084 static void gotmessage(LmMessageSubType type, const char *from,
  1084 static void gotmessage(LmMessageSubType type, const char *from,
  1085                        const char *body, const char *enc, const char *subject,
  1085                        const char *body, const char *enc,
  1086                        time_t timestamp, LmMessageNode *node_signed)
  1086                        const char *subject, time_t timestamp,
       
  1087                        LmMessageNode *node_signed, gboolean carbon)
  1087 {
  1088 {
  1088   char *bjid;
  1089   char *bjid;
  1089   const char *rname;
  1090   const char *rname;
  1090   char *decrypted_pgp = NULL;
  1091   char *decrypted_pgp = NULL;
  1091   char *decrypted_otr = NULL;
  1092   char *decrypted_otr = NULL;
  1183         fullbody = g_strdup_printf("[%s]\n%s", subject, body);
  1184         fullbody = g_strdup_printf("[%s]\n%s", subject, body);
  1184       else
  1185       else
  1185         fullbody = g_strdup_printf("[%s]\n", subject);
  1186         fullbody = g_strdup_printf("[%s]\n", subject);
  1186       body = fullbody;
  1187       body = fullbody;
  1187     }
  1188     }
       
  1189     // XXX/TODO: pass the carbon status (Mikael)
  1188     hk_message_in(bjid, rname, timestamp, body, type, encrypted);
  1190     hk_message_in(bjid, rname, timestamp, body, type, encrypted);
  1189     g_free(fullbody);
  1191     g_free(fullbody);
  1190   }
  1192   }
  1191 
  1193 
  1192 gotmessage_return:
  1194 gotmessage_return:
  1208   const char *body = NULL;
  1210   const char *body = NULL;
  1209   const char *enc = NULL;
  1211   const char *enc = NULL;
  1210   const char *subject = NULL;
  1212   const char *subject = NULL;
  1211   time_t timestamp = 0L;
  1213   time_t timestamp = 0L;
  1212   LmMessageSubType mstype;
  1214   LmMessageSubType mstype;
       
  1215   gboolean skip_process = FALSE;
       
  1216   LmMessageNode *ns_signed = NULL;
  1213 
  1217 
  1214   mstype = lm_message_get_sub_type(m);
  1218   mstype = lm_message_get_sub_type(m);
  1215 
  1219 
  1216   body = lm_message_node_get_child_value(m->node, "body");
  1220   body = lm_message_node_get_child_value(m->node, "body");
  1217 
  1221 
  1276     handle_state_events(from, m->node);
  1280     handle_state_events(from, m->node);
  1277   }
  1281   }
  1278 
  1282 
  1279   // Check for carbons!
  1283   // Check for carbons!
  1280   x = lm_message_node_find_xmlns(m->node, NS_CARBONS_2);
  1284   x = lm_message_node_find_xmlns(m->node, NS_CARBONS_2);
  1281   char carbons = 0;
  1285   gboolean carbons = FALSE;
  1282   if (x) {
  1286   if (x) {
  1283     carbons = 1;
  1287     carbons = TRUE;
  1284     // Parse a message that is send to one of our other resources
  1288     // Parse a message that is send to one of our other resources
  1285     if (!g_strcmp0(x->name, "received")) {
  1289     if (!g_strcmp0(x->name, "received")) {
  1286       // Go 1 level deeper to the forwarded message
  1290       // Go 1 level deeper to the forwarded message
  1287       x = lm_message_node_find_xmlns(x, "urn:xmpp:forward:0");
  1291       x = lm_message_node_find_xmlns(x, "urn:xmpp:forward:0");
  1288       x = lm_message_node_get_child(x, "message");
  1292       x = lm_message_node_get_child(x, "message");
  1289 
  1293 
  1290       from = lm_message_node_get_attribute(x, "from");
  1294       from = lm_message_node_get_attribute(x, "from");
       
  1295       if (!from) {
       
  1296         scr_LogPrint(LPRINT_LOGNORM, "Malformed carbon copy!");
       
  1297         goto handle_messages_return;
       
  1298       }
  1291       g_free(bjid);
  1299       g_free(bjid);
  1292       bjid = g_strdup(from);
  1300       bjid = g_strdup(from);
  1293       res = strchr(bjid, JID_RESOURCE_SEPARATOR);
  1301       res = strchr(bjid, JID_RESOURCE_SEPARATOR);
  1294       if (res) *res++ = 0;
  1302       if (res) *res++ = 0;
  1295       scr_WriteIncomingMessage(bjid, body, timestamp, HBB_PREFIX_IN, 0);
  1303 
  1296 
  1304       if (body && *body && !subject && !enc)
  1297       scr_LogPrint(LPRINT_DEBUG, "carbon from:%s",
  1305         ns_signed = lm_message_node_find_xmlns(x, NS_SIGNED);
  1298                    lm_message_node_get_attribute(x, "from"));
  1306       else
       
  1307         skip_process = TRUE;
       
  1308 
       
  1309       // We (probably) cannot handle encrypted forwarded messages
       
  1310       if (lm_message_node_find_xmlns(x, NS_ENCRYPTED))
       
  1311         skip_process = TRUE;
       
  1312 
       
  1313       // Try to handle forwarded chat state messages
       
  1314       if (!skip_process)
       
  1315         handle_state_events(from, x);
       
  1316 
       
  1317       scr_LogPrint(LPRINT_DEBUG, "Received incoming carbon from <%s>", from);
  1299 
  1318 
  1300     } else if (!g_strcmp0(x->name, "sent")) {
  1319     } else if (!g_strcmp0(x->name, "sent")) {
  1301       x = lm_message_node_find_xmlns(x, "urn:xmpp:forward:0");
  1320       x = lm_message_node_find_xmlns(x, "urn:xmpp:forward:0");
  1302       x = lm_message_node_get_child(x, "message");
  1321       x = lm_message_node_get_child(x, "message");
  1303 
  1322 
  1304       const char *to= lm_message_node_get_attribute(x, "to");
  1323       const char *to= lm_message_node_get_attribute(x, "to");
       
  1324       if (!to) {
       
  1325         scr_LogPrint(LPRINT_LOGNORM, "Malformed carbon copy!");
       
  1326         goto handle_messages_return;
       
  1327       }
  1305       g_free(bjid);
  1328       g_free(bjid);
  1306       bjid = g_strdup(to);
  1329       bjid = jidtodisp(to);
  1307       res = strchr(bjid, JID_RESOURCE_SEPARATOR);
  1330 
  1308       if (res) *res++ = 0;
  1331       if (body && *body)
  1309 
  1332         hk_message_out(bjid, NULL, timestamp, body, 0, NULL);
  1310       scr_write_outgoing_message(bjid, body, 0, NULL);
  1333 
  1311     }
  1334       scr_LogPrint(LPRINT_DEBUG, "Received outgoing carbon for <%s>", to);
  1312   }
  1335       goto handle_messages_return;
  1313 
  1336     }
  1314 
  1337   } else { // Not a Carbon
  1315   if (from && (body || subject) && !carbons)
  1338     ns_signed = lm_message_node_find_xmlns(m->node, NS_SIGNED);
       
  1339   }
       
  1340 
       
  1341   // Do not process the message if some fields are missing
       
  1342   if (!from || (!body && !subject))
       
  1343     skip_process = TRUE;
       
  1344 
       
  1345   if (!skip_process) {
  1316     gotmessage(mstype, from, body, enc, subject, timestamp,
  1346     gotmessage(mstype, from, body, enc, subject, timestamp,
  1317                lm_message_node_find_xmlns(m->node, NS_SIGNED));
  1347                ns_signed, carbons);
       
  1348   }
       
  1349 
       
  1350   // We're done if it was a Carbon forwarded message
       
  1351   if (carbons)
       
  1352     goto handle_messages_return;
  1318 
  1353 
  1319   // Report received message if message delivery receipt was requested
  1354   // Report received message if message delivery receipt was requested
  1320   if (lm_message_node_get_child(m->node, "request") &&
  1355   if (lm_message_node_get_child(m->node, "request") &&
  1321       (roster_getsubscription(bjid) & sub_from)) {
  1356       (roster_getsubscription(bjid) & sub_from)) {
  1322     const gchar *mid;
  1357     const gchar *mid;
  1373         got_invite(from, jid, reason, password, FALSE);
  1408         got_invite(from, jid, reason, password, FALSE);
  1374       }
  1409       }
  1375     }
  1410     }
  1376   }
  1411   }
  1377 
  1412 
       
  1413 handle_messages_return:
  1378   g_free(bjid);
  1414   g_free(bjid);
  1379   return LM_HANDLER_RESULT_REMOVE_MESSAGE;
  1415   return LM_HANDLER_RESULT_REMOVE_MESSAGE;
  1380 }
  1416 }
  1381 
  1417 
  1382 static LmHandlerResult cb_caps(LmMessageHandler *h, LmConnection *c,
  1418 static LmHandlerResult cb_caps(LmMessageHandler *h, LmConnection *c,