ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/irc.c
Revision: 6198
Committed: Thu Jun 25 14:36:47 2015 UTC (11 years ago) by michael
Content type: text/x-csrc
File size: 19759 byte(s)
Log Message:
- Change userinfo_create() to not always allocate memory for each user message; improve checking for malformed sender prefixes
- Remove now unused userinfo_free()

File Contents

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

Properties

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