Skip to content

Commit

Permalink
mammon.client: add some backend support for emulating usermodes with …
Browse files Browse the repository at this point in the history
…properties (ref #1)
  • Loading branch information
kaniini committed Jan 5, 2015
1 parent 991671e commit 06950ad
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
46 changes: 45 additions & 1 deletion mammon/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import logging
import time
import socket
import copy

from ircreactor.envelope import RFC1459Message
from .utility import CaseInsensitiveDict
from .property import user_property_items, user_mode_items
from .server import eventmgr, get_context
from . import __version__

Expand All @@ -43,6 +46,7 @@ def connection_made(self, transport):
self.hostname = self.peername[0] # XXX - handle rdns...
self.realaddr = self.peername[0]
self.realname = '<unregistered>'
self.props = CaseInsensitiveDict()

self.registered = False
self.registration_lock = 0
Expand All @@ -56,7 +60,6 @@ def do_rdns_check(self):
self.dump_notice('Looking up your hostname...')

rdns = yield from self.ctx.eventloop.getnameinfo(self.peername)

if rdns[0] == self.realaddr:
self.dump_notice('Could not find your hostname...')
self.release_registration_lock(REGISTRATION_LOCK_DNS)
Expand Down Expand Up @@ -140,6 +143,47 @@ def release_registration_lock(self, lock):
if not self.registration_lock:
self.register()

@property
def legacy_modes(self):
out = '+'
for i in self.props.keys():
if self.props[i] and k in user_property_items:
out += user_property_items[k]
return out

def set_legacy_modes(self, in_str):
before = copy.deepcopy(self.props)

mod = False
for i in in_str:
if i == '+':
mod = True
elif i == '-':
mod = False
else:
prop = user_mode_items[i]
self.props[prop] = mod

self.flush_legacy_mode_change(before, self.props)

def flush_legacy_mode_change(self, before, after):
props_added = list(filter(lambda x: before[x] != after[x] and after[x] == True, after.keys()))
props_removed = list(filter(lambda x: before[x] != after[x] and after[x] == False, after.keys()))

out = str()
if len(props_added) > 0:
out += '+'
for i in props_added:
out += user_mode_items[i]

if len(props_removed) > 0:
out += '-'
for i in props_removed:
out += user_mode_items[i]

msg = RFC1459Message.from_data('MODE', source=self.hostmask, params=[self.nickname, out])
self.dump_message(msg)

def sendto_common_peers(self, message):
[i.dump_message(message) for i in self.channels]

Expand Down
28 changes: 28 additions & 0 deletions mammon/property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# mammon - a useless ircd
#
# Copyright (c) 2015, William Pitcock <nenolod@dereferenced.org>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

from .utility import CaseInsensitiveDict

# map modes to boolean user properties.
user_property_items = {
'user:invisible': 'i',
'user:wallops': 'w',
'special:oper': 'o'
}

user_property_items = CaseInsensitiveDict(**user_property_items)
user_mode_items = {mode: prop for prop, mode in user_property_items.items()}

0 comments on commit 06950ad

Please sign in to comment.