+void csdb_dohelp_real(DBConn *, void *);
+
+struct helpinfo {
+ unsigned int numeric;
+ sstring *commandname;
+ Command *cmd;
+};
+
+/* Help stuff */
+void csdb_dohelp(nick *np, Command *cmd) {
+ struct helpinfo *hip;
+
+ hip=(struct helpinfo *)malloc(sizeof(struct helpinfo));
+
+ hip->numeric=np->numeric;
+ hip->commandname=getsstring(cmd->command->content, cmd->command->length);
+ hip->cmd=cmd;
+
+ q9asyncquery(csdb_dohelp_real, (void *)hip,
+ "SELECT languageID, fullinfo from chanserv.help where lower(command)=lower('%s')",cmd->command->content);
+}
+
+void csdb_dohelp_real(DBConn *dbconn, void *arg) {
+ struct helpinfo *hip=arg;
+ nick *np=getnickbynumeric(hip->numeric);
+ reguser *rup;
+ char *result;
+ DBResult *pgres;
+ int j,lang=0;
+
+ if(!dbconn) {
+ freesstring(hip->commandname);
+ free(hip);
+ return;
+ }
+
+ pgres=dbgetresult(dbconn);
+
+ if (!dbquerysuccessful(pgres)) {
+ Error("chanserv",ERR_ERROR,"Error loading help text.");
+ freesstring(hip->commandname);
+ free(hip);
+ return;
+ }
+
+ if (dbnumfields(pgres)!=2) {
+ Error("chanserv",ERR_ERROR,"Help text format error.");
+ dbclear(pgres);
+ freesstring(hip->commandname);
+ free(hip);
+ return;
+ }
+
+ if (!np) {
+ dbclear(pgres);
+ freesstring(hip->commandname);
+ free(hip);
+ return;
+ }
+
+ if ((rup=getreguserfromnick(np)))
+ lang=rup->languageid;
+
+ result=NULL;
+
+ while(dbfetchrow(pgres)) {
+ j=strtoul(dbgetvalue(pgres,0),NULL,10);
+ if ((j==0 && result==NULL) || (j==lang)) {
+ result=dbgetvalue(pgres,1);
+ if(strlen(result)==0)
+ result=NULL;
+ }
+ }
+
+ if (result) {
+ chanservsendmessage(np, "%s", result);
+ } else {
+ cmdsummary *sum=hip->cmd->ext;
+ if (sum->defhelp && *(sum->defhelp)) {
+ chanservsendmessage(np, "%s", sum->defhelp);
+ } else {
+ chanservstdmessage(np, QM_NOHELP, hip->commandname->content);
+ }
+ }
+
+ freesstring(hip->commandname);
+ free(hip);
+ dbclear(pgres);
+}