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