… | |
… | |
1585 | new_draw_info (NDI_UNIQUE, 0, op, buf); |
1585 | new_draw_info (NDI_UNIQUE, 0, op, buf); |
1586 | fix_player (op); |
1586 | fix_player (op); |
1587 | return 1; |
1587 | return 1; |
1588 | } |
1588 | } |
1589 | |
1589 | |
1590 | /* |
|
|
1591 | * Changing the custom name of an item |
|
|
1592 | * |
|
|
1593 | * Syntax is: rename <what object> to <new name> |
|
|
1594 | * if '<what object>' is omitted, marked object is used |
|
|
1595 | * if 'to <new name>' is omitted, custom name is cleared |
|
|
1596 | * |
|
|
1597 | * Names are considered for all purpose having a length <=127 (max length sent to client |
|
|
1598 | * by server) */ |
|
|
1599 | |
|
|
1600 | int |
|
|
1601 | command_rename_item (object *op, char *params) |
|
|
1602 | { |
|
|
1603 | char buf[VERY_BIG_BUF]; |
|
|
1604 | int itemnumber; |
|
|
1605 | object *item = NULL; |
|
|
1606 | char *closebrace; |
|
|
1607 | size_t counter; |
|
|
1608 | |
|
|
1609 | if (params) |
|
|
1610 | { |
|
|
1611 | /* Let's skip white spaces */ |
|
|
1612 | while (' ' == *params) |
|
|
1613 | params++; |
|
|
1614 | |
|
|
1615 | /* Checking the first part */ |
|
|
1616 | if ((itemnumber = atoi (params)) != 0) |
|
|
1617 | { |
|
|
1618 | for (item = op->inv; item && ((item->count != (tag_t) itemnumber) || item->invisible); item = item->below); |
|
|
1619 | if (!item) |
|
|
1620 | { |
|
|
1621 | new_draw_info (NDI_UNIQUE, 0, op, "Tried to rename an invalid item."); |
|
|
1622 | return 1; |
|
|
1623 | } |
|
|
1624 | while (isdigit (*params) || ' ' == *params) |
|
|
1625 | params++; |
|
|
1626 | } |
|
|
1627 | else if ('<' == *params) |
|
|
1628 | { |
|
|
1629 | /* Got old name, let's get it & find appropriate matching item */ |
|
|
1630 | closebrace = strchr (params, '>'); |
|
|
1631 | if (!closebrace) |
|
|
1632 | { |
|
|
1633 | new_draw_info (NDI_UNIQUE, 0, op, "Syntax error!"); |
|
|
1634 | return 1; |
|
|
1635 | } |
|
|
1636 | |
|
|
1637 | /* Sanity check for buffer overruns */ |
|
|
1638 | if ((closebrace - params) > 127) |
|
|
1639 | { |
|
|
1640 | new_draw_info (NDI_UNIQUE, 0, op, "Old name too long (up to 127 characters allowed)!"); |
|
|
1641 | return 1; |
|
|
1642 | } |
|
|
1643 | |
|
|
1644 | /* Copy the old name */ |
|
|
1645 | assign (buf, params + 1, closebrace - params - 1); |
|
|
1646 | |
|
|
1647 | /* Find best matching item */ |
|
|
1648 | item = find_best_object_match (op, buf); |
|
|
1649 | if (!item) |
|
|
1650 | { |
|
|
1651 | new_draw_info (NDI_UNIQUE, 0, op, "Could not find a matching item to rename."); |
|
|
1652 | return 1; |
|
|
1653 | } |
|
|
1654 | |
|
|
1655 | /* Now need to move pointer to just after > */ |
|
|
1656 | params = closebrace + 1; |
|
|
1657 | while (' ' == *params) |
|
|
1658 | params++; |
|
|
1659 | |
|
|
1660 | } |
|
|
1661 | else |
|
|
1662 | { |
|
|
1663 | /* Use marked item */ |
|
|
1664 | item = find_marked_object (op); |
|
|
1665 | if (!item) |
|
|
1666 | { |
|
|
1667 | new_draw_info (NDI_UNIQUE, 0, op, "No marked item to rename."); |
|
|
1668 | return 1; |
|
|
1669 | } |
|
|
1670 | } |
|
|
1671 | |
|
|
1672 | /* Now let's find the new name */ |
|
|
1673 | if (!strncmp (params, "to ", 3)) |
|
|
1674 | { |
|
|
1675 | params += 3; |
|
|
1676 | while (' ' == *params) |
|
|
1677 | params++; |
|
|
1678 | if ('<' != *params) |
|
|
1679 | { |
|
|
1680 | new_draw_info (NDI_UNIQUE, 0, op, "Syntax error, expecting < at start of new name!"); |
|
|
1681 | return 1; |
|
|
1682 | } |
|
|
1683 | closebrace = strchr (params + 1, '>'); |
|
|
1684 | if (!closebrace) |
|
|
1685 | { |
|
|
1686 | new_draw_info (NDI_UNIQUE, 0, op, "Syntax error, expecting > at end of new name!"); |
|
|
1687 | return 1; |
|
|
1688 | } |
|
|
1689 | |
|
|
1690 | /* Sanity check for buffer overruns */ |
|
|
1691 | if ((closebrace - params) > 127) |
|
|
1692 | { |
|
|
1693 | new_draw_info (NDI_UNIQUE, 0, op, "New name too long (up to 127 characters allowed)!"); |
|
|
1694 | return 1; |
|
|
1695 | } |
|
|
1696 | |
|
|
1697 | /* Copy the new name */ |
|
|
1698 | assign (buf, params + 1, closebrace - params - 1); |
|
|
1699 | |
|
|
1700 | /* Let's check it for weird characters */ |
|
|
1701 | for (counter = 0; counter < strlen (buf); counter++) |
|
|
1702 | { |
|
|
1703 | if (isalnum (buf[counter])) |
|
|
1704 | continue; |
|
|
1705 | if (' ' == buf[counter]) |
|
|
1706 | continue; |
|
|
1707 | if ('\'' == buf[counter]) |
|
|
1708 | continue; |
|
|
1709 | if ('+' == buf[counter]) |
|
|
1710 | continue; |
|
|
1711 | if ('_' == buf[counter]) |
|
|
1712 | continue; |
|
|
1713 | if ('-' == buf[counter]) |
|
|
1714 | continue; |
|
|
1715 | |
|
|
1716 | /* If we come here, then the name contains an invalid character... |
|
|
1717 | tell the player & exit */ |
|
|
1718 | new_draw_info (NDI_UNIQUE, 0, op, "Invalid new name!"); |
|
|
1719 | return 1; |
|
|
1720 | } |
|
|
1721 | |
|
|
1722 | } |
|
|
1723 | else |
|
|
1724 | { |
|
|
1725 | /* If param contains something, then syntax error... */ |
|
|
1726 | if (strlen (params)) |
|
|
1727 | { |
|
|
1728 | new_draw_info (NDI_UNIQUE, 0, op, "Syntax error, expected 'to <' after old name!"); |
|
|
1729 | return 1; |
|
|
1730 | } |
|
|
1731 | /* New name is empty */ |
|
|
1732 | buf[0] = '\0'; |
|
|
1733 | } |
|
|
1734 | } |
|
|
1735 | else |
|
|
1736 | { |
|
|
1737 | /* Last case: params==NULL */ |
|
|
1738 | item = find_marked_object (op); |
|
|
1739 | if (!item) |
|
|
1740 | { |
|
|
1741 | new_draw_info (NDI_UNIQUE, 0, op, "No marked item to rename."); |
|
|
1742 | return 1; |
|
|
1743 | } |
|
|
1744 | buf[0] = '\0'; |
|
|
1745 | } |
|
|
1746 | |
|
|
1747 | if (QUERY_FLAG (item, FLAG_UNPAID)) |
|
|
1748 | { |
|
|
1749 | new_draw_info (NDI_UNIQUE, 0, op, "You can't rename an unpaid item! You should pay for it first."); |
|
|
1750 | return 1; |
|
|
1751 | } |
|
|
1752 | |
|
|
1753 | /* Coming here, everything is fine... */ |
|
|
1754 | if (!strlen (buf)) |
|
|
1755 | { |
|
|
1756 | /* Clear custom name */ |
|
|
1757 | if (item->custom_name) |
|
|
1758 | { |
|
|
1759 | item->custom_name = 0; |
|
|
1760 | |
|
|
1761 | new_draw_info_format (NDI_UNIQUE, 0, op, "You stop calling your %s with weird names.", |
|
|
1762 | query_base_name (item, item->nrof > 1 ? 1 : 0)); |
|
|
1763 | esrv_update_item (UPD_NAME, op, item); |
|
|
1764 | } |
|
|
1765 | else |
|
|
1766 | { |
|
|
1767 | new_draw_info (NDI_UNIQUE, 0, op, "This item has no custom name."); |
|
|
1768 | } |
|
|
1769 | } |
|
|
1770 | else |
|
|
1771 | { |
|
|
1772 | /* Set custom name */ |
|
|
1773 | item->custom_name = buf; |
|
|
1774 | |
|
|
1775 | new_draw_info_format (NDI_UNIQUE, 0, op, "Your %s will now be called %s.", query_base_name (item, item->nrof > 1 ? 1 : 0), buf); |
|
|
1776 | esrv_update_item (UPD_NAME, op, item); |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | return 1; |
|
|
1780 | } |
|
|