[mw-devel] MW3 r1076 - trunk/src

arthur at sucs.org arthur at sucs.org
Mon Mar 16 10:18:33 GMT 2009


Author: arthur
Date: 2009-03-16 10:18:33 +0000 (Mon, 16 Mar 2009)
New Revision: 1076

Modified:
   trunk/src/log.c
Log:
make md5 threadsafe, add hashtag recording


Modified: trunk/src/log.c
===================================================================
--- trunk/src/log.c	2009-03-12 19:23:58 UTC (rev 1075)
+++ trunk/src/log.c	2009-03-16 10:18:33 UTC (rev 1076)
@@ -50,6 +50,7 @@
 
 /* local prototype */
 static void *file_url(void * data);
+static void *file_tag(void * data);
 
 /*
  * Check whats said for URLs and log them
@@ -59,7 +60,7 @@
 	char *	regex;	/* the pattern */
 	int   	regflags; /* compile flags */
 	regex_t	*patt; /* compiled version */
-	enum { END=0, URL, FLAG, IGNORE, NOLOG } type;
+	enum { END=0, URL, FLAG, IGNORE, NOLOG, TAG } type;
 	uint32_t flags;
 };
 
@@ -71,10 +72,11 @@
 	{"^http://", 		REG_ICASE, NULL, URL,    0},
 	{"^https://", 		REG_ICASE, NULL, URL,    0},
 	{"^www\\.", 		REG_ICASE, NULL, URL,    0},
-	{"^[[:alpha:].]+\\.(com|org|net|gov|(co|org|net|gov)\\.uk)", 	REG_ICASE|REG_EXTENDED, NULL, URL,    0},
+	{"^[[:alnum:]_.-]+\\.(com|org|net|gov|(co|org|net|gov)\\.uk)", 	REG_ICASE|REG_EXTENDED, NULL, URL,    0},
 	{"nsfw", 		REG_ICASE, NULL, FLAG,   URLFLAG_NSFW},
 	{"^-log", 		REG_ICASE, NULL, NOLOG, 0},
 	{"^(-log)$", 		REG_ICASE, NULL, NOLOG, 0},
+	{"^#[[:alnum:]]+$",	REG_ICASE|REG_EXTENDED, NULL, TAG, 0},
 	{ NULL, 0, NULL, END, 0 }
 };
 
@@ -83,6 +85,11 @@
 	uint32_t flags;
 };
 
+struct taghit {
+	char *tag;
+	char *line;
+};
+
 void catchuri(const char *what)
 {
 	char *text = strdup(what);
@@ -129,6 +136,19 @@
 					free(text);
 					return;
 				}
+				if (u->type == TAG) {
+					struct taghit *tag;
+					pthread_attr_t ptattr;
+
+					pthread_attr_init(&ptattr);
+					pthread_attr_setdetachstate(&ptattr, PTHREAD_CREATE_DETACHED);
+					pthread_t pt;
+					tag = malloc(sizeof(struct taghit));
+					tag->tag = strdup(token);
+					tag->line = strdup(what);
+					pthread_create(&pt, &ptattr, file_tag, tag);
+					pthread_attr_destroy(&ptattr);
+				}
 			}
 			u++;
 		}
@@ -148,12 +168,13 @@
 			struct urihit *uri;
 			pthread_t pt;
 			uri = malloc(sizeof(struct urihit));
-			uri->url = found[i];
+			uri->url = strdup(found[i]);
 			uri->flags = flags;
 			pthread_create(&pt, &ptattr, file_url, uri);
 		}
 		pthread_attr_destroy(&ptattr);
 	}
+	free(text);
 }
 
 char * cleanup_url(const char *in)
@@ -180,9 +201,10 @@
 char *md5(const char *in)
 {
 	int i;
-	static char out[33];
+	char *out;
 	unsigned char msg[16];
 	MD5((unsigned char *)in,strlen(in),msg);
+	out = malloc(33);
 	out[0]=0;
 	for(i=0;i<16;i++)
 		snprintf(&out[strlen(out)],3,"%02x", msg[i]);
@@ -223,6 +245,7 @@
 	url = cleanup_url(uri->url);
 	md = md5(url);
 	snprintf(path,sizeof(path), "http://feeds.delicious.com/v2/json/urlinfo/%s", md);
+	free(md);
 	curl_easy_setopt(c, CURLOPT_URL, path);
 	curl_easy_setopt(c, CURLOPT_NOPROGRESS, 1);
 	curl_easy_setopt(c, CURLOPT_AUTOREFERER, 1);
@@ -270,11 +293,35 @@
 	}
 	db_free(res);
 	sqlite3_free(query);
-	free(uri);
 	free(url);
 	if (title) free(title);
 	if (title) free(deli);
 	curl_easy_cleanup(c);
+	free(uri->url);
+	free(uri); /* aka data */
 	return NULL;
 }
 
+static void *file_tag(void * data) 
+{
+	struct taghit *tag = data;
+	char path[1024];
+	struct db_result *res;
+
+	snprintf(path,sizeof(path),"%s/mwuri.db", STATEDIR);
+	char *query = sqlite3_mprintf("INSERT INTO mwtag (user, tag, added, line) values (%Q,%Q,datetime('now'),%Q)", user->name, tag->tag, tag->line);
+	res = db_query(path, query, 1);
+	if (res == NULL) {
+		res = db_query(path, "CREATE TABLE mwtag (id INTEGER PRIMARY KEY AUTOINCREMENT, user text, tag text, added text, line text )", 0);
+		if (res != NULL) {
+			db_free(res);
+			res = db_query(path, query, 0);
+		}
+	}
+	db_free(res);
+	sqlite3_free(query);
+	free(tag->tag);
+	free(tag->line);
+	free(tag);
+	return NULL;
+}





More information about the mw-devel mailing list