Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 569 lines (551 sloc) 13.614 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010-11 Edward Cree

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "quirc.h"

int main(int argc, char *argv[])
{
#ifdef USE_MTRACE
mtrace();
#endif // USE_MTRACE

bufs=NULL;

setupterm((char *)0, fileno(stdout), (int *)0);
init_start_buffer();

sigpipe=0;
sigwinch=0;
sigusr1=0;
struct sigaction sa;
sa.sa_handler=handle_signals;
sa.sa_flags=0;
sigemptyset(&sa.sa_mask);

if(sigaction(SIGPIPE, &sa, NULL)==-1)
{
fprintf(stderr, "Failed to set SIGPIPE handler\n");
perror("sigaction");
push_buffer();
return(1);
}
if(sigaction(SIGWINCH, &sa, NULL)==-1)
{
fprintf(stderr, "Failed to set SIGWINCH handler\n");
perror("sigaction");
push_buffer();
return(1);
}
if(sigaction(SIGUSR1, &sa, NULL)==-1)
{
fprintf(stderr, "Failed to set SIGUSR1 handler\n");
perror("sigaction");
push_buffer();
return(1);
}

int infc=fcntl(fileno(stdin), F_GETFD);
if(infc>=0)
{
if(fcntl(fileno(stdin), F_SETFD, infc|O_NONBLOCK)==-1)
{
char *err=strerror(errno);
char msg[48+strlen(err)];
sprintf(msg, "Failed to mark stdin non-blocking: fcntl: %s", err);
asb_failsafe(c_status, msg);
}
}
if(initkeys())
{
fprintf(stderr, "Failed to initialise keymapping\n");
push_buffer();
return(1);
}
if(c_init()) // should be impossible
{
fprintf(stderr, "Failed to initialise colours\n");
push_buffer();
return(1);
}
if(def_config())
{
fprintf(stderr, "Failed to apply default configuration\n");
push_buffer();
return(1);
}
resetcol();
char *qmsg=fname;
char *home=getenv("HOME");
if(home)
{
char *qfld=malloc(strlen(home)+8);
if(qfld)
{
sprintf(qfld, "%s/.quirc", home);
if(chdir(qfld))
{
fprintf(stderr, "Failed to change directory into %s: %s", qfld, strerror(errno));
push_buffer();
return(1);
}
free(qfld);
}
else
{
perror("Failed to allocate space for 'qfld'");
push_buffer();
return(1);
}
}
else
{
fprintf(stderr, "Environment variable $HOME not set! Exiting\n");
return(1);
}
FILE *rcfp=fopen("rc", "r");
int rc_err=0;
if(rcfp)
{
rc_err=rcread(rcfp);
fclose(rcfp);
if(rc_err)
{
char msg[32];
sprintf(msg, "%d errors in ~/.quirc/rc", rc_err);
asb_failsafe(c_status, msg);
}
}
else
{
asb_failsafe(c_status, "no config file found. Install one at ~/.quirc/rc");
}
FILE *keyfp=fopen("keys", "r");
if(keyfp)
{
loadkeys(keyfp);
fclose(keyfp);
}
signed int e=pargs(argc, argv);
if(e>=0)
{
push_buffer();
return(e);
}

conf_check();

if(ttyraw(fileno(stdout)))
{
asb_failsafe(c_err, "Failed to set raw mode on tty: ttyraw:");
asb_failsafe(c_err, strerror(errno));
}

unsigned int i;
for(i=0;i<height;i++) // push old stuff off the top of the screen, so it's preserved
printf("\n");

e=initialise_buffers(buflines);
if(e)
{
fprintf(stderr, "Failed to set up buffers\n");
push_buffer();
return(1);
}

push_buffer();

fd_set master, readfds;
FD_ZERO(&master);
FD_SET(fileno(stdin), &master);
int fdmax=fileno(stdin);
if((!autoconnect(&master, &fdmax, servs))&&(!quiet))
add_to_buffer(0, c_status, "Not connected - use /server to connect", "");
iline inp={{NULL, 0, 0}, {NULL, 0, 0}};
in_update(inp);
struct timeval timeout;
int state=0; // odd-numbered states are fatal
while(!(state%2))
{
if(sigwinch)
{
if(winch)
{
int l, c;
if(termsize(fileno(stdin), &c, &l))
{
add_to_buffer(0, c_err, strerror(errno), "termsize: ioctl: ");
}
else
{
height=max(l, 5);
width=max(c, 30);
for(int buf=0;buf<nbufs;buf++)
bufs[buf].dirty=true;
redraw_buffer();
}
}
sigwinch=0;
}
#if ASYNCH_NL
if(sigusr1)
{
sigusr1=0; // no race condition, because after this point we loop over the whole nl_list
int serverhandle;
nl_list *list=nl_active;
while((list=nl_active)&&(serverhandle=irc_conn_found(&list, &master, &fdmax))&&list)
{
const char *server=list->nl_details->ar_name;
if(!server) server="server";
const char *port=list->nl_details->ar_service;
if(!port) port="port";
char dstr[32+strlen(server)+strlen(port)];
sprintf(dstr, "Found %s, connecting on %s...", server, port);
if(force_redraw<3) redraw_buffer();
if(list->reconn_b)
{
bufs[list->reconn_b].handle=serverhandle;
int b2;
for(b2=1;b2<nbufs;b2++)
{
if(bufs[b2].server==list->reconn_b)
bufs[b2].handle=serverhandle;
}
bufs[list->reconn_b].conninpr=true;
free(bufs[list->reconn_b].realsname);
bufs[list->reconn_b].realsname=NULL;
if(list->autoent)
bufs[list->reconn_b].autoent=list->autoent;
if(!quiet) add_to_buffer(list->reconn_b, c_status, dstr, "/server: ");
}
else
{
bufs=(buffer *)realloc(bufs, ++nbufs*sizeof(buffer));
init_buffer(nbufs-1, SERVER, server, buflines);
cbuf=nbufs-1;
bufs[cbuf].handle=serverhandle;
bufs[cbuf].nick=strdup(nick);
bufs[cbuf].server=cbuf;
bufs[cbuf].conninpr=true;
if(list->autoent)
{
free(bufs[cbuf].nick);
bufs[cbuf].nick=strdup(list->autoent->nick);
bufs[cbuf].ilist=list->autoent->igns;
bufs[cbuf].autoent=list->autoent;
}
if(!quiet) add_to_buffer(cbuf, c_status, dstr, "/server: ");
}
free((char *)list->nl_details->ar_name);
free((char *)list->nl_details->ar_service);
freeaddrinfo((void *)list->nl_details->ar_request);
if(list->prev) list->prev->next=list->next;
else nl_active=list->next;
if(list->next) list->next->prev=list->prev;
free(list);
}
}
#endif
// propagate liveness values into dependent tabs, and ping idle connections
{
time_t now=time(NULL);
int b;
for(b=0;b<nbufs;b++)
{
if(bufs[b].live)
{
if(bufs[b].type==SERVER)
{
unsigned int idle=now-bufs[b].last;
if(tping && (idle>tping)) // a tping value of 0 means "don't ping"
{
if(bufs[b].ping)
{
bufs[b].live=false;
if(bufs[b].handle)
{
close(bufs[b].handle);
FD_CLR(bufs[b].handle, &master);
bufs[b].handle=0; // de-bind fd
add_to_buffer(b, c_err, "Outbound ping timeout", "Disconnected: ");
}
bufs[b].alert=true;
bufs[b].hi_alert=5;
}
else
{
char pmsg[8+strlen(bufs[b].realsname?bufs[b].realsname:bufs[b].bname)];
sprintf(pmsg, "PING %s", bufs[b].realsname?bufs[b].realsname:bufs[b].bname);
irc_tx(bufs[b].handle, pmsg);
bufs[b].ping++;
}
bufs[b].last=now;
in_update(inp);
}
}
else if(!bufs[bufs[b].server].live)
{
bufs[b].live=false;
bufs[b].handle=0; // just in case
add_to_buffer(b, c_err, "Connection to server lost", "Disconnected: ");
}
}
}
}
timeout.tv_sec=0;
timeout.tv_usec=250000;

readfds=master;
if(select(fdmax+1, &readfds, NULL, NULL, &timeout)==-1)
{
if(errno!=EINTR) // nobody cares if select() was interrupted by a signal
add_to_buffer(0, c_err, strerror(errno), "select: ");
}
else
{
if(timeout.tv_usec==0) // flashing of hi-alert tabs
{
int b;
for(b=0;b<nbufs;b++)
{
switch(bufs[b].hi_alert%2)
{
case 1:
bufs[b].hi_alert=bufs[b].alert?4:bufs[b].hi_alert-1;
break;
case 0:
bufs[b].hi_alert=max(bufs[b].hi_alert-1, 0);
break;
}
}
in_update(inp);
}
int fd;
for(fd=0;fd<=fdmax;fd++)
{
if(FD_ISSET(fd, &readfds))
{
if(fd==fileno(stdin))
{
inputchar(&inp, &state);
/* WARNING! Possibly non-portable code; relies on edge-case behaviour */
bool loop=true;
while(loop && !state)
{
errno=0;
fflush(stdin);
if(errno==ESPIPE)
{
inputchar(&inp, &state); // ESPIPE only happens if there is data waiting to be read
}
else
{
loop=false;
}
}
break; // handle the input; everyone else can wait
}
else
{
int b;
for(b=0;b<nbufs;b++)
{
if((fd==bufs[b].handle) && (bufs[b].type==SERVER))
{
if(bufs[b].live)
{
char *packet;
int e;
if((e=irc_rx(fd, &packet, &master))!=0)
{
char emsg[64];
sprintf(emsg, "irc_rx(%d, &%p): %d", fd, packet, e);
close(fd);
bufs[b].handle=0; // de-bind fd
FD_CLR(fd, &master);
bufs[b].live=false;
add_to_buffer(0, c_err, emsg, "error: ");
redraw_buffer();
}
else if(packet)
{
bufs[b].ping=0;
bufs[b].last=time(NULL);
if(*packet)
{
message pkt=irc_breakdown(packet);
if(!bufs[b].realsname && pkt.prefix)
bufs[b].realsname=strdup(pkt.prefix);
if(isdigit(*pkt.cmd))
{
irc_numeric(pkt, b);
}
else if(strcmp(pkt.cmd, "PING")==0)
{
rx_ping(pkt, b);
}
else if(strcmp(pkt.cmd, "PONG")==0)
{
// ignore, as /anything/ resets the ping-timer
}
else if(strcmp(pkt.cmd, "MODE")==0)
{
rx_mode(pkt, b);
}
else if(strcmp(pkt.cmd, "KILL")==0)
{
rx_kill(pkt, b, &master);
}
else if(strcmp(pkt.cmd, "KICK")==0)
{
rx_kick(pkt, b);
}
else if(strcmp(pkt.cmd, "ERROR")==0)
{
rx_error(pkt, b, &master);
}
else if(strcmp(pkt.cmd, "PRIVMSG")==0)
{
rx_privmsg(pkt, b, false);
}
else if(strcmp(pkt.cmd, "NOTICE")==0)
{
rx_privmsg(pkt, b, true);
}
else if(strcmp(pkt.cmd, "TOPIC")==0)
{
rx_topic(pkt, b);
}
else if(strcmp(pkt.cmd, "JOIN")==0)
{
rx_join(pkt, b);
}
else if(strcmp(pkt.cmd, "PART")==0)
{
rx_part(pkt, b);
}
else if(strcmp(pkt.cmd, "QUIT")==0)
{
rx_quit(pkt, b);
}
else if(strcmp(pkt.cmd, "NICK")==0)
{
rx_nick(pkt, b);
}
else
{
e_buf_print(b, c_unk, pkt, "Unrecognised command: ");
}
message_free(pkt);
}
else // if we called irc_breakdown, then that already free()d packet
{
free(packet);
}
}
}
else if(bufs[b].conninpr)
{
irc_conn_rest(b, nick, username, pass, fname);
}
else
{
char emsg[32];
sprintf(emsg, "buf %d, fd %d", b, fd);
close(fd);
bufs[b].handle=0; // de-bind fd
FD_CLR(fd, &master);
bufs[b].live=false;
add_to_buffer(0, c_err, emsg, "error: read on a dead tab: ");
redraw_buffer();
}
in_update(inp);
b=nbufs+1;
}
}
if(b==nbufs)
{
char fmsg[48];
sprintf(fmsg, "select() returned data on unknown fd %d!", fd);
if(!quiet) add_to_buffer(0, c_err, fmsg, "main loop:");
FD_CLR(fd, &master); // prevent it from happening again
}
}
}
}
}
switch(state)
{
case 3:;
char *iptr=bufs[cbuf].input.line[(bufs[cbuf].input.ptr+bufs[cbuf].input.nlines-1)%bufs[cbuf].input.nlines];
if(!iptr)
{
fprintf(stderr, "\nInternal error - state==3 and iptr is NULL!\n");
break;
}
char *iinput=strdup(iptr);
if(iinput&&*iinput) // ignore empty lines, and ignore if iinput is NULL
{
fflush(stdout);
char *deq=slash_dequote(iinput); // dequote
free(iinput);
iinput=deq;
if(*iinput=='/')
{
state=cmd_handle(iinput, &qmsg, &master, &fdmax);
}
else
{
talk(iinput);
state=0;
}
free(iinput);iinput=NULL;
}
else
{
state=0;
}
if(force_redraw==2) // 'slight paranoia' mode
{
redraw_buffer();
}
if(force_redraw<3)
{
in_update(inp);
}
break;
}
if(force_redraw>=3) // 'extremely paranoid' mode
{
redraw_buffer();
in_update(inp);
}
}
if(state!=0)
printf("quirc exiting\n");
int b;
for(b=1;b<nbufs;b++)
{
if((bufs[b].live) && (bufs[b].type==SERVER) && (bufs[b].handle!=0))
{
if(!qmsg) qmsg="quIRC Quit";
char quit[7+strlen(qmsg)];
sprintf(quit, "QUIT %s", qmsg);
irc_tx(bufs[b].handle, quit);
}
bufs[b].live=false;
free_buffer(b);
b--;
}
bufs[0].live=false;
free_buffer(0);
free(bufs);
free(username);
free(fname);
free(nick);
free(portno);
freeservlist(servs);
n_free(igns);
ttyreset(fileno(stdout));
#ifdef USE_MTRACE
muntrace();
#endif // USE_MTRACE
return(state>0?state:0);
}
Something went wrong with that request. Please try again.