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