Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 453 lines (437 sloc) 10.613 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
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010 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

sigpipe=0;
    struct sigaction sa;
    sa.sa_handler = handle_sigpipe;
    sa.sa_flags=0;
    sigemptyset(&sa.sa_mask);

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

int infc=fcntl(STDIN_FILENO, F_GETFD);
char *fcfail=NULL;
if(infc>=0)
{
if(fcntl(STDIN_FILENO, F_SETFD, infc|O_NONBLOCK)==-1)
fcfail=strerror(errno);
}
if(c_init())
{
fprintf(stderr, "Failed to initialise colours\n"); // should be impossible
return(1);
}
if(def_config())
{
fprintf(stderr, "Failed to apply default configuration\n");
return(1);
}
settitle("quIRC - not connected");
resetcol();
char *qmsg=fname;
char *rcfile=".quirc";
char *home=getenv("HOME");
if(home) chdir(home);
FILE *rcfp=fopen(rcfile, "r");
if(rcfp)
{
rcread(rcfp);
fclose(rcfp);
}

signed int e=pargs(argc, argv);
if(e>=0)
{
return(e);
}

e=ttyraw(STDOUT_FILENO);
if(e)
{
fprintf(stderr, "Failed to set raw mode on tty\n");
perror("ttyraw");
return(1);
}

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");
return(1);
}

if(fcfail)
{
w_buf_print(0, c_status, fcfail, "fcntl: ");
}

fd_set master, readfds;
FD_ZERO(&master);
FD_SET(STDIN_FILENO, &master);
int fdmax=STDIN_FILENO;
int servers=0;
servlist *curr=servs;
while(curr)
{
servers |= autoconnect(&master, &fdmax, curr);
curr=curr->next;
}
if(!servers)
{
w_buf_print(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))
{
// 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)
{
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);
w_buf_print(b, c_err, "Outbound ping timeout", "Disconnected: ");
}
bufs[b].alert=true;
bufs[b].hi_alert=5;
}
else
{
char pmsg[8+strlen(bufs[b].realsname)];
sprintf(pmsg, "PING %s", bufs[b].realsname);
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;
w_buf_print(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)
{
perror("\nselect");
state=1;
}
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==STDIN_FILENO)
{
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))!=0)
{
char emsg[64];
sprintf(emsg, "irc_rx(%d, &%p): %d", fd, packet, e);
close(fd);
FD_CLR(fd, &master);
bufs[b].live=false;
w_buf_print(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(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, fname);
}
else
{
char emsg[32];
sprintf(emsg, "buf %d, fd %d", b, fd);
close(fd);
FD_CLR(fd, &master);
bufs[b].live=false;
w_buf_print(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);
w_buf_print(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);
free(iinput);iinput=NULL;
}
else
{
if((bufs[cbuf].type==CHANNEL)||(bufs[cbuf].type==PRIVATE))
{
if(bufs[cbuf].handle)
{
if(LIVE(cbuf))
{
char pmsg[12+strlen(bufs[cbuf].bname)+strlen(iinput)];
sprintf(pmsg, "PRIVMSG %s :%s", bufs[cbuf].bname, iinput);
irc_tx(bufs[cbuf].handle, pmsg);
char *cnick=strdup(bufs[bufs[cbuf].server].nick);
crush(&cnick, maxnlen);
char *tag=mktag("<%s> ", cnick);
free(cnick);
w_buf_print(cbuf, c_msg[0], iinput, tag);
free(tag);
}
else
{
w_buf_print(cbuf, c_err, "Can't talk - tab is not live!", "");
}
}
else
{
w_buf_print(cbuf, c_err, "Can't talk - tab is disconnected!", "");
}
}
else
{
w_buf_print(cbuf, c_err, "Can't talk - view is not a channel!", "");
}
free(iinput);iinput=NULL;
state=0;
}
}
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);
if(bufs) free(bufs);
if(username) free(username);
if(fname) free(fname);
if(nick) free(nick);
if(portno) free(portno);
freeservlist(servs);
n_free(igns);
ttyreset(STDOUT_FILENO);
#ifdef USE_MTRACE
muntrace();
#endif // USE_MTRACE
return(state>0?state:0);
}
Something went wrong with that request. Please try again.