]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_pgsql.cpp
Closer now, but somehow libpq invalidates the file descriptor on server shutdown...
[user/henk/code/inspircd.git] / src / modules / extra / m_pgsql.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <cstdlib>
15 #include <sstream>
16 #include <string>
17 #include <deque>
18 #include <map>
19 #include <libpq-fe.h>
20
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24
25 #include "inspircd.h"
26 #include "configreader.h"
27
28 #include "m_sqlv2.h"
29
30 /* $ModDesc: PostgreSQL Service Provider module for all other m_sql* modules, uses v2 of the SQL API */
31 /* $CompileFlags: -I`pg_config --includedir` `perl extra/pgsql_config.pl` */
32 /* $LinkerFlags: -L`pg_config --libdir` -lpq */
33 /* $ModDep: m_sqlv2.h */
34
35
36 /* SQLConn rewritten by peavey to
37  * use EventHandler instead of
38  * InspSocket. This is much neater
39  * and gives total control of destroy
40  * and delete of resources.
41  */
42
43 /* Forward declare, so we can have the typedef neatly at the top */
44 class SQLConn;
45
46 typedef std::map<std::string, SQLConn*> ConnMap;
47
48 /* CREAD,       Connecting and wants read event
49  * CWRITE,      Connecting and wants write event
50  * WREAD,       Connected/Working and wants read event
51  * WWRITE,      Connected/Working and wants write event
52  * RREAD,       Resetting and wants read event
53  * RWRITE,      Resetting and wants write event
54  */
55 enum SQLstatus { CREAD, CWRITE, WREAD, WWRITE, RREAD, RWRITE };
56
57 /** SQLhost::GetDSN() - Overload to return correct DSN for PostgreSQL
58  */
59 std::string SQLhost::GetDSN()
60 {
61         std::ostringstream conninfo("connect_timeout = '2'");
62
63         if (ip.length())
64                 conninfo << " hostaddr = '" << ip << "'";
65
66         if (port)
67                 conninfo << " port = '" << port << "'";
68
69         if (name.length())
70                 conninfo << " dbname = '" << name << "'";
71
72         if (user.length())
73                 conninfo << " user = '" << user << "'";
74
75         if (pass.length())
76                 conninfo << " password = '" << pass << "'";
77
78         if (ssl)
79         {
80                 conninfo << " sslmode = 'require'";
81         }
82         else
83         {
84                 conninfo << " sslmode = 'disable'";
85         }
86
87         return conninfo.str();
88 }
89
90 class ReconnectTimer : public InspTimer
91 {
92   private:
93         SQLConn* conn;
94         Module* mod;
95   public:
96         ReconnectTimer(InspIRCd* SI, SQLConn* c, Module* m)
97         : InspTimer(5, SI->Time(), false), conn(c), mod(m)
98         {
99         }
100         virtual void Tick(time_t TIME);
101 };
102
103
104 /** Used to resolve sql server hostnames
105  */
106 class SQLresolver : public Resolver
107 {
108  private:
109         SQLhost host;
110         Module* mod;
111  public:
112         SQLresolver(Module* m, InspIRCd* Instance, const SQLhost& hi, bool &cached)
113         : Resolver(Instance, hi.host, DNS_QUERY_FORWARD, cached, (Module*)m), host(hi), mod(m)
114         {
115         }
116
117         virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached);
118
119         virtual void OnError(ResolverError e, const std::string &errormessage)
120         {
121                 ServerInstance->Log(DEBUG, "DNS lookup failed (%s), dying horribly", errormessage.c_str());
122         }
123 };
124
125 /** QueryQueue, a queue of queries waiting to be executed.
126  * This maintains two queues internally, one for 'priority'
127  * queries and one for less important ones. Each queue has
128  * new queries appended to it and ones to execute are popped
129  * off the front. This keeps them flowing round nicely and no
130  * query should ever get 'stuck' for too long. If there are
131  * queries in the priority queue they will be executed first,
132  * 'unimportant' queries will only be executed when the
133  * priority queue is empty.
134  *
135  * We store lists of SQLrequest's here, by value as we want to avoid storing
136  * any data allocated inside the client module (in case that module is unloaded
137  * while the query is in progress).
138  *
139  * Because we want to work on the current SQLrequest in-situ, we need a way
140  * of accessing the request we are currently processing, QueryQueue::front(),
141  * but that call needs to always return the same request until that request
142  * is removed from the queue, this is what the 'which' variable is. New queries are
143  * always added to the back of one of the two queues, but if when front()
144  * is first called then the priority queue is empty then front() will return
145  * a query from the normal queue, but if a query is then added to the priority
146  * queue then front() must continue to return the front of the *normal* queue
147  * until pop() is called.
148  */
149
150 class QueryQueue : public classbase
151 {
152 private:
153         typedef std::deque<SQLrequest> ReqDeque;
154
155         ReqDeque priority;      /* The priority queue */
156         ReqDeque normal;        /* The 'normal' queue */
157         enum { PRI, NOR, NON } which;   /* Which queue the currently active element is at the front of */
158
159 public:
160         QueryQueue()
161         : which(NON)
162         {
163         }
164
165         void push(const SQLrequest &q)
166         {
167                 //ServerInstance->Log(DEBUG, "QueryQueue::push(): Adding %s query to queue: %s", ((q.pri) ? "priority" : "non-priority"), q.query.q.c_str());
168
169                 if(q.pri)
170                         priority.push_back(q);
171                 else
172                         normal.push_back(q);
173         }
174
175         void pop()
176         {
177                 if((which == PRI) && priority.size())
178                 {
179                         priority.pop_front();
180                 }
181                 else if((which == NOR) && normal.size())
182                 {
183                         normal.pop_front();
184                 }
185
186                 /* Reset this */
187                 which = NON;
188
189                 /* Silently do nothing if there was no element to pop() */
190         }
191
192         SQLrequest& front()
193         {
194                 switch(which)
195                 {
196                         case PRI:
197                                 return priority.front();
198                         case NOR:
199                                 return normal.front();
200                         default:
201                                 if(priority.size())
202                                 {
203                                         which = PRI;
204                                         return priority.front();
205                                 }
206
207                                 if(normal.size())
208                                 {
209                                         which = NOR;
210                                         return normal.front();
211                                 }
212
213                                 /* This will probably result in a segfault,
214                                  * but the caller should have checked totalsize()
215                                  * first so..meh - moron :p
216                                  */
217
218                                 return priority.front();
219                 }
220         }
221
222         std::pair<int, int> size()
223         {
224                 return std::make_pair(priority.size(), normal.size());
225         }
226
227         int totalsize()
228         {
229                 return priority.size() + normal.size();
230         }
231
232         void PurgeModule(Module* mod)
233         {
234                 DoPurgeModule(mod, priority);
235                 DoPurgeModule(mod, normal);
236         }
237
238 private:
239         void DoPurgeModule(Module* mod, ReqDeque& q)
240         {
241                 for(ReqDeque::iterator iter = q.begin(); iter != q.end(); iter++)
242                 {
243                         if(iter->GetSource() == mod)
244                         {
245                                 if(iter->id == front().id)
246                                 {
247                                         /* It's the currently active query.. :x */
248                                         iter->SetSource(NULL);
249                                 }
250                                 else
251                                 {
252                                         /* It hasn't been executed yet..just remove it */
253                                         iter = q.erase(iter);
254                                 }
255                         }
256                 }
257         }
258 };
259
260 /** PgSQLresult is a subclass of the mostly-pure-virtual class SQLresult.
261  * All SQL providers must create their own subclass and define it's methods using that
262  * database library's data retriveal functions. The aim is to avoid a slow and inefficient process
263  * of converting all data to a common format before it reaches the result structure. This way
264  * data is passes to the module nearly as directly as if it was using the API directly itself.
265  */
266
267 class PgSQLresult : public SQLresult
268 {
269         PGresult* res;
270         int currentrow;
271         int rows;
272         int cols;
273
274         SQLfieldList* fieldlist;
275         SQLfieldMap* fieldmap;
276 public:
277         PgSQLresult(Module* self, Module* to, unsigned long id, PGresult* result)
278         : SQLresult(self, to, id), res(result), currentrow(0), fieldlist(NULL), fieldmap(NULL)
279         {
280                 rows = PQntuples(res);
281                 cols = PQnfields(res);
282
283                 //ServerInstance->Log(DEBUG, "Created new PgSQL result; %d rows, %d columns, %s affected", rows, cols, PQcmdTuples(res));
284         }
285
286         ~PgSQLresult()
287         {
288                 /* If we allocated these, free them... */
289                 if(fieldlist)
290                         DELETE(fieldlist);
291
292                 if(fieldmap)
293                         DELETE(fieldmap);
294
295                 PQclear(res);
296         }
297
298         virtual int Rows()
299         {
300                 if(!cols && !rows)
301                 {
302                         return atoi(PQcmdTuples(res));
303                 }
304                 else
305                 {
306                         return rows;
307                 }
308         }
309
310         virtual int Cols()
311         {
312                 return PQnfields(res);
313         }
314
315         virtual std::string ColName(int column)
316         {
317                 char* name = PQfname(res, column);
318
319                 return (name) ? name : "";
320         }
321
322         virtual int ColNum(const std::string &column)
323         {
324                 int n = PQfnumber(res, column.c_str());
325
326                 if(n == -1)
327                 {
328                         throw SQLbadColName();
329                 }
330                 else
331                 {
332                         return n;
333                 }
334         }
335
336         virtual SQLfield GetValue(int row, int column)
337         {
338                 char* v = PQgetvalue(res, row, column);
339
340                 if(v)
341                 {
342                         return SQLfield(std::string(v, PQgetlength(res, row, column)), PQgetisnull(res, row, column));
343                 }
344                 else
345                 {
346                         //ServerInstance->Log(DEBUG, "PQgetvalue returned a null pointer..nobody wants to tell us what this means");
347                         throw SQLbadColName();
348                 }
349         }
350
351         virtual SQLfieldList& GetRow()
352         {
353                 /* In an effort to reduce overhead we don't actually allocate the list
354                  * until the first time it's needed...so...
355                  */
356                 if(fieldlist)
357                 {
358                         fieldlist->clear();
359                 }
360                 else
361                 {
362                         fieldlist = new SQLfieldList;
363                 }
364
365                 if(currentrow < PQntuples(res))
366                 {
367                         int cols = PQnfields(res);
368
369                         for(int i = 0; i < cols; i++)
370                         {
371                                 fieldlist->push_back(GetValue(currentrow, i));
372                         }
373
374                         currentrow++;
375                 }
376
377                 return *fieldlist;
378         }
379
380         virtual SQLfieldMap& GetRowMap()
381         {
382                 /* In an effort to reduce overhead we don't actually allocate the map
383                  * until the first time it's needed...so...
384                  */
385                 if(fieldmap)
386                 {
387                         fieldmap->clear();
388                 }
389                 else
390                 {
391                         fieldmap = new SQLfieldMap;
392                 }
393
394                 if(currentrow < PQntuples(res))
395                 {
396                         int cols = PQnfields(res);
397
398                         for(int i = 0; i < cols; i++)
399                         {
400                                 fieldmap->insert(std::make_pair(ColName(i), GetValue(currentrow, i)));
401                         }
402
403                         currentrow++;
404                 }
405
406                 return *fieldmap;
407         }
408
409         virtual SQLfieldList* GetRowPtr()
410         {
411                 SQLfieldList* fl = new SQLfieldList;
412
413                 if(currentrow < PQntuples(res))
414                 {
415                         int cols = PQnfields(res);
416
417                         for(int i = 0; i < cols; i++)
418                         {
419                                 fl->push_back(GetValue(currentrow, i));
420                         }
421
422                         currentrow++;
423                 }
424
425                 return fl;
426         }
427
428         virtual SQLfieldMap* GetRowMapPtr()
429         {
430                 SQLfieldMap* fm = new SQLfieldMap;
431
432                 if(currentrow < PQntuples(res))
433                 {
434                         int cols = PQnfields(res);
435
436                         for(int i = 0; i < cols; i++)
437                         {
438                                 fm->insert(std::make_pair(ColName(i), GetValue(currentrow, i)));
439                         }
440
441                         currentrow++;
442                 }
443
444                 return fm;
445         }
446
447         virtual void Free(SQLfieldMap* fm)
448         {
449                 DELETE(fm);
450         }
451
452         virtual void Free(SQLfieldList* fl)
453         {
454                 DELETE(fl);
455         }
456 };
457
458 /** SQLConn represents one SQL session.
459  */
460 class SQLConn : public EventHandler
461 {
462   private:
463         InspIRCd*               Instance;
464         SQLhost                 confhost;       /* The <database> entry */
465         Module*                 us;                     /* Pointer to the SQL provider itself */
466         PGconn*                 sql;            /* PgSQL database connection handle */
467         SQLstatus               status;         /* PgSQL database connection status */
468         bool                    qinprog;        /* If there is currently a query in progress */
469         QueryQueue              queue;          /* Queue of queries waiting to be executed on this connection */
470         time_t                  idle;           /* Time we last heard from the database */
471
472   public:
473         SQLConn(InspIRCd* SI, Module* self, const SQLhost& hi)
474         : EventHandler(), Instance(SI), confhost(hi), us(self), sql(NULL), status(CWRITE), qinprog(false)
475         {
476                 idle = this->Instance->Time();
477                 if(!DoConnect())
478                 {
479                         Instance->Log(DEFAULT, "WARNING: Could not connect to database with id: " + ConvToStr(hi.id));
480                         Close();
481                         DelayReconnect();
482                 }
483         }
484
485         ~SQLConn()
486         {
487                 Close();
488         }
489
490         virtual void HandleEvent(EventType et, int errornum)
491         {
492                 switch (et)
493                 {
494                         case EVENT_READ:
495                                 OnDataReady();
496                         break;
497
498                         case EVENT_WRITE:
499                                 OnWriteReady();
500                         break;
501
502                         case EVENT_ERROR:
503                                 Close();
504                                 DelayReconnect();
505                         break;
506
507                         default:
508                         break;
509                 }
510         }
511
512         bool DoConnect()
513         {
514                 if(!(sql = PQconnectStart(confhost.GetDSN().c_str())))
515                 {
516                         Instance->Log(DEBUG, "Couldn't allocate PGconn structure, aborting: %s", PQerrorMessage(sql));
517                         return false;
518                 }
519                 if(PQstatus(sql) == CONNECTION_BAD)
520                 {
521                         Instance->Log(DEBUG, "PQconnectStart failed: %s", PQerrorMessage(sql));
522                         return false;
523                 }
524                 ShowStatus();
525                 if(PQsetnonblocking(sql, 1) == -1)
526                 {
527                         Instance->Log(DEBUG, "Couldn't set connection nonblocking: %s", PQerrorMessage(sql));
528                         return false;
529                 }
530                 this->fd = PQsocket(sql);
531
532                 Instance->Log(DEBUG, "New SQL socket: %d", this->fd);
533
534                 if(this->fd <= -1)
535                 {
536                         Instance->Log(DEBUG, "PQsocket says we have an invalid FD: %d", this->fd);
537                         return false;
538                 }
539
540                 if (!this->Instance->SE->AddFd(this))
541                 {
542                         Instance->Log(DEBUG, "A PQsocket cant be added to the socket engine!");
543                         return false;
544                 }
545
546                 /* Socket all hooked into the engine, now to tell PgSQL to start connecting */
547
548                 return DoPoll();
549         }
550
551         bool DoPoll()
552         {
553                 ShowStatus();
554                 switch(PQconnectPoll(sql))
555                 {
556                         case PGRES_POLLING_WRITING:
557                                 Instance->SE->WantWrite(this);
558                                 status = CWRITE;
559                                 return true;
560                         case PGRES_POLLING_READING:
561                                 status = CREAD;
562                                 return true;
563                         case PGRES_POLLING_FAILED:
564                                 return false;
565                         case PGRES_POLLING_OK:
566                                 status = WWRITE;
567                                 return DoConnectedPoll();
568                         default:
569                                 return true;
570                 }
571         }
572
573         bool DoConnectedPoll()
574         {
575                 if(!qinprog && queue.totalsize())
576                 {
577                         /* There's no query currently in progress, and there's queries in the queue. */
578                         SQLrequest& query = queue.front();
579                         DoQuery(query);
580                 }
581
582                 if(PQconsumeInput(sql))
583                 {
584                         Instance->Log(DEBUG, "PQconsumeInput succeeded");
585
586                         /* We just read stuff from the server, that counts as it being alive
587                          * so update the idle-since time :p
588                          */
589                         idle = this->Instance->Time();
590
591                         ShowStatus();
592
593                         if(PQisBusy(sql))
594                         {
595                                 Instance->Log(DEBUG, "Still busy processing command though");
596                         }
597                         else if(qinprog)
598                         {
599                                 //ServerInstance->Log(DEBUG, "Looks like we have a result to process!");
600
601                                 /* Grab the request we're processing */
602                                 SQLrequest& query = queue.front();
603
604                                 Instance->Log(DEBUG, "ID is %lu", query.id);
605
606                                 /* Get a pointer to the module we're about to return the result to */
607                                 Module* to = query.GetSource();
608
609                                 /* Fetch the result.. */
610                                 PGresult* result = PQgetResult(sql);
611
612                                 /* PgSQL would allow a query string to be sent which has multiple
613                                  * queries in it, this isn't portable across database backends and
614                                  * we don't want modules doing it. But just in case we make sure we
615                                  * drain any results there are and just use the last one.
616                                  * If the module devs are behaving there will only be one result.
617                                  */
618                                 while (PGresult* temp = PQgetResult(sql))
619                                 {
620                                         PQclear(result);
621                                         result = temp;
622                                 }
623
624                                 if(to)
625                                 {
626                                         /* ..and the result */
627                                         PgSQLresult reply(us, to, query.id, result);
628
629                                         /* Fix by brain, make sure the original query gets sent back in the reply */
630                                         reply.query = query.query.q;
631
632                                         Instance->Log(DEBUG, "Got result, status code: %s; error message: %s", PQresStatus(PQresultStatus(result)), PQresultErrorMessage(result));
633
634                                         switch(PQresultStatus(result))
635                                         {
636                                                 case PGRES_EMPTY_QUERY:
637                                                 case PGRES_BAD_RESPONSE:
638                                                 case PGRES_FATAL_ERROR:
639                                                         reply.error.Id(QREPLY_FAIL);
640                                                         reply.error.Str(PQresultErrorMessage(result));
641                                                 default:;
642                                                         /* No action, other values are not errors */
643                                         }
644
645                                         reply.Send();
646
647                                         /* PgSQLresult's destructor will free the PGresult */
648                                 }
649                                 else
650                                 {
651                                         /* If the client module is unloaded partway through a query then the provider will set
652                                          * the pointer to NULL. We cannot just cancel the query as the result will still come
653                                          * through at some point...and it could get messy if we play with invalid pointers...
654                                          */
655                                         Instance->Log(DEBUG, "Looks like we're handling a zombie query from a module which unloaded before it got a result..fun. ID: %lu", query.id);
656                                         PQclear(result);
657                                 }
658                                 qinprog = false;
659                                 queue.pop();
660                                 DoConnectedPoll();
661                         }
662                         else
663                         {
664                                 ShowStatus();
665                                 Instance->Log(DEBUG, "Eh!? We just got a read event, and connection isn't busy..but no result :(");
666                         }
667                         return true;
668                 }
669                 else
670                 {
671                         /* I think we'll assume this means the server died...it might not,
672                          * but I think that any error serious enough we actually get here
673                          * deserves to reconnect [/excuse]
674                          * Returning true so the core doesn't try and close the connection.
675                          */
676                         Instance->Log(DEBUG, "PQconsumeInput failed: %s", PQerrorMessage(sql));
677                         Close();
678                         DelayReconnect();
679                         return false;
680                 }
681         }
682
683         bool DoResetPoll()
684         {
685                 switch(PQresetPoll(sql))
686                 {
687                         case PGRES_POLLING_WRITING:
688                                 //ServerInstance->Log(DEBUG, "PGresetPoll: PGRES_POLLING_WRITING");
689                                 Instance->SE->WantWrite(this);
690                                 status = CWRITE;
691                                 return DoResetPoll();
692                         case PGRES_POLLING_READING:
693                                 //ServerInstance->Log(DEBUG, "PGresetPoll: PGRES_POLLING_READING");
694                                 status = CREAD;
695                                 return true;
696                         case PGRES_POLLING_FAILED:
697                                 //ServerInstance->Log(DEBUG, "PGresetPoll: PGRES_POLLING_FAILED: %s", PQerrorMessage(sql));
698                                 return false;
699                         case PGRES_POLLING_OK:
700                                 //ServerInstance->Log(DEBUG, "PGresetPoll: PGRES_POLLING_OK");
701                                 status = WWRITE;
702                                 return DoConnectedPoll();
703                         default:
704                                 //ServerInstance->Log(DEBUG, "PGresetPoll: wtf?");
705                                 return true;
706                 }
707         }
708
709         void ShowStatus()
710         {
711                 switch(PQstatus(sql))
712                 {
713                         case CONNECTION_OK:
714                                 Instance->Log(DEBUG, "PQstatus: CONNECTION_OK: Ok.");
715                                 break;
716
717                         case CONNECTION_BAD:
718                                 Instance->Log(DEBUG, "PQstatus: CONNECTION_BAD: Bad.");
719                                 break;
720
721                         case CONNECTION_STARTED:
722                                 Instance->Log(DEBUG, "PQstatus: CONNECTION_STARTED: Waiting for connection to be made.");
723                                 break;
724
725                         case CONNECTION_MADE:
726                                 Instance->Log(DEBUG, "PQstatus: CONNECTION_MADE: Connection OK; waiting to send.");
727                                 break;
728
729                         case CONNECTION_AWAITING_RESPONSE:
730                                 Instance->Log(DEBUG, "PQstatus: CONNECTION_AWAITING_RESPONSE: Waiting for a response from the server.");
731                                 break;
732
733                         case CONNECTION_AUTH_OK:
734                                 Instance->Log(DEBUG, "PQstatus: CONNECTION_AUTH_OK: Received authentication; waiting for backend start-up to finish.");
735                                 break;
736
737                         case CONNECTION_SSL_STARTUP:
738                                 Instance->Log(DEBUG, "PQstatus: CONNECTION_SSL_STARTUP: Negotiating SSL encryption.");
739                                 break;
740
741                         case CONNECTION_SETENV:
742                                 Instance->Log(DEBUG, "PQstatus: CONNECTION_SETENV: Negotiating environment-driven parameter settings.");
743                                 break;
744
745                         default:
746                                 Instance->Log(DEBUG, "PQstatus: ???");
747                 }
748         }
749
750         bool OnDataReady()
751         {
752                 /* Always return true here, false would close the socket - we need to do that ourselves with the pgsql API */
753                 Instance->Log(DEBUG, "OnDataReady(): status = %s", StatusStr());
754                 return DoEvent();
755         }
756
757         bool OnWriteReady()
758         {
759                 /* Always return true here, false would close the socket - we need to do that ourselves with the pgsql API */
760                 Instance->Log(DEBUG, "OnWriteReady(): status = %s", StatusStr());
761                 return DoEvent();
762         }
763
764         bool OnConnected()
765         {
766                 Instance->Log(DEBUG, "OnConnected(): status = %s", StatusStr());
767                 return DoEvent();
768         }
769
770         void DelayReconnect()
771         {
772                 ReconnectTimer* timer = new ReconnectTimer(Instance, this, us);
773                 Instance->Timers->AddTimer(timer);
774         }
775
776         bool DoEvent()
777         {
778                 bool ret;
779
780                 if((status == CREAD) || (status == CWRITE))
781                 {
782                         ret = DoPoll();
783                 }
784                 else if((status == RREAD) || (status == RWRITE))
785                 {
786                         ret = DoResetPoll();
787                 }
788                 else
789                 {
790                         ret = DoConnectedPoll();
791                 }
792                 return ret;
793         }
794
795         const char* StatusStr()
796         {
797                 if(status == CREAD) return "CREAD";
798                 if(status == CWRITE) return "CWRITE";
799                 if(status == WREAD) return "WREAD";
800                 if(status == WWRITE) return "WWRITE";
801                 return "Err...what, erm..BUG!";
802         }
803
804         SQLerror DoQuery(SQLrequest &req)
805         {
806                 if((status == WREAD) || (status == WWRITE))
807                 {
808                         if(!qinprog)
809                         {
810                                 /* Parse the command string and dispatch it */
811
812                                 /* Pointer to the buffer we screw around with substitution in */
813                                 char* query;
814                                 /* Pointer to the current end of query, where we append new stuff */
815                                 char* queryend;
816                                 /* Total length of the unescaped parameters */
817                                 unsigned int paramlen;
818
819                                 paramlen = 0;
820
821                                 for(ParamL::iterator i = req.query.p.begin(); i != req.query.p.end(); i++)
822                                 {
823                                         paramlen += i->size();
824                                 }
825
826                                 /* To avoid a lot of allocations, allocate enough memory for the biggest the escaped query could possibly be.
827                                  * sizeofquery + (totalparamlength*2) + 1
828                                  *
829                                  * The +1 is for null-terminating the string for PQsendQuery()
830                                  */
831
832                                 query = new char[req.query.q.length() + (paramlen*2) + 1];
833                                 queryend = query;
834
835                                 /* Okay, now we have a buffer large enough we need to start copying the query into it and escaping and substituting
836                                  * the parameters into it...
837                                  */
838
839                                 for(unsigned int i = 0; i < req.query.q.length(); i++)
840                                 {
841                                         if(req.query.q[i] == '?')
842                                         {
843                                                 /* We found a place to substitute..what fun.
844                                                  * Use the PgSQL calls to escape and write the
845                                                  * escaped string onto the end of our query buffer,
846                                                  * then we "just" need to make sure queryend is
847                                                  * pointing at the right place.
848                                                  */
849
850                                                 if(req.query.p.size())
851                                                 {
852                                                         int error = 0;
853                                                         size_t len = 0;
854
855 #ifdef PGSQL_HAS_ESCAPECONN
856                                                         len = PQescapeStringConn(sql, queryend, req.query.p.front().c_str(), req.query.p.front().length(), &error);
857 #else
858                                                         len = PQescapeString         (queryend, req.query.p.front().c_str(), req.query.p.front().length());
859 #endif
860                                                         if(error)
861                                                         {
862                                                                 Instance->Log(DEBUG, "Apparently PQescapeStringConn() failed somehow...don't know how or what to do...");
863                                                         }
864
865                                                         Instance->Log(DEBUG, "Appended %d bytes of escaped string onto the query", len);
866
867                                                         /* Incremenet queryend to the end of the newly escaped parameter */
868                                                         queryend += len;
869
870                                                         /* Remove the parameter we just substituted in */
871                                                         req.query.p.pop_front();
872                                                 }
873                                                 else
874                                                 {
875                                                         Instance->Log(DEBUG, "Found a substitution location but no parameter to substitute :|");
876                                                         break;
877                                                 }
878                                         }
879                                         else
880                                         {
881                                                 *queryend = req.query.q[i];
882                                                 queryend++;
883                                         }
884                                 }
885
886                                 /* Null-terminate the query */
887                                 *queryend = 0;
888
889                                 Instance->Log(DEBUG, "Attempting to dispatch query: %s", query);
890
891                                 req.query.q = query;
892
893                                 if(PQsendQuery(sql, query))
894                                 {
895                                         Instance->Log(DEBUG, "Dispatched query successfully");
896                                         qinprog = true;
897                                         delete[] query;
898                                         return SQLerror();
899                                 }
900                                 else
901                                 {
902                                         Instance->Log(DEBUG, "Failed to dispatch query: %s", PQerrorMessage(sql));
903                                         delete[] query;
904                                         return SQLerror(QSEND_FAIL, PQerrorMessage(sql));
905                                 }
906                         }
907                 }
908                 return SQLerror(BAD_CONN, "Can't query until connection is complete");
909         }
910
911         SQLerror Query(const SQLrequest &req)
912         {
913                 queue.push(req);
914
915                 if(!qinprog && queue.totalsize())
916                 {
917                         /* There's no query currently in progress, and there's queries in the queue. */
918                         SQLrequest& query = queue.front();
919                         return DoQuery(query);
920                 }
921                 else
922                 {
923                         return SQLerror();
924                 }
925         }
926
927         void OnUnloadModule(Module* mod)
928         {
929                 queue.PurgeModule(mod);
930         }
931
932         const SQLhost GetConfHost()
933         {
934                 return confhost;
935         }
936
937         void Close() {
938                 DoClose();
939         }
940
941         void DoClose()
942         {
943                 Instance->Log(DEBUG,"SQLConn::Close");
944                 Instance->Log(DEBUG, "FD IS: %d", this->fd);
945
946                 if (!this->Instance->SE->DelFd(this))
947                 {
948                         Instance->Log(DEBUG, "PQsocket cant be removed from the socket engine!");
949                 }
950                 else {
951                         Instance->Log(DEBUG, "FD WAS REMOVED!");
952                 }
953                 if(sql)
954                 {
955                         PQfinish(sql);
956                         sql = NULL;
957                 }
958         }
959
960 };
961
962 class ModulePgSQL : public Module
963 {
964   private:
965         ConnMap connections;
966         unsigned long currid;
967         char* sqlsuccess;
968
969   public:
970         ModulePgSQL(InspIRCd* Me)
971         : Module::Module(Me), currid(0)
972         {
973                 ServerInstance->UseInterface("SQLutils");
974
975                 sqlsuccess = new char[strlen(SQLSUCCESS)+1];
976
977                 strlcpy(sqlsuccess, SQLSUCCESS, strlen(SQLSUCCESS));
978
979                 if (!ServerInstance->PublishFeature("SQL", this))
980                 {
981                         throw ModuleException("m_pgsql: Unable to publish feature 'SQL'");
982                 }
983
984                 ReadConf();
985
986                 ServerInstance->PublishInterface("SQL", this);
987         }
988
989         virtual ~ModulePgSQL()
990         {
991                 ClearAllConnections();
992                 delete[] sqlsuccess;
993                 ServerInstance->UnpublishInterface("SQL", this);
994                 ServerInstance->UnpublishFeature("SQL");
995                 ServerInstance->DoneWithInterface("SQLutils");
996         }
997
998         void Implements(char* List)
999         {
1000                 List[I_OnUnloadModule] = List[I_OnRequest] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
1001         }
1002
1003         virtual void OnRehash(userrec* user, const std::string &parameter)
1004         {
1005                 ReadConf();
1006         }
1007
1008         bool HasHost(const SQLhost &host)
1009         {
1010                 for (ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
1011                 {
1012                         if (host == iter->second->GetConfHost())
1013                                 return true;
1014                 }
1015                 return false;
1016         }
1017
1018         bool HostInConf(const SQLhost &h)
1019         {
1020                 ConfigReader conf(ServerInstance);
1021                 for(int i = 0; i < conf.Enumerate("database"); i++)
1022                 {
1023                         SQLhost host;
1024                         host.id         = conf.ReadValue("database", "id", i);
1025                         host.host       = conf.ReadValue("database", "hostname", i);
1026                         host.port       = conf.ReadInteger("database", "port", i, true);
1027                         host.name       = conf.ReadValue("database", "name", i);
1028                         host.user       = conf.ReadValue("database", "username", i);
1029                         host.pass       = conf.ReadValue("database", "password", i);
1030                         host.ssl        = conf.ReadFlag("database", "ssl", "0", i);
1031                         if (h == host)
1032                                 return true;
1033                 }
1034                 return false;
1035         }
1036
1037         void ReadConf()
1038         {
1039                 ClearOldConnections();
1040
1041                 ConfigReader conf(ServerInstance);
1042                 for(int i = 0; i < conf.Enumerate("database"); i++)
1043                 {
1044                         SQLhost host;
1045                         int ipvalid;
1046                         insp_inaddr blargle;
1047
1048                         host.id         = conf.ReadValue("database", "id", i);
1049                         host.host       = conf.ReadValue("database", "hostname", i);
1050                         host.port       = conf.ReadInteger("database", "port", i, true);
1051                         host.name       = conf.ReadValue("database", "name", i);
1052                         host.user       = conf.ReadValue("database", "username", i);
1053                         host.pass       = conf.ReadValue("database", "password", i);
1054                         host.ssl        = conf.ReadFlag("database", "ssl", "0", i);
1055
1056                         if (HasHost(host))
1057                                 continue;
1058
1059                         ipvalid = insp_aton(host.host.c_str(), &blargle);
1060
1061                         if(ipvalid > 0)
1062                         {
1063                                 /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */
1064                                 host.ip = host.host;
1065                                 this->AddConn(host);
1066                         }
1067                         else if(ipvalid == 0)
1068                         {
1069                                 /* Conversion failed, assume it's a host */
1070                                 SQLresolver* resolver;
1071
1072                                 try
1073                                 {
1074                                         bool cached;
1075                                         resolver = new SQLresolver(this, ServerInstance, host, cached);
1076                                         ServerInstance->AddResolver(resolver, cached);
1077                                 }
1078                                 catch(...)
1079                                 {
1080                                         /* THE WORLD IS COMING TO AN END! */
1081                                         ServerInstance->Log(DEBUG, "Couldn't make a SQLresolver..this connection is gonna diiiiiie...actually we just won't create it");
1082                                 }
1083                         }
1084                         else
1085                         {
1086                                 /* Invalid address family, die horribly. */
1087                                 ServerInstance->Log(DEBUG, "insp_aton failed returning -1, oh noes.");
1088                         }
1089                 }
1090         }
1091
1092         void ClearOldConnections()
1093         {
1094                 ConnMap::iterator iter,safei;
1095                 for (iter = connections.begin(); iter != connections.end(); iter++)
1096                 {
1097                         if (!HostInConf(iter->second->GetConfHost()))
1098                         {
1099                                 DELETE(iter->second);
1100                                 safei = iter;
1101                                 --iter;
1102                                 connections.erase(safei);
1103                         }
1104                 }
1105         }
1106
1107         void ClearAllConnections()
1108         {
1109                 ConnMap::iterator i;
1110                 while ((i = connections.begin()) != connections.end())
1111                 {
1112                         connections.erase(i);
1113                         DELETE(i->second);
1114                 }
1115         }
1116
1117         void AddConn(const SQLhost& hi)
1118         {
1119                 if (HasHost(hi))
1120                 {
1121                         ServerInstance->Log(DEFAULT, "WARNING: A pgsql connection with id: %s already exists, possibly due to DNS delay. Aborting connection attempt.", hi.id.c_str());
1122                         return;
1123                 }
1124
1125                 SQLConn* newconn;
1126
1127                 /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */
1128                 newconn = new SQLConn(ServerInstance, this, hi);
1129
1130                 connections.insert(std::make_pair(hi.id, newconn));
1131         }
1132
1133         void ReconnectConn(SQLConn* conn)
1134         {
1135                 for (ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
1136                 {
1137                         if (conn == iter->second)
1138                         {
1139                                 DELETE(iter->second);
1140                                 connections.erase(iter);
1141                                 break;
1142                         }
1143                 }
1144                 ReadConf();
1145         }
1146
1147         virtual char* OnRequest(Request* request)
1148         {
1149                 if(strcmp(SQLREQID, request->GetId()) == 0)
1150                 {
1151                         SQLrequest* req = (SQLrequest*)request;
1152                         ConnMap::iterator iter;
1153
1154                         ServerInstance->Log(DEBUG, "Got query: '%s' with %d replacement parameters on id '%s'", req->query.q.c_str(), req->query.p.size(), req->dbid.c_str());
1155
1156                         if((iter = connections.find(req->dbid)) != connections.end())
1157                         {
1158                                 /* Execute query */
1159                                 req->id = NewID();
1160                                 req->error = iter->second->Query(*req);
1161
1162                                 return (req->error.Id() == NO_ERROR) ? sqlsuccess : NULL;
1163                         }
1164                         else
1165                         {
1166                                 req->error.Id(BAD_DBID);
1167                                 return NULL;
1168                         }
1169                 }
1170
1171                 ServerInstance->Log(DEBUG, "Got unsupported API version string: %s", request->GetId());
1172
1173                 return NULL;
1174         }
1175
1176         virtual void OnUnloadModule(Module* mod, const std::string&     name)
1177         {
1178                 /* When a module unloads we have to check all the pending queries for all our connections
1179                  * and set the Module* specifying where the query came from to NULL. If the query has already
1180                  * been dispatched then when it is processed it will be dropped if the pointer is NULL.
1181                  *
1182                  * If the queries we find are not already being executed then we can simply remove them immediately.
1183                  */
1184                 for(ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
1185                 {
1186                         iter->second->OnUnloadModule(mod);
1187                 }
1188         }
1189
1190         unsigned long NewID()
1191         {
1192                 if (currid+1 == 0)
1193                         currid++;
1194
1195                 return ++currid;
1196         }
1197
1198         virtual Version GetVersion()
1199         {
1200                 return Version(1, 1, 0, 0, VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
1201         }
1202 };
1203
1204 /* move this here to use AddConn, rather that than having the whole
1205  * module above SQLConn, since this is buggin me right now :/
1206  */
1207 void SQLresolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
1208 {
1209         host.ip = result;
1210         ((ModulePgSQL*)mod)->AddConn(host);
1211         ((ModulePgSQL*)mod)->ClearOldConnections();
1212 }
1213
1214 void ReconnectTimer::Tick(time_t time)
1215 {
1216         ((ModulePgSQL*)mod)->ReconnectConn(conn);
1217 }
1218
1219
1220 class ModulePgSQLFactory : public ModuleFactory
1221 {
1222  public:
1223         ModulePgSQLFactory()
1224         {
1225         }
1226
1227         ~ModulePgSQLFactory()
1228         {
1229         }
1230
1231         virtual Module * CreateModule(InspIRCd* Me)
1232         {
1233                 return new ModulePgSQL(Me);
1234         }
1235 };
1236
1237
1238 extern "C" void * init_module( void )
1239 {
1240         return new ModulePgSQLFactory;
1241 }