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