Skip to content
Snippets Groups Projects
Select Git revision
  • 92dbd3abbadb6e7ab42c59a4858af56e4f65af50
  • master default protected
  • 9.0
  • marcus/wix3
  • 8.0
  • nt-tools
  • 7.8
  • 7.6
  • 7.4
  • 7.2
  • 7.0
  • 0.6
  • rosuav/latex-markdown-renderer
  • rxnpatch/rxnpatch
  • marcus/gobject-introspection
  • rxnpatch/8.0
  • rosuav/pre-listening-ports
  • rosuav/async-annotations
  • rosuav/pgsql-ssl
  • rxnpatch/rxnpatch-broken/2023-10-06T094250
  • grubba/fdlib
  • v8.0.2020
  • v8.0.2018
  • v8.0.2016
  • v8.0.2014
  • v8.0.2012
  • v8.0.2008
  • v8.0.2006
  • v8.0.2004
  • v8.0.2002
  • v8.0.2000
  • v8.0.1998
  • v8.0.1996
  • v8.0.1994
  • v8.0.1992
  • v8.0.1990
  • v8.0.1988
  • v8.0.1986
  • rxnpatch/clusters/8.0/2025-04-29T124414
  • rxnpatch/2025-04-29T124414
  • v8.0.1984
41 results

NNTP.pmod

Blame
  • NNTP.pmod 2.89 KiB
    // Not finished - Fredrik Hubinette
    
    class protocol
    {
      inherit Stdio.FILE : news;
    
      string rest;
    
      int readreturncode()
      {
        int space, code;
        do {
          space=' ';
          string tmp=news::gets();
          if(!tmp) return 0;
          sscanf(tmp,"%d%c%s",code,space,rest);
        } while(space == '-');
        return code;
      }
    
      string *read_body_lines()
      {
        string *ret=({});
        string s;
        while(s = news::gets())
        {
          if(s=="." || s==".\r") return ret;
          sscanf(s,".%s",s);
          ret+=({s});
        }
        throw(({"NNTP: connection closed by news server.\n",backtrace()}));
      }
    
      string readreturnbody()
      {
        string *tmp=read_body_lines();
        return tmp*"\n"+"\n";
      }
    
      void writebody(string s)
      {
        s=replace(s,"\r","");
        foreach(s/"\n",string line)
          {
    	if(strlen(line) && line[0]=='.')
    	  line="."+line+"\r\n";
    	else
    	  line=line+"\r\n";
    	if(news::write(line) != strlen(line))
    	  throw(({"NNTP: Failed to write body\n",backtrace()}));
          }
        news::write(".\r\n");
      }
    
      int command(string cmd)
      {
        news::write(cmd+"\r\n");
        return readreturncode();
      }
    
      int failsafe_command(string cmd)
      {
        if(command(cmd)/100 != 2)
          throw(({"NEWS "+cmd+" failed\n",backtrace()}));
      }
    
      string do_cmd_with_body(string cmd)
      {
        failsafe_command(cmd);
        return readreturnbody();
      }
    
    };
    
    class client
    {
      inherit protocol;
    
      class Group
      {
        string group;
        int min;
        int max;
      }
    
      array(object(Group)) list_groups()
      {
        array(object(Group)) ret=({});
        failsafe_command("list active");
        foreach(read_body_lines(),string line)
          {
    	object o=Group();
    	if(sscanf(line,"%s %d %d",o->group,o->max,o->min)==3)
    	  ret+=({o});
          }
    
        return ret;
        
      }
    
      object(Group) current_group;
    
      void set_group(object(Group) o)
      {
        if(current_group==o)
          return;
        failsafe_command("group "+o->group);
        current_group=o;
      }
    
      object(Group) go_to_group(string group)
      {
        failsafe_command("group "+group);
        object o=Group();
        o->group=group;
        sscanf(rest,"%d %d %d",int num,o->min,o->max);
        current_group=o;
        return o;
      }
    
      string head(void|int|string x)
      {
        failsafe_command("head"+(x?" "+x:""));
        return readreturnbody();
      }
    
      string body(void|int|string x)
      {
        failsafe_command("body"+(x?" "+x:""));
        return readreturnbody();
      }
    
      string article(void|int|string x)
      {
        failsafe_command("article"+(x?" "+x:""));
        return readreturnbody();
      }
    
      void create(string|void server)
      {
        if(!server)
        {
          server=getenv("NNTPSERVER");
    
          if(!server)
          {
    	// Check /etc/nntpserver here 
          }
        }
    
        if(!connect(server,119))
        {
          throw(({"Failed to connect to news server.\n",backtrace()}));
        }
    
        if(readreturncode()/100 != 2)
          throw(({"Connection refused by NNTP server.\n",backtrace()}));
    
        if(command("mode reader")/100 !=2)
          throw(({"NNTP: mode reader failed.\n",backtrace()}));
        
      }
    };