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