3 * Copyright (c) 1998-2009 by the citadel.org team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
51 #include <libcitadel.h>
54 #include "citserver.h"
61 #include "internet_addressing.h"
64 #include "ctdl_module.h"
65 #include "locate_host.h"
66 #include "citadel_dirs.h"
68 #include "event_client.h"
70 HashList *UrlShorteners = NULL;
72 size_t GetLocationString( void *ptr, size_t size, size_t nmemb, void *userdata)
74 #define LOCATION "location"
75 if (strncasecmp((char*)ptr, LOCATION, sizeof(LOCATION) - 1) == 0)
77 StrBuf *pURL = (StrBuf*) userdata;
78 char *pch = (char*) ptr;
81 pche = pch + (size * nmemb);
82 pch += sizeof(LOCATION);
84 while (isspace(*pch) || (*pch == ':'))
87 while (isspace(*pche) || (*pche == '\0'))
91 StrBufPlain(pURL, pch, pche - pch + 1);
96 eNextState TerminateLookupUrl(AsyncIO *IO)
100 eNextState LookupUrlResult(AsyncIO *IO)
102 return eTerminateConnection; /// /TODO
105 int LookupUrl(StrBuf *ShorterUrlStr)
113 IO = (AsyncIO*) malloc(sizeof(AsyncIO));
114 memset(IO, 0, sizeof(AsyncIO));
115 IO->CitContext = CloneContext(CC);
117 ParseURL(&IO->ConnectMe, ShorterUrlStr, 80);
118 CurlPrepareURL(IO->ConnectMe);
119 if (! evcurl_init(IO,
122 "Citadel RSS ShorterURL Expander",
126 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
129 chnd = IO->HttpReq.chnd;
131 OPT(SSL_VERIFYPEER, 0);
132 OPT(SSL_VERIFYHOST, 0);
133 OPT(FOLLOWLOCATION, 10);
134 #ifdef CURLOPT_HTTP_CONTENT_DECODING
135 OPT(HTTP_CONTENT_DECODING, 1);
138 OPT(HEADERFUNCTION , GetLocationString);
139 OPT(WRITEHEADER, ShorterUrlStr);
142 if (CtdlThreadCheckStop())
145 evcurl_handle_start(IO);
155 void CrawlMessageForShorterUrls(HashList *pUrls, StrBuf *Message)
164 while (GetHash(UrlShorteners, IKEY(nShorter), &pv))
167 pch = ChrPtr(Message);
168 pUrl = strstr(pch, ChrPtr((StrBuf*)pv));
169 while ((pUrl != NULL) && (nHits < 99))
171 pCUrl = malloc(sizeof(ConstStr));
174 pch = pUrl + StrLength((StrBuf*)pv);
175 while (isalnum(*pch)||(*pch == '-')||(*pch == '/'))
177 pCUrl->len = pch - pCUrl->Key;
179 Put(pUrls, IKEY(nHits), pCUrl, NULL);
181 pUrl = strstr(pch, ChrPtr((StrBuf*)pv));
186 int SortConstStrByPosition(const void *Item1, const void *Item2)
188 const ConstStr *p1, *p2;
189 p1 = (const ConstStr*) Item1;
190 p2 = (const ConstStr*) Item2;
191 if (p1->Key == p2->Key)
193 if (p1->Key > p2->Key)
198 HashList *GetShorterUrls(StrBuf *Message)
201 /* we just suspect URL shorteners to be inside of feeds from twitter
202 * or other short content messages, so don't crawl through real blogs.
204 if (StrLength(Message) > 500)
207 pUrls = NewHash(1, Flathash);
208 CrawlMessageForShorterUrls(pUrls, Message);
210 if (GetCount(pUrls) > 0)
217 void ExpandShortUrls(StrBuf *Message, HashList *pUrls, int Callback)
224 StrBuf *ShorterUrlStr;
230 Shadow = NewStrBufPlain(NULL, StrLength(Message));
231 SortByPayload (pUrls, SortConstStrByPosition);
233 ShorterUrlStr = NewStrBufPlain(NULL, StrLength(Message));
235 pch = ChrPtr(Message);
236 pche = pch + StrLength(Message);
237 Pos = GetNewHashPos(pUrls, 1);
238 while (GetNextHashPos(pUrls, Pos, &len, &Key, &pv))
240 pCUrl = (ConstStr*) pv;
242 if (pch != pCUrl->Key)
243 StrBufAppendBufPlain(Shadow, pch, pCUrl->Key - pch, 0);
245 StrBufPlain(ShorterUrlStr, CKEY(*pCUrl));
246 if (LookupUrl(ShorterUrlStr))
248 StrBufAppendBufPlain(Shadow, HKEY("<a href=\""), 0);
249 StrBufAppendBuf(Shadow, ShorterUrlStr, 0);
250 StrBufAppendBufPlain(Shadow, HKEY("\">"), 0);
251 StrBufAppendBuf(Shadow, ShorterUrlStr, 0);
252 StrBufAppendBufPlain(Shadow, HKEY("["), 0);
253 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
254 StrBufAppendBufPlain(Shadow, HKEY("]</a>"), 0);
258 StrBufAppendBufPlain(Shadow, HKEY("<a href=\""), 0);
259 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
260 StrBufAppendBufPlain(Shadow, HKEY("\">"), 0);
261 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
262 StrBufAppendBufPlain(Shadow, HKEY("</a>"), 0);
264 pch = pCUrl->Key + pCUrl->len + 1;
268 StrBufAppendBufPlain(Shadow, pch, pche - pch, 0);
269 FlushStrBuf(Message);
270 StrBufAppendBuf(Message, Shadow, 0);
272 FreeStrBuf(&ShorterUrlStr);
280 void LoadUrlShorteners(void)
284 const char *POS = NULL;
285 const char *Err = NULL;
286 StrBuf *Content, *Line;
289 UrlShorteners = NewHash(0, Flathash);
291 fd = open(file_citadel_urlshorteners, 0);
295 Content = NewStrBufPlain(NULL, SIZ);
297 while (POS != StrBufNOTNULL)
299 StrBufTCP_read_buffered_line_fast (Line, Content, &POS, &fd, 1, 1, &Err);
301 if ((*ChrPtr(Line) != '#') && (StrLength(Line) > 0))
303 Put(UrlShorteners, IKEY(i), Line, HFreeStrBuf);
313 FreeStrBuf(&Content);
318 void shorter_url_cleanup(void)
320 DeleteHash(&UrlShorteners);
324 CTDL_MODULE_INIT(urldeshortener)
328 syslog(LOG_INFO, "%s\n", curl_version());
332 LoadUrlShorteners ();
333 CtdlRegisterCleanupHook(shorter_url_cleanup);
335 return "UrlShortener";