Removed the logging facility from citserver, use syslog instead
[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 eNextState TerminateLookupUrl(AsyncIO *IO)
97 {
98 //TOOD
99 }
100 eNextState LookupUrlResult(AsyncIO *IO)
101 {
102         return eTerminateConnection; /// /TODO
103 }
104
105 int LookupUrl(StrBuf *ShorterUrlStr)
106 {
107         CURLcode sta;
108         int rc = 0;
109         CURL *chnd;
110         AsyncIO *IO;
111
112
113         IO = (AsyncIO*) malloc(sizeof(AsyncIO));
114         memset(IO, 0, sizeof(AsyncIO));
115         IO->CitContext = CloneContext(CC);
116
117         ParseURL(&IO->ConnectMe, ShorterUrlStr, 80);
118         CurlPrepareURL(IO->ConnectMe);
119         if (! evcurl_init(IO, 
120 //                        Ctx, 
121                           NULL,
122                           "Citadel RSS ShorterURL Expander",
123                           LookupUrlResult, 
124                           TerminateLookupUrl))
125         {
126                 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
127                 goto shutdown;
128         }
129         chnd = IO->HttpReq.chnd;
130
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);
136         OPT(ENCODING, "");
137 #endif 
138         OPT(HEADERFUNCTION , GetLocationString);
139         OPT(WRITEHEADER, ShorterUrlStr);
140
141
142         if (CtdlThreadCheckStop())
143                 goto shutdown ;
144
145         evcurl_handle_start(IO);
146
147 shutdown:
148
149         return rc;
150
151 }
152
153
154
155 void CrawlMessageForShorterUrls(HashList *pUrls, StrBuf *Message)
156 {
157         int nHits = 0;
158         void *pv;
159         int nShorter = 0;
160         const char *pch;
161         const char *pUrl;
162         ConstStr *pCUrl;
163
164         while (GetHash(UrlShorteners, IKEY(nShorter), &pv))
165         {
166                 nShorter++;
167                 pch = ChrPtr(Message);
168                 pUrl = strstr(pch, ChrPtr((StrBuf*)pv));
169                 while ((pUrl != NULL) && (nHits < 99))
170                 {
171                         pCUrl = malloc(sizeof(ConstStr));
172
173                         pCUrl->Key = pUrl;
174                         pch = pUrl + StrLength((StrBuf*)pv);
175                         while (isalnum(*pch)||(*pch == '-')||(*pch == '/'))
176                                 pch++;
177                         pCUrl->len = pch - pCUrl->Key;
178
179                         Put(pUrls, IKEY(nHits), pCUrl, NULL);
180                         nHits ++;
181                         pUrl = strstr(pch, ChrPtr((StrBuf*)pv));
182                 }
183         }
184 }
185
186 int SortConstStrByPosition(const void *Item1, const void *Item2)
187 {
188         const ConstStr *p1, *p2;
189         p1 = (const ConstStr*) Item1;
190         p2 = (const ConstStr*) Item2;
191         if (p1->Key == p2->Key)
192                 return 0;
193         if (p1->Key > p2->Key)
194                 return 1;
195         return -1;
196 }
197
198 HashList *GetShorterUrls(StrBuf *Message)
199 {
200         HashList *pUrls;
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.
203          */
204         if (StrLength(Message) > 500)
205                 return NULL;
206
207         pUrls = NewHash(1, Flathash);
208         CrawlMessageForShorterUrls(pUrls, Message);
209
210         if (GetCount(pUrls) > 0)
211                 return pUrls;
212         else 
213                 return NULL;
214
215 }
216
217 void ExpandShortUrls(StrBuf *Message, HashList *pUrls, int Callback)
218 {
219         StrBuf *Shadow;
220         ConstStr *pCUrl;
221         const char *pch;
222         const char *pche;
223
224         StrBuf *ShorterUrlStr;
225         HashPos *Pos;
226         const char *Key;
227         void *pv;
228         long len;
229         
230         Shadow = NewStrBufPlain(NULL, StrLength(Message));
231         SortByPayload (pUrls, SortConstStrByPosition);
232                 
233         ShorterUrlStr = NewStrBufPlain(NULL, StrLength(Message));
234                 
235         pch = ChrPtr(Message);
236         pche = pch + StrLength(Message);
237         Pos = GetNewHashPos(pUrls, 1);
238         while (GetNextHashPos(pUrls, Pos, &len, &Key, &pv))
239         {
240                 pCUrl = (ConstStr*) pv;
241
242                 if (pch != pCUrl->Key)
243                         StrBufAppendBufPlain(Shadow, pch, pCUrl->Key - pch, 0);
244                         
245                 StrBufPlain(ShorterUrlStr, CKEY(*pCUrl));
246                 if (LookupUrl(ShorterUrlStr))
247                 {
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);
255                 }
256                 else
257                 {
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);
263                 }
264                 pch = pCUrl->Key + pCUrl->len + 1;
265
266         }
267         if (pch < pche)
268                 StrBufAppendBufPlain(Shadow, pch, pche - pch, 0);
269         FlushStrBuf(Message);
270         StrBufAppendBuf(Message, Shadow, 0);
271
272         FreeStrBuf(&ShorterUrlStr);
273         FreeStrBuf(&Shadow);
274         DeleteHashPos(&Pos);
275         
276
277         DeleteHash(&pUrls);
278 }
279
280 void LoadUrlShorteners(void)
281 {
282         int i = 0;
283         int fd;
284         const char *POS = NULL;
285         const char *Err = NULL;
286         StrBuf *Content, *Line;
287
288
289         UrlShorteners = NewHash(0, Flathash);
290
291         fd = open(file_citadel_urlshorteners, 0);
292
293         if (fd != 0)
294         {
295                 Content = NewStrBufPlain(NULL, SIZ);
296                 Line = NewStrBuf();
297                 while (POS != StrBufNOTNULL)
298                 {
299                         StrBufTCP_read_buffered_line_fast (Line, Content, &POS, &fd, 1, 1, &Err);
300                         StrBufTrim(Line);
301                         if ((*ChrPtr(Line) != '#') && (StrLength(Line) > 0))
302                         {
303                                 Put(UrlShorteners, IKEY(i), Line, HFreeStrBuf);
304                                 i++;
305                                 Line = NewStrBuf();
306                         }
307                         else
308                                 FlushStrBuf(Line);
309                         if (POS == NULL)
310                                 POS = StrBufNOTNULL;
311                 }
312                 FreeStrBuf(&Line);
313                 FreeStrBuf(&Content);
314         }
315         close(fd);
316 }
317
318 void shorter_url_cleanup(void)
319 {
320         DeleteHash(&UrlShorteners);
321 }
322
323
324 CTDL_MODULE_INIT(urldeshortener)
325 {
326         if (threading)
327         {
328                 syslog(LOG_INFO, "%s\n", curl_version());
329         }
330         else 
331         {
332                 LoadUrlShorteners ();
333                 CtdlRegisterCleanupHook(shorter_url_cleanup);
334         }
335         return "UrlShortener";
336 }