upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/irc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/irc.cpp')
-rw-r--r--src/irc.cpp271
1 files changed, 0 insertions, 271 deletions
diff --git a/src/irc.cpp b/src/irc.cpp
deleted file mode 100644
index ff82c66..0000000
--- a/src/irc.cpp
+++ /dev/null
@@ -1,271 +0,0 @@
1// Copyright (c) 2009 Satoshi Nakamoto
2// Distributed under the MIT/X11 software license, see the accompanying
3// file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5#include "headers.h"
6
7
8
9
10#pragma pack(1)
11struct ircaddr
12{
13 int ip;
14 short port;
15};
16
17string EncodeAddress(const CAddress& addr)
18{
19 struct ircaddr tmp;
20 tmp.ip = addr.ip;
21 tmp.port = addr.port;
22
23 vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
24 return string("u") + EncodeBase58Check(vch);
25}
26
27bool DecodeAddress(string str, CAddress& addr)
28{
29 vector<unsigned char> vch;
30 if (!DecodeBase58Check(str.substr(1), vch))
31 return false;
32
33 struct ircaddr tmp;
34 if (vch.size() != sizeof(tmp))
35 return false;
36 memcpy(&tmp, &vch[0], sizeof(tmp));
37
38 addr = CAddress(tmp.ip, tmp.port);
39 return true;
40}
41
42
43
44
45
46
47static bool Send(SOCKET hSocket, const char* pszSend)
48{
49 if (strstr(pszSend, "PONG") != pszSend)
50 printf("SENDING: %s\n", pszSend);
51 const char* psz = pszSend;
52 const char* pszEnd = psz + strlen(psz);
53 while (psz < pszEnd)
54 {
55 int ret = send(hSocket, psz, pszEnd - psz, 0);
56 if (ret < 0)
57 return false;
58 psz += ret;
59 }
60 return true;
61}
62
63bool RecvLine(SOCKET hSocket, string& strLine)
64{
65 strLine = "";
66 loop
67 {
68 char c;
69 int nBytes = recv(hSocket, &c, 1, 0);
70 if (nBytes > 0)
71 {
72 if (c == '\n')
73 continue;
74 if (c == '\r')
75 return true;
76 strLine += c;
77 }
78 else if (nBytes <= 0)
79 {
80 if (!strLine.empty())
81 return true;
82 // socket closed
83 printf("IRC socket closed\n");
84 return false;
85 }
86 else
87 {
88 // socket error
89 int nErr = WSAGetLastError();
90 if (nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
91 {
92 printf("IRC recv failed: %d\n", nErr);
93 return false;
94 }
95 }
96 }
97}
98
99bool RecvLineIRC(SOCKET hSocket, string& strLine)
100{
101 loop
102 {
103 bool fRet = RecvLine(hSocket, strLine);
104 if (fRet)
105 {
106 if (fShutdown)
107 return false;
108 vector<string> vWords;
109 ParseString(strLine, ' ', vWords);
110 if (vWords[0] == "PING")
111 {
112 strLine[1] = 'O';
113 strLine += '\r';
114 Send(hSocket, strLine.c_str());
115 continue;
116 }
117 }
118 return fRet;
119 }
120}
121
122bool RecvUntil(SOCKET hSocket, const char* psz1, const char* psz2=NULL, const char* psz3=NULL)
123{
124 loop
125 {
126 string strLine;
127 if (!RecvLineIRC(hSocket, strLine))
128 return false;
129 printf("IRC %s\n", strLine.c_str());
130 if (psz1 && strLine.find(psz1) != -1)
131 return true;
132 if (psz2 && strLine.find(psz2) != -1)
133 return true;
134 if (psz3 && strLine.find(psz3) != -1)
135 return true;
136 }
137}
138
139
140
141
142bool fRestartIRCSeed = false;
143
144void ThreadIRCSeed(void* parg)
145{
146 // IRC peer discovery disabled: the 2009 IRC network no longer exists (and
147 // what answers today crashes the parser). Replaced by Nostr relay
148 // discovery.
149 printf("ThreadIRCSeed: IRC peer discovery disabled (using Nostr)\n");
150 return;
151
152 loop
153 {
154 struct hostent* phostent = gethostbyname("chat.freenode.net");
155 CAddress addrConnect(*(u_long*)phostent->h_addr_list[0], htons(6667));
156
157 SOCKET hSocket;
158 if (!ConnectSocket(addrConnect, hSocket))
159 {
160 printf("IRC connect failed\n");
161 return;
162 }
163
164 if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname"))
165 {
166 closesocket(hSocket);
167 return;
168 }
169
170 string strMyName = EncodeAddress(addrLocalHost);
171
172 if (!addrLocalHost.IsRoutable())
173 strMyName = strprintf("x%u", GetRand(1000000000));
174
175 Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
176 Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());
177
178 if (!RecvUntil(hSocket, " 004 "))
179 {
180 closesocket(hSocket);
181 return;
182 }
183 Sleep(500);
184
185 Send(hSocket, "JOIN #bitcoin\r");
186 Send(hSocket, "WHO #bitcoin\r");
187
188 while (!fRestartIRCSeed)
189 {
190 string strLine;
191 if (fShutdown || !RecvLineIRC(hSocket, strLine))
192 {
193 closesocket(hSocket);
194 return;
195 }
196 if (strLine.empty() || strLine[0] != ':')
197 continue;
198 printf("IRC %s\n", strLine.c_str());
199
200 vector<string> vWords;
201 ParseString(strLine, ' ', vWords);
202 if (vWords.size() < 2)
203 continue;
204
205 char pszName[10000];
206 pszName[0] = '\0';
207
208 if (vWords[1] == "352" && vWords.size() >= 8)
209 {
210 // index 7 is limited to 16 characters
211 // could get full length name at index 10, but would be different from join messages
212 strcpy(pszName, vWords[7].c_str());
213 printf("GOT WHO: [%s] ", pszName);
214 }
215
216 if (vWords[1] == "JOIN")
217 {
218 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
219 strcpy(pszName, vWords[0].c_str() + 1);
220 if (strchr(pszName, '!'))
221 *strchr(pszName, '!') = '\0';
222 printf("GOT JOIN: [%s] ", pszName);
223 }
224
225 if (pszName[0] == 'u')
226 {
227 CAddress addr;
228 if (DecodeAddress(pszName, addr))
229 {
230 CAddrDB addrdb;
231 if (AddAddress(addrdb, addr))
232 printf("new ");
233 addr.print();
234 }
235 else
236 {
237 printf("decode failed\n");
238 }
239 }
240 }
241
242 fRestartIRCSeed = false;
243 closesocket(hSocket);
244 }
245}
246
247
248
249
250
251
252
253
254
255
256#ifdef TEST
257int main(int argc, char *argv[])
258{
259 WSADATA wsadata;
260 if (WSAStartup(MAKEWORD(2,2), &wsadata) != NO_ERROR)
261 {
262 printf("Error at WSAStartup()\n");
263 return false;
264 }
265
266 ThreadIRCSeed(NULL);
267
268 WSACleanup();
269 return 0;
270}
271#endif