4 * Copyright (c) 1998-2009 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/types.h>
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
38 # include <sys/time.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
52 #include <libcitadel.h>
55 #include "citserver.h"
62 #include "internet_addressing.h"
65 #include "ctdl_module.h"
66 #include "locate_host.h"
67 #include "citadel_dirs.h"
69 #include "event_client.h"
71 HashList *UrlShorteners = NULL;
73 size_t GetLocationString( void *ptr, size_t size, size_t nmemb, void *userdata)
75 #define LOCATION "location"
76 if (strncasecmp((char*)ptr, LOCATION, sizeof(LOCATION) - 1) == 0)
78 StrBuf *pURL = (StrBuf*) userdata;
79 char *pch = (char*) ptr;
82 pche = pch + (size * nmemb);
83 pch += sizeof(LOCATION);
85 while (isspace(*pch) || (*pch == ':'))
88 while (isspace(*pche) || (*pche == '\0'))
92 StrBufPlain(pURL, pch, pche - pch + 1);
97 eNextState TerminateLookupUrl(AsyncIO *IO)
102 eNextState LookupUrlResult(AsyncIO *IO)
104 return eTerminateConnection; /// /TODO
107 int LookupUrl(StrBuf *ShorterUrlStr)
115 IO = (AsyncIO*) malloc(sizeof(AsyncIO));
116 memset(IO, 0, sizeof(AsyncIO));
117 IO->CitContext = CloneContext(CC);
119 ParseURL(&IO->ConnectMe, ShorterUrlStr, 80);
120 CurlPrepareURL(IO->ConnectMe);
121 if (! evcurl_init(IO,
124 "Citadel RSS ShorterURL Expander",
128 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
131 chnd = IO->HttpReq.chnd;
133 OPT(SSL_VERIFYPEER, 0);
134 OPT(SSL_VERIFYHOST, 0);
135 OPT(FOLLOWLOCATION, 10);
136 #ifdef CURLOPT_HTTP_CONTENT_DECODING
137 OPT(HTTP_CONTENT_DECODING, 1);
140 OPT(HEADERFUNCTION , GetLocationString);
141 OPT(WRITEHEADER, ShorterUrlStr);
144 if (server_shutting_down)
147 QueueCurlContext(IO);
157 void CrawlMessageForShorterUrls(HashList *pUrls, StrBuf *Message)
166 while (GetHash(UrlShorteners, IKEY(nShorter), &pv))
169 pch = ChrPtr(Message);
170 pUrl = strstr(pch, ChrPtr((StrBuf*)pv));
171 while ((pUrl != NULL) && (nHits < 99))
173 pCUrl = malloc(sizeof(ConstStr));
176 pch = pUrl + StrLength((StrBuf*)pv);
177 while (isalnum(*pch)||(*pch == '-')||(*pch == '/'))
179 pCUrl->len = pch - pCUrl->Key;
181 Put(pUrls, IKEY(nHits), pCUrl, NULL);
183 pUrl = strstr(pch, ChrPtr((StrBuf*)pv));
188 int SortConstStrByPosition(const void *Item1, const void *Item2)
190 const ConstStr *p1, *p2;
191 p1 = (const ConstStr*) Item1;
192 p2 = (const ConstStr*) Item2;
193 if (p1->Key == p2->Key)
195 if (p1->Key > p2->Key)
200 HashList *GetShorterUrls(StrBuf *Message)
203 /* we just suspect URL shorteners to be inside of feeds from twitter
204 * or other short content messages, so don't crawl through real blogs.
206 if (StrLength(Message) > 500)
209 pUrls = NewHash(1, Flathash);
210 CrawlMessageForShorterUrls(pUrls, Message);
212 if (GetCount(pUrls) > 0)
219 void ExpandShortUrls(StrBuf *Message, HashList *pUrls, int Callback)
226 StrBuf *ShorterUrlStr;
232 Shadow = NewStrBufPlain(NULL, StrLength(Message));
233 SortByPayload (pUrls, SortConstStrByPosition);
235 ShorterUrlStr = NewStrBufPlain(NULL, StrLength(Message));
237 pch = ChrPtr(Message);
238 pche = pch + StrLength(Message);
239 Pos = GetNewHashPos(pUrls, 1);
240 while (GetNextHashPos(pUrls, Pos, &len, &Key, &pv))
242 pCUrl = (ConstStr*) pv;
244 if (pch != pCUrl->Key)
245 StrBufAppendBufPlain(Shadow, pch, pCUrl->Key - pch, 0);
247 StrBufPlain(ShorterUrlStr, CKEY(*pCUrl));
248 if (LookupUrl(ShorterUrlStr))
250 StrBufAppendBufPlain(Shadow, HKEY("<a href=\""), 0);
251 StrBufAppendBuf(Shadow, ShorterUrlStr, 0);
252 StrBufAppendBufPlain(Shadow, HKEY("\">"), 0);
253 StrBufAppendBuf(Shadow, ShorterUrlStr, 0);
254 StrBufAppendBufPlain(Shadow, HKEY("["), 0);
255 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
256 StrBufAppendBufPlain(Shadow, HKEY("]</a>"), 0);
260 StrBufAppendBufPlain(Shadow, HKEY("<a href=\""), 0);
261 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
262 StrBufAppendBufPlain(Shadow, HKEY("\">"), 0);
263 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
264 StrBufAppendBufPlain(Shadow, HKEY("</a>"), 0);
266 pch = pCUrl->Key + pCUrl->len + 1;
270 StrBufAppendBufPlain(Shadow, pch, pche - pch, 0);
271 FlushStrBuf(Message);
272 StrBufAppendBuf(Message, Shadow, 0);
274 FreeStrBuf(&ShorterUrlStr);
282 void LoadUrlShorteners(void)
286 const char *POS = NULL;
287 const char *Err = NULL;
288 StrBuf *Content, *Line;
291 UrlShorteners = NewHash(0, Flathash);
293 fd = open(file_citadel_urlshorteners, 0);
297 Content = NewStrBufPlain(NULL, SIZ);
299 while (POS != StrBufNOTNULL)
301 StrBufTCP_read_buffered_line_fast (Line, Content, &POS, &fd, 1, 1, &Err);
303 if ((*ChrPtr(Line) != '#') && (StrLength(Line) > 0))
305 Put(UrlShorteners, IKEY(i), Line, HFreeStrBuf);
315 FreeStrBuf(&Content);
320 void shorter_url_cleanup(void)
322 DeleteHash(&UrlShorteners);
326 CTDL_MODULE_INIT(urldeshortener)
330 syslog(LOG_INFO, "%s\n", curl_version());
334 LoadUrlShorteners ();
335 CtdlRegisterCleanupHook(shorter_url_cleanup);
337 return "UrlShortener";