4 * Copyright (c) 1998-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
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.
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);
96 eNextState ShutdownLookuUrl(AsyncIO *IO)
101 eNextState TerminateLookupUrl(AsyncIO *IO)
106 eNextState LookupUrlResult(AsyncIO *IO)
108 return eTerminateConnection; /// /TODO
111 int LookupUrl(StrBuf *ShorterUrlStr)
119 IO = (AsyncIO*) malloc(sizeof(AsyncIO));
120 memset(IO, 0, sizeof(AsyncIO));
121 IO->CitContext = CloneContext(CC);
123 ParseURL(&IO->ConnectMe, ShorterUrlStr, 80);
124 CurlPrepareURL(IO->ConnectMe);
125 if (! InitcURLIOStruct(IO,
128 "Citadel RSS ShorterURL Expander",
133 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
136 chnd = IO->HttpReq.chnd;
138 OPT(SSL_VERIFYPEER, 0);
139 OPT(SSL_VERIFYHOST, 0);
140 OPT(FOLLOWLOCATION, 10);
141 #ifdef CURLOPT_HTTP_CONTENT_DECODING
142 OPT(HTTP_CONTENT_DECODING, 1);
145 OPT(HEADERFUNCTION , GetLocationString);
146 OPT(WRITEHEADER, ShorterUrlStr);
149 if (server_shutting_down)
152 QueueCurlContext(IO);
162 void CrawlMessageForShorterUrls(HashList *pUrls, StrBuf *Message)
171 while (GetHash(UrlShorteners, IKEY(nShorter), &pv))
174 pch = ChrPtr(Message);
175 pUrl = strstr(pch, ChrPtr((StrBuf*)pv));
176 while ((pUrl != NULL) && (nHits < 99))
178 pCUrl = malloc(sizeof(ConstStr));
181 pch = pUrl + StrLength((StrBuf*)pv);
182 while (isalnum(*pch)||(*pch == '-')||(*pch == '/'))
184 pCUrl->len = pch - pCUrl->Key;
186 Put(pUrls, IKEY(nHits), pCUrl, NULL);
188 pUrl = strstr(pch, ChrPtr((StrBuf*)pv));
193 int SortConstStrByPosition(const void *Item1, const void *Item2)
195 const ConstStr *p1, *p2;
196 p1 = (const ConstStr*) Item1;
197 p2 = (const ConstStr*) Item2;
198 if (p1->Key == p2->Key)
200 if (p1->Key > p2->Key)
205 HashList *GetShorterUrls(StrBuf *Message)
208 /* we just suspect URL shorteners to be inside of feeds from twitter
209 * or other short content messages, so don't crawl through real blogs.
211 if (StrLength(Message) > 500)
214 pUrls = NewHash(1, Flathash);
215 CrawlMessageForShorterUrls(pUrls, Message);
217 if (GetCount(pUrls) > 0)
224 void ExpandShortUrls(StrBuf *Message, HashList *pUrls, int Callback)
231 StrBuf *ShorterUrlStr;
237 Shadow = NewStrBufPlain(NULL, StrLength(Message));
238 SortByPayload (pUrls, SortConstStrByPosition);
240 ShorterUrlStr = NewStrBufPlain(NULL, StrLength(Message));
242 pch = ChrPtr(Message);
243 pche = pch + StrLength(Message);
244 Pos = GetNewHashPos(pUrls, 1);
245 while (GetNextHashPos(pUrls, Pos, &len, &Key, &pv))
247 pCUrl = (ConstStr*) pv;
249 if (pch != pCUrl->Key)
250 StrBufAppendBufPlain(Shadow, pch, pCUrl->Key - pch, 0);
252 StrBufPlain(ShorterUrlStr, CKEY(*pCUrl));
253 if (LookupUrl(ShorterUrlStr))
255 StrBufAppendBufPlain(Shadow, HKEY("<a href=\""), 0);
256 StrBufAppendBuf(Shadow, ShorterUrlStr, 0);
257 StrBufAppendBufPlain(Shadow, HKEY("\">"), 0);
258 StrBufAppendBuf(Shadow, ShorterUrlStr, 0);
259 StrBufAppendBufPlain(Shadow, HKEY("["), 0);
260 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
261 StrBufAppendBufPlain(Shadow, HKEY("]</a>"), 0);
265 StrBufAppendBufPlain(Shadow, HKEY("<a href=\""), 0);
266 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
267 StrBufAppendBufPlain(Shadow, HKEY("\">"), 0);
268 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
269 StrBufAppendBufPlain(Shadow, HKEY("</a>"), 0);
271 pch = pCUrl->Key + pCUrl->len + 1;
275 StrBufAppendBufPlain(Shadow, pch, pche - pch, 0);
276 FlushStrBuf(Message);
277 StrBufAppendBuf(Message, Shadow, 0);
279 FreeStrBuf(&ShorterUrlStr);
287 void LoadUrlShorteners(void)
291 const char *POS = NULL;
292 const char *Err = NULL;
293 StrBuf *Content, *Line;
296 UrlShorteners = NewHash(0, Flathash);
298 fd = open(file_citadel_urlshorteners, 0);
302 Content = NewStrBufPlain(NULL, SIZ);
304 while (POS != StrBufNOTNULL)
306 StrBufTCP_read_buffered_line_fast (Line, Content, &POS, &fd, 1, 1, &Err);
308 if ((*ChrPtr(Line) != '#') && (StrLength(Line) > 0))
310 Put(UrlShorteners, IKEY(i), Line, HFreeStrBuf);
320 FreeStrBuf(&Content);
325 void shorter_url_cleanup(void)
327 DeleteHash(&UrlShorteners);
331 CTDL_MODULE_INIT(urldeshortener)
335 syslog(LOG_INFO, "%s\n", curl_version());
339 LoadUrlShorteners ();
340 CtdlRegisterCleanupHook(shorter_url_cleanup);
342 return "UrlShortener";