跳转到内容

User:Antigng-bot/invisible

维基百科,自由的百科全书
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <pthread.h>
#include "network.h"
#include "convert.h"
#include "struct.h"
#include "auth.h"
struct problemlist
{
        char *title;
         char *id;
        struct problemlist *next;
} ;
struct neditargv
{
    int count;
        char *title;
        HTTP newtext;
};
FILE *debug;
HTTP dump;
int threadc[1200];
char threads[1050][3000];
pthread_t threadpool[1200];
struct hashlist *hash;
struct  queue *ql;
pthread_mutex_t cs;
pthread_mutex_t tcs;
pthread_mutex_t lock;
pthread_mutex_t hcs;
struct problemlist *head;
int action=0;
char low[10000]={0},mid[10000]={0},high[10000]={0};
void show(struct problemlist *head);
void *threadfunc(void *i);
int threadini(int count);
int proceedchild(char *title);
int smartedit(struct neditargv *p);
int checkhash(struct hashlist *h);
int verify(int a);
int ini();
struct problemlist *allpagequery();
int main(int argc,char *argv[])
{     int count=0;
       char passwd[30];
        struct neditargv p;
        pthread_t thd;
        if(argc!=2) return -1;
        else strcpy(passwd,argv[1]);
        hash=hashini();
        ql=queueini();
        hastoken=1;
        debug=fopen("/data/project/antigng-bot/ilog.txt","w+");
       pthread_mutex_init(&cs,NULL);
        pthread_mutex_init(&lock,NULL);
       pthread_mutex_init(&tcs,NULL);
      pthread_mutex_init(&hcs,NULL);
        login("antigng-bot",passwd);
       printf("%d\n",  ini());
        hastoken=0;
        pthread_create(&thd,0,tokenmanage,0);
        fflush(stdout);
        threadini(1010);
        head=allpagequery();
        printf("query ok\n");
        fflush(stdout);
        dump=hopen();
        hputs("&text=",strlen("&text="),dump);
        action=1;
        while(head!=NULL)
        {
         sleep(1);
        }
        hashdestroy(hash);

        p.title="User:Antigng/奇怪的字符";
        p.newtext=dump;
  hclose(dump);
        pthread_mutex_destroy(&cs);
        pthread_mutex_destroy(&tcs);
        pthread_mutex_destroy(&lock);
        pthread_mutex_destroy(&hcs);
        fclose(debug);
        system("cp /data/project/antigng-bot/ilog.txt /data/project/antigng-bot/public_html/invisible.txt");
        return 0;
}
int threadini(int count)
{
  pthread_attr_t a;
   int i=0;
   int flag=0;
   pthread_attr_init (&a);
  pthread_attr_setdetachstate (&a, PTHREAD_CREATE_DETACHED);
  for(i=0;i<count;i++)
  {
      threadc[i]=i;
      threads[i][0]=0;
      flag=pthread_create(&threadpool[i],&a,threadfunc,(void *)(threadc+i));
      fflush(stdout);
  }
  pthread_attr_destroy(&a);

    return 0;
}
int ini()
{
        HTTP h;
        int i;
        char ch,temp[15],flag;
        h=hopen();
        i=get("/w/index.php?title=User:Antigng/rtl&action=raw",0,h);
        if(i)
        {
                hclose(h);
                return -1;
 }
       if(skipresponseheader(h)) return -2;

                while(!heof(h))
                {
                        hgets(temp,9,h);
                        sscanf(temp,"%d",&i);
                        if(i<10000)
                        {
                                low[i]=1;
                        }
                        else if(i>60000&&i<70000)
                        {
                                mid[i-60000]=1;
                        }
                        else if(i>120000&&i<130000)
                        {
                                high[i-120000]=1;
                        }
                }
                return 0;
}
int verify(int a)
{
        if(a<10000) return low[a];
        if(a>60000&&a<70000) return mid[a-60000];
        if(a>120000&&a<130000) return high[a-120000];
        return 0;
}

void show(struct problemlist *head)
{
   FILE *f;
   f=fopen("/data/project/Antigng-bot/b.txt","w+");
   while(head)
   {
     fprintf(f,"#[[%s]]\n",head->title);
         head=head->next;
   }
   fclose(f);
}
void *threadfunc(void *c)
{
     int i=*(int *)c;
    int exit=0;
      while(!action) sleep(1);
      while(1)
      {
            pthread_mutex_lock(&hcs);
            if(head)
            {
                  strcpy(threads[i],head->title);
                  head=head->next;
            }
            else exit=1;
            pthread_mutex_unlock(&hcs);
            if(exit) break;
            else
            {
                while(!hastoken) sleep(1);
 proceedchild(threads[i]);
            }

      }
      return NULL;
}
int proceedchild(char *title)
{
    HTTP f,result;
        int i=0,j=0,k=0,count=0,flag,status=0,error=0,todo=0,bra=0,countl=0,warning=0,pos=0,check=0,mul=0;
        char chk[5]={0};
        char c[1000],line[20000],tt[500],tmp[500],temp[800],ch;
        const char match[4]={-30,-128,-114};
        const int length=strlen(match);
        struct neditargv point;
        if(!title)
        {
          return -1;
        }
        strcpy(c,"/w/index.php?action=raw&title=");
        URLEncode(title,strlen(title),tt,500);
        strcat(c,tt);
        f=hopen();

        if(get(c,0,f))
        {
           hclose(f);
           return -5;
         }
         if(skipresponseheader(f)) return -1;
         status=0;todo=0;countl=0;
        bra=0;result=hopen();hputs("&text=",strlen("&text="),result);
        while(!heof(f))
        {

                hgets(line,9500,f);
                i=0;
                while(line[i])
                {

                        if(line[i]==match[0])
 {
                                for(j=0;j<length;j++)
                                {
                                        if(line[i+j]!=match[j]) break;
                                }

                                if(j==length)
                                {
                                    i=i+j;

                                        j=0;
                                if(todo==0)                     todo=1;
                                }

                        }
                      if(!line[i]) break;
                      if(status==0&&line[i])
                        {
                                if(-64<=line[i]&&line[i]<-32)
                                {
                                        status=2;
                                        countl=0;
                                        chk[countl]=line[i]+64;
                                        countl++;
                                }
                                else if(-32<=line[i]&&line[i]<-16)
                                {
                                        status=3;
                                        countl=0;
                                        chk[countl]=line[i]+32;
                                        countl++;
                                }
                                else if(-16<=line[i]&&line[i]<-8)
                                {
                                        status=4;
                                        countl=0;
                                        chk[countl]=line[i]+16;
                                        countl++;
                                }
                                else if(line[i]>0)
                                {
 status=countl=0;
                                }
                                else status= -1;
                        }
                        else if(status>0)
                        {
                                 if(-128<=line[i]&&line[i]<-64)
                                        {
                                                chk[countl]=line[i]+128;
                                                countl++;

                                        }
                                        else
                                        {
                                                printf("%d",line[i]);
                                                status=-1;
                                        }
                                        if(countl==status)
                                        {
                                                mul=1;check=0;

                                                for(countl--;countl>=0;countl--)
                                                {
                                                        check+=mul*chk[countl];
                                                        mul*=64;
                                                }
                                                if(verify(check)) status=-2;
                                                else status=0;
                                                countl=0;
                                        }
                        }
                       smartURLEncode(line[i],result);
                        i++;
                        }
                }
        if(todo)
        {

                 pthread_mutex_lock(&lock);
                  fprintf(debug,"%s %d\r\n",title,status);
                  fflush(debug);
pthread_mutex_unlock(&lock);
                  URLEncode(title,strlen(title),tt,490);
                 point.title=tt;

              point.newtext=result;
        //if(status>=0)     smartedit(&point);
        }
        hclose(f);
        hclose(result);
    return 0;
}

int smartedit(struct neditargv *p)
{
        HTTP f;

        const char *invalid1="notoken",*invalid2="badtoken";
        const int length1=strlen(invalid1),length2=strlen(invalid2);
        char line[1050],url[10240],aft[1000];

        int resultlength=0,i=0,j=0,error=0,count=0,find;

        strcpy(url,"/w/api.php?action=edit&title=");
        strcat(url,p->title);
        strcpy(aft,"&summary=bot:remove%20invisible%20characters&bot=1&minor=1&nocreate=1&format=xml&token=");
        find=strlen(aft);
    do
        {
                f=hopen();
                error=0;count++;
        while(!hastoken)
    {
                usleep(100000);
        }
        pthread_mutex_lock(&cs);
        aft[find]=0;
        strcat(aft,token);
        pthread_mutex_unlock(&cs);
        hrewind(p->newtext);
        smartpost(url,p->newtext,aft,1,f);
        resultlength=0;error=0;
 while(!heof(f))
        {
            hgets(line,1000,f);
                i=0;j=0;
                while(line[j])
                {
                if(line[j]==invalid1[0])
                {
                        for(i=0;i<length1;i++)
                        {
                                if(line[j+i]!=invalid1[i]) break;
                        }
                        if(i==length1) {error=1;break;}
                }
                if(line[j]==invalid2[0])
                {
                        for(i=0;i<length2;i++)
                        {
                                if(line[j+i]!=invalid2[i]) break;
                        }
                        if(i==length2) {error=1;break;}
                }
                j++;

                }
        }




        hclose(f);

        if(error)
        {
                pthread_mutex_lock(&cs);
                i=0;
                while(token[i])
                {
                        if(aft[find+i]!=token[i]) break;
                        i++;
}
                if(token[i]==0) hastoken=0;
                pthread_mutex_unlock(&cs);
        }
    }while(error&&count<3);
        return 0;
}

int checkhash(struct hashlist *h)
{
        FILE *f;
        extern HASHMAX;
        struct ext *temp;
        int i;
        f=fopen("hash.txt","w+");
        for(i=0;i<HASHMAX;i++)
        {
                fprintf(f,"%d:%d",i,h[i].flag);
                if(h[i].flag)
                {
                        temp=h[i].node;
                        while(temp)
                        {
                                fprintf(f," %s",temp->title);
                                temp=temp->next;
                        }
                }
                fprintf(f,"\n");
        }
        fclose(f);
        return 0;
}

struct problemlist *allpagequery()
{
        HTTP f;
        char line[2000]={0},url[1000]={0},snd[1000]={0},ecd[400]={0},id[400]={0},title[400]={0},sroffset[400]={0};
        const char *match="p pageid=\"",*nextmatch="continue apcontinue=\"",*titlematch="title=\"";
        const int mtl=strlen(match),nxl=strlen(nextmatch),ttl=strlen(titlematch);
        int mtc=0,nxc=0,idc=0,ttc=0,tgc=0,itc=0;
        int status=0,next=0,count=0;
        char ch,chr;
        char xml[10];
        int tpl=0;
        struct problemlist *pre,*temp,*head=0;
        strcpy(url,"/w/api.php?action=query&format=xml&list=allpages&apnamespace=0&aplimit=500");
        do
        {
                strcpy(snd,url);
                if(next)
                {
                     strcat(snd,"&apcontinue=");
                         strcat(snd,sroffset);
                }
                f=hopen();
       if(get(snd,1,f))
           {
                hclose(f);
                        return NULL;
                }
          shipresponseheader(f);
          next=0;status=0;
          do
          {
             if(ch=='<')
                 {
                    if(status==0)
                        {
                        status=1;
                        mtc=0,nxc=0,idc=0,ttc=0,tgc=0,itc=0;
                        }
                        else {
                        printf("%d\n",status);
                        return NULL;
                        }
                 }
                 if(status==1)
                 {
                    if(ch!='>')
 {
                      if(match[mtc]==ch&&mtc < mtl)
                          {
                             mtc++;
                          }
                          else
                          {
                             mtc=0;
                          }
                          if(nextmatch[nxc]==ch&&mtc < nxl)
                          {
                             nxc++;
                          }
                          else
                          {
                             nxc=0;
                          }
                          if(mtc==mtl)
                          {
                             status=3;

                           }
                          if(nxc==nxl) status=2;
                         }
                         else status=0;
                  }
                  if(status==2)
                  {
                     if(ch!='\"')
                         {
                             sroffset[next]=chr;
                                 next++;
                                 sroffset[next]=0;
                          }
                          else if(next)
                          {
                             status=0;
                          }
                  }
                        if(status==3)
                        {
                         if(ch!='\"')
                           {
                              id[idc]=chr;
                                 idc++;
                                 id[idc]=0;
                          }
                          else if(idc)
                          {
                             status=4;
                          }
                        }
                        if(status==4)
                        {
                           if(titlematch[ttc]==ch&&ttc < ttl)
                          {
                             ttc++;
                          }
                          else
                          {
                             ttc=0;
                          }
                          if(ttc==ttl)
                          {
                             status=5;

                           }
                        }
                        if(status==5)
                        {

                          if(ch!='\"')
                           {
                              title[tgc]=chr;
                                 tgc++;
                                 title[tgc]=0;
                          }
                          else if(tgc)
                          {
                            temp=(struct problemlist *)malloc(sizeof(struct problemlist));
                                temp->title=(char *)calloc(tgc+3,1);
                                strcpy(temp->title,title);
                               temp->id=(char *)calloc(idc+3,1);
                                strcpy(temp->id,id);
                                temp->next=0;
                            if(head)
                                {
                                   pre->next=temp;
                                   pre=temp;
                                }
                                else head=pre=temp;
                             status=0;
                          }
                        }

                ch=hgetc(f);
                 if(ch=='&')
                 {
                    tpl=0;
                     do
                         {
                             xml[tpl]=ch;
                                 tpl++;
                                 ch=hgetc(f);
                        }
                        while(!heof(f)&&tpl<8&&ch!=';');

                        xml[tpl]=ch;
                        tpl++;
                        xml[tpl]=0;
                        if(ch!=';')
                        {

                           return NULL;
                        }
                        if(xmlparse(xml,&chr)) return NULL;
                  }
                  else chr=ch;
          }while(!heof(f));
     hclose(f);
        }while(next);
        return head;
}
老版本
新版本
更新的版本