Posting to nntp newsgroup with Perl (Net::NNTP)

Discussion in 'Perl Misc' started by sadie-no-reply, Mar 4, 2007.

  1. I am trying to post a message to a newsgroup using Net::NNTP, but I keep
    getting an error message. Help!
    I can read from the newsgroup without problems and $nntp->postok() gives OK.
    I suspect there's something wrong with way I've constructed my message
    array.
    Here's the code.

    use Net::NNTP;

    $server="news.yourserver.com"; #replace this with your nntp server name

    #connect to the nntp server (this works fine)
    $nntp = Net::NNTP->new($server);

    #open a newsgroup (this works fine)
    $nntp->group("nl.test") or die "cannot open nl.test";

    #download and print the last message (this works fine)
    $articleId = $nntp->last();
    @tekst = $nntp->article($articleId);
    for ($i=1; $t=$tekst[0][$i]; $i++)
    {print $t."<BR>\n";}

    #check if I'm alowed to post (returns: ok)
    if ($nntp->postok())
    {print "Posting would be ok\n";}else
    {print "Posting not ok\n";}

    #construct a message - I'm not sure about this.
    #For example, should there be \r\n at the end of each line??
    @message=(
    "Subject: this is test 1\r\n",
    "From: cllq\@noreply.nil\r\n",
    "Newsgroups: nl.test\r\n",
    "Date: Sat, 3 Mar 2007 20:14:13 +0100\r\n",
    "\r\n",
    "This is my message\r\n",
    "Yours sincerely\r\n",
    "Bla\r\n");

    #now post the message
    $nntp->post(@message) or die "post failed"; #this one fails, but why?
    $nntp->quit();

    #alternative code that also fails:
    $nntp->post() or die "post failed"; #this one ok
    $nntp->datasend(@message) or die "datasend nok"; #this one ok
    $nntp->dataend() or die "dataend nok"; #this one fails but why?
    $nntp->quit();
    sadie-no-reply, Mar 4, 2007
    #1
    1. Advertising

  2. On 2007-03-04 12:05, sadie-no-reply <> wrote:
    > I am trying to post a message to a newsgroup using Net::NNTP, but I keep
    > getting an error message. Help!

    [...]
    > #now post the message
    > $nntp->post(@message) or die "post failed"; #this one fails, but why?


    Why ask us? Ask perl:

    $nntp->post(@message) or die "post failed: " . $nntp->message();

    hp


    --
    _ | Peter J. Holzer | Es ist ganz einfach ihn zu verstehen, wenn
    |_|_) | Sysadmin WSR | man nur alle wichtigen Worte im Satz durch
    | | | | andere ersetzt.
    __/ | http://www.hjp.at/ | -- Nils Ketelsen in danr
    Peter J. Holzer, Mar 4, 2007
    #2
    1. Advertising

  3. Thanks for the tip. It returns:
    340 <45eb1d1b$0$3748$> (desired) Article has no
    body -- just headers

    After checking RFC977&850, I changed @message into:

    @message=(
    "Subject: dit is test 2
    From: cllq\@noreply.nil
    Newsgroups: nl.test
    Date: Sat, 4 Mar 2007 20:45:13 +0100

    Hier staat de eerste regel
    Hier de tweede
    ..");

    And it seems to work. At least returns ok status. Haven't seen the result
    back, though...

    "Peter J. Holzer" <> schreef in bericht
    news:...
    > On 2007-03-04 12:05, sadie-no-reply <> wrote:
    >> I am trying to post a message to a newsgroup using Net::NNTP, but I keep
    >> getting an error message. Help!

    > [...]
    >> #now post the message
    >> $nntp->post(@message) or die "post failed"; #this one fails, but why?

    >
    > Why ask us? Ask perl:
    >
    > $nntp->post(@message) or die "post failed: " . $nntp->message();
    >
    > hp
    >
    >
    > --
    > _ | Peter J. Holzer | Es ist ganz einfach ihn zu verstehen, wenn
    > |_|_) | Sysadmin WSR | man nur alle wichtigen Worte im Satz durch
    > | | | | andere ersetzt.
    > __/ | http://www.hjp.at/ | -- Nils Ketelsen in danr
    sadie-no-reply, Mar 4, 2007
    #3
  4. sadie-no-reply

    Jamie Guest

    In <45eb3cf1$0$13606$>,
    "sadie-no-reply" <> mentions:
    >Thanks for the tip. It returns:
    >340 <45eb1d1b$0$3748$> (desired) Article has no
    >body -- just headers
    >
    >After checking RFC977&850, I changed @message into:
    >
    >@message=(
    >"Subject: dit is test 2
    >From: cllq\@noreply.nil
    >Newsgroups: nl.test
    >Date: Sat, 4 Mar 2007 20:45:13 +0100
    >
    >Hier staat de eerste regel
    >Hier de tweede
    >.");
    >
    >And it seems to work. At least returns ok status. Haven't seen the result
    >back, though...


    You might look into the datasend(), dataend() methods of Net::Cmd (these
    are inherited by Net::NNTP, so you can use them withing a $nntp object
    as well)

    Not saying it's proper, but I generally just use telnet combined with
    Net::Cmd and Net::NNTP. (actually, you could easily just use Net::Cmd if
    you wanted) telnet for debugging and getting a feel for what the perl
    modules are doing.


    # The critical bits from one of my "inews" clone scripts:
    #
    # I left out the connect and alarm wrappers.
    #
    $nntp->post();
    while(my $line = <>){
    $nntp->datasend($line);
    }
    $nntp->dataend();
    unless($nntp->ok()){
    die sprintf("NNTP[%03d] %s\n",$nntp->code(),$nntp->message());
    }

    Try posting a small article (to a test group) with telnet to get a feel
    for how NNTP works. It'll make using the perl modules easier.

    Jamie
    --
    http://www.geniegate.com Custom web programming
    Perl * Java * UNIX User Management Solutions
    Jamie, Mar 5, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lloyd Dupont

    [OT] NNTP/newsgroup server

    Lloyd Dupont, Jun 6, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    359
    Lloyd Dupont
    Jun 6, 2005
  2. Anton Bangratz
    Replies:
    0
    Views:
    151
    Anton Bangratz
    Jun 4, 2008
  3. Mike

    Posting with Net::NNTP

    Mike, Sep 11, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    117
  4. Mark Day

    nntp.perl.org posting problem

    Mark Day, Nov 4, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    112
    brian d foy
    Nov 4, 2004
  5. Replies:
    7
    Views:
    370
    Dr.Ruud
    Aug 9, 2006
Loading...

Share This Page