| 118 #define TOC_CONNECT_STEPS 4 |
118 #define TOC_CONNECT_STEPS 4 |
| 119 |
119 |
| 120 static void toc_login_callback(gpointer, gint, GaimInputCondition); |
120 static void toc_login_callback(gpointer, gint, GaimInputCondition); |
| 121 static void toc_callback(gpointer, gint, GaimInputCondition); |
121 static void toc_callback(gpointer, gint, GaimInputCondition); |
| 122 |
122 |
| 123 /* The following were added for win32 port - Herman */ |
|
| 124 |
|
| 125 int toc_write(int fd, const void *buffer, int len) |
|
| 126 { |
|
| 127 #ifndef _WIN32 |
|
| 128 return write(fd, buffer, len); |
|
| 129 #else |
|
| 130 return send(fd, buffer, len, 0); |
|
| 131 #endif |
|
| 132 } |
|
| 133 |
|
| 134 int toc_read(int fd, void *buffer, int size) |
|
| 135 { |
|
| 136 #ifndef _WIN32 |
|
| 137 return read(fd, buffer, size); |
|
| 138 #else |
|
| 139 return recv(fd, buffer, size, 0); |
|
| 140 #endif |
|
| 141 } |
|
| 142 |
|
| 143 int toc_soc_close( int fd ) |
|
| 144 { |
|
| 145 #ifndef _WIN32 |
|
| 146 return close(fd); |
|
| 147 #else |
|
| 148 return closesocket(fd); |
|
| 149 #endif |
|
| 150 } |
|
| 151 |
|
| 152 |
|
| 153 /* ok. this function used to take username/password, and return 0 on success. |
123 /* ok. this function used to take username/password, and return 0 on success. |
| 154 * now, it takes username/password, and returns NULL on error or a new gaim_connection |
124 * now, it takes username/password, and returns NULL on error or a new gaim_connection |
| 155 * on success. */ |
125 * on success. */ |
| 156 static void toc_login(GaimAccount *account) |
126 static void toc_login(GaimAccount *account) |
| 157 { |
127 { |
| 212 else |
182 else |
| 213 strncpy(tdt->toc_ip, gaim_account_get_string(gc->account, "server", TOC_HOST), sizeof(tdt->toc_ip)); |
183 strncpy(tdt->toc_ip, gaim_account_get_string(gc->account, "server", TOC_HOST), sizeof(tdt->toc_ip)); |
| 214 |
184 |
| 215 gaim_debug(GAIM_DEBUG_INFO, "toc", |
185 gaim_debug(GAIM_DEBUG_INFO, "toc", |
| 216 "Client sends \"FLAPON\\r\\n\\r\\n\"\n"); |
186 "Client sends \"FLAPON\\r\\n\\r\\n\"\n"); |
| 217 if (toc_write(tdt->toc_fd, FLAPON, strlen(FLAPON)) < 0) { |
187 if (write(tdt->toc_fd, FLAPON, strlen(FLAPON)) < 0) { |
| 218 gaim_connection_error(gc, _("Disconnected.")); |
188 gaim_connection_error(gc, _("Disconnected.")); |
| 219 return; |
189 return; |
| 220 } |
190 } |
| 221 tdt->state = STATE_FLAPON; |
191 tdt->state = STATE_FLAPON; |
| 222 |
192 |
| 1671 |
1641 |
| 1672 if (ft->hdr.hdrtype != 0x202) { |
1642 if (ft->hdr.hdrtype != 0x202) { |
| 1673 char *buf; |
1643 char *buf; |
| 1674 frombase64(ft->cookie, &buf, NULL); |
1644 frombase64(ft->cookie, &buf, NULL); |
| 1675 |
1645 |
| 1676 toc_read(source, ft, 8); |
1646 read(source, ft, 8); |
| 1677 toc_read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
1647 read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
| 1678 debug_header(ft); |
1648 debug_header(ft); |
| 1679 |
1649 |
| 1680 ft->hdr.hdrtype = 0x202; |
1650 ft->hdr.hdrtype = 0x202; |
| 1681 memcpy(ft->hdr.bcookie, buf, 8); |
1651 memcpy(ft->hdr.bcookie, buf, 8); |
| 1682 g_free(buf); |
1652 g_free(buf); |
| 1683 ft->hdr.encrypt = 0; ft->hdr.compress = 0; |
1653 ft->hdr.encrypt = 0; ft->hdr.compress = 0; |
| 1684 debug_header(ft); |
1654 debug_header(ft); |
| 1685 toc_write(source, ft, 256); |
1655 write(source, ft, 256); |
| 1686 |
1656 |
| 1687 if (ft->files == 1) { |
1657 if (ft->files == 1) { |
| 1688 ft->file = fopen(ft->filename, "w"); |
1658 ft->file = fopen(ft->filename, "w"); |
| 1689 if (!ft->file) { |
1659 if (!ft->file) { |
| 1690 buf = g_strdup_printf(_("Could not open %s for writing!"), ft->filename); |
1660 buf = g_strdup_printf(_("Could not open %s for writing!"), ft->filename); |
| 1691 gaim_notify_error(ft->gc, NULL, buf, strerror(errno)); |
1661 gaim_notify_error(ft->gc, NULL, buf, strerror(errno)); |
| 1692 g_free(buf); |
1662 g_free(buf); |
| 1693 gaim_input_remove(ft->inpa); |
1663 gaim_input_remove(ft->inpa); |
| 1694 toc_soc_close(source); |
1664 close(source); |
| 1695 g_free(ft->filename); |
1665 g_free(ft->filename); |
| 1696 g_free(ft->user); |
1666 g_free(ft->user); |
| 1697 g_free(ft->ip); |
1667 g_free(ft->ip); |
| 1698 g_free(ft->cookie); |
1668 g_free(ft->cookie); |
| 1699 g_free(ft); |
1669 g_free(ft); |
| 1718 } |
1688 } |
| 1719 |
1689 |
| 1720 return; |
1690 return; |
| 1721 } |
1691 } |
| 1722 |
1692 |
| 1723 rt = toc_read(source, buf, MIN(ntohl(ft->hdr.size) - ft->recvsize, 1024)); |
1693 rt = read(source, buf, MIN(ntohl(ft->hdr.size) - ft->recvsize, 1024)); |
| 1724 if (rt < 0) { |
1694 if (rt < 0) { |
| 1725 gaim_notify_error(ft->gc, NULL, |
1695 gaim_notify_error(ft->gc, NULL, |
| 1726 _("File transfer failed; other side probably " |
1696 _("File transfer failed; other side probably " |
| 1727 "canceled."), NULL); |
1697 "canceled."), NULL); |
| 1728 gaim_input_remove(ft->inpa); |
1698 gaim_input_remove(ft->inpa); |
| 1729 toc_soc_close(source); |
1699 close(source); |
| 1730 g_free(ft->user); |
1700 g_free(ft->user); |
| 1731 g_free(ft->ip); |
1701 g_free(ft->ip); |
| 1732 g_free(ft->cookie); |
1702 g_free(ft->cookie); |
| 1733 if (ft->file) |
1703 if (ft->file) |
| 1734 fclose(ft->file); |
1704 fclose(ft->file); |
| 1744 ft->hdr.filesleft = htons(ntohs(ft->hdr.filesleft) - 1); |
1714 ft->hdr.filesleft = htons(ntohs(ft->hdr.filesleft) - 1); |
| 1745 ft->hdr.partsleft = htons(ntohs(ft->hdr.partsleft) - 1); |
1715 ft->hdr.partsleft = htons(ntohs(ft->hdr.partsleft) - 1); |
| 1746 ft->hdr.recvcsum = ft->hdr.checksum; /* uh... */ |
1716 ft->hdr.recvcsum = ft->hdr.checksum; /* uh... */ |
| 1747 ft->hdr.nrecvd = htons(ntohs(ft->hdr.nrecvd) + 1); |
1717 ft->hdr.nrecvd = htons(ntohs(ft->hdr.nrecvd) + 1); |
| 1748 ft->hdr.flags = 0; |
1718 ft->hdr.flags = 0; |
| 1749 toc_write(source, ft, 256); |
1719 write(source, ft, 256); |
| 1750 debug_header(ft); |
1720 debug_header(ft); |
| 1751 ft->recvsize = 0; |
1721 ft->recvsize = 0; |
| 1752 fclose(ft->file); |
1722 fclose(ft->file); |
| 1753 if (ft->hdr.filesleft == 0) { |
1723 if (ft->hdr.filesleft == 0) { |
| 1754 gaim_input_remove(ft->inpa); |
1724 gaim_input_remove(ft->inpa); |
| 1755 toc_soc_close(source); |
1725 close(source); |
| 1756 g_free(ft->filename); |
1726 g_free(ft->filename); |
| 1757 g_free(ft->user); |
1727 g_free(ft->user); |
| 1758 g_free(ft->ip); |
1728 g_free(ft->ip); |
| 1759 g_free(ft->cookie); |
1729 g_free(ft->cookie); |
| 1760 g_free(ft); |
1730 g_free(ft); |
| 1827 if (cond & GAIM_INPUT_WRITE) { |
1797 if (cond & GAIM_INPUT_WRITE) { |
| 1828 int remain = MIN(ntohl(ft->hdr.totsize) - ft->recvsize, 1024); |
1798 int remain = MIN(ntohl(ft->hdr.totsize) - ft->recvsize, 1024); |
| 1829 int i; |
1799 int i; |
| 1830 for (i = 0; i < remain; i++) |
1800 for (i = 0; i < remain; i++) |
| 1831 fscanf(ft->file, "%c", &buf[i]); |
1801 fscanf(ft->file, "%c", &buf[i]); |
| 1832 toc_write(source, buf, remain); |
1802 write(source, buf, remain); |
| 1833 ft->recvsize += remain; |
1803 ft->recvsize += remain; |
| 1834 if (ft->recvsize == ntohl(ft->hdr.totsize)) { |
1804 if (ft->recvsize == ntohl(ft->hdr.totsize)) { |
| 1835 gaim_input_remove(ft->inpa); |
1805 gaim_input_remove(ft->inpa); |
| 1836 ft->inpa = gaim_input_add(source, GAIM_INPUT_READ, |
1806 ft->inpa = gaim_input_add(source, GAIM_INPUT_READ, |
| 1837 toc_get_file_callback, ft); |
1807 toc_get_file_callback, ft); |
| 1842 if (ft->hdr.hdrtype == htons(0x1108)) { |
1812 if (ft->hdr.hdrtype == htons(0x1108)) { |
| 1843 struct tm *fortime; |
1813 struct tm *fortime; |
| 1844 struct stat st; |
1814 struct stat st; |
| 1845 char *basename; |
1815 char *basename; |
| 1846 |
1816 |
| 1847 toc_read(source, ft, 8); |
1817 read(source, ft, 8); |
| 1848 toc_read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
1818 read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
| 1849 debug_header(ft); |
1819 debug_header(ft); |
| 1850 |
1820 |
| 1851 stat(ft->filename, &st); |
1821 stat(ft->filename, &st); |
| 1852 fortime = localtime(&st.st_mtime); |
1822 fortime = localtime(&st.st_mtime); |
| 1853 basename = g_path_get_basename(ft->filename); |
1823 basename = g_path_get_basename(ft->filename); |
| 1854 g_snprintf(buf, sizeof(buf), "%2d/%2d/%4d %2d:%2d %8ld %s\r\n", |
1824 g_snprintf(buf, sizeof(buf), "%2d/%2d/%4d %2d:%2d %8ld %s\r\n", |
| 1855 fortime->tm_mon + 1, fortime->tm_mday, fortime->tm_year + 1900, |
1825 fortime->tm_mon + 1, fortime->tm_mday, fortime->tm_year + 1900, |
| 1856 fortime->tm_hour + 1, fortime->tm_min + 1, (long)st.st_size, |
1826 fortime->tm_hour + 1, fortime->tm_min + 1, (long)st.st_size, |
| 1857 basename); |
1827 basename); |
| 1858 toc_write(source, buf, ntohl(ft->hdr.size)); |
1828 write(source, buf, ntohl(ft->hdr.size)); |
| 1859 g_free(basename); |
1829 g_free(basename); |
| 1860 return; |
1830 return; |
| 1861 } |
1831 } |
| 1862 |
1832 |
| 1863 if (ft->hdr.hdrtype == htons(0x1209)) { |
1833 if (ft->hdr.hdrtype == htons(0x1209)) { |
| 1864 toc_read(source, ft, 8); |
1834 read(source, ft, 8); |
| 1865 toc_read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
1835 read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
| 1866 debug_header(ft); |
1836 debug_header(ft); |
| 1867 return; |
1837 return; |
| 1868 } |
1838 } |
| 1869 |
1839 |
| 1870 if (ft->hdr.hdrtype == htons(0x120b)) { |
1840 if (ft->hdr.hdrtype == htons(0x120b)) { |
| 1871 toc_read(source, ft, 8); |
1841 read(source, ft, 8); |
| 1872 toc_read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
1842 read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
| 1873 debug_header(ft); |
1843 debug_header(ft); |
| 1874 |
1844 |
| 1875 if (ft->hdr.hdrtype != htons(0x120c)) { |
1845 if (ft->hdr.hdrtype != htons(0x120c)) { |
| 1876 g_snprintf(buf, sizeof(buf), "%s decided to cancel the transfer", ft->user); |
1846 g_snprintf(buf, sizeof(buf), "%s decided to cancel the transfer", ft->user); |
| 1877 gaim_notify_error(ft->gc, NULL, buf, NULL); |
1847 gaim_notify_error(ft->gc, NULL, buf, NULL); |
| 1878 gaim_input_remove(ft->inpa); |
1848 gaim_input_remove(ft->inpa); |
| 1879 toc_soc_close(source); |
1849 close(source); |
| 1880 g_free(ft->filename); |
1850 g_free(ft->filename); |
| 1881 g_free(ft->user); |
1851 g_free(ft->user); |
| 1882 g_free(ft->ip); |
1852 g_free(ft->ip); |
| 1883 g_free(ft->cookie); |
1853 g_free(ft->cookie); |
| 1884 if (ft->file) |
1854 if (ft->file) |
| 1888 } |
1858 } |
| 1889 |
1859 |
| 1890 ft->hdr.hdrtype = 0x0101; |
1860 ft->hdr.hdrtype = 0x0101; |
| 1891 ft->hdr.totfiles = htons(1); ft->hdr.filesleft = htons(1); |
1861 ft->hdr.totfiles = htons(1); ft->hdr.filesleft = htons(1); |
| 1892 ft->hdr.flags = 0x20; |
1862 ft->hdr.flags = 0x20; |
| 1893 toc_write(source, ft, 256); |
1863 write(source, ft, 256); |
| 1894 return; |
1864 return; |
| 1895 } |
1865 } |
| 1896 |
1866 |
| 1897 if (ft->hdr.hdrtype == 0x0101) { |
1867 if (ft->hdr.hdrtype == 0x0101) { |
| 1898 toc_read(source, ft, 8); |
1868 read(source, ft, 8); |
| 1899 toc_read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
1869 read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
| 1900 debug_header(ft); |
1870 debug_header(ft); |
| 1901 |
1871 |
| 1902 gaim_input_remove(ft->inpa); |
1872 gaim_input_remove(ft->inpa); |
| 1903 ft->inpa = gaim_input_add(source, GAIM_INPUT_WRITE, |
1873 ft->inpa = gaim_input_add(source, GAIM_INPUT_WRITE, |
| 1904 toc_get_file_callback, ft); |
1874 toc_get_file_callback, ft); |
| 1905 return; |
1875 return; |
| 1906 } |
1876 } |
| 1907 |
1877 |
| 1908 if (ft->hdr.hdrtype == 0x0202) { |
1878 if (ft->hdr.hdrtype == 0x0202) { |
| 1909 toc_read(source, ft, 8); |
1879 read(source, ft, 8); |
| 1910 toc_read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
1880 read(source, &ft->hdr.bcookie, MIN(256 - 8, ntohs(ft->hdr.hdrlen) - 8)); |
| 1911 debug_header(ft); |
1881 debug_header(ft); |
| 1912 |
1882 |
| 1913 gaim_input_remove(ft->inpa); |
1883 gaim_input_remove(ft->inpa); |
| 1914 toc_soc_close(source); |
1884 close(source); |
| 1915 g_free(ft->filename); |
1885 g_free(ft->filename); |
| 1916 g_free(ft->user); |
1886 g_free(ft->user); |
| 1917 g_free(ft->ip); |
1887 g_free(ft->ip); |
| 1918 g_free(ft->cookie); |
1888 g_free(ft->cookie); |
| 1919 if (ft->file) |
1889 if (ft->file) |
| 1961 g_snprintf(hdr->idstring, 32, "OFT_Windows ICBMFT V1.1 32"); |
1931 g_snprintf(hdr->idstring, 32, "OFT_Windows ICBMFT V1.1 32"); |
| 1962 hdr->flags = 0x02; |
1932 hdr->flags = 0x02; |
| 1963 hdr->lnameoffset = 0x1A; |
1933 hdr->lnameoffset = 0x1A; |
| 1964 hdr->lsizeoffset = 0x10; |
1934 hdr->lsizeoffset = 0x10; |
| 1965 g_snprintf(hdr->name, 64, "listing.txt"); |
1935 g_snprintf(hdr->name, 64, "listing.txt"); |
| 1966 if (toc_write(src, hdr, 256) < 0) { |
1936 if (write(src, hdr, 256) < 0) { |
| 1967 gaim_notify_error(ft->gc, NULL, |
1937 gaim_notify_error(ft->gc, NULL, |
| 1968 _("Could not write file header. The file will " |
1938 _("Could not write file header. The file will " |
| 1969 "not be transferred."), NULL); |
1939 "not be transferred."), NULL); |
| 1970 fclose(ft->file); |
1940 fclose(ft->file); |
| 1971 g_free(ft->filename); |
1941 g_free(ft->filename); |