Split RSS Parser...
[citadel.git] / citadel / modules / urldeshortener / serv_expand_shorter_urls.c
1 /*
2  *
3  * Copyright (c) 1998-2009 by the citadel.org team
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "sysdep.h"
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <termios.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <pwd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <syslog.h>
31
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
34 # include <time.h>
35 #else
36 # if HAVE_SYS_TIME_H
37 #  include <sys/time.h>
38 # else
39 #  include <time.h>
40 # endif
41 #endif
42 #include <sys/wait.h>
43 #include <ctype.h>
44 #include <string.h>
45 #include <limits.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <assert.h>
50
51 #include <libcitadel.h>
52 #include "citadel.h"
53 #include "server.h"
54 #include "citserver.h"
55 #include "support.h"
56 #include "config.h"
57 #include "control.h"
58 #include "user_ops.h"
59 #include "database.h"
60 #include "msgbase.h"
61 #include "internet_addressing.h"
62 #include "genstamp.h"
63 #include "domain.h"
64 #include "ctdl_module.h"
65 #include "locate_host.h"
66 #include "citadel_dirs.h"
67
68 #include "event_client.h"
69
70 HashList *UrlShorteners = NULL;
71
72 size_t GetLocationString( void *ptr, size_t size, size_t nmemb, void *userdata)
73 {
74 #define LOCATION "location"
75         if (strncasecmp((char*)ptr, LOCATION, sizeof(LOCATION) - 1) == 0)
76         {
77                 StrBuf *pURL = (StrBuf*) userdata;
78                 char *pch = (char*) ptr;
79                 char *pche;
80                 
81                 pche = pch + (size * nmemb);
82                 pch += sizeof(LOCATION);
83                 
84                 while (isspace(*pch) || (*pch == ':'))
85                         pch ++;
86
87                 while (isspace(*pche) || (*pche == '\0'))
88                         pche--;
89                 
90                 FlushStrBuf(pURL);
91                 StrBufPlain(pURL, pch, pche - pch + 1); 
92         }
93         return size * nmemb;
94 }
95
96
97 eNextState LookupUrlResult(AsyncIO *IO)
98 {
99         return eTerminateConnection; /// /TODO
100 }
101
102 int LookupUrl(StrBuf *ShorterUrlStr)
103 {
104         CURLcode sta;
105         int rc = 0;
106         CURL *chnd;
107         AsyncIO *IO;
108
109
110         IO = (AsyncIO*) malloc(sizeof(AsyncIO));
111         memset(IO, 0, sizeof(AsyncIO));
112         IO->CitContext = CloneContext(CC);
113
114         ParseURL(&IO->ConnectMe, ShorterUrlStr, 80);
115         CurlPrepareURL(IO->ConnectMe);
116
117         if (! evcurl_init(IO, 
118 //                        Ctx, 
119                           NULL,
120                           "Citadel RSS ShorterURL Expander",
121                           LookupUrlResult))
122         {
123                 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
124                 goto shutdown;
125         }
126         chnd = IO->HttpReq.chnd;
127
128         OPT(SSL_VERIFYPEER, 0);
129         OPT(SSL_VERIFYHOST, 0);
130         OPT(FOLLOWLOCATION, 10);
131 #ifdef CURLOPT_HTTP_CONTENT_DECODING
132         OPT(HTTP_CONTENT_DECODING, 1);
133         OPT(ENCODING, "");
134 #endif 
135         OPT(HEADERFUNCTION , GetLocationString);
136         OPT(WRITEHEADER, ShorterUrlStr);
137
138
139         if (CtdlThreadCheckStop())
140                 goto shutdown ;
141
142         evcurl_handle_start(IO);
143
144 shutdown:
145
146         return rc;
147
148 }
149
150
151
152 void CrawlMessageForShorterUrls(HashList *pUrls, StrBuf *Message)
153 {
154         int nHits = 0;
155         void *pv;
156         int nShorter = 0;
157         const char *pch;
158         const char *pUrl;
159         ConstStr *pCUrl;
160
161         while (GetHash(UrlShorteners, IKEY(nShorter), &pv))
162         {
163                 nShorter++;
164                 pch = ChrPtr(Message);
165                 pUrl = strstr(pch, ChrPtr((StrBuf*)pv));
166                 while ((pUrl != NULL) && (nHits < 99))
167                 {
168                         pCUrl = malloc(sizeof(ConstStr));
169
170                         pCUrl->Key = pUrl;
171                         pch = pUrl + StrLength((StrBuf*)pv);
172                         while (isalnum(*pch)||(*pch == '-')||(*pch == '/'))
173                                 pch++;
174                         pCUrl->len = pch - pCUrl->Key;
175
176                         Put(pUrls, IKEY(nHits), pCUrl, NULL);
177                         nHits ++;
178                         pUrl = strstr(pch, ChrPtr((StrBuf*)pv));
179                 }
180         }
181 }
182
183 int SortConstStrByPosition(const void *Item1, const void *Item2)
184 {
185         const ConstStr *p1, *p2;
186         p1 = (const ConstStr*) Item1;
187         p2 = (const ConstStr*) Item2;
188         if (p1->Key == p2->Key)
189                 return 0;
190         if (p1->Key > p2->Key)
191                 return 1;
192         return -1;
193 }
194
195 void ExpandShortUrls(StrBuf *Message)
196 {
197         StrBuf *Shadow;
198         HashList *pUrls;
199         ConstStr *pCUrl;
200         const char *pch;
201         const char *pche;
202
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.
205          */
206         if (StrLength(Message) > 500)
207                 return;
208
209         pUrls = NewHash(1, Flathash);
210         CrawlMessageForShorterUrls(pUrls, Message);
211
212         if (GetCount(pUrls) > 0)
213         {
214                 StrBuf *ShorterUrlStr;
215                 HashPos *Pos;
216                 const char *Key;
217                 void *pv;
218                 long len;
219
220                 Shadow = NewStrBufPlain(NULL, StrLength(Message));
221                 SortByPayload (pUrls, SortConstStrByPosition);
222
223                 ShorterUrlStr = NewStrBufPlain(NULL, StrLength(Message));
224
225                 pch = ChrPtr(Message);
226                 pche = pch + StrLength(Message);
227                 Pos = GetNewHashPos(pUrls, 1);
228                 while (GetNextHashPos(pUrls, Pos, &len, &Key, &pv))
229                 {
230                         pCUrl = (ConstStr*) pv;
231
232                         if (pch != pCUrl->Key)
233                                 StrBufAppendBufPlain(Shadow, pch, pCUrl->Key - pch, 0);
234                         
235                         StrBufPlain(ShorterUrlStr, CKEY(*pCUrl));
236                         if (LookupUrl(ShorterUrlStr))
237                         {
238                                 StrBufAppendBufPlain(Shadow, HKEY("<a href=\""), 0);
239                                 StrBufAppendBuf(Shadow, ShorterUrlStr, 0);
240                                 StrBufAppendBufPlain(Shadow, HKEY("\">"), 0);
241                                 StrBufAppendBuf(Shadow, ShorterUrlStr, 0);
242                                 StrBufAppendBufPlain(Shadow, HKEY("["), 0);
243                                 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
244                                 StrBufAppendBufPlain(Shadow, HKEY("]</a>"), 0);
245                         }
246                         else
247                         {
248                                 StrBufAppendBufPlain(Shadow, HKEY("<a href=\""), 0);
249                                 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
250                                 StrBufAppendBufPlain(Shadow, HKEY("\">"), 0);
251                                 StrBufAppendBufPlain(Shadow, pCUrl->Key, pCUrl->len, 0);
252                                 StrBufAppendBufPlain(Shadow, HKEY("</a>"), 0);
253                         }
254                         pch = pCUrl->Key + pCUrl->len + 1;
255
256                 }
257                 if (pch < pche)
258                         StrBufAppendBufPlain(Shadow, pch, pche - pch, 0);
259                 FlushStrBuf(Message);
260                 StrBufAppendBuf(Message, Shadow, 0);
261
262                 FreeStrBuf(&ShorterUrlStr);
263                 FreeStrBuf(&Shadow);
264                 DeleteHashPos(&Pos);
265         }
266
267         DeleteHash(&pUrls);
268 }
269
270 void LoadUrlShorteners(void)
271 {
272         int i = 0;
273         int fd;
274         const char *POS = NULL;
275         const char *Err = NULL;
276         StrBuf *Content, *Line;
277
278
279         UrlShorteners = NewHash(0, Flathash);
280
281         fd = open(file_citadel_urlshorteners, 0);
282
283         if (fd != 0)
284         {
285                 Content = NewStrBufPlain(NULL, SIZ);
286                 Line = NewStrBuf();
287                 while (POS != StrBufNOTNULL)
288                 {
289                         StrBufTCP_read_buffered_line_fast (Line, Content, &POS, &fd, 1, 1, &Err);
290                         StrBufTrim(Line);
291                         if ((*ChrPtr(Line) != '#') && (StrLength(Line) > 0))
292                         {
293                                 Put(UrlShorteners, IKEY(i), Line, HFreeStrBuf);
294                                 i++;
295                                 Line = NewStrBuf();
296                         }
297                         else
298                                 FlushStrBuf(Line);
299                         if (POS == NULL)
300                                 POS = StrBufNOTNULL;
301                 }
302                 FreeStrBuf(&Line);
303                 FreeStrBuf(&Content);
304         }
305         close(fd);
306 }
307
308 void shorter_url_cleanup(void)
309 {
310         DeleteHash(&UrlShorteners);
311 }
312
313
314 CTDL_MODULE_INIT(urldeshortener)
315 {
316         if (threading)
317         {
318                 CtdlLogPrintf(CTDL_INFO, "%s\n", curl_version());
319         }
320         else 
321         {
322                 LoadUrlShorteners ();
323                 CtdlRegisterCleanupHook(shorter_url_cleanup);
324         }
325         return "UrlShortener";
326 }