Skip to content

Instantly share code, notes, and snippets.

@LeetCodes
Forked from mcroydon/nntpd.js
Created September 9, 2021 02:20
Show Gist options
  • Select an option

  • Save LeetCodes/4a5a459d0c504c5aedc28ea189fb1ce8 to your computer and use it in GitHub Desktop.

Select an option

Save LeetCodes/4a5a459d0c504c5aedc28ea189fb1ce8 to your computer and use it in GitHub Desktop.

Revisions

  1. @mcroydon mcroydon renamed this gist Feb 2, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @mcroydon mcroydon revised this gist Feb 2, 2011. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions run.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    $ sudo node nntpd.js
    nntpd.js running.
    HELP
    LIST
    retrieving article <[email protected].1>
  3. @mcroydon mcroydon revised this gist Feb 2, 2011. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions console.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    $ python
    Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from nntplib import NNTP
    >>> nntp = NNTP('127.0.0.1')
    >>> nntp.help()
    ('100 help text follows', ['nntpd.js is a minimal implementation of NNTP in node.js.', 'Available commends:', 'HELP'])
    >>> nntp.list()
    ('215 list of newsgroups follows', [('nodejs.test', '2', '1', 'n')])
    >>> nntp.article('<[email protected]>')
    ('220 1 <[email protected]> article retrieved - head and body follow', '1', '<[email protected]>', ['An article.'])

  4. @mcroydon mcroydon created this gist Feb 2, 2011.
    99 changes: 99 additions & 0 deletions nntpd.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    var net = require('net');

    var END = '\r\n';

    var groups = ['nodejs.test'];
    var articles = {
    '<[email protected]>' : {body : 'An article.'},
    '<[email protected]>' : {body : 'Another article.'},
    };

    var messages = {
    'nodejs.test' : ['<[email protected]>', '<[email protected]>'],
    };

    var startswith = function(string, substring) {
    if (string.indexOf(substring) == 0) {
    return true;
    }
    else {
    return false;
    }
    }

    net.createServer(function (socket) {
    // Party like it's 1986
    socket.setEncoding("ascii");

    socket.on('connect', function() {
    this.client = {socket : socket, group : null, article_index : null};
    socket.write('201 nntpd.js server ready (no posting allowed)' + END);
    });

    socket.on('data', function(data) {
    command = data.toLowerCase();
    if (startswith(command, 'help')) {
    console.log('HELP');
    socket.write('100 help text follows' + END);
    socket.write('nntpd.js is a minimal implementation of NNTP in node.js.' + END);
    socket.write('Available commends:' + END);
    socket.write('HELP' + END);
    socket.write('.' + END);
    }
    else if (startswith(command, 'list')) {
    console.log('LIST');
    socket.write('215 list of newsgroups follows' + END);
    groups.forEach(function(group) {
    socket.write(group + ' ' + messages[group].length + ' ' + '1' + ' ' + 'n' + END);
    });
    socket.write('.' + END);
    }
    else if (startswith(command, 'group')) {
    var group = command.replace(/^group\s/, '').replace(/\r\n$/, '');
    console.log('GROUP ' + group)
    if (messages[group]) {
    this.client.group = group;
    console.log('group set to ' + this.client.group);
    // 1 should be repalced with the first article number
    socket.write('211' + ' ' + messages[group].length + ' ' + 1 + messages[group].length + group + END);
    }
    else {
    socket.write('411 no such news group' + END);
    }
    }
    else if (startswith(command, 'article')) {
    if (command.indexOf('<') != -1) {
    // Accessing article by id
    var id = command.replace(/^article\s/, '').replace(/\r\n$/, '');
    console.log('retrieving article ' + id);
    if (articles[id] !== undefined) {
    // 1 should be replaced with the article number.
    socket.write('220 ' + 1 + ' ' + id + ' article retrieved - head and body follow' + END);
    socket.write(articles[id]['body'] + END);
    socket.write('.' + END);
    }
    else {
    socket.write('430 no such article found' + END);
    }
    }
    else {
    if (this.client.group === null) {
    socket.write('412 no newsgroup has been selected' + END);
    }
    else {
    article = messages[group][int(id)];
    socket.write('220 ' + 1 + article.id + 'article retrieved - head and body follow' + END);
    socket.write(articles[id]['body'] + END);
    socket.write('.' + END);
    }
    }
    }
    else {
    console.log('command ' + command + ' not recognized');
    socket.write('500 command not recognized.' + END);
    }
    });

    }).listen(119, "127.0.0.1")

    console.log('nntpd.js running.');