Пример #1
0
void eraseshuttle()
{
	eraseline(shuttle[0],shuttle[1]);
	eraseline(shuttle[1],shuttle[2]);
	eraseline(shuttle[2],shuttle[3]);
	eraseline(shuttle[3],shuttle[0]);
}
Пример #2
0
void drawthrust(double radius)
{
	struct cord thrustp[1];
	struct cord bottom[1];
	bottom[0].x = (shuttle[2].x+shuttle[3].x)/2;
	bottom[0].y = (shuttle[2].y+shuttle[3].y)/2;
	thrustp[0].y = bottom[0].y+5;
	thrustp[0].x = bottom[0].x+5*sin(radius);
	drawline(shuttle[2],thrustp[0]);
	drawline(thrustp[0],shuttle[3]);
	fflush(executable);
	eraseline(shuttle[2],thrustp[0]);
	eraseline(thrustp[0],shuttle[3]);
	fflush(executable);
}
Пример #3
0
/* PROTO */
void
buddy_unaway(void *c, const char *who)
{
	struct BuddyList *trav;
	char           *sn = simplify_sn(who);

	trav = buddylist;

	while (trav != NULL) {
		if (strcmp(sn, trav->sn) == 0) {
			trav->away = 0;
			break;
		}
		trav = trav->next;
	}

	free(sn);

	if (conn->squelchaway)
		return;

	eraseline();
	b_echostr();

	if (conn->timestamps) {
		putchar(' ');
		addts();
	}
	set_color(COLOR_BUDDY_AWAY);
	printf(" %s ", who);
	set_color(0);
	printf("is no longer away.\n");
	show_prompt();
	return;
}
Пример #4
0
/* PROTO */
void
buddy_idle(void *c, const char *who, long idletime)
{
	struct BuddyList *trav;
	int             changed = 1;
	char           *sn = simplify_sn(who);

	trav = buddylist;

	while (trav != NULL) {
		if (strcmp(trav->sn, sn) == 0) {
			trav->idletime = idletime;

			if (idletime >= 10) {
				if (trav->idle)
					changed = 0;
				trav->idle = 1;
			} else {
				if (trav->idle == 0)
					changed = 0;
				trav->idle = 0;
			}
			break;
		}
		trav = trav->next;
	}

	free(sn);

	/*
         * in case for whatever reason the buddy isn't in the list (shouldn't
         * happen)
         */
	if (trav == NULL) {
		return;
	}
	if (!changed) {
		return;
	}
	if (conn->squelchidle)
		return;

	eraseline();
	b_echostr();

	if (conn->timestamps) {
		putchar(' ');
		addts();
	}
	set_color(COLOR_BUDDY_IDLE);
	printf(" %s ", who);
	set_color(0);

	printf("is %s idle.\n", (trav->idle ? "now" : "no longer"));
	show_prompt();
	return;
}
Пример #5
0
/* PROTO */
void
buddy_profile(void *c, const char *who, const char *msg)
{
	char           *ptext;
	long            days, hours, minutes;
	struct BuddyList *trav;
	char           *sname;
#ifdef DUMP_PROFILE
	FILE           *profile_dump;
#endif

	sname = simplify_sn(who);
	eraseline();

	for (trav = buddylist; trav; trav = trav->next) {
		if (strcmp(trav->sn, sname) == 0) {
			if (trav->idle) {
				b_echostr_s();
				printf("[%s] Idle: ", who);
				days = trav->idletime / 1440;
				hours = (trav->idletime % 1440) / 60;
				minutes = trav->idletime % 60;
				if (days > 0)
					printf("%ld days, ", days);
				if (hours > 0)
					printf("%ld hour%s, ", hours, (hours != 1 ? "s" : ""));

				printf("%ld minute%s\n", minutes,
				       (minutes != 1 ? "s" : ""));
				break;
			}
		}
	}

	b_echostr_s();
	printf("[%s] Info:\n", who);

	free(sname);

	ptext = strip_html(msg);
	prettyprint_echostr(ptext, PROFILE_ECHOSTR);

	free(ptext);

#ifdef DUMP_PROFILE
	if ((profile_dump = fopen(PROFILE_DUMP_PATH, "w")) != NULL) {
		fprintf(profile_dump, "%s", msg);
		fclose(profile_dump);
	}
#endif

	show_prompt();
}
Пример #6
0
/* PROTO */
void
buddy_awaymsg(void *c, const char *who, const char *msg)
{
	char           *ptext;

	eraseline();
	b_echostr_s();
	printf("[%s] Away: ", who);
	ptext = strip_html(msg);
	prettyprint(ptext, 12 + strlen(who));

	if (ptext[0] == 0)
		printf("\n");

	free(ptext);
	show_prompt();
}
Пример #7
0
/* PROTO */
void
buddy_offline(void *c, const char *who)
{
	char           *sname;
	int             found = 0;
	struct BuddyList *trav;

	sname = simplify_sn(who);

	trav = buddylist;
	while (trav != NULL) {
		if (strcmp(trav->sn, sname) == 0) {
			found = 1;

			if (trav->otr == 1) {
				otrl_message_disconnect_all_instances(userstate, &ui_ops, NULL,
      	                	conn->username, otr_proto, trav->sn);
             			trav->otr = 0;
                        	trav->otr_context = NULL;
                        	printf("[OTR] Ending OTR session with %s\n", trav->sn);
			}

			if (trav->prev == NULL) {
				buddylist = buddylist->next;
				if (buddylist != NULL)
					buddylist->prev = NULL;
				free(trav->sn);
				free(trav->formattedsn);
				free(trav);
				break;
			} else {
				trav->prev->next = trav->next;
				if (trav->next != NULL)
					trav->next->prev = trav->prev;
				free(trav->sn);
				free(trav->formattedsn);
				free(trav);
				break;
			}
		}
		trav = trav->next;
	}

	if (!found) {
		free(sname);
		return;
	}
	conn->buddiesonline--;

	if (conn->squelchconnect) {
		free(sname);
		return;
	}
	eraseline();
	b_echostr();

	if (conn->timestamps) {
		putchar(' ');
		addts();
	}
	set_color(COLOR_BUDDY_SIGNOFF);
	printf(" %s ", who);
	set_color(0);

	printf("has signed off.\n");
	log_event(EVENT_SIGNOFF, sname, NULL);
	free(sname);
	show_prompt();
}
Пример #8
0
/* PROTO */
void
buddy_online(void *c, const char *who)
{
	struct BuddyList *trav, *newbuddy;
	char           *sname;

	trav = buddylist;

	sname = simplify_sn(who);
	if (buddylist == NULL) {
		buddylist = malloc(sizeof(struct BuddyList));
		newbuddy = buddylist;
		newbuddy->prev = NULL;
		newbuddy->next = NULL;
	} else {
		for (trav = buddylist; trav != NULL; trav = trav->next) {
			if (strcmp(sname, trav->sn) < 0)
				break;
		}

		newbuddy = malloc(sizeof(struct BuddyList));

		if (trav == NULL) {	/* if it's the last entry */
			for (trav = buddylist; trav->next != NULL; trav = trav->next);
			trav->next = newbuddy;
			newbuddy->prev = trav;
			newbuddy->next = NULL;
		} else {
			if (trav == buddylist) {
				buddylist->prev = newbuddy;
				newbuddy->prev = NULL;
				newbuddy->next = buddylist;
				buddylist = newbuddy;
			} else {
				trav->prev->next = newbuddy;
				newbuddy->prev = trav->prev;
				newbuddy->next = trav;
				trav->prev = newbuddy;
			}
		}

	}

	newbuddy->sn = strdup(sname);
	newbuddy->formattedsn = strdup(who);
	newbuddy->away = 0;
	newbuddy->idle = 0;
	newbuddy->otr = 0;
	newbuddy->otr_context = otrl_context_find(userstate, newbuddy->sn,
					conn->username, otr_proto,
					OTRL_INSTAG_BEST, 1, 0,
					NULL, NULL);

	conn->buddiesonline++;

	if (conn->squelchconnect) {
		free(sname);
		return;
	}
	eraseline();
	b_echostr();

	if (conn->timestamps) {
		putchar(' ');
		addts();
	}
	set_color(COLOR_BUDDY_SIGNON);
	printf(" %s ", who);
	set_color(0);
	printf("is now online.\n");
	log_event(EVENT_SIGNON, sname, NULL);
	free(sname);
	show_prompt();
}
Пример #9
0
/* PROTO */
void
getmessage(void *c, const char *who, const int automessage, const char *message)
{
	const char	*msgin = message;
	char           *msg = NULL, *tempmsg = NULL;

	char           *sname;
	struct Waiting *wtemp, *wptr = NULL;
	int             offset, foundWaiting = 0;

	int otr_message = 0;
	if (conn->otr) {
		struct BuddyList	*buddy = NULL;
		buddy = find_buddy(who);

		if (buddy) {
			if (buddy->otr != -1) {
				char *newmsg;
				int ret = otrl_message_receiving(userstate, &ui_ops, NULL, conn->username,
								otr_proto, buddy->sn, msgin, &newmsg, NULL,
								&buddy->otr_context, NULL, NULL);
				if (ret) {
#ifdef DEBUG
					b_echostr_s();
					printf("[OTR] debug: internal msg %s\n", msgin);
#endif
					return;
				} else {
					if (newmsg) {
						msgin = strdup(newmsg);
						otrl_message_free(newmsg);
	                                        if (buddy->otr_context->msgstate == OTRL_MSGSTATE_ENCRYPTED)
							otr_message = 1;
					}
				}
			}
		}
	}

	tempmsg = strip_html(msgin);

	
	if (tempmsg == NULL)
		return;
	if (strlen(tempmsg) == 0) {
		free(tempmsg);
		return;
	}
	if (conn->netspeak_filter) {
		msg = undo_netspeak(tempmsg);
		free(tempmsg);
	} else {
		msg = tempmsg;
	}

	if (msg == NULL)
		return;

	if (strlen(msg) == 0) {
		free(msg);
		return;
	}
	if (conn->istyping == 0) {
		if (conn->lastsn != NULL)
			free(conn->lastsn);
		conn->lastsn = simplify_sn(who);
	}
	sname = simplify_sn(who);

	if (waiting == NULL) {
		waiting = malloc(sizeof(struct Waiting));
		wptr = waiting;
	} else {
		for (wtemp = waiting; wtemp != NULL; wtemp = wtemp->next)
			if (imcomm_compare_nicks(c, wtemp->sn, who)) {
				foundWaiting = 1;
				wptr = wtemp;
				break;
			}
		if (!foundWaiting) {
			for (wtemp = waiting; wtemp->next != NULL;
			     wtemp = wtemp->next);
			wtemp->next = malloc(sizeof(struct Waiting));
			wptr = wtemp->next;
		}
	}

	if (!foundWaiting) {
		wptr->sn = strdup(who);
		wptr->next = NULL;

		if (conn->isaway && !automessage) {
			if ((conn->respond_idle_only && conn->isidle)
			    || (!conn->respond_idle_only)) {
				imcomm_im_send_message(c, who, conn->awaymsg, 1);
				eraseline();
				b_echostr_s();

				if (conn->timestamps) {
					addts();
					putchar(' ');
				}
				printf("Sent auto-response to %s.\n", who);
				show_prompt();
			}
		}
	}
#ifdef MESSAGE_QUEUE
	if (conn->isaway)
		wptr->mqueue = addToMQueue(wptr->mqueue, msg, who);
#endif				/* MESSAGE_QUEUE */


	eraseline();

	if (conn->bell_on_incoming)
		putchar('\a');

	if (conn->timestamps) {
		addts();
		putchar(' ');
		offset = 11;
	} else {
		offset = 0;
	}

	offset += strlen(who) + 2;
	if (automessage) {
		set_color(COLOR_AUTORESPONSE);
		printf("*AUTO RESPONSE* ");
		set_color(0);
		offset += 16;
	}
	set_color(COLOR_INCOMING_IM);
	if (!otr_message)
		printf("%s", who);
	else {
		offset += 5;
		printf("(otr)%s", who);
	}
	set_color(0);
	printf(": ");
	wordwrap_print(msg, offset);
	if (automessage)
		log_event(EVENT_IM_AUTORESPONSE, sname, msg);
	else
		log_event(EVENT_IM, sname, msg);

	free(msg);
	free(sname);
	show_prompt();
}
Пример #10
0
/* PROTO */
void
input_reconnect(char *arg)
{
	struct ConnPtr *temp, *trav;

	eraseline();
	b_echostr_s();

	if (conn->timestamps) {
		addts();
		putchar(' ');
	}
	if (arg[0] != '\0') {
		if (conn->username) {
			free(conn->username);
			conn->username = NULL;
		}
		if (conn->password) {
			free(conn->password);
			conn->password = NULL;
		}
		printf("Reconnecting as %s...\n", arg);

		conn->username = strdup(arg);
		b_getpassword();
	} else {
		printf("Reconnecting...\n");
	}

	if (conn->conn != NULL) {

		delete_buddylist(buddylist);

		buddylist = NULL;
		conn->buddiesonline = 0;

		if (conn->clist->conn == conn->conn) {
			temp = conn->clist;
			conn->clist = conn->clist->next;
			if (temp->username)
				free(temp->username);

			free(temp);
		} else {
			for (trav = conn->clist; trav->next;) {
				if (trav->next->conn == conn->conn) {
					temp = trav->next;
					trav->next = trav->next->next;

					if (temp->username)
						free(temp->username);

					free(temp);
				}
			}
		}

		imcomm_delete_handle_now(conn->conn);

		conn->conn = NULL;
	}
	create_new_connection();

	if (conn->proxytype != PROXY_TYPE_NONE) {
		imcomm_set_proxy(conn->clist->conn, conn->proxytype, conn->proxy,
				 (uint16_t) conn->proxyport);
	}
	if (conn->oscarport != 0) {
		imcomm_set_oscar_port(conn->clist->conn, conn->oscarport);
	}
	imcomm_im_signon(conn->clist->conn, conn->username, conn->password);
	conn->conn = conn->clist->conn;

	if (conn->isaway)
		imcomm_set_away(conn->conn, conn->awaymsg);
}
Пример #11
0
/* PROTO */
void
input_send_message(char *arg)
{
	char           *msg, *msg_strip, *temp, *sn;
	char           *fullmsg;
	size_t          fullmsg_len;
	int             offset;

	if (conn->conn == NULL)
		return;

	temp = strchr(arg, ' ');

	if (temp == NULL) {
		printf("\n");
		b_echostr_s();
		printf("No message to send.\n");
		return;
	}
	if (strlen(temp + 1) == 0) {
		printf("\nNo message to send.\n");
		return;
	}
	if (conn->netspeak_filter)
		msg = undo_netspeak(temp + 1);
	else
		msg = temp + 1;

	sn = malloc(temp - arg + 1);
	sn[temp - arg] = 0;
	strncpy(sn, arg, temp - arg);
	fullmsg_len =
		strlen(msg) + strlen(SEND_FORMAT_BEGIN) + strlen(SEND_FORMAT_END) +
		1;
	fullmsg = malloc(fullmsg_len + 1);
	snprintf(fullmsg, fullmsg_len, "%s%s%s", SEND_FORMAT_BEGIN, msg,
		 SEND_FORMAT_END);
	imcomm_im_send_message(conn->conn, sn, fullmsg, 0);
	free(fullmsg);
	eraseline();

	if (conn->timestamps) {
		addts();
		putchar(' ');
		offset = 13;
	} else {
		offset = 2;
	}

	offset += strlen(sn) + 2;
	set_color(COLOR_OUTGOING_IM);
	printf("->%s", sn);
	set_color(0);
	printf(": ");
	msg_strip = strip_html(msg);
	wordwrap_print(msg_strip, offset);
	free(msg_strip);

	if (conn->lastsn != NULL)
		free(conn->lastsn);

	conn->lastsn = strdup(sn);
	log_event(EVENT_IMSEND, sn, msg);
	free(sn);

	if (conn->netspeak_filter)
		free(msg);
}
Пример #12
0
void livetrafficmeter(char iface[32], int mode)
{
	/* received bytes packets errs drop fifo frame compressed multicast */
	/* transmitted bytes packets errs drop fifo colls carrier compressed */
	uint64_t rx, tx, rxp, txp, timespent, timeslept;
	uint64_t rxtotal, txtotal, rxptotal, txptotal;
	uint64_t rxpmin, txpmin, rxpmax, txpmax;
	uint64_t rxmin, txmin, rxmax, txmax;
	uint64_t index = 1;
	int ratewidth, ppswidth, paddingwidth, json = 0;
	char buffer[256], buffer2[256];
	IFINFO previnfo;

	if (cfg.qmode == 10) {
		json = 1;
	}

	if (!json) {
		printf("Monitoring %s...    (press CTRL-C to stop)\n\n", iface);
		if (cfg.ostyle != 4) {
			printf("   getting traffic...");
			fflush(stdout);
		}
	}

	/* enable signal trap */
	intsignal = 0;
	if (signal(SIGINT, sighandler) == SIG_ERR) {
		perror("signal");
		exit(EXIT_FAILURE);
	}

	/* set some defaults */
	rxtotal = txtotal = rxptotal = txptotal = rxpmax = txpmax = 0;
	rxpmin = txpmin = rxmin = txmin = MAX64;
	rxmax = txmax = 0;
	timeslept = 0;

	timespent = (uint64_t)time(NULL);

	/* read /proc/net/dev and get values to the first list */
	if (!getifinfo(iface)) {
		printf("Error: Interface \"%s\" not available, exiting.\n", iface);
		exit(EXIT_FAILURE);
	}

	ratewidth = 15;
	ppswidth = 5;
	paddingwidth = 8;

	/* narrow output mode */
	if (cfg.ostyle == 0) {
		ratewidth = 12;
		ppswidth = 3;
		paddingwidth = 4;
	}

	if (!json) {
		cursorhide();
	} else {
		printf("{\"jsonversion\":\"%d\",", JSONVERSION_LIVE);
		printf("\"vnstatversion\":\"%s\",", getversion());
		printf("\"interface\":\"%s\",", iface);
		printf("\"sampletime\":%d}\n", LIVETIME);
	}

	/* loop until user gets bored */
	while (intsignal == 0) {

		timeslept = (uint64_t)time(NULL);

		/* wait 2 seconds for more traffic */
		sleep(LIVETIME);

		timeslept = (uint64_t)time(NULL) - timeslept;

		/* break loop without calculations because sleep was probably interrupted */
		if (intsignal) {
			break;
		}

		/* use values from previous loop if this isn't the first time */
		previnfo.rx = ifinfo.rx;
		previnfo.tx = ifinfo.tx;
		previnfo.rxp = ifinfo.rxp;
		previnfo.txp = ifinfo.txp;

		/* read those values again... */
		if (!getifinfo(iface)) {
			cursorshow();
			printf("Error: Interface \"%s\" not available, exiting.\n", iface);
			exit(EXIT_FAILURE);
		}

		/* calculate traffic and packets seen between updates */
		rx = countercalc(&previnfo.rx, &ifinfo.rx, ifinfo.is64bit);
		tx = countercalc(&previnfo.tx, &ifinfo.tx, ifinfo.is64bit);
		rxp = countercalc(&previnfo.rxp, &ifinfo.rxp, ifinfo.is64bit);
		txp = countercalc(&previnfo.txp, &ifinfo.txp, ifinfo.is64bit);

		/* update totals */
		rxtotal += rx;
		txtotal += tx;
		rxptotal += rxp;
		txptotal += txp;

		/* update min & max */
		if (rxmin > rx) {
			rxmin = rx;
		}
		if (txmin > tx) {
			txmin = tx;
		}
		if (rxmax < rx) {
			rxmax = rx;
		}
		if (txmax < tx) {
			txmax = tx;
		}
		if (rxpmin > rxp) {
			rxpmin = rxp;
		}
		if (txpmin > txp) {
			txpmin = txp;
		}
		if (rxpmax < rxp) {
			rxpmax = rxp;
		}
		if (txpmax < txp) {
			txpmax = txp;
		}

		/* show the difference in a readable format or json */
		if (!json) {
			if (mode == 0) {
				/* packets per second visible */
				snprintf(buffer, 128, "   rx: %s %*" PRIu64 " p/s", gettrafficrate(rx, LIVETIME, ratewidth), ppswidth, (uint64_t)rxp / LIVETIME);
				snprintf(buffer2, 128, " %*s tx: %s %*" PRIu64 " p/s", paddingwidth, " ", gettrafficrate(tx, LIVETIME, ratewidth), ppswidth, (uint64_t)txp / LIVETIME);
			} else {
				/* total transfer amount visible */
				snprintf(buffer, 128, "   rx: %s   %s", gettrafficrate(rx, LIVETIME, ratewidth), getvalue(rxtotal, 1, RT_Normal));
				snprintf(buffer2, 128, " %*s tx: %s   %s", paddingwidth, " ", gettrafficrate(tx, LIVETIME, ratewidth), getvalue(txtotal, 1, RT_Normal));
			}
			strcat(buffer, buffer2);

			if (cfg.ostyle != 4 || !debug) {
				cursortocolumn(1);
				eraseline();
			}
			if (cfg.ostyle != 4) {
				printf("%s", buffer);
				fflush(stdout);
			} else {
				printf("%s\n", buffer);
			}
		} else {
			printf("{\"index\":%" PRIu64 ",", index);
			printf("\"seconds\":%" PRIu64 ",", (uint64_t)time(NULL) - timespent);
			printf("\"rx\":{");
			printf("\"ratestring\":\"%s\",", gettrafficrate(rx, LIVETIME, 0));
			printf("\"bytespersecond\":%" PRIu64 ",", (uint64_t)(rx / LIVETIME));
			printf("\"packetspersecond\":%" PRIu64 ",", (uint64_t)(rxp / LIVETIME));
			printf("\"bytes\":%" PRIu64 ",", rx);
			printf("\"packets\":%" PRIu64 ",", rxp);
			printf("\"totalbytes\":%" PRIu64 ",", rxtotal);
			printf("\"totalpackets\":%" PRIu64 "", rxptotal);
			printf("},");
			printf("\"tx\":{");
			printf("\"ratestring\":\"%s\",", gettrafficrate(tx, LIVETIME, 0));
			printf("\"bytespersecond\":%" PRIu64 ",", (uint64_t)(tx / LIVETIME));
			printf("\"packetspersecond\":%" PRIu64 ",", (uint64_t)(txp / LIVETIME));
			printf("\"bytes\":%" PRIu64 ",", tx);
			printf("\"packets\":%" PRIu64 ",", txp);
			printf("\"totalbytes\":%" PRIu64 ",", txtotal);
			printf("\"totalpackets\":%" PRIu64 "", txptotal);
			printf("}}\n");
			index++;
		}
	}

	timespent = (uint64_t)time(NULL) - timespent - timeslept;

	if (!json) {
		cursorshow();
		printf("\n\n");
	}

	/* print some statistics if enough time did pass */
	if (!json && timespent >= 10) {

		printf("\n %s  /  traffic statistics\n\n", iface);

		printf("                           rx         |       tx\n");
		printf("--------------------------------------+------------------\n");
		printf("  bytes              %s", getvalue(rxtotal, 15, RT_Normal));
		printf("  | %s", getvalue(txtotal, 15, RT_Normal));
		printf("\n");
		printf("--------------------------------------+------------------\n");
		printf("          max        %s", gettrafficrate(rxmax, LIVETIME, 15));
		printf("  | %s\n", gettrafficrate(txmax, LIVETIME, 15));
		printf("      average        %s", gettrafficrate(rxtotal, (time_t)timespent, 15));
		printf("  | %s\n", gettrafficrate(txtotal, (time_t)timespent, 15));
		printf("          min        %s", gettrafficrate(rxmin, LIVETIME, 15));
		printf("  | %s\n", gettrafficrate(txmin, LIVETIME, 15));
		printf("--------------------------------------+------------------\n");
		printf("  packets               %12" PRIu64 "  |    %12" PRIu64 "\n", rxptotal, txptotal);
		printf("--------------------------------------+------------------\n");
		printf("          max          %9" PRIu64 " p/s  |   %9" PRIu64 " p/s\n", rxpmax / LIVETIME, txpmax / LIVETIME);
		printf("      average          %9" PRIu64 " p/s  |   %9" PRIu64 " p/s\n", rxptotal / timespent, txptotal / timespent);
		printf("          min          %9" PRIu64 " p/s  |   %9" PRIu64 " p/s\n", rxpmin / LIVETIME, txpmin / LIVETIME);
		printf("--------------------------------------+------------------\n");

		if (timespent <= 60) {
			printf("  time             %9" PRIu64 " seconds\n", timespent);
		} else {
			printf("  time               %7.2f minutes\n", timespent / (double)60);
		}

		printf("\n");
	} else if (json) {
		printf("{\"seconds\":%" PRIu64 ",", timespent);
		printf("\"rx\":{");
		printf("\"maxratestring\":\"%s\",", gettrafficrate(rxmax, LIVETIME, 0));
		printf("\"averageratestring\":\"%s\",", gettrafficrate(rxtotal, (time_t)timespent, 0));
		printf("\"minratestring\":\"%s\",", gettrafficrate(rxmin, LIVETIME, 0));
		printf("\"totalbytes\":%" PRIu64 ",", rxtotal);
		printf("\"maxbytes\":%" PRIu64 ",", rxmax);
		printf("\"minbytes\":%" PRIu64 ",", rxmin);
		printf("\"totalpackets\":%" PRIu64 ",", rxptotal);
		printf("\"maxpackets\":%" PRIu64 ",", rxpmax);
		printf("\"minpackets\":%" PRIu64 "", rxpmin);
		printf("},");
		printf("\"tx\":{");
		printf("\"maxratestring\":\"%s\",", gettrafficrate(txmax, LIVETIME, 0));
		printf("\"averageratestring\":\"%s\",", gettrafficrate(txtotal, (time_t)timespent, 0));
		printf("\"minratestring\":\"%s\",", gettrafficrate(txmin, LIVETIME, 0));
		printf("\"totalbytes\":%" PRIu64 ",", txtotal);
		printf("\"maxbytes\":%" PRIu64 ",", txmax);
		printf("\"minbytes\":%" PRIu64 ",", txmin);
		printf("\"totalpackets\":%" PRIu64 ",", txptotal);
		printf("\"maxpackets\":%" PRIu64 ",", txpmax);
		printf("\"minpackets\":%" PRIu64 "", txpmin);
		printf("}}\n");
	}
}
Пример #13
0
void trafficmeter(char iface[], unsigned int sampletime)
{
	/* received bytes packets errs drop fifo frame compressed multicast */
	/* transmitted bytes packets errs drop fifo colls carrier compressed */
	uint64_t rx, tx, rxp, txp;
	int json = 0;
	IFINFO firstinfo;
	char buffer[256];

	if (cfg.qmode == 10) {
		json = 1;
	}

	/* less than 2 seconds doesn't produce good results */
	if (sampletime < 2) {
		printf("Error: Time for sampling too short.\n");
		exit(EXIT_FAILURE);
	}

	/* read interface info and get values to the first list */
	if (!getifinfo(iface)) {
		printf("Error: Interface \"%s\" not available, exiting.\n", iface);
		exit(EXIT_FAILURE);
	}
	firstinfo.rx = ifinfo.rx;
	firstinfo.tx = ifinfo.tx;
	firstinfo.rxp = ifinfo.rxp;
	firstinfo.txp = ifinfo.txp;

	/* wait sampletime and print some nice dots so that the user thinks
	something is done :) */
	if (!json) {
		snprintf(buffer, 256, "Sampling %s (%u seconds average)", iface, sampletime);
		printf("%s", buffer);
		fflush(stdout);
		sleep(sampletime / 3);
		printf(".");
		fflush(stdout);
		sleep(sampletime / 3);
		printf(".");
		fflush(stdout);
		sleep(sampletime / 3);
		printf(".");
		fflush(stdout);
		if ((sampletime / 3) * 3 != sampletime) {
			sleep(sampletime - ((sampletime / 3) * 3));
		}

		cursortocolumn(1);
		eraseline();
	} else {
		sleep(sampletime);
	}

	/* read those values again... */
	if (!getifinfo(iface)) {
		printf("Error: Interface \"%s\" not available, exiting.\n", iface);
		exit(EXIT_FAILURE);
	}

	/* calculate traffic and packets seen between updates */
	rx = countercalc(&firstinfo.rx, &ifinfo.rx, ifinfo.is64bit);
	tx = countercalc(&firstinfo.tx, &ifinfo.tx, ifinfo.is64bit);
	rxp = countercalc(&firstinfo.rxp, &ifinfo.rxp, ifinfo.is64bit);
	txp = countercalc(&firstinfo.txp, &ifinfo.txp, ifinfo.is64bit);

	/* show the difference in a readable format or json */
	if (!json) {
		printf("%" PRIu64 " packets sampled in %d seconds\n", rxp + txp, sampletime);
		printf("Traffic average for %s\n", iface);
		printf("\n      rx     %s         %5" PRIu64 " packets/s\n", gettrafficrate(rx, sampletime, 15), (uint64_t)(rxp / sampletime));
		printf("      tx     %s         %5" PRIu64 " packets/s\n\n", gettrafficrate(tx, sampletime, 15), (uint64_t)(txp / sampletime));
	} else {
		printf("{\"jsonversion\":\"%d\",", JSONVERSION_TR);
		printf("\"vnstatversion\":\"%s\",", getversion());
		printf("\"interface\":\"%s\",", iface);
		printf("\"sampletime\":%u,", sampletime);
		printf("\"rx\":{");
		printf("\"ratestring\":\"%s\",", gettrafficrate(rx, sampletime, 0));
		printf("\"bytespersecond\":%" PRIu64 ",", (uint64_t)(rx / sampletime));
		printf("\"packetspersecond\":%" PRIu64 ",", (uint64_t)(rxp / sampletime));
		printf("\"bytes\":%" PRIu64 ",", rx);
		printf("\"packets\":%" PRIu64 "", rxp);
		printf("},");
		printf("\"tx\":{");
		printf("\"ratestring\":\"%s\",", gettrafficrate(tx, sampletime, 0));
		printf("\"bytespersecond\":%" PRIu64 ",", (uint64_t)(tx / sampletime));
		printf("\"packetspersecond\":%" PRIu64 ",", (uint64_t)(txp / sampletime));
		printf("\"bytes\":%" PRIu64 ",", tx);
		printf("\"packets\":%" PRIu64 "", txp);
		printf("}}\n");
	}
}
Пример #14
0
/* PROTO */
void
buddy_offline(void *c, const char *who)
{
	char           *sname;
	int             found = 0;
	struct BuddyList *trav;

	sname = simplify_sn(who);

	trav = buddylist;
	while (trav != NULL) {
		if (strcmp(trav->sn, sname) == 0) {
			found = 1;

			if (trav->prev == NULL) {
				buddylist = buddylist->next;
				if (buddylist != NULL)
					buddylist->prev = NULL;
				free(trav->sn);
				free(trav->formattedsn);
				free(trav);
				break;
			} else {
				trav->prev->next = trav->next;
				if (trav->next != NULL)
					trav->next->prev = trav->prev;
				free(trav->sn);
				free(trav->formattedsn);
				free(trav);
				break;
			}
		}
		trav = trav->next;
	}

	if (!found) {
		free(sname);
		return;
	}
	conn->buddiesonline--;

	if (conn->squelchconnect) {
		free(sname);
		return;
	}
	eraseline();
	b_echostr();

	if (conn->timestamps) {
		putchar(' ');
		addts();
	}
	set_color(COLOR_BUDDY_SIGNOFF);
	printf(" %s ", who);
	set_color(0);

	printf("has signed off.\n");
	log_event(EVENT_SIGNOFF, sname, NULL);
	free(sname);
	show_prompt();
}
Пример #15
0
/* PROTO */
void
getmessage(void *c, const char *who, const int automessage, const char *message)
{
	char           *msg, *tempmsg;

	char           *sname;
	struct Waiting *wtemp, *wptr = NULL;
	int             offset, foundWaiting = 0;

	tempmsg = strip_html(message);
	if (tempmsg == NULL)
		return;
	if (strlen(tempmsg) == 0) {
		free(tempmsg);
		return;
	}
	if (conn->netspeak_filter) {
		msg = undo_netspeak(tempmsg);
		free(tempmsg);
	} else {
		msg = tempmsg;
	}

	if (msg == NULL)
		return;

	if (strlen(msg) == 0) {
		free(msg);
		return;
	}
	if (conn->istyping == 0) {
		if (conn->lastsn != NULL)
			free(conn->lastsn);
		conn->lastsn = simplify_sn(who);
	}
	sname = simplify_sn(who);

	if (waiting == NULL) {
		waiting = malloc(sizeof(struct Waiting));
		wptr = waiting;
	} else {
		for (wtemp = waiting; wtemp != NULL; wtemp = wtemp->next)
			if (imcomm_compare_nicks(c, wtemp->sn, who)) {
				foundWaiting = 1;
				wptr = wtemp;
				break;
			}
		if (!foundWaiting) {
			for (wtemp = waiting; wtemp->next != NULL;
			     wtemp = wtemp->next);
			wtemp->next = malloc(sizeof(struct Waiting));
			wptr = wtemp->next;
		}
	}

	if (!foundWaiting) {
		wptr->sn = strdup(who);
		wptr->next = NULL;

		if (conn->isaway && !automessage) {
			if ((conn->respond_idle_only && conn->isidle)
			    || (!conn->respond_idle_only)) {
				imcomm_im_send_message(c, who, conn->awaymsg, 1);
				eraseline();
				b_echostr_s();

				if (conn->timestamps) {
					addts();
					putchar(' ');
				}
				printf("Sent auto-response to %s.\n", who);
				show_prompt();
			}
		}
	}
#ifdef MESSAGE_QUEUE
	if (conn->isaway)
		wptr->mqueue = addToMQueue(wptr->mqueue, msg, who);
#endif				/* MESSAGE_QUEUE */


	eraseline();

	if (conn->bell_on_incoming)
		putchar('\a');

	if (conn->timestamps) {
		addts();
		putchar(' ');
		offset = 11;
	} else {
		offset = 0;
	}

	offset += strlen(who) + 2;
	if (automessage) {
		set_color(COLOR_AUTORESPONSE);
		printf("*AUTO RESPONSE* ");
		set_color(0);
		offset += 16;
	}
	set_color(COLOR_INCOMING_IM);
	printf("%s", who);
	set_color(0);
	printf(": ");
	wordwrap_print(msg, offset);
	if (automessage)
		log_event(EVENT_IM_AUTORESPONSE, sname, msg);
	else
		log_event(EVENT_IM, sname, msg);

	free(msg);
	free(sname);
	show_prompt();
}