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