comparison libpurple/plugins/log_reader.c @ 17528:beeef78409b2

A patch from o_sukhodolsky to restructure the Trillian log formatting code to avoid warnings.
author Richard Laager <rlaager@wiktel.com>
date Thu, 07 Jun 2007 05:23:31 +0000
parents 0e22098e1421
children c529c95e7733
comparison
equal deleted inserted replaced
17527:89adae2f1a6e 17528:beeef78409b2
1423 FILE *file; 1423 FILE *file;
1424 PurpleBuddy *buddy; 1424 PurpleBuddy *buddy;
1425 char *escaped; 1425 char *escaped;
1426 GString *formatted; 1426 GString *formatted;
1427 char *c; 1427 char *c;
1428 char *line; 1428 const char *line;
1429 1429
1430 g_return_val_if_fail(log != NULL, g_strdup("")); 1430 g_return_val_if_fail(log != NULL, g_strdup(""));
1431 1431
1432 data = log->logger_data; 1432 data = log->logger_data;
1433 1433
1461 1461
1462 /* Apply formatting... */ 1462 /* Apply formatting... */
1463 formatted = g_string_new(""); 1463 formatted = g_string_new("");
1464 c = read; 1464 c = read;
1465 line = read; 1465 line = read;
1466 while (*c) 1466 while (c)
1467 { 1467 {
1468 if (*c == '\n') 1468 const char *link;
1469 const char *footer = NULL;
1470 GString *temp;
1471
1472 c = strstr(c, "\n");
1473
1474 if (c) {
1475 *c = '\0';
1476 ++c;
1477 }
1478
1479 /* Convert links.
1480 *
1481 * The format is (Link: URL)URL
1482 * So, I want to find each occurance of "(Link: " and replace that chunk with:
1483 * <a href="
1484 * Then, replace the next ")" with:
1485 * ">
1486 * Then, replace the next " " (or add this if the end-of-line is reached) with:
1487 * </a>
1488 *
1489 * As implemented, this isn't perfect, but it should cover common cases.
1490 */
1491 temp = g_string_sized_new(strlen(line));
1492 while (line && (link = strstr(line, "(Link: ")))
1469 { 1493 {
1470 char *link_temp_line; 1494 const char *tmp = link;
1471 char *link; 1495
1472 char *timestamp; 1496 link += 7;
1473 char *footer = NULL; 1497 if (*link)
1474 *c = '\0';
1475
1476 /* Convert links.
1477 *
1478 * The format is (Link: URL)URL
1479 * So, I want to find each occurance of "(Link: " and replace that chunk with:
1480 * <a href="
1481 * Then, replace the next ")" with:
1482 * ">
1483 * Then, replace the next " " (or add this if the end-of-line is reached) with:
1484 * </a>
1485 *
1486 * As implemented, this isn't perfect, but it should cover common cases.
1487 */
1488 link_temp_line = NULL;
1489 while ((link = strstr(line, "(Link: ")))
1490 { 1498 {
1491 char *tmp = link; 1499 char *end_paren;
1492 1500 char *space;
1493 link += 7; 1501
1494 if (*link) 1502 if (!(end_paren = strstr(link, ")")))
1495 { 1503 {
1496 char *end_paren; 1504 /* Something is not as we expect. Bail out. */
1497 char *space; 1505 break;
1498 GString *temp; 1506 }
1499 1507
1500 if (!(end_paren = strstr(link, ")"))) 1508 g_string_append_len(temp, line, (tmp - line));
1501 { 1509
1502 /* Something is not as we expect. Bail out. */ 1510 /* Start an <a> tag. */
1503 break; 1511 g_string_append(temp, "<a href=\"");
1512
1513 /* Append up to the ) */
1514 g_string_append_len(temp, link, end_paren - link);
1515
1516 /* Finish the <a> tag. */
1517 g_string_append(temp, "\">");
1518
1519 /* The \r is a bit of a hack to keep there from being a \r in
1520 * the link text, which may not matter. */
1521 if ((space = strstr(end_paren, " ")) || (space = strstr(end_paren, "\r")))
1522 {
1523 g_string_append_len(temp, end_paren + 1, space - end_paren - 1);
1524
1525 /* Close the <a> tag. */
1526 g_string_append(temp, "</a>");
1527
1528 space++;
1529 }
1530 else
1531 {
1532 /* There is no space before the end of the line. */
1533 g_string_append(temp, end_paren + 1);
1534 /* Close the <a> tag. */
1535 g_string_append(temp, "</a>");
1536 }
1537 line = space;
1538 }
1539 }
1540
1541 if (line) {
1542 g_string_append(temp, line);
1543 }
1544 line = temp->str;
1545
1546 if (*line == '[') {
1547 const char *timestamp;
1548
1549 if ((timestamp = strstr(line, "]"))) {
1550 line++;
1551 /* TODO: Parse the timestamp and convert it to Purple's format. */
1552 g_string_append(formatted, "<font size=\"2\">(");
1553 g_string_append_len(formatted, line, (timestamp - line));
1554 g_string_append(formatted,")</font> ");
1555 line = timestamp + 1;
1556 if (line[0] && line[1])
1557 line++;
1558 }
1559
1560 if (purple_str_has_prefix(line, "*** ")) {
1561 line += (sizeof("*** ") - 1);
1562 g_string_append(formatted, "<b>");
1563 footer = "</b>";
1564 if (purple_str_has_prefix(line, "NOTE: This user is offline.")) {
1565 line = _("User is offline.");
1566 } else if (purple_str_has_prefix(line,
1567 "NOTE: Your status is currently set to ")) {
1568
1569 line += (sizeof("NOTE: ") - 1);
1570 } else if (purple_str_has_prefix(line, "Auto-response sent to ")) {
1571 g_string_append(formatted, _("Auto-response sent:"));
1572 while (*line && *line != ':')
1573 line++;
1574 if (*line)
1575 line++;
1576 g_string_append(formatted, "</b>");
1577 footer = NULL;
1578 } else if (strstr(line, " signed off ")) {
1579 if (buddy != NULL && buddy->alias)
1580 g_string_append_printf(formatted,
1581 _("%s has signed off."), buddy->alias);
1582 else
1583 g_string_append_printf(formatted,
1584 _("%s has signed off."), log->name);
1585 line = "";
1586 } else if (strstr(line, " signed on ")) {
1587 if (buddy != NULL && buddy->alias)
1588 g_string_append(formatted, buddy->alias);
1589 else
1590 g_string_append(formatted, log->name);
1591 line = " logged in.";
1592 } else if (purple_str_has_prefix(line,
1593 "One or more messages may have been undeliverable.")) {
1594
1595 g_string_append(formatted,
1596 "<span style=\"color: #ff0000;\">");
1597 g_string_append(formatted,
1598 _("One or more messages may have been "
1599 "undeliverable."));
1600 line = "";
1601 footer = "</span></b>";
1602 } else if (purple_str_has_prefix(line,
1603 "You have been disconnected.")) {
1604
1605 g_string_append(formatted,
1606 "<span style=\"color: #ff0000;\">");
1607 g_string_append(formatted,
1608 _("You were disconnected from the server."));
1609 line = "";
1610 footer = "</span></b>";
1611 } else if (purple_str_has_prefix(line,
1612 "You are currently disconnected.")) {
1613
1614 g_string_append(formatted,
1615 "<span style=\"color: #ff0000;\">");
1616 line = _("You are currently disconnected. Messages "
1617 "will not be received unless you are "
1618 "logged in.");
1619 footer = "</span></b>";
1620 } else if (purple_str_has_prefix(line,
1621 "Your previous message has not been sent.")) {
1622
1623 g_string_append(formatted,
1624 "<span style=\"color: #ff0000;\">");
1625
1626 if (purple_str_has_prefix(line,
1627 "Your previous message has not been sent. "
1628 "Reason: Maximum length exceeded.")) {
1629
1630 g_string_append(formatted,
1631 _("Message could not be sent because "
1632 "the maximum length was exceeded."));
1633 line = "";
1634 } else {
1635 g_string_append(formatted,
1636 _("Message could not be sent."));
1637 line += (sizeof(
1638 "Your previous message "
1639 "has not been sent. ") - 1);
1504 } 1640 }
1505 1641
1506 *tmp = '\0'; 1642 footer = "</span></b>";
1507 temp = g_string_new(line); 1643 }
1508 1644 } else if (purple_str_has_prefix(line, data->their_nickname)) {
1509 /* Start an <a> tag. */ 1645 if (buddy != NULL && buddy->alias) {
1510 g_string_append(temp, "<a href=\""); 1646 line += strlen(data->their_nickname) + 2;
1511 1647 g_string_append_printf(formatted,
1512 /* Append up to the ) */ 1648 "<span style=\"color: #A82F2F;\">"
1513 g_string_append_len(temp, link, end_paren - link); 1649 "<b>%s</b></span>: ", buddy->alias);
1514 1650 }
1515 /* Finish the <a> tag. */ 1651 } else {
1516 g_string_append(temp, "\">"); 1652 const char *line2 = strstr(line, ":");
1517 1653 if (line2) {
1518 /* The \r is a bit of a hack to keep there from being a \r in 1654 const char *acct_name;
1519 * the link text, which may not matter. */ 1655 line2++;
1520 if ((space = strstr(end_paren, " ")) || (space = strstr(end_paren, "\r"))) 1656 line = line2;
1521 { 1657 acct_name = purple_account_get_alias(log->account);
1522 g_string_append_len(temp, end_paren + 1, space - end_paren - 1); 1658 if (!acct_name)
1523 1659 acct_name = purple_account_get_username(log->account);
1524 /* Close the <a> tag. */ 1660
1525 g_string_append(temp, "</a>"); 1661 g_string_append_printf(formatted,
1526 1662 "<span style=\"color: #16569E;\">"
1527 space++; 1663 "<b>%s</b></span>:", acct_name);
1528 if (*space)
1529 {
1530 g_string_append_c(temp, ' ');
1531 /* Keep the rest of the line. */
1532 g_string_append(temp, space);
1533 }
1534 }
1535 else
1536 {
1537 /* There is no space before the end of the line. */
1538 g_string_append(temp, end_paren + 1);
1539 /* Close the <a> tag. */
1540 g_string_append(temp, "</a>");
1541 }
1542
1543 g_free(link_temp_line);
1544 line = g_string_free(temp, FALSE);
1545
1546 /* Save this memory location so we can free it later. */
1547 link_temp_line = line;
1548 } 1664 }
1549 } 1665 }
1550 1666 }
1551 timestamp = ""; 1667
1552 if (*line == '[') { 1668 g_string_append(formatted, line);
1553 timestamp = line; 1669
1554 while (*timestamp && *timestamp != ']') 1670 if (footer)
1555 timestamp++; 1671 g_string_append(formatted, footer);
1556 if (*timestamp == ']') { 1672
1557 *timestamp = '\0'; 1673 g_string_append_c(formatted, '\n');
1558 line++; 1674
1559 /* TODO: Parse the timestamp and convert it to Purple's format. */ 1675 g_string_free(temp, TRUE);
1560 g_string_append_printf(formatted, 1676
1561 "<font size=\"2\">(%s)</font> ", line); 1677 line = c;
1562 line = timestamp;
1563 if (line[1] && line[2])
1564 line += 2;
1565 }
1566
1567 if (purple_str_has_prefix(line, "*** ")) {
1568 line += (sizeof("*** ") - 1);
1569 g_string_append(formatted, "<b>");
1570 footer = "</b>";
1571 if (purple_str_has_prefix(line, "NOTE: This user is offline.")) {
1572 line = _("User is offline.");
1573 } else if (purple_str_has_prefix(line,
1574 "NOTE: Your status is currently set to ")) {
1575
1576 line += (sizeof("NOTE: ") - 1);
1577 } else if (purple_str_has_prefix(line, "Auto-response sent to ")) {
1578 g_string_append(formatted, _("Auto-response sent:"));
1579 while (*line && *line != ':')
1580 line++;
1581 if (*line)
1582 line++;
1583 g_string_append(formatted, "</b>");
1584 footer = NULL;
1585 } else if (strstr(line, " signed off ")) {
1586 if (buddy != NULL && buddy->alias)
1587 g_string_append_printf(formatted,
1588 _("%s has signed off."), buddy->alias);
1589 else
1590 g_string_append_printf(formatted,
1591 _("%s has signed off."), log->name);
1592 line = "";
1593 } else if (strstr(line, " signed on ")) {
1594 if (buddy != NULL && buddy->alias)
1595 g_string_append(formatted, buddy->alias);
1596 else
1597 g_string_append(formatted, log->name);
1598 line = " logged in.";
1599 } else if (purple_str_has_prefix(line,
1600 "One or more messages may have been undeliverable.")) {
1601
1602 g_string_append(formatted,
1603 "<span style=\"color: #ff0000;\">");
1604 g_string_append(formatted,
1605 _("One or more messages may have been "
1606 "undeliverable."));
1607 line = "";
1608 footer = "</span></b>";
1609 } else if (purple_str_has_prefix(line,
1610 "You have been disconnected.")) {
1611
1612 g_string_append(formatted,
1613 "<span style=\"color: #ff0000;\">");
1614 g_string_append(formatted,
1615 _("You were disconnected from the server."));
1616 line = "";
1617 footer = "</span></b>";
1618 } else if (purple_str_has_prefix(line,
1619 "You are currently disconnected.")) {
1620
1621 g_string_append(formatted,
1622 "<span style=\"color: #ff0000;\">");
1623 line = _("You are currently disconnected. Messages "
1624 "will not be received unless you are "
1625 "logged in.");
1626 footer = "</span></b>";
1627 } else if (purple_str_has_prefix(line,
1628 "Your previous message has not been sent.")) {
1629
1630 g_string_append(formatted,
1631 "<span style=\"color: #ff0000;\">");
1632
1633 if (purple_str_has_prefix(line,
1634 "Your previous message has not been sent. "
1635 "Reason: Maximum length exceeded.")) {
1636
1637 g_string_append(formatted,
1638 _("Message could not be sent because "
1639 "the maximum length was exceeded."));
1640 line = "";
1641 } else {
1642 g_string_append(formatted,
1643 _("Message could not be sent."));
1644 line += (sizeof(
1645 "Your previous message "
1646 "has not been sent. ") - 1);
1647 }
1648
1649 footer = "</span></b>";
1650 }
1651 } else if (purple_str_has_prefix(line, data->their_nickname)) {
1652 if (buddy != NULL && buddy->alias) {
1653 line += strlen(data->their_nickname) + 2;
1654 g_string_append_printf(formatted,
1655 "<span style=\"color: #A82F2F;\">"
1656 "<b>%s</b></span>: ", buddy->alias);
1657 }
1658 } else {
1659 char *line2 = line;
1660 while (*line2 && *line2 != ':')
1661 line2++;
1662 if (*line2 == ':') {
1663 const char *acct_name;
1664 line2++;
1665 line = line2;
1666 acct_name = purple_account_get_alias(log->account);
1667 if (!acct_name)
1668 acct_name = purple_account_get_username(log->account);
1669
1670 g_string_append_printf(formatted,
1671 "<span style=\"color: #16569E;\">"
1672 "<b>%s</b></span>:", acct_name);
1673 }
1674 }
1675 }
1676
1677 g_string_append(formatted, line);
1678
1679 if (footer)
1680 g_string_append(formatted, footer);
1681
1682 g_string_append_c(formatted, '\n');
1683
1684 g_free(link_temp_line);
1685
1686 c++;
1687 line = c;
1688 } else
1689 c++;
1690 } 1678 }
1691 1679
1692 g_free(read); 1680 g_free(read);
1693 read = formatted->str; 1681 read = formatted->str;
1694 g_string_free(formatted, FALSE); 1682 g_string_free(formatted, FALSE);