]>
jfr.im git - irc/evilnet/x3.git/blob - src/modpython.py
6 # - impliment handle_* functions for everything x3 has register fetaures for
7 # - impliment script load/unload for user scripts.
8 # - load a script via this script.
9 # - script calls functions from here to set its functions up for calling on various actions
10 # - provide helper functions for subscripts to save settings attached to users/chanels
11 # - provide helper functions for scripts to do common things like msg a person or a channel,
23 """Used to interact with the world of IRC from module scripts"""
25 # some defaults to make shorthand easy
30 def __init__(self
, service
= None, caller
= None, target
= None):
32 self
.caller
= caller
#the person who sent the command/message
33 self
.service
= service
#the service who saw the message
34 self
.target
= target
#the channel message was in (if public)
36 def send_target_privmsg(self
, source
, target
, message
):
37 _svc
.send_target_privmsg(source
, target
, "%s "%(message))
39 def reply(self
, message
):
40 """ Send a private reply to the user using convenience values"""
41 #print "DEBUG: sending a message from %s to %s: %s"%(self.service, self.caller, message)
43 self
.send_target_privmsg(self
.service
, self
.target
, "%s: %s"%(self
.caller
, message
))
45 self
.send_target_privmsg(self
.service
, self
.caller
, message
)
48 """ Main hub of python system. Handle callbacks from c. """
51 #print "DEBUG: constructor for handler initing"
52 self
.plugins
= plugins_(self
)
54 print "DEBUG: unable to make self.plugins!?!"
55 self
.newplugins
= plugins
.load()
57 def init(self
, irc
): # not to be confused with __init__!
58 """ This gets called once all the objects are up and running. Otherwise,
59 were not done initing this own instance to be able to start calling it """
60 #print "DEBUG: in handler.init()"
64 def join(self
, irc
, channel
, nick
):
65 #user = _svc.get_user(nick)
66 #print "DEBUG: handler.join()"
67 return self
.plugins
.callhandler("join", irc
, [channel
, nick
], [channel
, nick
])
69 def server_link(self
, server
):
70 for plugin
in self
.newplugins
:
71 if plugin
.server_link(server
):
75 def new_user(self
, user
):
76 for plugin
in self
.newplugins
:
77 if plugin
.new_user(user
):
81 def nick_change(self
, user
, oldnick
):
82 for plugin
in self
.newplugins
:
83 plugin
.nick_change(user
, oldnick
)
85 def del_user(self
, user
, killer
, why
):
86 for plugin
in self
.newplugins
:
87 plugin
.del_user(user
, killer
, why
)
89 def topic(self
, who
, chan
, old_topic
):
90 for plugin
in self
.newplugins
:
91 if plugin
.topic(who
, chan
, old_topic
):
95 def cmd_run(self
, irc
, cmd
):
96 #print "DEBUG: handler.cmd_run: %s"%cmd
100 def addhook(self
, event
, method
, filter=[None], data
=None):
101 self
.plugins
.addhook(event
, method
, filter, data
)
104 def addcommand(self
, plugin
, command
, method
):
105 self
.addhook("command", method
, [plugin
, command
])
107 def cmd_command(self
, irc
, plugin
, cmd
, args
):
108 #print "DEBUG: handel.cmd_command; %s %s; args= %s"%(plugin, cmd, args)
109 return self
.plugins
.callhandler("command", irc
, [plugin
, cmd
], [args
])
111 def load(self
, irc
, plugin
):
112 return self
.plugins
.load(plugin
)
115 """Class to handle loading/unloading of plugins"""
120 """ This is a request from a plugin to be called on an event """
121 event
= "" # Event to be called on (eg "join")
122 method
= None # Method to call
123 filter = None # Arguments to filter
124 data
= "" # plugin-supplied data for plugin use
126 def __init__(self
, event
, method
, filter, data
):
132 def event_is(self
, event
, evdata
):
133 if(self
.event
== event
):
134 for i
in range(len(self
.filter)):
135 if( self
.filter[i
] != None
136 and self
.filter[i
] != evdata
[i
]): # should be case insensitive? or how to compare?
137 #print "DEBUG: rejecting event, %s is not %s"%(self.filter[i], evdata[i])
143 def trigger(self
, irc
, args
):
144 #print "DEBUG: Triggering %s event. with '%s' arguments."%(self.event, args)
145 self
.method(irc
, *args
)
147 def __init__(self
, handler
):
149 #print "DEBUG: constructor for plugins initing"
150 self
.handler
= handler
153 #print "DEBUG: in plugins.init()"
157 def addhook(self
, event
, method
, filter=[None], data
=None):
158 #print "DEBUG: Adding hook for %s."%event
159 self
.hooks
.append(self
.hook(event
, method
, filter, data
))
161 def findhooksforevent(self
, event
, data
):
163 #print "DEBUG: findhooksforevent() looking..."
164 for hook
in self
.hooks
:
165 #print "DEBUG: looking at a %s hook..."%hook.event
166 if(hook
.event_is(event
, data
)):
170 def callhandler(self
, event
, irc
, filter, args
):
171 for hook
in self
.findhooksforevent(event
, filter):
172 if(hook
.trigger(irc
, args
)):
176 def load(self
, name
):
177 """ Loads a plugin by name """
178 mod_name
= "plugins.%s"%name
180 if(sys
.modules
.has_key(mod_name
)):
182 #TODO: try to catch compile errors etc.
184 if(need_reload
== False):
186 module
= sys
.modules
[mod_name
]
187 if(need_reload
== True):
188 reload(module
) # to ensure its read fresh
190 pluginObj
= Class(self
.handler
, irc())
191 self
.loaded_plugins
[mod_name
] = pluginObj