3 # Copyright (c) 2014 Kristina Brooks
8 from pseudoclient
.cmd_manager
import *
9 import mythreading
as threading
10 from datetime
import datetime
11 from utils
import strip_ascii_irc
12 from core
import anope_major
14 import pyva_net_rizon_acid_core_User
as User
15 import pyva_net_rizon_acid_core_Channel
as Channel
17 #---------------------------------------------------------------------#
20 def private_channel_request(self
, manager
, opts
, arg
, channel
, sender
, userinfo
):
21 if self
.channels
.is_valid(arg
):
22 self
.notice(channel
, "I'm already in @b%s@b." % arg
)
23 elif self
.channels
.is_banned(arg
):
24 chan
= self
.channels
[arg
]
25 self
.elog
.request('Request for banned channel @b%s@b from @b%s@b.' % (arg
, sender
))
26 message
= 'Request failed, channel @b%s@b was banned by @b%s@b.' % (arg
, chan
.ban_source
)
28 if chan
.ban_reason
!= None:
29 message
+= ' Reason: @b%s@b.' % chan
.ban_reason
31 if chan
.ban_expiry
!= None:
32 message
+= ' Expires: @b%s@b.' % datetime
.fromtimestamp(chan
.ban_expiry
)
34 self
.notice(channel
, message
)
35 # self.notice(channel, 'Please email @c3????@rizon.net@o to appeal.')
37 self
.auth
.request(sender
, arg
, 'request')
39 def private_channel_remove(self
, manager
, opts
, arg
, channel
, sender
, userinfo
):
40 if not self
.channels
.is_valid(arg
):
41 self
.notice(channel
, "I'm not in @b%s@b." % arg
)
43 self
.auth
.request(sender
, arg
, 'remove')
45 def private_help(self
, manager
, opts
, arg
, channel
, sender
, userinfo
):
49 message
= manager
.get_help()
51 message
= manager
.get_help(command
)
54 message
= ['%s is not a valid command.' % arg
]
57 self
.notice(channel
, line
)
59 class PrivateCommandManager(CommandManager
):
63 def get_invalid(self
):
64 return 'Invalid message. Say help for a list of valid messages.'
66 def get_commands(self
):
68 'request': (private_channel_request
, ARG_YES|ARG_OFFLINE
, 'requests a channel (must be founder)', [], '#channel'),
69 'remove': (private_channel_remove
, ARG_YES|ARG_OFFLINE
, 'removes a channel (must be founder)', [], '#channel'),
72 'help': (private_help
, ARG_OPT
, 'displays help text', []),
75 #---------------------------------------------------------------------#
77 class InviteablePseudoclient(object):
82 self
.commands_private
= PrivateCommandManager()
84 # override this if you want to add additional crap
85 # (ie check if the chan is eligible or not)
86 def do_accept(self
, nick
, channel
):
87 self
.auth
.accept(nick
)
89 def join(self
, channel
):
90 me
= User
.findUser(self
.nick
)
93 def part(self
, channel
):
94 me
= User
.findUser(self
.nick
)
95 chan
= Channel
.findChannel(channel
)
99 def onNotice(self
, source
, target
, message
):
100 if not self
.initialized
:
103 me
= User
.findUser(self
.nick
)
104 user
= User
.findUser(target
)
105 userinfo
= User
.findUser(source
)
107 if me
!= user
or (userinfo
!= None and userinfo
['nick'] != 'ChanServ'):
111 msg
= message
.strip()
115 if msg
.startswith('[#'): #It's a channel welcome message. Let's ignore it.
118 self
.elog
.chanserv('%s' % msg
)
122 if 'tried to kick you from' in msg
:
123 nick
= strip_ascii_irc(sp
[1])
124 channel
= strip_ascii_irc(sp
[7])
125 self
.notice(nick
, 'To remove this bot (must be channel founder): @b/msg %s remove %s@b' % (self
.nick
, channel
))
129 if anope_major
== 2 and msg
== 'Password authentication required for that command.':
130 self
.elog
.error('%s is not authed with services (maybe a wrong NickServ password was configured?).' % target
)
132 # common to both anope1 and anope2
133 if "isn't registered" in msg
:
134 self
.auth
.reject_not_registered(strip_ascii_irc(sp
[1]))
140 if 'inviting' in sp
[2]: #It's an invite notice. Let's ignore it.
143 nick
= strip_ascii_irc(sp
[0])
147 should_accept
= 'Founder' in sp
[2]
148 elif anope_major
== 2:
149 channel
= strip_ascii_irc(channel
)
150 should_accept
= ('founder' == sp
[3])
153 self
.do_accept(nick
, channel
)
155 self
.auth
.reject_not_founder(nick
, channel
)
157 def onPrivmsg(self
, source
, target
, message
):
158 if not self
.initialized
:
161 userinfo
= User
.findUser(source
)
162 myself
= User
.findUser(self
.nick
)
164 sender
= userinfo
['nick']
166 msg
= message
.strip()
167 index
= msg
.find(' ')
173 command
= msg
[:index
]
174 argument
= msg
[index
+ 1:]
176 # do we have a deferal command handler?
177 if hasattr(self
, 'execute'):
178 if channel
== myself
['nick'] and command
.startswith(self
.commands_private
.prefix
): # XXX uid
179 self
.elog
.debug('Deferred parsing of private message (sender: @b%s@b, command: @b%s@b, argument: @b%s@b)' % (sender
, command
, argument
))
180 threading
.deferToThread(self
.execute
, self
.commands_private
, command
, argument
, sender
, sender
, userinfo
)
181 elif self
.channels
.is_valid(channel
) and command
.startswith(self
.commands_user
.prefix
):
182 self
.elog
.debug('Deferred parsing of channel message (sender: @b%s@b, channel: @b%s@b, command: @b%s@b, argument: @b%s@b)' % (sender
, channel
, command
, argument
))
183 threading
.deferToThread(self
.execute
, self
.commands_user
, command
, argument
, channel
, sender
, userinfo
)
184 elif target
== self
.nick
:
185 cmd
= self
.commands_private
.get_command(command
)
187 # not found, forward through
189 # (self, manager, opts, arg, channel, sender, userinfo):
190 cmd
[0](self
, self
.commands_private
, command
, argument
, sender
, sender
, userinfo
)