]>
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
, irc
, nick
, ident
, hostname
, info
):
76 # we may filter on all the user fields, but we only pass the nick because
77 # the plugin can get the rest itself
78 return self
.plugins
.callhandler("new_user", irc
, [nick
, ident
, hostname
, info
], [nick
])
80 def nick_change(self
, irc
, nick
, old_nick
):
81 return self
.plugins
.callhandler("nick_change", irc
, [nick
, old_nick
], [nick
, old_nick
])
83 def cmd_run(self
, irc
, cmd
):
84 #print "DEBUG: handler.cmd_run: %s"%cmd
88 def addhook(self
, event
, method
, filter=[None], data
=None):
89 self
.plugins
.addhook(event
, method
, filter, data
)
92 def addcommand(self
, plugin
, command
, method
):
93 self
.addhook("command", method
, [plugin
, command
])
95 def cmd_command(self
, irc
, plugin
, cmd
, args
):
96 #print "DEBUG: handel.cmd_command; %s %s; args= %s"%(plugin, cmd, args)
97 return self
.plugins
.callhandler("command", irc
, [plugin
, cmd
], [args
])
99 def load(self
, irc
, plugin
):
100 return self
.plugins
.load(plugin
)
103 """Class to handle loading/unloading of plugins"""
108 """ This is a request from a plugin to be called on an event """
109 event
= "" # Event to be called on (eg "join")
110 method
= None # Method to call
111 filter = None # Arguments to filter
112 data
= "" # plugin-supplied data for plugin use
114 def __init__(self
, event
, method
, filter, data
):
120 def event_is(self
, event
, evdata
):
121 if(self
.event
== event
):
122 for i
in range(len(self
.filter)):
123 if( self
.filter[i
] != None
124 and self
.filter[i
] != evdata
[i
]): # should be case insensitive? or how to compare?
125 #print "DEBUG: rejecting event, %s is not %s"%(self.filter[i], evdata[i])
131 def trigger(self
, irc
, args
):
132 #print "DEBUG: Triggering %s event. with '%s' arguments."%(self.event, args)
133 self
.method(irc
, *args
)
135 def __init__(self
, handler
):
137 #print "DEBUG: constructor for plugins initing"
138 self
.handler
= handler
141 #print "DEBUG: in plugins.init()"
145 def addhook(self
, event
, method
, filter=[None], data
=None):
146 #print "DEBUG: Adding hook for %s."%event
147 self
.hooks
.append(self
.hook(event
, method
, filter, data
))
149 def findhooksforevent(self
, event
, data
):
151 #print "DEBUG: findhooksforevent() looking..."
152 for hook
in self
.hooks
:
153 #print "DEBUG: looking at a %s hook..."%hook.event
154 if(hook
.event_is(event
, data
)):
158 def callhandler(self
, event
, irc
, filter, args
):
159 for hook
in self
.findhooksforevent(event
, filter):
160 if(hook
.trigger(irc
, args
)):
164 def load(self
, name
):
165 """ Loads a plugin by name """
166 mod_name
= "plugins.%s"%name
168 if(sys
.modules
.has_key(mod_name
)):
170 #TODO: try to catch compile errors etc.
172 if(need_reload
== False):
174 module
= sys
.modules
[mod_name
]
175 if(need_reload
== True):
176 reload(module
) # to ensure its read fresh
178 pluginObj
= Class(self
.handler
, irc())
179 self
.loaded_plugins
[mod_name
] = pluginObj