[mw-devel] MW3 r1069 - trunk/src

arthur at sucs.org arthur at sucs.org
Thu Mar 12 16:30:31 GMT 2009


Author: arthur
Date: 2009-03-12 16:30:30 +0000 (Thu, 12 Mar 2009)
New Revision: 1069

Added:
   trunk/src/log.c
   trunk/src/log.h
Log:
new files to go with [1068] regs #38


Added: trunk/src/log.c
===================================================================
--- trunk/src/log.c	                        (rev 0)
+++ trunk/src/log.c	2009-03-12 16:30:30 UTC (rev 1069)
@@ -0,0 +1,263 @@
+#include <stdio.h>
+#include <signal.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/wait.h>
+#include <arpa/telnet.h>
+#include <ctype.h>
+#include <termcap.h>
+#include <sys/types.h>
+#include <regex.h>
+#include <pthread.h>
+#include <sqlite3.h>
+#include <openssl/md5.h>
+#include <curl/curl.h>
+
+#include "bb.h"
+#include "Parse.h"
+#include "talker_privs.h"
+#include "special.h"
+#include "proto.h"
+#include "talker.h"
+#include "gags.h"
+#include "strings.h"
+#include "special.h"
+#include "script.h"
+#include "js.h"
+#include "files.h"
+
+#include "rooms.h"
+#include "ipc.h"
+#include "sqlite.h"
+
+extern int busy; /* if true dont display messages  i.e. during new/write */
+extern unsigned long rights;
+
+extern struct person *user;
+extern struct folder *fold;
+extern int32_t userposn;
+extern unsigned long loggedin; 
+extern int quietmode;
+extern int runautoexec;
+
+extern struct room myroom;
+extern DirInfo *roomlink;
+
+extern GagInfo gaglist[];
+
+/* ignore list for current user */
+extern struct IgnoreList *ignored;
+
+/* local prototype */
+static void *file_url(void * data);
+
+/*
+ * Check whats said for URLs and log them
+ */
+
+struct uripatt {
+	char *	regex;	/* the pattern */
+	int   	regflags; /* compile flags */
+	regex_t	*patt; /* compiled version */
+	enum { END=0, URL, FLAG, IGNORE } type;
+	uint32_t flags;
+};
+
+#define URLFLAG_NSFW	0x0001
+
+struct uripatt urilist[] = {
+	{"^http:", 		REG_ICASE, NULL, URL,    0},
+	{"^https:", 		REG_ICASE, NULL, URL,    0},
+	{"^www\\.", 		REG_ICASE, NULL, URL,    0},
+	{"nsfw", 		REG_ICASE, NULL, FLAG,   URLFLAG_NSFW},
+	{"^-log", 		REG_ICASE, NULL, IGNORE, 0},
+	{ NULL, 0, NULL, END, 0 }
+};
+
+struct urihit {
+	char * url;
+	uint32_t flags;
+};
+
+void catchuri(const char *what)
+{
+	char *text = strdup(what);
+	char *token;
+	/* kludge, find at most 20 URLs */
+	char *found[20];
+	int nfound=0;
+	uint32_t flags = 0;
+
+	/* split the line into words on whitespace */
+	for (token = strtok(text, " \t"); token != NULL; token=strtok(NULL, " \t")) {
+		struct uripatt *u = urilist;
+		/* compare the words again the regex list */
+		while (u!=NULL && u->regex!=NULL && u->type != END) {
+			/* compile the pattern, first time only */
+			if (u->patt == NULL) {
+				int err;
+				u->patt = malloc(sizeof(regex_t));
+				if ((err=regcomp(u->patt, u->regex, u->regflags))) {
+					char errmsg[128];
+					regerror(err, u->patt, errmsg,128);
+					printf("%s:%d %s\n", __FILE__, __LINE__, errmsg);
+					continue;
+				}
+			}
+			/* compare the pattern and store a hit */
+			if (regexec(u->patt, token, 0, NULL, 0) == 0) {
+				if (u->type == URL) {
+					/* found a url */
+					/*printf("Found url: '%s'\n", token); */
+					if (nfound < 20) found[nfound++] = token;
+				} 
+				if (u->type == FLAG) {
+					/* printf("Found flag: '%s' -> %x\n", token, u->flags); */
+					flags |= u->flags;
+				}
+				if (u->type == IGNORE) {
+					/* ignore whole line */
+					/* printf("IGNORE LINE '%s'\n", token); */
+					free(text);
+					return;
+				}
+			}
+			u++;
+		}
+	}
+
+	/*
+	 * we found urls, so launch background threads to 
+	 * categorise and file the details
+	 * cant do it in foreground, would stall the ui
+	 */
+	if (nfound>0) {
+		int i;
+		pthread_attr_t ptattr;
+		pthread_attr_init(&ptattr);
+		pthread_attr_setdetachstate(&ptattr, PTHREAD_CREATE_DETACHED);
+		for (i=0;i<nfound;i++) {
+			struct urihit *uri;
+			pthread_t pt;
+			uri = malloc(sizeof(struct urihit));
+			uri->url = found[i];
+			uri->flags = flags;
+			pthread_create(&pt, &ptattr, file_url, uri);
+		}
+		pthread_attr_destroy(&ptattr);
+	}
+}
+
+char * cleanup_url(const char *in)
+{
+	char *out;
+	const char *p;
+	if (in == NULL) return NULL;
+	out = malloc(strlen(in)+10);
+	out[0]=0;
+
+	if ((p=strstr(in, "://")) == NULL) {
+		strcpy(out, "http://");
+		p = in;
+	} else {
+		p += 3;
+	}
+	strcat(out, in);
+	if (strchr(p, '/')==NULL) {
+		strcat(out, "/");
+	}
+	return out;	
+}
+
+char *md5(const char *in)
+{
+	int i;
+	static char out[33];
+	unsigned char msg[16];
+	MD5((unsigned char *)in,strlen(in),msg);
+	out[0]=0;
+	for(i=0;i<16;i++)
+		snprintf(&out[strlen(out)],3,"%02x", msg[i]);
+	return out;
+}
+
+char * extract_title(const char *body)
+{
+	char *p,*q;
+	char *out;
+	int len;
+	if ((p=strcasestr(body, "<title>"))==NULL) return NULL;
+	p += 7;
+	if ((q=strcasestr(p, "</title>"))==NULL) return NULL;
+	len = q - p;
+	out = malloc(len+1);
+	memcpy(out, p, len);
+	out[len]=0;
+	return out;
+}
+
+
+static void *file_url(void * data) 
+{
+	struct urihit *uri = data;
+	char path[1024];
+	struct db_result *res;
+	char *url;
+	CURL *c;
+	char *md = NULL;
+	char *deli=NULL;
+	char *body=NULL;
+	char *title=NULL;
+
+	c = curl_easy_init();
+
+	/* lets go fishing on del.icio.us */
+	url = cleanup_url(uri->url);
+	md = md5(url);
+	snprintf(path,sizeof(path), "http://feeds.delicious.com/v2/json/urlinfo/%s", md);
+	curl_easy_setopt(c, CURLOPT_URL, path);
+	curl_easy_setopt(c, CURLOPT_NOPROGRESS, 1);
+	curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, urldata);
+	curl_easy_setopt(c, CURLOPT_WRITEDATA, &deli);
+	curl_easy_perform(c);
+
+	if (deli != NULL && *deli == 0) {
+		free(deli);
+		deli=NULL;
+	}
+
+	/* grab the pages title */
+	curl_easy_setopt(c, CURLOPT_URL, url);
+	curl_easy_setopt(c, CURLOPT_WRITEDATA, &body);
+	curl_easy_perform(c);
+
+	if (body != NULL) {
+		title = extract_title(body);
+		free(body);
+		if (title != NULL && *title == 0) {
+			free(title);
+			title=NULL;
+		}
+	}
+
+	/* fishing mission complete, store the results */
+	snprintf(path,sizeof(path),"%s/mwuri.db", STATEDIR);
+	char *query = sqlite3_mprintf("INSERT INTO mwuri (user, url, added, flags, title, tags) values (%Q,%Q,datetime('now'),%Q,%Q,%Q)", user->name, url, (uri->flags&URLFLAG_NSFW)?"nsfw":NULL, title, deli);
+	res = db_query(path, query, 1);
+	if (res == NULL) {
+		res = db_query(path, "CREATE TABLE mwuri (id INTEGER PRIMARY KEY AUTOINCREMENT, user text, url text, added text, flags text, title text, tags text )", 0);
+		if (res != NULL) {
+			db_free(res);
+			res = db_query(path, query, 0);
+		}
+	}
+	db_free(res);
+	sqlite3_free(query);
+	free(uri);
+	free(url);
+	if (title) free(title);
+	if (title) free(deli);
+	curl_easy_cleanup(c);
+	return NULL;
+}
+

Added: trunk/src/log.h
===================================================================
--- trunk/src/log.h	                        (rev 0)
+++ trunk/src/log.h	2009-03-12 16:30:30 UTC (rev 1069)
@@ -0,0 +1,2 @@
+/* log.c */
+void catchuri(const char *what);





More information about the mw-devel mailing list