ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/branches/1.1.x/src/irc.c
Revision: 9481
Committed: Sat Jul 4 19:07:07 2020 UTC (6 years ago) by michael
Content type: text/x-csrc
File size: 21967 byte(s)
Log Message:
- irc.c:irc_init(): remove SSLeay_add_ssl_algorithms() call not needed

File Contents

# User Rev Content
1 michael 5052 /*
2 michael 5351 * Copyright (c) 2002-2003 Erik Fears
3 michael 9103 * Copyright (c) 2014-2020 ircd-hybrid development team
4 michael 5052 *
5 michael 5351 * This program is free software; you can redistribute it and/or modify
6     * it under the terms of the GNU General Public License as published by
7     * the Free Software Foundation; either version 2 of the License, or
8     * (at your option) any later version.
9 michael 5052 *
10 michael 5351 * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14 michael 5052 *
15 michael 5351 * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18     * USA
19 michael 5052 */
20    
21     #include "setup.h"
22    
23     #include <stdio.h>
24     #include <unistd.h>
25 michael 5207 #include <stdlib.h>
26     #include <string.h>
27 michael 5052 #include <sys/types.h>
28     #include <sys/socket.h>
29 michael 5363 #include <netdb.h>
30 michael 5052 #include <netinet/in.h>
31     #include <arpa/inet.h>
32 michael 5226 #include <poll.h>
33 michael 5266 #include <time.h>
34 michael 5052 #include <errno.h>
35     #include <stdarg.h>
36     #include <regex.h>
37 michael 6215 #include <assert.h>
38 michael 9477 #ifdef HAVE_LIBCRYPTO
39     #include <openssl/ssl.h>
40     #include <openssl/x509v3.h>
41     #include <openssl/err.h>
42     #endif
43 michael 5052
44     #include "config.h"
45     #include "irc.h"
46     #include "log.h"
47     #include "opercmd.h"
48     #include "scan.h"
49     #include "dnsbl.h"
50     #include "stats.h"
51     #include "options.h"
52     #include "match.h"
53     #include "compat.h"
54     #include "negcache.h"
55 michael 5333 #include "memory.h"
56 michael 5052 #include "main.h"
57 michael 5964 #include "serno.h"
58 michael 5052
59    
60     /*
61     * Certain variables we don't want to allocate memory for over and over
62     * again so global scope is given.
63     */
64 michael 5695 static char IRC_RAW[MSGLENMAX]; /* Buffer to read data into */
65     static unsigned int IRC_RAW_LEN; /* Position of IRC_RAW */
66 michael 6215 static int IRC_FD = -1; /* File descriptor for IRC client */
67 michael 5052
68 michael 5695 static struct sockaddr_storage IRC_SVR; /* Sock Address Struct for IRC server */
69 michael 5856 static socklen_t svr_addrlen;
70 michael 5695 static time_t IRC_LAST; /* Last full line of data from irc server */
71     static time_t IRC_LASTRECONNECT; /* Time of last reconnection */
72 michael 5052
73 michael 9477 #ifdef HAVE_LIBCRYPTO
74     static SSL_CTX *ssl_ctx;
75     static SSL *ssl_handle;
76     #endif
77 michael 5052
78 michael 9477
79 michael 5052 /* get_channel
80     *
81     * Check if a channel is defined in our conf. If so return
82     * a pointer to it.
83     *
84     * Parameters:
85     * channel: channel to search conf for
86     *
87     * Return: Pointer to ChannelConf containing the channel
88     */
89 michael 5678 static const struct ChannelConf *
90 michael 8138 get_channel(const char *name)
91 michael 5052 {
92 michael 5114 node_t *node;
93 michael 5052
94 michael 8580 LIST_FOREACH(node, IRCItem.channels.head)
95 michael 5114 {
96 michael 5116 struct ChannelConf *item = node->data;
97 michael 5052
98 michael 8138 if (strcasecmp(item->name, name) == 0)
99 michael 5114 return item;
100     }
101 michael 5052
102 michael 5114 return NULL;
103 michael 5052 }
104    
105     /* m_perform
106     *
107     * actions to perform on IRC connection
108     *
109     * Parameters:
110     * parv[0] = source
111     * parv[1] = PING
112     * parv[2] = PING TS/Package
113     *
114     * source_p: UserInfo struct of the source user, or NULL if
115     * the source (parv[0]) is a server.
116     */
117 michael 5114 static void
118 michael 6198 m_perform(char *parv[], unsigned int parc, const char *msg, const char *source_p)
119 michael 5052 {
120 michael 5114 node_t *node;
121 michael 5052
122 michael 8580 log_printf("IRC -> Connected to %s/%d", IRCItem.server, IRCItem.port);
123 michael 5052
124 michael 5114 /* Identify to nickserv if needed */
125 michael 8580 if (!EmptyString(IRCItem.nickserv))
126     irc_send("%s", IRCItem.nickserv);
127 michael 5052
128 michael 5114 /* Oper */
129 michael 8580 irc_send("OPER %s", IRCItem.oper);
130 michael 5052
131 michael 5114 /* Set modes */
132 michael 8580 irc_send("MODE %s %s", IRCItem.nick, IRCItem.mode);
133 michael 5052
134 michael 5114 /* Set Away */
135 michael 8580 if (!EmptyString(IRCItem.away))
136     irc_send("AWAY :%s", IRCItem.away);
137 michael 5052
138 michael 5114 /* Perform */
139 michael 8580 LIST_FOREACH(node, IRCItem.performs.head)
140 michael 5114 irc_send("%s", node->data);
141 michael 5052
142 michael 5114 /* Join all listed channels. */
143 michael 8580 LIST_FOREACH(node, IRCItem.channels.head)
144 michael 5114 {
145 michael 5279 const struct ChannelConf *channel = node->data;
146 michael 5052
147 michael 5114 if (EmptyString(channel->name))
148     continue;
149 michael 5052
150 michael 5114 if (!EmptyString(channel->key))
151     irc_send("JOIN %s %s", channel->name, channel->key);
152     else
153     irc_send("JOIN %s", channel->name);
154     }
155 michael 5052 }
156    
157     /* m_ping
158     *
159     * parv[0] = source
160     * parv[1] = PING
161     * parv[2] = PING TS/Package
162     *
163     * source_p: UserInfo struct of the source user, or NULL if
164     * the source (parv[0]) is a server.
165     */
166 michael 5114 static void
167 michael 6198 m_ping(char *parv[], unsigned int parc, const char *msg, const char *source_p)
168 michael 5052 {
169 michael 5114 if (parc < 3)
170     return;
171 michael 5052
172 michael 5114 if (OPT_DEBUG >= 2)
173     log_printf("IRC -> PING? PONG!");
174 michael 5052
175 michael 5114 irc_send("PONG %s", parv[2]);
176 michael 5052 }
177    
178     /* m_invite
179     *
180     * parv[0] = source
181     * parv[1] = INVITE
182     * parv[2] = target
183     * parv[3] = channel
184     *
185     * source_p: UserInfo struct of the source user, or NULL if
186     * the source (parv[0]) is a server.
187     */
188 michael 5114 static void
189 michael 6198 m_invite(char *parv[], unsigned int parc, const char *msg, const char *source_p)
190 michael 5052 {
191 michael 5114 if (parc < 4)
192     return;
193 michael 5052
194 michael 5114 log_printf("IRC -> Invited to %s by %s", parv[3], parv[0]);
195 michael 5052
196 michael 9329 const struct ChannelConf *channel = get_channel(parv[3]);
197     if (channel == NULL)
198 michael 5114 return;
199    
200     irc_send("JOIN %s %s", channel->name, channel->key);
201 michael 5052 }
202    
203 michael 6167 /* m_ctcp
204     * parv[0] = source
205     * parv[1] = PRIVMSG
206     * parv[2] = target (channel or user)
207     * parv[3] = message
208     *
209     * source_p: UserInfo struct of the source user, or NULL if
210     * the source (parv[0]) is a server.
211     */
212     static void
213 michael 6198 m_ctcp(char *parv[], unsigned int parc, const char *msg, const char *source_p)
214 michael 6167 {
215     if (strncasecmp(parv[3], "\001VERSION\001", 9) == 0)
216     irc_send("NOTICE %s :\001VERSION Hybrid Open Proxy Monitor %s(%s)\001",
217 michael 6198 source_p, VERSION, SERIALNUM);
218 michael 6167 }
219    
220 michael 5052 /* m_privmsg
221     *
222     * parv[0] = source
223     * parv[1] = PRIVMSG
224     * parv[2] = target (channel or user)
225     * parv[3] = message
226     *
227     * source_p: UserInfo struct of the source user, or NULL if
228     * the source (parv[0]) is a server.
229     */
230 michael 5114 static void
231 michael 6198 m_privmsg(char *parv[], unsigned int parc, const char *msg, const char *source_p)
232 michael 5052 {
233 michael 5114 if (source_p == NULL)
234     return;
235 michael 5052
236 michael 5114 if (parc < 4)
237     return;
238 michael 5052
239 michael 5114 /* CTCP */
240 michael 6011 if (*parv[3] == '\001')
241 michael 6005 {
242 michael 5114 m_ctcp(parv, parc, msg, source_p);
243 michael 6005 return;
244     }
245 michael 5052
246 michael 5114 /* Only interested in privmsg to channels */
247 michael 6011 if (*parv[2] != '#' && *parv[2] != '&')
248 michael 5114 return;
249 michael 5052
250 michael 5114 /* Get a target */
251 michael 9329 const struct ChannelConf *channel = get_channel(parv[2]);
252     if (channel == NULL)
253 michael 5114 return;
254 michael 5052
255 michael 8125 int hit = strncasecmp(parv[3], "!all ", 5) == 0;
256     if (hit == 0)
257     {
258 michael 8580 size_t nick_len = strlen(IRCItem.nick);
259 michael 5052
260 michael 8580 if (strncasecmp(parv[3], IRCItem.nick, nick_len) == 0)
261 michael 8225 hit = *(parv[3] + nick_len) == ' ' ||
262     *(parv[3] + nick_len) == ',' ||
263     *(parv[3] + nick_len) == ':';
264 michael 8125 }
265 michael 5052
266 michael 8125 if (hit)
267 michael 5114 /* XXX command_parse will alter parv[3]. */
268 michael 8194 command_parse(parv[3], channel->name, source_p);
269 michael 5114 }
270 michael 5052
271     /* m_notice
272     *
273     * parv[0] = source
274     * parv[1] = NOTICE
275     * parv[2] = target
276     * parv[3] = message
277     *
278     *
279     * source_p: UserInfo struct of the source user, or NULL if
280     * the source (parv[0]) is a server.
281     */
282 michael 5114 static void
283 michael 6198 m_notice(char *parv[], unsigned int parc, const char *msg, const char *source_p)
284 michael 5052 {
285 michael 5114 static regex_t *preg = NULL;
286     regmatch_t pmatch[5];
287 michael 5279 int errnum;
288 michael 5338 const char *user[4];
289 michael 5405 const node_t *node;
290 michael 5052
291 michael 5114 /* Not interested in notices from users */
292     if (source_p)
293     return;
294 michael 5052
295 michael 5322 if (parc < 4)
296     return;
297    
298 michael 5114 /* Compile the regular expression if it has not been already */
299     if (preg == NULL)
300     {
301 michael 6149 preg = xcalloc(sizeof(*preg));
302 michael 5052
303 michael 8580 if ((errnum = regcomp(preg, IRCItem.connregex, REG_ICASE | REG_EXTENDED)))
304 michael 5114 {
305 michael 5290 char errmsg[256];
306    
307     regerror(errnum, preg, errmsg, sizeof(errmsg));
308 michael 6014 log_printf("IRC REGEX -> Error when compiling regular expression: %s", errmsg);
309 michael 5052
310 michael 5426 xfree(preg);
311 michael 5114 preg = NULL;
312 michael 5052 return;
313 michael 5114 }
314     }
315 michael 5052
316 michael 5114 /* Match the expression against the possible connection notice */
317     if (regexec(preg, parv[3], 5, pmatch, 0))
318     return;
319 michael 5052
320 michael 5114 if (OPT_DEBUG > 0)
321     log_printf("IRC REGEX -> Regular expression caught connection notice. Parsing.");
322 michael 5052
323 michael 5114 if (pmatch[4].rm_so == -1)
324     {
325     log_printf("IRC REGEX -> pmatch[4].rm_so is -1 while parsing??? Aborting.");
326     return;
327     }
328 michael 5052
329 michael 5114 /*
330     * Offsets for data in the connection notice:
331     *
332     * NICKNAME: pmatch[1].rm_so TO pmatch[1].rm_eo
333     * USERNAME: pmatch[2].rm_so TO pmatch[2].rm_eo
334     * HOSTNAME: pmatch[3].rm_so TO pmatch[3].rm_eo
335     * IP : pmatch[4].rm_so TO pmatch[4].rm_eo
336     */
337 michael 5279 for (unsigned int i = 0; i < 4; ++i)
338 michael 5114 {
339     user[i] = (parv[3] + pmatch[i + 1].rm_so);
340     *(parv[3] + pmatch[i + 1].rm_eo) = '\0';
341     }
342 michael 5052
343 michael 5114 if (OPT_DEBUG > 0)
344     log_printf("IRC REGEX -> Parsed %s!%s@%s [%s] from connection notice.",
345     user[0], user[1], user[2], user[3]);
346 michael 5052
347 michael 8580 LIST_FOREACH(node, IRCItem.notices.head)
348 michael 5405 irc_send("NOTICE %s :%s", user[0], node->data);
349    
350 michael 5114 /* Pass this information off to scan.c */
351     scan_connect(user, msg);
352 michael 5052
353 michael 5114 /* Record the connect for stats purposes */
354     stats_connect();
355 michael 5052 }
356    
357     /* m_userhost
358     *
359     * parv[0] = source
360     * parv[1] = USERHOST
361 michael 5069 * parv[2] = target (hopm)
362 michael 5052 * parv[3] = :nick=(flags)user@host
363     *
364     *
365     * source_p: UserInfo struct of the source user, or NULL if
366     * the source (parv[0]) is a server.
367     */
368 michael 5114 static void
369 michael 6198 m_userhost(char *parv[], unsigned int parc, const char *msg, const char *source_p)
370 michael 5052 {
371 michael 5114 if (parc < 4)
372     return;
373 michael 5052
374 michael 5114 command_userhost(parv[3]);
375 michael 5052 }
376    
377     /* m_cannot_join
378     *
379     * parv[0] = source
380     * parv[1] = numeric
381 michael 5069 * parv[2] = target (hopm)
382 michael 5052 * parv[3] = channel
383     * parv[4] = error text
384     */
385 michael 5114 static void
386 michael 6198 m_cannot_join(char *parv[], unsigned int parc, const char *msg, const char *source_p)
387 michael 5052 {
388 michael 5114 if (parc < 5)
389     return;
390 michael 5052
391 michael 5114 /* Is it one of our channels? */
392 michael 9329 const struct ChannelConf *channel = get_channel(parv[3]);
393     if (channel == NULL)
394 michael 5114 return;
395 michael 5052
396 michael 5114 if (EmptyString(channel->invite))
397     return;
398 michael 5052
399 michael 5114 irc_send("%s", channel->invite);
400 michael 5052 }
401    
402     /* m_kill
403     *
404     * parv[0] = source
405     * parv[1] = numeric
406 michael 5069 * parv[2] = target (hopm)
407 michael 5052 * parv[3] = channel
408     * parv[4] = error text
409     */
410 michael 5114 static void
411 michael 6198 m_kill(char *parv[], unsigned int parc, const char *msg, const char *source_p)
412 michael 5052 {
413 michael 5084 /* Restart hopm to rehash */
414     main_restart();
415 michael 5052 }
416 michael 6167
417     /* userinfo_create
418     *
419     * Parse a nick!user@host into a UserInfo struct
420     * and return a pointer to the new struct.
421     *
422     * Parameters:
423     * source: nick!user@host to parse
424     *
425     * Return:
426     * pointer to new UserInfo struct, or NULL if parsing failed
427     */
428 michael 6198 static const char *
429 michael 6167 userinfo_create(const char *source)
430     {
431 michael 6198 static char name[MSGLENMAX];
432     unsigned int has_user = 0;
433     unsigned int has_host = 0;
434 michael 6167
435 michael 6198 strlcpy(name, source, sizeof(name));
436 michael 6167
437 michael 6198 for (char *p = name; *p; ++p)
438 michael 6167 {
439     if (*p == '!')
440     {
441     *p = '\0';
442 michael 6198 ++has_user;
443 michael 6167 continue;
444     }
445    
446 michael 6198 if (*p == '@' && has_user)
447 michael 6167 {
448 michael 6198 ++has_host;
449 michael 6167 continue;
450     }
451     }
452    
453 michael 6198 if (has_user == 1 && has_host == 1)
454     return name;
455     return NULL;
456 michael 6167 };
457    
458     /* irc_init
459     *
460     * Resolve IRC host and perform other initialization.
461     *
462     * Parameters:
463     * None
464     *
465     * Return:
466     * None
467     */
468     static void
469     irc_init(void)
470     {
471 michael 9329 const void *address;
472 michael 6167
473 michael 6215 assert(IRC_FD == -1);
474 michael 6167
475     memset(&IRC_SVR, 0, sizeof(IRC_SVR));
476    
477     /* Resolve IRC host. */
478 michael 8580 if ((address = firedns_resolveip6(IRCItem.server)))
479 michael 6167 {
480     struct sockaddr_in6 *in = (struct sockaddr_in6 *)&IRC_SVR;
481    
482 michael 8138 svr_addrlen = sizeof(*in);
483 michael 6167 IRC_SVR.ss_family = AF_INET6;
484 michael 8580 in->sin6_port = htons(IRCItem.port);
485 michael 6167 memcpy(&in->sin6_addr, address, sizeof(in->sin6_addr));
486     }
487 michael 8580 else if ((address = firedns_resolveip4(IRCItem.server)))
488 michael 6167 {
489     struct sockaddr_in *in = (struct sockaddr_in *)&IRC_SVR;
490    
491 michael 8138 svr_addrlen = sizeof(*in);
492 michael 6167 IRC_SVR.ss_family = AF_INET;
493 michael 8580 in->sin_port = htons(IRCItem.port);
494 michael 6167 memcpy(&in->sin_addr, address, sizeof(in->sin_addr));
495     }
496     else
497     {
498 michael 8580 log_printf("IRC -> firedns_resolveip(\"%s\"): %s", IRCItem.server,
499 michael 6167 firedns_strerror(firedns_errno));
500     exit(EXIT_FAILURE);
501     }
502    
503     /* Request file desc for IRC client socket */
504     IRC_FD = socket(IRC_SVR.ss_family, SOCK_STREAM, 0);
505    
506     if (IRC_FD == -1)
507     {
508     log_printf("IRC -> socket(): error creating socket: %s", strerror(errno));
509     exit(EXIT_FAILURE);
510     }
511    
512     /* Bind */
513 michael 8580 if (!EmptyString(IRCItem.vhost))
514 michael 6167 {
515     struct addrinfo hints, *res;
516    
517     memset(&hints, 0, sizeof(hints));
518    
519     hints.ai_family = AF_UNSPEC;
520     hints.ai_socktype = SOCK_STREAM;
521 michael 8123 hints.ai_flags = AI_NUMERICHOST;
522 michael 6167
523 michael 9329 int n = getaddrinfo(IRCItem.vhost, NULL, &hints, &res);
524     if (n)
525 michael 6167 {
526 michael 8580 log_printf("IRC -> error binding to %s: %s", IRCItem.vhost, gai_strerror(n));
527 michael 6167 exit(EXIT_FAILURE);
528     }
529     else if (bind(IRC_FD, res->ai_addr, res->ai_addrlen))
530     {
531 michael 8580 log_printf("IRC -> error binding to %s: %s", IRCItem.vhost, strerror(errno));
532 michael 6167 exit(EXIT_FAILURE);
533     }
534    
535     freeaddrinfo(res);
536     }
537 michael 9477
538     if (IRCItem.tls)
539     {
540     #ifdef HAVE_LIBCRYPTO
541     /* Initialize SSL */
542     static int tls_init = 0;
543     if (tls_init == 0)
544     {
545     tls_init = 1;
546    
547     ssl_ctx = SSL_CTX_new(SSLv23_client_method());
548     if (ssl_ctx == NULL)
549     {
550     log_printf("IRC -> unable to create SSL context");
551     exit(EXIT_FAILURE);
552     }
553    
554     SSL_CTX_set_default_verify_paths(ssl_ctx);
555     }
556    
557     ssl_handle = SSL_new(ssl_ctx);
558     SSL_set_fd(ssl_handle, IRC_FD);
559     SSL_set_hostflags(ssl_handle, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
560    
561     if (!SSL_set1_host(ssl_handle, IRCItem.server))
562     {
563     log_printf("IRC -> unable to set expected DNS hostname");
564     /* OpenSSL is unable to verify the server hostname at this point, so we exit. */
565     exit(EXIT_FAILURE);
566     }
567    
568     SSL_set_verify(ssl_handle, SSL_VERIFY_PEER, NULL);
569     #else
570     log_printf("IRC -> HOPM is not compiled with OpenSSL support");
571     exit(EXIT_FAILURE);
572     #endif
573     }
574 michael 6167 }
575    
576     /* irc_reconnect
577     *
578     * Close connection to IRC server.
579     *
580     * Parameters: NONE
581     *
582     * Return: NONE
583     */
584     static void
585     irc_reconnect(void)
586     {
587     time_t present;
588    
589     time(&present);
590    
591 michael 8580 /* Only try to reconnect every IRCItem.reconnectinterval seconds */
592     if ((present - IRC_LASTRECONNECT) < IRCItem.reconnectinterval)
593 michael 6167 {
594     /* Sleep to avoid excessive CPU */
595     sleep(1);
596     return;
597     }
598    
599     time(&IRC_LASTRECONNECT);
600    
601 michael 6215 if (IRC_FD > -1)
602     {
603 michael 9477 #ifdef HAVE_LIBCRYPTO
604     if (ssl_handle)
605     {
606     SSL_shutdown(ssl_handle);
607     SSL_free(ssl_handle);
608     ssl_handle = NULL;
609     }
610     #endif
611    
612 michael 6167 close(IRC_FD);
613 michael 6215 IRC_FD = -1; /* Set IRC_FD -1 for reconnection on next irc_cycle(). */
614     }
615 michael 6167
616 michael 8580 log_printf("IRC -> Connection to (%s) failed, reconnecting.", IRCItem.server);
617 michael 6167 }
618    
619     /* irc_connect
620     *
621     * Connect to IRC server.
622     * XXX: FD allocation done here
623     *
624     * Parameters: NONE
625     * Return: NONE
626     */
627     static void
628     irc_connect(void)
629     {
630     /* Connect to IRC server as client. */
631     if (connect(IRC_FD, (struct sockaddr *)&IRC_SVR, svr_addrlen) == -1)
632     {
633     log_printf("IRC -> connect(): error connecting to %s: %s",
634 michael 8580 IRCItem.server, strerror(errno));
635 michael 6167
636     if (errno == EISCONN /* Already connected */ || errno == EALREADY /* Previous attempt not complete */)
637     return;
638    
639     /* Try to connect again */
640     irc_reconnect();
641     return;
642     }
643    
644 michael 9477 #ifdef HAVE_LIBCRYPTO
645     if (ssl_handle)
646     {
647     int ret = SSL_connect(ssl_handle);
648     if (ret != 1)
649     {
650     const char *error = ERR_error_string(ERR_get_error(), NULL);
651     log_printf("IRC -> connect(): error performing TLS handshake with %s: %s",
652     IRCItem.server, error);
653    
654     /* Try to connect again */
655     irc_reconnect();
656     return;
657     }
658     }
659     #endif
660    
661 michael 8580 irc_send("NICK %s", IRCItem.nick);
662 michael 6167
663 michael 8580 if (!EmptyString(IRCItem.password))
664     irc_send("PASS %s", IRCItem.password);
665 michael 6167
666     irc_send("USER %s %s %s :%s",
667 michael 8580 IRCItem.username,
668     IRCItem.username,
669     IRCItem.username,
670     IRCItem.realname);
671 michael 6167 time(&IRC_LAST);
672     }
673    
674     /* irc_parse
675     *
676     * irc_parse is called by irc_read when a full line of data
677     * is ready to be parsed.
678     *
679     * Parameters: NONE
680     * Return: NONE
681     */
682     static void
683     irc_parse(void)
684     {
685     char *pos;
686    
687     /*
688     * parv stores the parsed token, parc is the count of the parsed
689     * tokens
690     *
691     * parv[0] is ALWAYS the source, and is the server name of the
692     * source did not exist
693     */
694     char *parv[17];
695     unsigned int parc = 1;
696     char msg[MSGLENMAX]; /* Temporarily stores IRC msg to pass to handlers */
697    
698 michael 8177 struct CommandHash
699     {
700     const char *command;
701     void (*handler)(char *[], unsigned int, const char *, const char *);
702     };
703    
704 michael 6167 /*
705     * Table should be ordered with most occuring (or priority)
706     * commands at the top of the list.
707     */
708     static const struct CommandHash COMMAND_TABLE[] =
709     {
710 michael 8156 { .command = "NOTICE", .handler = m_notice },
711     { .command = "PRIVMSG", .handler = m_privmsg },
712     { .command = "PING", .handler = m_ping },
713     { .command = "INVITE", .handler = m_invite },
714     { .command = "001", .handler = m_perform },
715     { .command = "302", .handler = m_userhost },
716     { .command = "471", .handler = m_cannot_join },
717     { .command = "473", .handler = m_cannot_join },
718     { .command = "474", .handler = m_cannot_join },
719     { .command = "475", .handler = m_cannot_join },
720     { .command = "KILL", .handler = m_kill },
721     { .command = NULL }
722 michael 6167 };
723    
724     if (IRC_RAW_LEN == 0)
725     return;
726    
727     if (OPT_DEBUG >= 2)
728     log_printf("IRC READ -> %s", IRC_RAW);
729    
730     time(&IRC_LAST);
731    
732     /* Store a copy of IRC_RAW for the handlers (for functions that need PROOF) */
733     strlcpy(msg, IRC_RAW, sizeof(msg));
734    
735     /* parv[0] is always the source */
736     if (IRC_RAW[0] == ':')
737     parv[0] = IRC_RAW + 1;
738     else
739     {
740 michael 8580 parv[0] = IRCItem.server;
741 michael 6167 parv[parc++] = IRC_RAW;
742     }
743    
744     pos = IRC_RAW;
745    
746     while ((pos = strchr(pos, ' ')) && parc <= 17)
747     {
748     /* Avoid excessive spaces and end of IRC_RAW */
749     if (*(pos + 1) == ' ' || *(pos + 1) == '\0')
750     {
751     pos++;
752     continue;
753     }
754    
755     /* Anything after a : is considered the final string of the message */
756     if (*(pos + 1) == ':')
757     {
758     parv[parc++] = pos + 2;
759     *pos = '\0';
760     break;
761     }
762    
763     /*
764     * Set the next parv at this position and replace the space with a
765     * \0 for the previous parv
766     */
767     parv[parc++] = pos + 1;
768     *pos = '\0';
769     pos++;
770     }
771    
772     /*
773     * Determine which command this is from the command table
774     * and let the handler for that command take control
775     */
776     for (const struct CommandHash *cmd = COMMAND_TABLE; cmd->command; ++cmd)
777     {
778     if (strcasecmp(cmd->command, parv[1]) == 0)
779     {
780 michael 6198 cmd->handler(parv, parc, msg, userinfo_create(parv[0]));
781 michael 6167 break;
782     }
783     }
784     }
785    
786     /* irc_read
787     *
788     * irc_read is called by irc_cycle when new data is ready to be
789     * read from the irc server.
790     *
791     * Parameters: NONE
792     * Return: NONE
793     */
794     static void
795     irc_read(void)
796     {
797 michael 6265 ssize_t len;
798 michael 6167 char c;
799    
800 michael 9477 while (1)
801 michael 6167 {
802 michael 9477 #ifdef HAVE_LIBCRYPTO
803     if (ssl_handle)
804     len = SSL_read(ssl_handle, &c, 1);
805     else
806     #endif
807     len = recv(IRC_FD, &c, 1, 0);
808    
809     if (len <= 0)
810     break;
811 michael 6167 if (c == '\r')
812     continue;
813    
814     if (c == '\n')
815     {
816     /* Null string. */
817     IRC_RAW[IRC_RAW_LEN] = '\0';
818    
819     /* Parse line. */
820     irc_parse();
821    
822     /* Reset counter. */
823     IRC_RAW_LEN = 0;
824     break;
825     }
826    
827     if (c != '\0')
828     IRC_RAW[IRC_RAW_LEN++] = c;
829     }
830    
831     if ((len <= 0) && (errno != EAGAIN))
832     {
833 michael 6271 if (errno != EINTR)
834     log_printf("IRC -> Error reading data from server: %s", strerror(errno));
835    
836 michael 6269 irc_reconnect();
837 michael 6167 IRC_RAW_LEN = 0;
838     return;
839     }
840     }
841    
842     /* irc_cycle
843     *
844     * Pass control to the IRC portion of HOPM to handle any awaiting IRC events.
845     *
846     * Parameters:
847     * None
848     *
849     * Return:
850     * None
851     */
852     void
853     irc_cycle(void)
854     {
855     static struct pollfd pfd;
856    
857 michael 6215 if (IRC_FD == -1)
858 michael 6167 {
859 michael 8138 /* Initialize negative cache. */
860 michael 8580 if (OptionsItem.negcache)
861 michael 7880 negcache_init();
862 michael 6167
863     /* Resolve remote host. */
864     irc_init();
865    
866     /* Connect to remote host. */
867     irc_connect();
868     return; /* In case connect() immediately failed */
869     }
870    
871     pfd.fd = IRC_FD;
872     pfd.events = POLLIN;
873    
874     /* Block .050 seconds to avoid excessive CPU use on poll(). */
875     switch (poll(&pfd, 1, 50))
876     {
877     case 0:
878     case -1:
879     break;
880     default:
881     /* Check if IRC data is available. */
882     if (pfd.revents & POLLIN)
883     irc_read();
884     else if (pfd.revents & (POLLERR | POLLHUP))
885     irc_reconnect();
886    
887     break;
888     }
889     }
890    
891     /* irc_send
892     *
893     * Send data to remote IRC host.
894     *
895     * Parameters:
896     * data: Format of data to send
897     * ...: varargs to format with
898     *
899     * Return: NONE
900     */
901     void
902     irc_send(const char *data, ...)
903     {
904     va_list arglist;
905     char buf[MSGLENMAX];
906    
907     va_start(arglist, data);
908 michael 9471 ssize_t len = vsnprintf(buf, sizeof(buf), data, arglist);
909 michael 6167 va_end(arglist);
910    
911     if (OPT_DEBUG >= 2)
912     log_printf("IRC SEND -> %s", buf);
913    
914     if (len > 510)
915     len = 510;
916    
917     buf[len++] = '\r';
918     buf[len++] = '\n';
919    
920 michael 9477 ssize_t sent;
921     #ifdef HAVE_LIBCRYPTO
922     if (ssl_handle)
923     sent = SSL_write(ssl_handle, buf, len);
924     else
925     #endif
926     sent = send(IRC_FD, buf, len, 0);
927    
928     if (sent != len)
929 michael 6167 {
930     /* Return of -1 indicates error sending data; we reconnect. */
931     log_printf("IRC -> Error sending data to server: %s", strerror(errno));
932     irc_reconnect();
933     }
934     }
935    
936 michael 6563 /* irc_send_channels
937 michael 6167 *
938     * Send privmsg to all channels.
939     *
940     * Parameters:
941     * data: Format of data to send
942     * ...: varargs to format with
943     *
944     * Return: NONE
945     */
946     void
947     irc_send_channels(const char *data, ...)
948     {
949     const node_t *node;
950     va_list arglist;
951     char buf[MSGLENMAX];
952    
953     va_start(arglist, data);
954     vsnprintf(buf, sizeof(buf), data, arglist);
955     va_end(arglist);
956    
957 michael 8580 LIST_FOREACH(node, IRCItem.channels.head)
958 michael 6167 {
959     const struct ChannelConf *chan = node->data;
960    
961     irc_send("PRIVMSG %s :%s", chan->name, buf);
962     }
963     }
964    
965     /* irc_timer
966     *
967     * Functions to be performed every ~seconds.
968     *
969     * Parameters: NONE
970     * Return: NONE
971     */
972     void
973     irc_timer(void)
974     {
975     time_t present, delta;
976    
977     time(&present);
978    
979     delta = present - IRC_LAST;
980    
981 michael 8580 /* No data in IRCItem.readtimeout seconds */
982     if (delta >= IRCItem.readtimeout)
983 michael 6167 {
984     log_printf("IRC -> Timeout awaiting data from server.");
985     irc_reconnect();
986    
987     /* Make sure we don't do this again for a while */
988     time(&IRC_LAST);
989     }
990 michael 8580 else if (delta >= IRCItem.readtimeout / 2)
991 michael 6167 {
992     /*
993     * Generate some data so high ping times don't cause uneeded
994     * reconnections
995     */
996     irc_send("PING :HOPM");
997     }
998     }

Properties

Name Value
svn:eol-style native
svn:keywords Id