pcw / patches / ii-1.4-emg.diff

diff -r d93eaacde742 config.mk
--- a/config.mk	Fri Jun 25 10:55:05 2010 +0200
+++ b/config.mk	Fri Jul 30 01:45:19 2010 -0700
@@ -12,7 +12,7 @@
 
 INCDIR      = ${PREFIX}/include
 LIBDIR      = ${PREFIX}/lib
-VERSION     = 1.4
+VERSION     = 1.4-emg
 
 # includes and libs
 INCLUDES    = -I. -I${INCDIR} -I/usr/include
diff -r d93eaacde742 ii.1
--- a/ii.1	Fri Jun 25 10:55:05 2010 +0200
+++ b/ii.1	Fri Jul 30 01:45:19 2010 -0700
@@ -70,12 +70,18 @@
 .FN /a " [<message>]"
 mark yourself as away
 .TP
+.FN /e " command"
+execute command and write output to current channel
+.TP
 .FN /j " #channel/nickname [<message>]"
 join a channel or open private conversation with user
 .TP
 .FN /l " #channel/nickname"
 leave a channel or query
 .TP
+.FN /m " action"
+send action as a CTCP ACTION (think /me)
+.TP
 .FN /n " nick"
 change the nick name
 .TP
diff -r d93eaacde742 ii.c
--- a/ii.c	Fri Jun 25 10:55:05 2010 +0200
+++ b/ii.c	Fri Jul 30 01:45:19 2010 -0700
@@ -33,6 +33,9 @@
 	Channel *next;
 };
 
+#define IS_CHANNEL(s) (((s)[0]=='#')||((s)[0]=='&')||((s)[0]=='+')||((s)[0]=='!'))
+
+#define EXIT_TIMEOUT 2
 #define PING_TIMEOUT 300
 #define SERVER_PORT 6667
 static int irc;
@@ -105,6 +108,7 @@
 	return open(infile, O_RDONLY | O_NONBLOCK, 0);
 }
 
+static void print_out(char *channel, char *buf); // needs to be declared
 static void add_channel(char *name) {
 	Channel *c;
 	int fd;
@@ -123,13 +127,16 @@
 		perror("ii: cannot allocate memory");
 		exit(EXIT_FAILURE);
 	}
-	if(!channels) channels = c;
-	else {
-		c->next = channels;
-		channels = c;
-	}
 	c->fd = fd;
 	c->name = strdup(name);
+	c->next = channels;
+	channels = c;
+
+	if(name[0] && !IS_CHANNEL(name)) {
+		char msg[128];
+		snprintf(msg, sizeof(msg), "-!- %s has joined %s", nick, name);
+		print_out(name, msg);
+	}
 }
 
 static void rm_channel(Channel *c) {
@@ -210,6 +217,7 @@
 	if(channel) snprintf(server, sizeof(server), "-!- %s", channel);
 	if(strstr(buf, server)) channel="";
 	create_filepath(outfile, sizeof(outfile), channel, "out");
+	if(channel && channel[0]) add_channel(channel);
 	if(!(out = fopen(outfile, "a"))) return;
 
 	strftime(buft, sizeof(buft), "%F %R", localtime(&t));
@@ -228,7 +236,9 @@
 	static char infile[256];
 	char *p = NULL;
 
-	if(buf[0] != '/' && buf[0] != 0) {
+	if(buf[0] == '\0')
+		return;
+	if(buf[0] != '/') {
 		proc_channels_privmsg(c->name, buf);
 		return;
 	}
@@ -237,17 +247,14 @@
 		case 'j':
 			p = strchr(&buf[3], ' ');
 			if(p) *p = 0;
-			if((buf[3]=='#')||(buf[3]=='&')||(buf[3]=='+')||(buf[3]=='!')){
+			add_channel(&buf[3]);
+			if(IS_CHANNEL(&buf[3])) {
 				if(p) snprintf(message, PIPE_BUF, "JOIN %s %s\r\n", &buf[3], p + 1); /* password protected channel */
 				else snprintf(message, PIPE_BUF, "JOIN %s\r\n", &buf[3]);
-				add_channel(&buf[3]);
 			}
-			else {
-				if(p){
-					add_channel(&buf[3]);
-					proc_channels_privmsg(&buf[3], p + 1);
-					return;
-				}
+			else if(p) {
+				proc_channels_privmsg(&buf[3], p + 1);
+				return;
 			}
 			break;
 		case 't':
@@ -277,13 +284,42 @@
 			else
 				snprintf(message, PIPE_BUF,
 						"PART %s :ii - 500 SLOC are too much\r\n", c->name);
-			write(irc, message, strlen(message));
+			if(IS_CHANNEL(c->name))
+				write(irc, message, strlen(message));
 			close(c->fd);
 			create_filepath(infile, sizeof(infile), c->name, "in");
 			unlink(infile);
 			rm_channel(c);
 			return;
 			break;
+		case 'e':
+			if(strlen(buf)>=3) {
+				char newbuf[PIPE_BUF];
+				FILE *fp;
+				int len;
+
+				if(!(fp = popen(&buf[3], "r"))) {
+					fprintf(stderr, "error running %s\n", &buf[3]);
+					return;
+				}
+				while (fgets(newbuf, sizeof(newbuf), fp)) {
+					len = strlen(newbuf);
+					if (newbuf[len - 1] == '\n')
+						newbuf[len - 1] = '\0';
+					proc_channels_privmsg(c->name, newbuf);
+				}
+				pclose(fp);
+			}
+			return;
+			break;
+		case 'm':
+			if(strlen(buf)>=3) {
+				char newbuf[PIPE_BUF];
+				snprintf(newbuf, PIPE_BUF, "\01ACTION %s\01", &buf[3]);
+				proc_channels_privmsg(c->name, newbuf);
+			}
+			return;
+			break;
 		default:
 			snprintf(message, PIPE_BUF, "%s\r\n", &buf[1]);
 			break;
@@ -343,7 +379,7 @@
 		write(irc, message, strlen(message));
 		return;
 	} else if(!argv[TOK_NICKSRV] || !argv[TOK_USER]) {	/* server command */
-		snprintf(message, PIPE_BUF, "%s%s", argv[TOK_ARG] ? argv[TOK_ARG] : "", argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
+		snprintf(message, PIPE_BUF, "<%s> %s%s", host, argv[TOK_ARG] ? argv[TOK_ARG] : "", argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
 		print_out(0, message);
 		return;
 	} else if(!strncmp("ERROR", argv[TOK_CMD], 6))
@@ -357,6 +393,8 @@
 		argv[TOK_CHAN] = argv[TOK_TEXT];
 		snprintf(message, PIPE_BUF, "-!- %s(%s) has joined %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_TEXT]);
 	} else if(!strncmp("PART", argv[TOK_CMD], 5)) {
+		if (!strcmp(nick, argv[TOK_NICKSRV]))
+			return;
 		snprintf(message, PIPE_BUF, "-!- %s(%s) has left %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_CHAN]);
 	} else if(!strncmp("MODE", argv[TOK_CMD], 5))
 		snprintf(message, PIPE_BUF, "-!- %s changed mode/%s -> %s %s", argv[TOK_NICKSRV], argv[TOK_CMD + 1] ? argv[TOK_CMD + 1] : "" , argv[TOK_CMD + 2]? argv[TOK_CMD + 2] : "", argv[TOK_CMD + 3] ? argv[TOK_CMD + 3] : "");
@@ -443,7 +481,7 @@
 		} else if(r == 0) {
 			if(time(NULL) - last_response >= PING_TIMEOUT) {
 				print_out(NULL, "-!- ii shutting down: ping timeout");
-				exit(EXIT_FAILURE);
+				exit(EXIT_TIMEOUT);
 			}
 			write(irc, ping_msg, strlen(ping_msg));
 			continue;
@@ -471,7 +509,6 @@
 	}
 	snprintf(nick, sizeof(nick), "%s", spw->pw_name);
 	snprintf(prefix, sizeof(prefix),"%s/irc", spw->pw_dir);
-	if (argc <= 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')) usage();
 
 	for(i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) {
 		switch (argv[i][1]) {
@@ -484,6 +521,9 @@
 			default: usage(); break;
 		}
 	}
+
+	if(i != argc) usage();
+
 	irc = tcpopen(port);
 	if(!snprintf(path, sizeof(path), "%s/%s", prefix, host)) {
 		fprintf(stderr, "%s", "ii: path to irc directory too long\n");
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.